]> granicus.if.org Git - linux-pam/blob - modules/pam_unix/pam_unix_acct.c
Relevant BUGIDs: debian#514437 rhbz#487216
[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   void (*sighandler)(int) = NULL;
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     sighandler = signal(SIGCHLD, SIG_DFL);
89   }
90
91   /* fork */
92   child = fork();
93   if (child == 0) {
94     int i=0;
95     struct rlimit rlim;
96     static char *envp[] = { NULL };
97     char *args[] = { NULL, NULL, NULL, NULL };
98
99     /* reopen stdout as pipe */
100     dup2(fds[1], STDOUT_FILENO);
101
102     /* XXX - should really tidy up PAM here too */
103
104     if (getrlimit(RLIMIT_NOFILE,&rlim)==0) {
105       if (rlim.rlim_max >= MAX_FD_NO)
106         rlim.rlim_max = MAX_FD_NO;
107       for (i=0; i < (int)rlim.rlim_max; i++) {
108         if (i != STDOUT_FILENO) {
109           close(i);
110         }
111       }
112     }
113
114     if (geteuid() == 0) {
115       /* must set the real uid to 0 so the helper will not error
116          out if pam is called from setuid binary (su, sudo...) */
117       setuid(0);
118     }
119
120     /* exec binary helper */
121     args[0] = x_strdup(CHKPWD_HELPER);
122     args[1] = x_strdup(user);
123     args[2] = x_strdup("chkexpiry");
124
125     execve(CHKPWD_HELPER, args, envp);
126
127     pam_syslog(pamh, LOG_ERR, "helper binary execve failed: %m");
128     /* should not get here: exit with error */
129     D(("helper binary is not available"));
130     printf("-1\n");
131     exit(PAM_AUTHINFO_UNAVAIL);
132   } else {
133     close(fds[1]);
134     if (child > 0) {
135       char buf[32];
136       int rc=0;
137       rc=waitpid(child, &retval, 0);  /* wait for helper to complete */
138       if (rc<0) {
139         pam_syslog(pamh, LOG_ERR, "unix_chkpwd waitpid returned %d: %m", rc);
140         retval = PAM_AUTH_ERR;
141       } else {
142         retval = WEXITSTATUS(retval);
143         rc = pam_modutil_read(fds[0], buf, sizeof(buf) - 1);
144         if(rc > 0) {
145               buf[rc] = '\0';
146               if (sscanf(buf,"%d", daysleft) != 1 )
147                 retval = PAM_AUTH_ERR;
148             }
149         else {
150             pam_syslog(pamh, LOG_ERR, "read unix_chkpwd output error %d: %m", rc);
151             retval = PAM_AUTH_ERR;
152           }
153       }
154     } else {
155       pam_syslog(pamh, LOG_ERR, "Fork failed: %m");
156       D(("fork failed"));
157       retval = PAM_AUTH_ERR;
158     }
159     close(fds[0]);
160   }
161   if (sighandler != SIG_ERR) {
162     (void) signal(SIGCHLD, sighandler);   /* restore old signal handler */
163   }
164   D(("Returning %d",retval));
165   return retval;
166 }
167
168 /*
169  * PAM framework looks for this entry-point to pass control to the
170  * account management module.
171  */
172
173 PAM_EXTERN int pam_sm_acct_mgmt(pam_handle_t * pamh, int flags,
174                                 int argc, const char **argv)
175 {
176         unsigned int ctrl;
177         const void *void_uname;
178         const char *uname;
179         int retval, daysleft;
180         struct spwd *spent;
181         struct passwd *pwent;
182         char buf[256];
183
184         D(("called."));
185
186         ctrl = _set_ctrl(pamh, flags, NULL, NULL, argc, argv);
187
188         retval = pam_get_item(pamh, PAM_USER, &void_uname);
189         uname = void_uname;
190         D(("user = `%s'", uname));
191         if (retval != PAM_SUCCESS || uname == NULL) {
192                 pam_syslog(pamh, LOG_ALERT,
193                          "could not identify user (from uid=%lu)",
194                          (unsigned long int)getuid());
195                 return PAM_USER_UNKNOWN;
196         }
197
198         retval = get_account_info(pamh, uname, &pwent, &spent);
199         if (retval == PAM_USER_UNKNOWN) {
200                 pam_syslog(pamh, LOG_ALERT,
201                          "could not identify user (from getpwnam(%s))",
202                          uname);
203                 return retval;
204         }
205
206         if (retval == PAM_SUCCESS && spent == NULL)
207                 return PAM_SUCCESS;
208
209         if (retval == PAM_UNIX_RUN_HELPER) {
210                 retval = _unix_run_verify_binary(pamh, ctrl, uname, &daysleft);
211                 if (retval == PAM_AUTHINFO_UNAVAIL &&
212                         on(UNIX_BROKEN_SHADOW, ctrl))
213                         return PAM_SUCCESS;
214         } else if (retval != PAM_SUCCESS) {
215                 if (on(UNIX_BROKEN_SHADOW,ctrl))
216                         return PAM_SUCCESS;
217                 else
218                         return retval;
219         } else
220                 retval = check_shadow_expiry(pamh, spent, &daysleft);
221
222         switch (retval) {
223         case PAM_ACCT_EXPIRED:
224                 pam_syslog(pamh, LOG_NOTICE,
225                         "account %s has expired (account expired)",
226                         uname);
227                 _make_remark(pamh, ctrl, PAM_ERROR_MSG,
228                         _("Your account has expired; please contact your system administrator"));
229                 break;
230         case PAM_NEW_AUTHTOK_REQD:
231                 if (daysleft == 0) {
232                         pam_syslog(pamh, LOG_NOTICE,
233                                 "expired password for user %s (root enforced)",
234                                 uname);
235                         _make_remark(pamh, ctrl, PAM_ERROR_MSG,
236                                 _("You are required to change your password immediately (root enforced)"));
237                 } else {
238                         pam_syslog(pamh, LOG_DEBUG,
239                                 "expired password for user %s (password aged)",
240                                 uname);
241                         _make_remark(pamh, ctrl, PAM_ERROR_MSG,
242                                 _("You are required to change your password immediately (password aged)"));
243                 }
244                 break;
245         case PAM_AUTHTOK_EXPIRED:
246                 pam_syslog(pamh, LOG_NOTICE,
247                         "account %s has expired (failed to change password)",
248                         uname);
249                 _make_remark(pamh, ctrl, PAM_ERROR_MSG,
250                         _("Your account has expired; please contact your system administrator"));
251                 break;
252         case PAM_AUTHTOK_ERR:
253                 retval = PAM_SUCCESS;
254                 /* fallthrough */
255         case PAM_SUCCESS:
256                 if (daysleft >= 0) {
257                         pam_syslog(pamh, LOG_DEBUG,
258                                 "password for user %s will expire in %d days",
259                                 uname, daysleft);
260 #if defined HAVE_DNGETTEXT && defined ENABLE_NLS
261                         snprintf (buf, sizeof (buf),
262                                 dngettext(PACKAGE,
263                                   "Warning: your password will expire in %d day",
264                                   "Warning: your password will expire in %d days",
265                                   daysleft),
266                                 daysleft);
267 #else
268                         if (daysleft == 1)
269                             snprintf(buf, sizeof (buf),
270                                 _("Warning: your password will expire in %d day"),
271                                 daysleft);
272                         else
273                             snprintf(buf, sizeof (buf),
274                             /* TRANSLATORS: only used if dngettext is not supported */
275                                 _("Warning: your password will expire in %d days"),
276                                 daysleft);
277 #endif
278                         _make_remark(pamh, ctrl, PAM_TEXT_INFO, buf);
279                 }
280         }
281
282         D(("all done"));
283
284         return retval;
285 }
286
287
288 /* static module data */
289 #ifdef PAM_STATIC
290 struct pam_module _pam_unix_acct_modstruct = {
291     "pam_unix_acct",
292     NULL,
293     NULL,
294     pam_sm_acct_mgmt,
295     NULL,
296     NULL,
297     NULL,
298 };
299 #endif