]> 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
51 #include <security/_pam_macros.h>
52
53 /* indicate that the following groups are defined */
54
55 #define PAM_SM_ACCOUNT
56
57 #include <security/pam_modules.h>
58 #include <security/pam_ext.h>
59 #include <security/pam_modutil.h>
60
61 #include "support.h"
62 #include "passverify.h"
63
64 int _unix_run_verify_binary(pam_handle_t *pamh, unsigned int ctrl,
65         const char *user, int *daysleft)
66 {
67   int retval=0, child, fds[2];
68   struct sigaction newsa, oldsa;
69   D(("running verify_binary"));
70
71   /* create a pipe for the messages */
72   if (pipe(fds) != 0) {
73     D(("could not make pipe"));
74     pam_syslog(pamh, LOG_ERR, "Could not make pipe: %m");
75     return PAM_AUTH_ERR;
76   }
77   D(("called."));
78
79   if (off(UNIX_NOREAP, ctrl)) {
80     /*
81      * This code arranges that the demise of the child does not cause
82      * the application to receive a signal it is not expecting - which
83      * may kill the application or worse.
84      *
85      * The "noreap" module argument is provided so that the admin can
86      * override this behavior.
87      */
88      memset(&newsa, '\0', sizeof(newsa));
89      newsa.sa_handler = SIG_DFL;
90      sigaction(SIGCHLD, &newsa, &oldsa);
91   }
92
93   /* fork */
94   child = fork();
95   if (child == 0) {
96     int i=0;
97     struct rlimit rlim;
98     static char *envp[] = { NULL };
99     char *args[] = { NULL, NULL, NULL, NULL };
100
101     /* reopen stdout as pipe */
102     dup2(fds[1], STDOUT_FILENO);
103
104     /* XXX - should really tidy up PAM here too */
105
106     if (getrlimit(RLIMIT_NOFILE,&rlim)==0) {
107       if (rlim.rlim_max >= MAX_FD_NO)
108         rlim.rlim_max = MAX_FD_NO;
109       for (i=0; i < (int)rlim.rlim_max; i++) {
110         if (i != STDOUT_FILENO) {
111           close(i);
112         }
113       }
114     }
115
116     if (geteuid() == 0) {
117       /* must set the real uid to 0 so the helper will not error
118          out if pam is called from setuid binary (su, sudo...) */
119       setuid(0);
120     }
121
122     /* exec binary helper */
123     args[0] = x_strdup(CHKPWD_HELPER);
124     args[1] = x_strdup(user);
125     args[2] = x_strdup("chkexpiry");
126
127     execve(CHKPWD_HELPER, args, envp);
128
129     pam_syslog(pamh, LOG_ERR, "helper binary execve failed: %m");
130     /* should not get here: exit with error */
131     D(("helper binary is not available"));
132     printf("-1\n");
133     fflush(stdout);
134     _exit(PAM_AUTHINFO_UNAVAIL);
135   } else {
136     close(fds[1]);
137     if (child > 0) {
138       char buf[32];
139       int rc=0;
140       rc=waitpid(child, &retval, 0);  /* wait for helper to complete */
141       if (rc<0) {
142         pam_syslog(pamh, LOG_ERR, "unix_chkpwd waitpid returned %d: %m", rc);
143         retval = PAM_AUTH_ERR;
144       } else if (!WIFEXITED(retval)) {
145         pam_syslog(pamh, LOG_ERR, "unix_chkpwd abnormal exit: %d", retval);
146         retval = PAM_AUTH_ERR;
147       } else {
148         retval = WEXITSTATUS(retval);
149         rc = pam_modutil_read(fds[0], buf, sizeof(buf) - 1);
150         if(rc > 0) {
151               buf[rc] = '\0';
152               if (sscanf(buf,"%d", daysleft) != 1 )
153                 retval = PAM_AUTH_ERR;
154             }
155         else {
156             pam_syslog(pamh, LOG_ERR, "read unix_chkpwd output error %d: %m", rc);
157             retval = PAM_AUTH_ERR;
158           }
159       }
160     } else {
161       pam_syslog(pamh, LOG_ERR, "Fork failed: %m");
162       D(("fork failed"));
163       retval = PAM_AUTH_ERR;
164     }
165     close(fds[0]);
166   }
167
168   if (off(UNIX_NOREAP, ctrl)) {
169         sigaction(SIGCHLD, &oldsa, NULL);   /* restore old signal handler */
170   }
171
172   D(("Returning %d",retval));
173   return retval;
174 }
175
176 /*
177  * PAM framework looks for this entry-point to pass control to the
178  * account management module.
179  */
180
181 PAM_EXTERN int pam_sm_acct_mgmt(pam_handle_t * pamh, int flags,
182                                 int argc, const char **argv)
183 {
184         unsigned int ctrl;
185         const void *void_uname;
186         const char *uname;
187         int retval, daysleft;
188         struct spwd *spent;
189         struct passwd *pwent;
190         char buf[256];
191
192         D(("called."));
193
194         ctrl = _set_ctrl(pamh, flags, NULL, NULL, argc, argv);
195
196         retval = pam_get_item(pamh, PAM_USER, &void_uname);
197         uname = void_uname;
198         D(("user = `%s'", uname));
199         if (retval != PAM_SUCCESS || uname == NULL) {
200                 pam_syslog(pamh, LOG_ALERT,
201                          "could not identify user (from uid=%lu)",
202                          (unsigned long int)getuid());
203                 return PAM_USER_UNKNOWN;
204         }
205
206         retval = get_account_info(pamh, uname, &pwent, &spent);
207         if (retval == PAM_USER_UNKNOWN) {
208                 pam_syslog(pamh, LOG_ALERT,
209                          "could not identify user (from getpwnam(%s))",
210                          uname);
211                 return retval;
212         }
213
214         if (retval == PAM_SUCCESS && spent == NULL)
215                 return PAM_SUCCESS;
216
217         if (retval == PAM_UNIX_RUN_HELPER) {
218                 retval = _unix_run_verify_binary(pamh, ctrl, uname, &daysleft);
219                 if (retval == PAM_AUTHINFO_UNAVAIL &&
220                         on(UNIX_BROKEN_SHADOW, ctrl))
221                         return PAM_SUCCESS;
222         } else if (retval != PAM_SUCCESS) {
223                 if (on(UNIX_BROKEN_SHADOW,ctrl))
224                         return PAM_SUCCESS;
225                 else
226                         return retval;
227         } else
228                 retval = check_shadow_expiry(pamh, spent, &daysleft);
229
230         switch (retval) {
231         case PAM_ACCT_EXPIRED:
232                 pam_syslog(pamh, LOG_NOTICE,
233                         "account %s has expired (account expired)",
234                         uname);
235                 _make_remark(pamh, ctrl, PAM_ERROR_MSG,
236                         _("Your account has expired; please contact your system administrator"));
237                 break;
238         case PAM_NEW_AUTHTOK_REQD:
239                 if (daysleft == 0) {
240                         pam_syslog(pamh, LOG_NOTICE,
241                                 "expired password for user %s (root enforced)",
242                                 uname);
243                         _make_remark(pamh, ctrl, PAM_ERROR_MSG,
244                                 _("You are required to change your password immediately (root enforced)"));
245                 } else {
246                         pam_syslog(pamh, LOG_DEBUG,
247                                 "expired password for user %s (password aged)",
248                                 uname);
249                         _make_remark(pamh, ctrl, PAM_ERROR_MSG,
250                                 _("You are required to change your password immediately (password aged)"));
251                 }
252                 break;
253         case PAM_AUTHTOK_EXPIRED:
254                 pam_syslog(pamh, LOG_NOTICE,
255                         "account %s has expired (failed to change password)",
256                         uname);
257                 _make_remark(pamh, ctrl, PAM_ERROR_MSG,
258                         _("Your account has expired; please contact your system administrator"));
259                 break;
260         case PAM_AUTHTOK_ERR:
261                 retval = PAM_SUCCESS;
262                 /* fallthrough */
263         case PAM_SUCCESS:
264                 if (daysleft >= 0) {
265                         pam_syslog(pamh, LOG_DEBUG,
266                                 "password for user %s will expire in %d days",
267                                 uname, daysleft);
268 #if defined HAVE_DNGETTEXT && defined ENABLE_NLS
269                         snprintf (buf, sizeof (buf),
270                                 dngettext(PACKAGE,
271                                   "Warning: your password will expire in %d day",
272                                   "Warning: your password will expire in %d days",
273                                   daysleft),
274                                 daysleft);
275 #else
276                         if (daysleft == 1)
277                             snprintf(buf, sizeof (buf),
278                                 _("Warning: your password will expire in %d day"),
279                                 daysleft);
280                         else
281                             snprintf(buf, sizeof (buf),
282                             /* TRANSLATORS: only used if dngettext is not supported */
283                                 _("Warning: your password will expire in %d days"),
284                                 daysleft);
285 #endif
286                         _make_remark(pamh, ctrl, PAM_TEXT_INFO, buf);
287                 }
288         }
289
290         D(("all done"));
291
292         return retval;
293 }
294
295
296 /* static module data */
297 #ifdef PAM_STATIC
298 struct pam_module _pam_unix_acct_modstruct = {
299     "pam_unix_acct",
300     NULL,
301     NULL,
302     pam_sm_acct_mgmt,
303     NULL,
304     NULL,
305     NULL,
306 };
307 #endif