]> granicus.if.org Git - linux-pam/blob - modules/pam_unix/pam_unix_acct.c
Relevant BUGIDs:
[linux-pam] / modules / pam_unix / pam_unix_acct.c
1 /*
2  * Copyright Elliot Lee, 1996.  All rights reserved.
3  * Copyright Jan Rêkorajski, 1999.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, and the entire permission notice in its entirety,
10  *    including the disclaimer of warranties.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  * 3. The name of the author may not be used to endorse or promote
15  *    products derived from this software without specific prior
16  *    written permission.
17  *
18  * ALTERNATIVELY, this product may be distributed under the terms of
19  * the GNU Public License, in which case the provisions of the GPL are
20  * required INSTEAD OF the above restrictions.  (This clause is
21  * necessary due to a potential bad interaction between the GPL and
22  * the restrictions contained in a BSD-style copyright.)
23  *
24  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
25  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
26  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
27  * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
28  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
29  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
30  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
32  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
33  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
34  * OF THE POSSIBILITY OF SUCH DAMAGE.
35  */
36
37 #include "config.h"
38
39 #include <stdlib.h>
40 #include <stdio.h>
41 #include <string.h>
42 #include <unistd.h>
43 #include <sys/types.h>
44 #include <syslog.h>
45 #include <pwd.h>
46 #include <shadow.h>
47 #include <time.h>               /* for time() */
48 #include <errno.h>
49 #include <sys/wait.h>
50 #ifdef WITH_SELINUX
51 #include <selinux/selinux.h>
52 #define SELINUX_ENABLED is_selinux_enabled()>0
53 #endif
54
55 #include <security/_pam_macros.h>
56
57 /* indicate that the following groups are defined */
58
59 #define PAM_SM_ACCOUNT
60
61 #include <security/pam_modules.h>
62 #include <security/pam_ext.h>
63 #include <security/pam_modutil.h>
64
65 #include "support.h"
66 #include "passverify.h"
67
68 #ifdef WITH_SELINUX
69
70 struct spwd spwd;
71
72 struct spwd *_unix_run_verify_binary(pam_handle_t *pamh, unsigned int ctrl, const char *user)
73 {
74   int retval=0, child, fds[2];
75   void (*sighandler)(int) = NULL;
76   D(("running verify_binary"));
77
78   /* create a pipe for the messages */
79   if (pipe(fds) != 0) {
80     D(("could not make pipe"));
81     pam_syslog(pamh, LOG_ERR, "Could not make pipe: %m");
82     return NULL;
83   }
84   D(("called."));
85
86   if (off(UNIX_NOREAP, ctrl)) {
87     /*
88      * This code arranges that the demise of the child does not cause
89      * the application to receive a signal it is not expecting - which
90      * may kill the application or worse.
91      *
92      * The "noreap" module argument is provided so that the admin can
93      * override this behavior.
94      */
95     sighandler = signal(SIGCHLD, SIG_DFL);
96   }
97
98   /* fork */
99   child = fork();
100   if (child == 0) {
101     size_t i=0;
102     struct rlimit rlim;
103     static char *envp[] = { NULL };
104     char *args[] = { NULL, NULL, NULL, NULL };
105
106     close(0); close(1);
107     /* reopen stdin as pipe */
108     close(fds[0]);
109     dup2(fds[1], STDOUT_FILENO);
110
111     /* XXX - should really tidy up PAM here too */
112
113     if (getrlimit(RLIMIT_NOFILE,&rlim)==0) {
114       for (i=2; i < rlim.rlim_max; i++) {
115         if ((unsigned int)fds[1] != i) {
116           close(i);
117         }
118       }
119     }
120
121     if (SELINUX_ENABLED && geteuid() == 0) {
122       /* must set the real uid to 0 so the helper will not error
123          out if pam is called from setuid binary (su, sudo...) */
124       setuid(0);
125     }
126
127     /* exec binary helper */
128     args[0] = x_strdup(CHKPWD_HELPER);
129     args[1] = x_strdup(user);
130     args[2] = x_strdup("verify");
131
132     execve(CHKPWD_HELPER, args, envp);
133
134     pam_syslog(pamh, LOG_ERR, "helper binary execve failed: %m");
135     /* should not get here: exit with error */
136     close (fds[1]);
137     D(("helper binary is not available"));
138     exit(PAM_AUTHINFO_UNAVAIL);
139   } else {
140     close(fds[1]);
141     if (child > 0) {
142       char buf[1024];
143       int rc=0;
144       rc=waitpid(child, &retval, 0);  /* wait for helper to complete */
145       if (rc<0) {
146         pam_syslog(pamh, LOG_ERR, "unix_chkpwd waitpid returned %d: %m", rc);
147         retval = PAM_AUTH_ERR;
148       } else {
149         retval = WEXITSTATUS(retval);
150         if (retval != PAM_AUTHINFO_UNAVAIL) {
151           rc = pam_modutil_read(fds[0], buf, sizeof(buf) - 1);
152           if(rc > 0) {
153               buf[rc] = '\0';
154               if (sscanf(buf,"%ld:%ld:%ld:%ld:%ld:%ld",
155                      &spwd.sp_lstchg, /* last password change */
156                      &spwd.sp_min, /* days until change allowed. */
157                      &spwd.sp_max, /* days before change required */
158                      &spwd.sp_warn, /* days warning for expiration */
159                      &spwd.sp_inact, /* days before account inactive */
160                      &spwd.sp_expire) /* date when account expires */ != 6 ) retval = PAM_AUTH_ERR;
161             }
162           else {
163             pam_syslog(pamh, LOG_ERR, " ERROR %d: %m", rc); retval = PAM_AUTH_ERR;
164           }
165         }
166       }
167     } else {
168       pam_syslog(pamh, LOG_ERR, "Fork failed: %m");
169       D(("fork failed"));
170       retval = PAM_AUTH_ERR;
171     }
172     close(fds[0]);
173   }
174   if (sighandler != SIG_ERR) {
175     (void) signal(SIGCHLD, sighandler);   /* restore old signal handler */
176   }
177   D(("Returning %d",retval));
178   if (retval != PAM_SUCCESS) {
179     return NULL;
180   }
181   return &spwd;
182 }
183
184 #endif
185
186
187 /*
188  * PAM framework looks for this entry-point to pass control to the
189  * account management module.
190  */
191
192 PAM_EXTERN int pam_sm_acct_mgmt(pam_handle_t * pamh, int flags,
193                                 int argc, const char **argv)
194 {
195         unsigned int ctrl;
196         const void *void_uname;
197         const char *uname;
198         int retval, daysleft;
199         time_t curdays;
200         struct spwd *spent;
201         struct passwd *pwent;
202         char buf[256];
203
204         D(("called."));
205
206         ctrl = _set_ctrl(pamh, flags, NULL, argc, argv);
207
208         retval = pam_get_item(pamh, PAM_USER, &void_uname);
209         uname = void_uname;
210         D(("user = `%s'", uname));
211         if (retval != PAM_SUCCESS || uname == NULL) {
212                 pam_syslog(pamh, LOG_ALERT,
213                          "could not identify user (from uid=%lu)",
214                          (unsigned long int)getuid());
215                 return PAM_USER_UNKNOWN;
216         }
217
218         pwent = pam_modutil_getpwnam(pamh, uname);
219         if (!pwent) {
220                 pam_syslog(pamh, LOG_ALERT,
221                          "could not identify user (from getpwnam(%s))",
222                          uname);
223                 return PAM_USER_UNKNOWN;
224         }
225
226         if (!strcmp( pwent->pw_passwd, "*NP*" )) { /* NIS+ */
227                 uid_t save_euid, save_uid;
228
229                 save_euid = geteuid();
230                 save_uid = getuid();
231                 if (save_uid == pwent->pw_uid)
232                         setreuid( save_euid, save_uid );
233                 else  {
234                         setreuid( 0, -1 );
235                         if (setreuid( -1, pwent->pw_uid ) == -1) {
236                                 setreuid( -1, 0 );
237                                 setreuid( 0, -1 );
238                                 if(setreuid( -1, pwent->pw_uid ) == -1)
239                                         return PAM_CRED_INSUFFICIENT;
240                         }
241                 }
242                 spent = pam_modutil_getspnam (pamh, uname);
243                 if (save_uid == pwent->pw_uid)
244                         setreuid( save_uid, save_euid );
245                 else {
246                         if (setreuid( -1, 0 ) == -1)
247                         setreuid( save_uid, -1 );
248                         setreuid( -1, save_euid );
249                 }
250
251         } else if (_unix_shadowed (pwent))
252                 spent = pam_modutil_getspnam (pamh, uname);
253         else
254                 return PAM_SUCCESS;
255
256 #ifdef WITH_SELINUX
257         if (!spent && SELINUX_ENABLED )
258             spent = _unix_run_verify_binary(pamh, ctrl, uname);
259 #endif
260
261         if (!spent)
262                 if (on(UNIX_BROKEN_SHADOW,ctrl))
263                         return PAM_SUCCESS;
264
265         if (!spent)
266                 return PAM_AUTHINFO_UNAVAIL;    /* Couldn't get username from shadow */
267
268         curdays = time(NULL) / (60 * 60 * 24);
269         D(("today is %d, last change %d", curdays, spent->sp_lstchg));
270         if ((curdays > spent->sp_expire) && (spent->sp_expire != -1)) {
271                 pam_syslog(pamh, LOG_NOTICE,
272                          "account %s has expired (account expired)",
273                          uname);
274                 _make_remark(pamh, ctrl, PAM_ERROR_MSG,
275                              _("Your account has expired; please contact your system administrator"));
276                 D(("account expired"));
277                 return PAM_ACCT_EXPIRED;
278         }
279         if (spent->sp_lstchg == 0) {
280                 pam_syslog(pamh, LOG_NOTICE,
281                          "expired password for user %s (root enforced)",
282                          uname);
283                 _make_remark(pamh, ctrl, PAM_ERROR_MSG,
284                              _("You are required to change your password immediately (root enforced)"));
285                 D(("need a new password"));
286                 return PAM_NEW_AUTHTOK_REQD;
287         }
288         if (curdays < spent->sp_lstchg) {
289                 pam_syslog(pamh, LOG_DEBUG,
290                          "account %s has password changed in future",
291                          uname);
292                 return PAM_SUCCESS;
293         }
294         if ((curdays - spent->sp_lstchg > spent->sp_max)
295             && (curdays - spent->sp_lstchg > spent->sp_inact)
296             && (curdays - spent->sp_lstchg > spent->sp_max + spent->sp_inact)
297             && (spent->sp_max != -1) && (spent->sp_inact != -1)) {
298                 pam_syslog(pamh, LOG_NOTICE,
299                     "account %s has expired (failed to change password)",
300                     uname);
301                 _make_remark(pamh, ctrl, PAM_ERROR_MSG,
302                              _("Your account has expired; please contact your system administrator"));
303                 D(("account expired 2"));
304                 return PAM_ACCT_EXPIRED;
305         }
306         if ((curdays - spent->sp_lstchg > spent->sp_max) && (spent->sp_max != -1)) {
307                 pam_syslog(pamh, LOG_DEBUG,
308                          "expired password for user %s (password aged)",
309                          uname);
310                 _make_remark(pamh, ctrl, PAM_ERROR_MSG,
311                              _("You are required to change your password immediately (password aged)"));
312                 D(("need a new password 2"));
313                 return PAM_NEW_AUTHTOK_REQD;
314         }
315         if ((curdays - spent->sp_lstchg > spent->sp_max - spent->sp_warn)
316             && (spent->sp_max != -1) && (spent->sp_warn != -1)) {
317                 daysleft = (spent->sp_lstchg + spent->sp_max) - curdays;
318                 pam_syslog(pamh, LOG_DEBUG,
319                          "password for user %s will expire in %d days",
320                          uname, daysleft);
321 #if defined HAVE_DNGETTEXT && defined ENABLE_NLS
322                 snprintf (buf, sizeof (buf),
323                           dngettext(PACKAGE,
324                                     "Warning: your password will expire in %d day",
325                                     "Warning: your password will expire in %d days",
326                                     daysleft),
327                           daysleft);
328 #else
329                 if (daysleft == 1)
330                   snprintf(buf, sizeof (buf),
331                            _("Warning: your password will expire in %d day"),
332                            daysleft);
333                 else
334                   snprintf(buf, sizeof (buf),
335                            /* TRANSLATORS: only used if dngettext is not support
336 ed */
337                            _("Warning: your password will expire in %d days"),
338                            daysleft);
339 #endif
340                 _make_remark(pamh, ctrl, PAM_TEXT_INFO, buf);
341         }
342
343         D(("all done"));
344
345         return PAM_SUCCESS;
346 }
347
348
349 /* static module data */
350 #ifdef PAM_STATIC
351 struct pam_module _pam_unix_acct_modstruct = {
352     "pam_unix_acct",
353     NULL,
354     NULL,
355     pam_sm_acct_mgmt,
356     NULL,
357     NULL,
358     NULL,
359 };
360 #endif