]> 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 #include <syslog.h>
50
51 #define PAM_SM_SESSION
52
53 #include <security/pam_modules.h>
54 #include <security/_pam_modutil.h>
55 #include <security/pam_ext.h>
56
57 struct options_t {
58   int debug;
59   int usergroups;
60   char *umask;
61 };
62 typedef struct options_t options_t;
63
64 static void
65 parse_option (const pam_handle_t *pamh, const char *argv, options_t *options)
66 {
67   if (argv == NULL || argv[0] == '\0')
68     return;
69
70   if (strcasecmp (argv, "debug") == 0)
71     options->debug = 1;
72   else if (strncasecmp (argv, "umask=", 6) == 0)
73     options->umask = strdup (&argv[6]);
74   else if (strcasecmp (argv, "usergroups") == 0)
75     options->usergroups = 1;
76   else
77     pam_syslog (pamh, LOG_ERR, "Unknown option: `%s'", argv);
78 }
79
80 static char *
81 search_key (const char *filename)
82 {
83   FILE *fp;
84   char *buf = NULL;
85   size_t buflen = 0;
86   char *retval = NULL;
87
88   fp = fopen (filename, "r");
89   if (NULL == fp)
90     return NULL;
91
92   while (!feof (fp))
93     {
94       char *tmp, *cp;
95 #if defined(HAVE_GETLINE)
96       ssize_t n = getline (&buf, &buflen, fp);
97 #elif defined (HAVE_GETDELIM)
98       ssize_t n = getdelim (&buf, &buflen, '\n', fp);
99 #else
100       ssize_t n;
101
102       if (buf == NULL)
103         {
104           buflen = 8096;
105           buf = malloc (buflen);
106         }
107       buf[0] = '\0';
108       fgets (buf, buflen - 1, fp);
109       if (buf != NULL)
110         n = strlen (buf);
111       else
112         n = 0;
113 #endif /* HAVE_GETLINE / HAVE_GETDELIM */
114       cp = buf;
115
116       if (n < 1)
117         break;
118
119       tmp = strchr (cp, '#');  /* remove comments */
120       if (tmp)
121         *tmp = '\0';
122       while (isspace ((int)*cp))    /* remove spaces and tabs */
123         ++cp;
124       if (*cp == '\0')        /* ignore empty lines */
125         continue;
126
127       if (cp[strlen (cp) - 1] == '\n')
128         cp[strlen (cp) - 1] = '\0';
129
130       tmp = strsep (&cp, " \t=");
131       if (cp != NULL)
132         while (isspace ((int)*cp) || *cp == '=')
133           ++cp;
134
135       if (strcasecmp (tmp, "UMASK") == 0)
136         {
137           retval = strdup (cp);
138           break;
139         }
140     }
141   fclose (fp);
142
143   if (buf)
144     free (buf);
145
146   return retval;
147 }
148
149 static int
150 get_options (const pam_handle_t *pamh, options_t *options,
151              int argc, const char **argv)
152 {
153   memset (options, 0, sizeof (options_t));
154   /* Parse parameters for module */
155   for ( ; argc-- > 0; argv++)
156     parse_option (pamh, *argv, options);
157
158   if (options->umask == NULL)
159     options->umask = search_key ("/etc/login.defs");
160   if (options->umask == NULL)
161     options->umask = search_key ("/etc/default/login");
162
163   return 0;
164 }
165
166 static void
167 set_umask (const char *value)
168 {
169   const char *value_orig = value;
170   mode_t mask;
171   char *endptr;
172
173   mask = strtol (value, &endptr, 8) & 0777;
174   if ((mask == 0) && (value_orig == endptr))
175     return;
176   umask (mask);
177   return;
178 }
179
180 /* Set the process nice, ulimit, and umask from the
181    password file entry.  */
182 static void
183 setup_limits_from_gecos (pam_handle_t *pamh, options_t *options,
184                          struct passwd *pw)
185 {
186   char *cp;
187
188   if (options->usergroups)
189     {
190       /* if not root, and UID == GID, and username is the same as
191          primary group name, set umask group bits to be the same as
192          owner bits (examples: 022 -> 002, 077 -> 007).  */
193       if (pw->pw_uid != 0 && pw->pw_uid == pw->pw_gid)
194         {
195           struct group *grp = _pammodutil_getgrgid (pamh, pw->pw_gid);
196           if (grp && (strcmp (pw->pw_name, grp->gr_name) == 0))
197             {
198               mode_t oldmask = umask (0777);
199               umask ((oldmask & ~070) | ((oldmask >> 3) & 070));
200             }
201         }
202     }
203
204   /* See if the GECOS field contains values for NICE, UMASK or ULIMIT.  */
205   for (cp = pw->pw_gecos; cp != NULL; cp = strchr (cp, ','))
206     {
207       if (*cp == ',')
208         cp++;
209
210       if (strncasecmp (cp, "umask=", 6) == 0)
211         umask (strtol (cp + 6, NULL, 8) & 0777);
212       else if (strncasecmp (cp, "pri=", 4) == 0)
213         nice (strtol (cp + 4, NULL, 10));
214       else if (strncasecmp (cp, "ulimit=", 7) == 0)
215         {
216           struct rlimit rlimit_fsize;
217           rlimit_fsize.rlim_cur = 512L * strtol (cp + 7, NULL, 10);
218           rlimit_fsize.rlim_max = rlimit_fsize.rlim_cur;
219           setrlimit (RLIMIT_FSIZE, &rlimit_fsize);
220         }
221     }
222 }
223
224
225 PAM_EXTERN int
226 pam_sm_open_session (pam_handle_t *pamh, int flags UNUSED,
227                      int argc, const char **argv)
228 {
229   struct passwd *pw;
230   options_t options;
231   const char *name;
232   int retval = PAM_SUCCESS;
233
234   get_options (pamh, &options, argc, argv);
235
236   /* get the user name. */
237   if ((retval = pam_get_user (pamh, &name, NULL)) != PAM_SUCCESS)
238     {
239       pam_syslog (pamh, LOG_ERR, "pam_get_user failed: return %d", retval);
240       return (retval == PAM_CONV_AGAIN ? PAM_INCOMPLETE:retval);
241     }
242
243   if (name == NULL || name[0] == '\0')
244     {
245       if (name)
246         {
247           pam_syslog (pamh, LOG_ERR, "bad username [%s]", name);
248           return PAM_USER_UNKNOWN;
249         }
250       return PAM_SERVICE_ERR;
251     }
252
253   pw = _pammodutil_getpwnam (pamh, name);
254   if (pw == NULL)
255     {
256       pam_syslog (pamh, LOG_ERR, "account for %s not found", name);
257       return PAM_USER_UNKNOWN;
258     }
259
260   if (options.umask != NULL)
261     {
262       set_umask (options.umask);
263       free (options.umask);
264     }
265
266   setup_limits_from_gecos (pamh, &options, pw);
267
268   return retval;
269 }
270
271 PAM_EXTERN int
272 pam_sm_close_session (pam_handle_t *pamh UNUSED, int flags UNUSED,
273                       int argc UNUSED, const char **argv UNUSED)
274 {
275   return PAM_SUCCESS;
276 }
277
278 #ifdef PAM_STATIC
279
280 /* static module data */
281
282 struct pam_module _pam_umask_modstruct = {
283      "pam_umask",
284      NULL,
285      NULL,
286      NULL,
287      pam_sm_open_session,
288      pam_sm_close_session,
289      NULL
290 };
291
292 #endif
293
294 /* end of module definition */