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