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