]> granicus.if.org Git - linux-pam/blob - modules/pam_umask/pam_umask.c
Relevant BUGIDs: none
[linux-pam] / modules / pam_umask / pam_umask.c
1 /*
2  * Copyright (c) 2005 Thorsten Kukuk <kukuk@thkukuk.de>
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, and the entire permission notice in its entirety,
9  *    including the disclaimer of warranties.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. The name of the author may not be used to endorse or promote
14  *    products derived from this software without specific prior
15  *    written permission.
16  *
17  * ALTERNATIVELY, this product may be distributed under the terms of
18  * the GNU Public License, in which case the provisions of the GPL are
19  * required INSTEAD OF the above restrictions.  (This clause is
20  * necessary due to a potential bad interaction between the GPL and
21  * the restrictions contained in a BSD-style copyright.)
22  *
23  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
24  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
25  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
26  * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
27  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
28  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
29  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
31  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
33  * OF THE POSSIBILITY OF SUCH DAMAGE.
34  */
35
36 #include "config.h"
37
38 #include <pwd.h>
39 #include <grp.h>
40 #include <stdio.h>
41 #include <ctype.h>
42 #include <string.h>
43 #include <stdarg.h>
44 #include <unistd.h>
45 #include <stdlib.h>
46 #include <sys/stat.h>
47 #include <sys/types.h>
48 #include <sys/resource.h>
49
50 #define PAM_SM_SESSION
51
52 #include <security/pam_modules.h>
53 #include <security/_pam_modutil.h>
54
55 struct options_t {
56   int debug;
57   int usergroups;
58   char *umask;
59 };
60 typedef struct options_t options_t;
61
62 /* syslogging function for errors and other information */
63 static void
64 __pam_log (int err, const char *format,...)
65 {
66   va_list args;
67   char *str;
68
69   va_start (args, format);
70   if (vasprintf (&str, format, args) < 0)
71     return;
72   syslog (err, "pam_umask: %s", str);
73   va_end (args);
74 }
75
76 static void
77 parse_option (const char *argv, options_t *options)
78 {
79   if (argv == NULL || argv[0] == '\0')
80     return;
81
82   if (strcasecmp (argv, "debug") == 0)
83     options->debug = 1;
84   else if (strncasecmp (argv, "umask=", 6) == 0)
85     options->umask = strdup (&argv[6]);
86   else if (strcasecmp (argv, "usergroups") == 0)
87     options->usergroups = 1;
88   else
89     __pam_log (LOG_ERR, "Unknown option: `%s'", argv);
90 }
91
92 static char *
93 search_key (const char *filename)
94 {
95   FILE *fp;
96   char *buf = NULL;
97   size_t buflen = 0;
98   char *retval = NULL;
99
100   fp = fopen (filename, "r");
101   if (NULL == fp)
102     return NULL;
103
104   while (!feof (fp))
105     {
106       char *tmp, *cp;
107 #if defined(HAVE_GETLINE)
108       ssize_t n = getline (&buf, &buflen, fp);
109 #elif defined (HAVE_GETDELIM)
110       ssize_t n = getdelim (&buf, &buflen, '\n', fp);
111 #else
112       ssize_t n;
113
114       if (buf == NULL)
115         {
116           buflen = 8096;
117           buf = malloc (buflen);
118         }
119       buf[0] = '\0';
120       fgets (buf, buflen - 1, fp);
121       if (buf != NULL)
122         n = strlen (buf);
123       else
124         n = 0;
125 #endif /* HAVE_GETLINE / HAVE_GETDELIM */
126       cp = buf;
127
128       if (n < 1)
129         break;
130
131       tmp = strchr (cp, '#');  /* remove comments */
132       if (tmp)
133         *tmp = '\0';
134       while (isspace ((int)*cp))    /* remove spaces and tabs */
135         ++cp;
136       if (*cp == '\0')        /* ignore empty lines */
137         continue;
138
139       if (cp[strlen (cp) - 1] == '\n')
140         cp[strlen (cp) - 1] = '\0';
141
142       tmp = strsep (&cp, " \t=");
143       if (cp != NULL)
144         while (isspace ((int)*cp) || *cp == '=')
145           ++cp;
146
147       if (strcasecmp (tmp, "UMASK") == 0)
148         {
149           retval = strdup (cp);
150           break;
151         }
152     }
153   fclose (fp);
154
155   if (buf)
156     free (buf);
157
158   return retval;
159 }
160
161 static int
162 get_options (options_t *options, int argc, const char **argv)
163 {
164   memset (options, 0, sizeof (options_t));
165   /* Parse parameters for module */
166   for ( ; argc-- > 0; argv++)
167     parse_option (*argv, options);
168
169   if (options->umask == NULL)
170     options->umask = search_key ("/etc/login.defs");
171   if (options->umask == NULL)
172     options->umask = search_key ("/etc/default/login");
173
174   return 0;
175 }
176
177 static void
178 set_umask (const char *value)
179 {
180   const char *value_orig = value;
181   mode_t mask;
182   char *endptr;
183
184   mask = strtol (value, &endptr, 8) & 0777;
185   if ((mask == 0) && (value_orig == endptr))
186     return;
187   umask (mask);
188   return;
189 }
190
191 /* Set the process nice, ulimit, and umask from the
192    password file entry.  */
193 static void
194 setup_limits_from_gecos (pam_handle_t *pamh, options_t *options,
195                          struct passwd *pw)
196 {
197   char *cp;
198
199   if (options->usergroups)
200     {
201       /* if not root, and UID == GID, and username is the same as
202          primary group name, set umask group bits to be the same as
203          owner bits (examples: 022 -> 002, 077 -> 007).  */
204       if (pw->pw_uid != 0 && pw->pw_uid == pw->pw_gid)
205         {
206           struct group *grp = _pammodutil_getgrgid (pamh, pw->pw_gid);
207           if (grp && (strcmp (pw->pw_name, grp->gr_name) == 0))
208             {
209               mode_t oldmask = umask (0777);
210               umask ((oldmask & ~070) | ((oldmask >> 3) & 070));
211             }
212         }
213     }
214
215   /* See if the GECOS field contains values for NICE, UMASK or ULIMIT.  */
216   for (cp = pw->pw_gecos; cp != NULL; cp = strchr (cp, ','))
217     {
218       if (*cp == ',')
219         cp++;
220
221       if (strncasecmp (cp, "umask=", 6) == 0)
222         umask (strtol (cp + 6, NULL, 8) & 0777);
223       else if (strncasecmp (cp, "pri=", 4) == 0)
224         nice (strtol (cp + 4, NULL, 10));
225       else if (strncasecmp (cp, "ulimit=", 7) == 0)
226         {
227           struct rlimit rlimit_fsize;
228           rlimit_fsize.rlim_cur = 512L * strtol (cp + 7, NULL, 10);
229           rlimit_fsize.rlim_max = rlimit_fsize.rlim_cur;
230           setrlimit (RLIMIT_FSIZE, &rlimit_fsize);
231         }
232     }
233 }
234
235
236 PAM_EXTERN int
237 pam_sm_open_session (pam_handle_t *pamh, int flags,
238                      int argc, const char **argv)
239 {
240   struct passwd *pw;
241   options_t options;
242   const char *name;
243   int retval = PAM_SUCCESS;
244
245   get_options (&options, argc, argv);
246
247   /* get the user name. */
248   if ((retval = pam_get_user (pamh, &name, NULL)) != PAM_SUCCESS)
249     {
250       __pam_log (LOG_ERR, "pam_get_user failed: return %d", retval);
251       return (retval == PAM_CONV_AGAIN ? PAM_INCOMPLETE:retval);
252     }
253
254   if (name == NULL || name[0] == '\0')
255     {
256       if (name)
257         {
258           __pam_log (LOG_ERR, "bad username [%s]", name);
259           return PAM_USER_UNKNOWN;
260         }
261       return PAM_SERVICE_ERR;
262     }
263
264   pw = _pammodutil_getpwnam (pamh, name);
265   if (pw == NULL)
266     {
267       __pam_log (LOG_ERR, "account for %s not found", name);
268       return PAM_USER_UNKNOWN;
269     }
270
271   if (options.umask != NULL)
272     {
273       set_umask (options.umask);
274       free (options.umask);
275     }
276
277   setup_limits_from_gecos (pamh, &options, pw);
278
279   return retval;
280 }
281
282 PAM_EXTERN int
283 pam_sm_close_session (pam_handle_t *pamh, int flags,
284                       int argc, const char **argv)
285 {
286   return PAM_SUCCESS;
287 }
288
289 #ifdef PAM_STATIC
290
291 /* static module data */
292
293 struct pam_module _pam_umask_modstruct = {
294      "pam_umask",
295      NULL,
296      NULL,
297      NULL,
298      pam_sm_open_session,
299      pam_sm_close_session,
300      NULL
301 };
302
303 #endif
304
305 /* end of module definition */