]> 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
67 #ifdef WITH_SELINUX
68
69 struct spwd spwd;
70
71 struct spwd *_unix_run_verify_binary(pam_handle_t *pamh, unsigned int ctrl, const char *user)
72 {
73   int retval=0, child, fds[2];
74   void (*sighandler)(int) = NULL;
75   D(("running verify_binary"));
76
77   /* create a pipe for the messages */
78   if (pipe(fds) != 0) {
79     D(("could not make pipe"));
80     pam_syslog(pamh, LOG_ERR, "Could not make pipe: %m");
81     return NULL;
82   }
83   D(("called."));
84
85   if (off(UNIX_NOREAP, ctrl)) {
86     /*
87      * This code arranges that the demise of the child does not cause
88      * the application to receive a signal it is not expecting - which
89      * may kill the application or worse.
90      *
91      * The "noreap" module argument is provided so that the admin can
92      * override this behavior.
93      */
94     sighandler = signal(SIGCHLD, SIG_DFL);
95   }
96
97   /* fork */
98   child = fork();
99   if (child == 0) {
100     size_t i=0;
101     struct rlimit rlim;
102     static char *envp[] = { NULL };
103     char *args[] = { NULL, NULL, NULL, NULL };
104
105     close(0); close(1);
106     /* reopen stdin as pipe */
107     close(fds[0]);
108     dup2(fds[1], STDOUT_FILENO);
109
110     /* XXX - should really tidy up PAM here too */
111
112     if (getrlimit(RLIMIT_NOFILE,&rlim)==0) {
113       for (i=2; i < rlim.rlim_max; i++) {
114         if ((unsigned int)fds[1] != i) {
115           close(i);
116         }
117       }
118     }
119
120     if (SELINUX_ENABLED && geteuid() == 0) {
121       /* must set the real uid to 0 so the helper will not error
122          out if pam is called from setuid binary (su, sudo...) */
123       setuid(0);
124     }
125
126     /* exec binary helper */
127     args[0] = x_strdup(CHKPWD_HELPER);
128     args[1] = x_strdup(user);
129     args[2] = x_strdup("verify");
130
131     execve(CHKPWD_HELPER, args, envp);
132
133     pam_syslog(pamh, LOG_ERR, "helper binary execve failed: %m");
134     /* should not get here: exit with error */
135     close (fds[1]);
136     D(("helper binary is not available"));
137     exit(PAM_AUTHINFO_UNAVAIL);
138   } else {
139     close(fds[1]);
140     if (child > 0) {
141       char buf[1024];
142       int rc=0;
143       rc=waitpid(child, &retval, 0);  /* wait for helper to complete */
144       if (rc<0) {
145         pam_syslog(pamh, LOG_ERR, "unix_chkpwd waitpid returned %d: %m", rc);
146         retval = PAM_AUTH_ERR;
147       } else {
148         retval = WEXITSTATUS(retval);
149         if (retval != PAM_AUTHINFO_UNAVAIL) {
150           rc = pam_modutil_read(fds[0], buf, sizeof(buf) - 1);
151           if(rc > 0) {
152               buf[rc] = '\0';
153               if (sscanf(buf,"%ld:%ld:%ld:%ld:%ld:%ld",
154                      &spwd.sp_lstchg, /* last password change */
155                      &spwd.sp_min, /* days until change allowed. */
156                      &spwd.sp_max, /* days before change required */
157                      &spwd.sp_warn, /* days warning for expiration */
158                      &spwd.sp_inact, /* days before account inactive */
159                      &spwd.sp_expire) /* date when account expires */ != 6 ) retval = PAM_AUTH_ERR;
160             }
161           else {
162             pam_syslog(pamh, LOG_ERR, " ERROR %d: %m", rc); retval = PAM_AUTH_ERR;
163           }
164         }
165       }
166     } else {
167       pam_syslog(pamh, LOG_ERR, "Fork failed: %m");
168       D(("fork failed"));
169       retval = PAM_AUTH_ERR;
170     }
171     close(fds[0]);
172   }
173   if (sighandler != NULL) {
174     (void) signal(SIGCHLD, sighandler);   /* restore old signal handler */
175   }
176   D(("Returning %d",retval));
177   if (retval != PAM_SUCCESS) {
178     return NULL;
179   }
180   return &spwd;
181 }
182
183 #endif
184
185
186 /*
187  * PAM framework looks for this entry-point to pass control to the
188  * account management module.
189  */
190
191 PAM_EXTERN int pam_sm_acct_mgmt(pam_handle_t * pamh, int flags,
192                                 int argc, const char **argv)
193 {
194         unsigned int ctrl;
195         const void *void_uname;
196         const char *uname;
197         int retval, daysleft;
198         time_t curdays;
199         struct spwd *spent;
200         struct passwd *pwent;
201         char buf[256];
202
203         D(("called."));
204
205         ctrl = _set_ctrl(pamh, flags, NULL, argc, argv);
206
207         retval = pam_get_item(pamh, PAM_USER, &void_uname);
208         uname = void_uname;
209         D(("user = `%s'", uname));
210         if (retval != PAM_SUCCESS || uname == NULL) {
211                 pam_syslog(pamh, LOG_ALERT,
212                          "could not identify user (from uid=%d)",
213                          getuid());
214                 return PAM_USER_UNKNOWN;
215         }
216
217         pwent = pam_modutil_getpwnam(pamh, uname);
218         if (!pwent) {
219                 pam_syslog(pamh, LOG_ALERT,
220                          "could not identify user (from getpwnam(%s))",
221                          uname);
222                 return PAM_USER_UNKNOWN;
223         }
224
225         if (!strcmp( pwent->pw_passwd, "*NP*" )) { /* NIS+ */
226                 uid_t save_euid, save_uid;
227
228                 save_euid = geteuid();
229                 save_uid = getuid();
230                 if (save_uid == pwent->pw_uid)
231                         setreuid( save_euid, save_uid );
232                 else  {
233                         setreuid( 0, -1 );
234                         if (setreuid( -1, pwent->pw_uid ) == -1) {
235                                 setreuid( -1, 0 );
236                                 setreuid( 0, -1 );
237                                 if(setreuid( -1, pwent->pw_uid ) == -1)
238                                         return PAM_CRED_INSUFFICIENT;
239                         }
240                 }
241                 spent = pam_modutil_getspnam (pamh, uname);
242                 if (save_uid == pwent->pw_uid)
243                         setreuid( save_uid, save_euid );
244                 else {
245                         if (setreuid( -1, 0 ) == -1)
246                         setreuid( save_uid, -1 );
247                         setreuid( -1, save_euid );
248                 }
249
250         } else if (_unix_shadowed (pwent))
251                 spent = pam_modutil_getspnam (pamh, uname);
252         else
253                 return PAM_SUCCESS;
254
255 #ifdef WITH_SELINUX
256         if (!spent && SELINUX_ENABLED )
257             spent = _unix_run_verify_binary(pamh, ctrl, uname);
258 #endif
259
260         if (!spent)
261                 if (on(UNIX_BROKEN_SHADOW,ctrl))
262                         return PAM_SUCCESS;
263
264         if (!spent)
265                 return PAM_AUTHINFO_UNAVAIL;    /* Couldn't get username from shadow */
266
267         curdays = time(NULL) / (60 * 60 * 24);
268         D(("today is %d, last change %d", curdays, spent->sp_lstchg));
269         if ((curdays > spent->sp_expire) && (spent->sp_expire != -1)) {
270                 pam_syslog(pamh, LOG_NOTICE,
271                          "account %s has expired (account expired)",
272                          uname);
273                 _make_remark(pamh, ctrl, PAM_ERROR_MSG,
274                              _("Your account has expired; please contact your system administrator"));
275                 D(("account expired"));
276                 return PAM_ACCT_EXPIRED;
277         }
278         if (spent->sp_lstchg == 0) {
279                 pam_syslog(pamh, LOG_NOTICE,
280                          "expired password for user %s (root enforced)",
281                          uname);
282                 _make_remark(pamh, ctrl, PAM_ERROR_MSG,
283                              _("You are required to change your password immediately (root enforced)"));
284                 D(("need a new password"));
285                 return PAM_NEW_AUTHTOK_REQD;
286         }
287         if (curdays < spent->sp_lstchg) {
288                 pam_syslog(pamh, LOG_DEBUG,
289                          "account %s has password changed in future",
290                          uname);
291                 return PAM_SUCCESS;
292         }
293         if ((curdays - spent->sp_lstchg > spent->sp_max)
294             && (curdays - spent->sp_lstchg > spent->sp_inact)
295             && (curdays - spent->sp_lstchg > spent->sp_max + spent->sp_inact)
296             && (spent->sp_max != -1) && (spent->sp_inact != -1)) {
297                 pam_syslog(pamh, LOG_NOTICE,
298                     "account %s has expired (failed to change password)",
299                     uname);
300                 _make_remark(pamh, ctrl, PAM_ERROR_MSG,
301                              _("Your account has expired; please contact your system administrator"));
302                 D(("account expired 2"));
303                 return PAM_ACCT_EXPIRED;
304         }
305         if ((curdays - spent->sp_lstchg > spent->sp_max) && (spent->sp_max != -1)) {
306                 pam_syslog(pamh, LOG_DEBUG,
307                          "expired password for user %s (password aged)",
308                          uname);
309                 _make_remark(pamh, ctrl, PAM_ERROR_MSG,
310                              _("You are required to change your password immediately (password aged)"));
311                 D(("need a new password 2"));
312                 return PAM_NEW_AUTHTOK_REQD;
313         }
314         if ((curdays - spent->sp_lstchg > spent->sp_max - spent->sp_warn)
315             && (spent->sp_max != -1) && (spent->sp_warn != -1)) {
316                 daysleft = (spent->sp_lstchg + spent->sp_max) - curdays;
317                 pam_syslog(pamh, LOG_DEBUG,
318                          "password for user %s will expire in %d days",
319                          uname, daysleft);
320                 snprintf(buf, sizeof (buf), _("Warning: your password will expire in %d day%.2s"),
321                          daysleft, daysleft == 1 ? "" : "s");
322                 _make_remark(pamh, ctrl, PAM_TEXT_INFO, buf);
323         }
324
325         D(("all done"));
326
327         return PAM_SUCCESS;
328 }
329
330
331 /* static module data */
332 #ifdef PAM_STATIC
333 struct pam_module _pam_unix_acct_modstruct = {
334     "pam_unix_acct",
335     NULL,
336     NULL,
337     pam_sm_acct_mgmt,
338     NULL,
339     NULL,
340     NULL,
341 };
342 #endif