]> granicus.if.org Git - linux-pam/blob - modules/pam_unix/pam_unix_acct.c
Relevant BUGIDs: 126036
[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
49 #include <security/_pam_macros.h>
50
51 /* indicate that the following groups are defined */
52
53 #define PAM_SM_ACCOUNT
54
55 #include <security/pam_modules.h>
56
57 #ifndef LINUX_PAM
58 #include <security/pam_appl.h>
59 #endif                          /* LINUX_PAM */
60
61 #include "support.h"
62  
63 /*
64  * PAM framework looks for this entry-point to pass control to the
65  * account management module.
66  */
67
68 PAM_EXTERN int pam_sm_acct_mgmt(pam_handle_t * pamh, int flags,
69                                 int argc, const char **argv)
70 {
71         unsigned int ctrl;
72         const char *uname;
73         int retval, daysleft;
74         time_t curdays;
75         struct spwd *spent;
76         struct passwd *pwent;
77         char buf[80];
78
79         D(("called."));
80
81         ctrl = _set_ctrl(flags, NULL, argc, argv);
82
83         retval = pam_get_item(pamh, PAM_USER, (const void **) &uname);
84         D(("user = `%s'", uname));
85         if (retval != PAM_SUCCESS || uname == NULL) {
86                 _log_err(LOG_ALERT
87                          ,"could not identify user (from uid=%d)"
88                          ,getuid());
89                 return PAM_USER_UNKNOWN;
90         }
91
92         pwent = getpwnam(uname);
93         if (!pwent) {
94                 _log_err(LOG_ALERT
95                          ,"could not identify user (from getpwnam(%s))"
96                          ,uname);
97                 return PAM_USER_UNKNOWN;
98         }
99
100         if (!strcmp( pwent->pw_passwd, "*NP*" )) { /* NIS+ */
101                 uid_t save_euid, save_uid;
102
103                 save_euid = geteuid();
104                 save_uid = getuid();
105                 if (save_uid == pwent->pw_uid)
106                         setreuid( save_euid, save_uid );
107                 else  {
108                         setreuid( 0, -1 );
109                         if (setreuid( -1, pwent->pw_uid ) == -1) {
110                                 setreuid( -1, 0 );
111                                 setreuid( 0, -1 );
112                                 if(setreuid( -1, pwent->pw_uid ) == -1)
113                                         return PAM_CRED_INSUFFICIENT;
114                         }
115                 }
116                 spent = getspnam( uname );
117                 if (save_uid == pwent->pw_uid)
118                         setreuid( save_uid, save_euid );
119                 else {
120                         if (setreuid( -1, 0 ) == -1)
121                         setreuid( save_uid, -1 );
122                         setreuid( -1, save_euid );
123                 }
124
125         } else if (!strcmp( pwent->pw_passwd, "x" )) {
126                 spent = getspnam(uname);
127         } else {
128                 return PAM_SUCCESS;
129         }
130
131         if (!spent)
132                 return PAM_AUTHINFO_UNAVAIL;    /* Couldn't get username from shadow */
133
134         curdays = time(NULL) / (60 * 60 * 24);
135         D(("today is %d, last change %d", curdays, spent->sp_lstchg));
136         if ((curdays > spent->sp_expire) && (spent->sp_expire != -1)
137             && (spent->sp_lstchg != 0)) {
138                 _log_err(LOG_NOTICE
139                          ,"account %s has expired (account expired)"
140                          ,uname);
141                 _make_remark(pamh, ctrl, PAM_ERROR_MSG,
142                             "Your account has expired; please contact your system administrator");
143                 D(("account expired"));
144                 return PAM_ACCT_EXPIRED;
145         }
146         if ((curdays > (spent->sp_lstchg + spent->sp_max + spent->sp_inact))
147             && (spent->sp_max != -1) && (spent->sp_inact != -1)
148             && (spent->sp_lstchg != 0)) {
149                 _log_err(LOG_NOTICE
150                     ,"account %s has expired (failed to change password)"
151                          ,uname);
152                 _make_remark(pamh, ctrl, PAM_ERROR_MSG,
153                             "Your account has expired; please contact your system administrator");
154                 D(("account expired 2"));
155                 return PAM_ACCT_EXPIRED;
156         }
157         D(("when was the last change"));
158         if (spent->sp_lstchg == 0) {
159                 _log_err(LOG_NOTICE
160                          ,"expired password for user %s (root enforced)"
161                          ,uname);
162                 _make_remark(pamh, ctrl, PAM_ERROR_MSG,
163                             "You are required to change your password immediately (root enforced)");
164                 D(("need a new password"));
165                 return PAM_NEW_AUTHTOK_REQD;
166         }
167         if (((spent->sp_lstchg + spent->sp_max) < curdays) && (spent->sp_max != -1)) {
168                 _log_err(LOG_DEBUG
169                          ,"expired password for user %s (password aged)"
170                          ,uname);
171                 _make_remark(pamh, ctrl, PAM_ERROR_MSG,
172                             "You are required to change your password immediately (password aged)");
173                 D(("need a new password 2"));
174                 return PAM_NEW_AUTHTOK_REQD;
175         }
176         if ((curdays > (spent->sp_lstchg + spent->sp_max - spent->sp_warn))
177             && (spent->sp_max != -1) && (spent->sp_warn != -1)) {
178                 daysleft = (spent->sp_lstchg + spent->sp_max) - curdays;
179                 _log_err(LOG_DEBUG
180                          ,"password for user %s will expire in %d days"
181                          ,uname, daysleft);
182                 snprintf(buf, 80, "Warning: your password will expire in %d day%.2s",
183                          daysleft, daysleft == 1 ? "" : "s");
184                 _make_remark(pamh, ctrl, PAM_TEXT_INFO, buf);
185         }
186
187         D(("all done"));
188
189         return PAM_SUCCESS;
190 }
191
192
193 /* static module data */
194 #ifdef PAM_STATIC
195 struct pam_module _pam_unix_acct_modstruct = {
196     "pam_unix_acct",
197     NULL,
198     NULL,
199     pam_sm_acct_mgmt,
200     NULL,
201     NULL,
202     NULL,
203 };
204 #endif