pam_umask.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. /*
  2. * pam_umask module
  3. *
  4. * Copyright (c) 2005, 2006, 2007, 2010, 2013 Thorsten Kukuk <kukuk@thkukuk.de>
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, and the entire permission notice in its entirety,
  11. * including the disclaimer of warranties.
  12. * 2. Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. * 3. The name of the author may not be used to endorse or promote
  16. * products derived from this software without specific prior
  17. * written permission.
  18. *
  19. * ALTERNATIVELY, this product may be distributed under the terms of
  20. * the GNU Public License V2, in which case the provisions of the GPL
  21. * are required INSTEAD OF the above restrictions. (This clause is
  22. * necessary due to a potential bad interaction between the GPL and
  23. * the restrictions contained in a BSD-style copyright.)
  24. *
  25. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
  26. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  27. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  28. * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
  29. * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  30. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  31. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  32. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  33. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  34. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  35. * OF THE POSSIBILITY OF SUCH DAMAGE.
  36. */
  37. #include "config.h"
  38. #include <pwd.h>
  39. #include <grp.h>
  40. #include <stdio.h>
  41. #include <ctype.h>
  42. #include <errno.h>
  43. #include <limits.h>
  44. #include <string.h>
  45. #include <stdarg.h>
  46. #include <unistd.h>
  47. #include <stdlib.h>
  48. #include <sys/stat.h>
  49. #include <sys/types.h>
  50. #include <sys/resource.h>
  51. #include <syslog.h>
  52. #include <security/pam_modules.h>
  53. #include <security/pam_modutil.h>
  54. #include <security/pam_ext.h>
  55. #include "pam_inline.h"
  56. #define LOGIN_DEFS "/etc/login.defs"
  57. #define LOGIN_CONF "/etc/default/login"
  58. struct options_t {
  59. int debug;
  60. int usergroups;
  61. int silent;
  62. const char *umask;
  63. char *login_umask;
  64. };
  65. typedef struct options_t options_t;
  66. static void
  67. parse_option (const pam_handle_t *pamh, const char *argv, options_t *options)
  68. {
  69. const char *str;
  70. if (argv == NULL || argv[0] == '\0')
  71. return;
  72. if (strcasecmp (argv, "debug") == 0)
  73. options->debug = 1;
  74. else if ((str = pam_str_skip_icase_prefix (argv, "umask=")) != NULL)
  75. options->umask = str;
  76. else if (strcasecmp (argv, "usergroups") == 0)
  77. options->usergroups = 1;
  78. else if (strcasecmp (argv, "nousergroups") == 0)
  79. options->usergroups = 0;
  80. else if (strcasecmp (argv, "silent") == 0)
  81. options->silent = 1;
  82. else
  83. pam_syslog (pamh, LOG_ERR, "Unknown option: `%s'", argv);
  84. }
  85. static int
  86. get_options (pam_handle_t *pamh, options_t *options,
  87. int argc, const char **argv)
  88. {
  89. memset (options, 0, sizeof (options_t));
  90. options->usergroups = DEFAULT_USERGROUPS_SETTING;
  91. /* Parse parameters for module */
  92. for ( ; argc-- > 0; argv++)
  93. parse_option (pamh, *argv, options);
  94. if (options->umask == NULL) {
  95. options->login_umask = pam_modutil_search_key (pamh, LOGIN_DEFS, "UMASK");
  96. if (options->login_umask == NULL)
  97. options->login_umask = pam_modutil_search_key (pamh, LOGIN_CONF, "UMASK");
  98. options->umask = options->login_umask;
  99. }
  100. return 0;
  101. }
  102. static void
  103. set_umask (const char *value)
  104. {
  105. const char *value_orig = value;
  106. mode_t mask;
  107. char *endptr;
  108. mask = strtoul (value, &endptr, 8) & 0777;
  109. if (((mask == 0) && (value_orig == endptr)) ||
  110. ((mask == UINT_MAX) && (errno == ERANGE)))
  111. return;
  112. umask (mask);
  113. return;
  114. }
  115. /* Set the process nice, ulimit, and umask from the
  116. password file entry. */
  117. static void
  118. setup_limits_from_gecos (pam_handle_t *pamh, options_t *options,
  119. struct passwd *pw)
  120. {
  121. char *cp;
  122. if (options->usergroups)
  123. {
  124. /* if not root and username is the same as primary group name,
  125. set umask group bits to be the same as owner bits
  126. (examples: 022 -> 002, 077 -> 007). */
  127. if (pw->pw_uid != 0)
  128. {
  129. struct group *grp = pam_modutil_getgrgid (pamh, pw->pw_gid);
  130. if (grp && (strcmp (pw->pw_name, grp->gr_name) == 0))
  131. {
  132. mode_t oldmask = umask (0777);
  133. umask ((oldmask & ~070) | ((oldmask >> 3) & 070));
  134. }
  135. }
  136. }
  137. /* See if the GECOS field contains values for NICE, UMASK or ULIMIT. */
  138. for (cp = pw->pw_gecos; cp != NULL; cp = strchr (cp, ','))
  139. {
  140. const char *str;
  141. if (*cp == ',')
  142. cp++;
  143. if ((str = pam_str_skip_icase_prefix (cp, "umask=")) != NULL)
  144. umask (strtol (str, NULL, 8) & 0777);
  145. else if ((str = pam_str_skip_icase_prefix (cp, "pri=")) != NULL)
  146. {
  147. errno = 0;
  148. if (nice (strtol (str, NULL, 10)) == -1 && errno != 0)
  149. {
  150. if (!options->silent || options->debug)
  151. pam_error (pamh, "nice failed: %m\n");
  152. pam_syslog (pamh, LOG_ERR, "nice failed: %m");
  153. }
  154. }
  155. else if ((str = pam_str_skip_icase_prefix (cp, "ulimit=")) != NULL)
  156. {
  157. struct rlimit rlimit_fsize;
  158. rlimit_fsize.rlim_cur = 512L * strtol (str, NULL, 10);
  159. rlimit_fsize.rlim_max = rlimit_fsize.rlim_cur;
  160. if (setrlimit (RLIMIT_FSIZE, &rlimit_fsize) == -1)
  161. {
  162. if (!options->silent || options->debug)
  163. pam_error (pamh, "setrlimit failed: %m\n");
  164. pam_syslog (pamh, LOG_ERR, "setrlimit failed: %m");
  165. }
  166. }
  167. }
  168. }
  169. int
  170. pam_sm_open_session (pam_handle_t *pamh, int flags UNUSED,
  171. int argc, const char **argv)
  172. {
  173. struct passwd *pw;
  174. options_t options;
  175. const char *name;
  176. int retval = PAM_SUCCESS;
  177. get_options (pamh, &options, argc, argv);
  178. if (flags & PAM_SILENT)
  179. options.silent = 1;
  180. /* get the user name. */
  181. if ((retval = pam_get_user (pamh, &name, NULL)) != PAM_SUCCESS)
  182. {
  183. pam_syslog(pamh, LOG_NOTICE, "cannot determine user name: %s",
  184. pam_strerror(pamh, retval));
  185. return (retval == PAM_CONV_AGAIN ? PAM_INCOMPLETE:retval);
  186. }
  187. pw = pam_modutil_getpwnam (pamh, name);
  188. if (pw == NULL)
  189. {
  190. pam_syslog (pamh, LOG_NOTICE, "account for %s not found", name);
  191. return PAM_USER_UNKNOWN;
  192. }
  193. if (options.umask != NULL)
  194. {
  195. set_umask (options.umask);
  196. free (options.login_umask);
  197. options.umask = options.login_umask = NULL;
  198. }
  199. setup_limits_from_gecos (pamh, &options, pw);
  200. return retval;
  201. }
  202. int
  203. pam_sm_close_session (pam_handle_t *pamh UNUSED, int flags UNUSED,
  204. int argc UNUSED, const char **argv UNUSED)
  205. {
  206. return PAM_SUCCESS;
  207. }
  208. /* end of module definition */