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