Add GNU LGPL headers to all .c .C and .h files
[oweals/cde.git] / cde / lib / pam / pam_modules / dce / dce_acct_mgmt.c
1 /*
2  * CDE - Common Desktop Environment
3  *
4  * Copyright (c) 1993-2012, The Open Group. All rights reserved.
5  *
6  * These libraries and programs are free software; you can
7  * redistribute them and/or modify them under the terms of the GNU
8  * Lesser General Public License as published by the Free Software
9  * Foundation; either version 2 of the License, or (at your option)
10  * any later version.
11  *
12  * These libraries and programs are distributed in the hope that
13  * they will be useful, but WITHOUT ANY WARRANTY; without even the
14  * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15  * PURPOSE. See the GNU Lesser General Public License for more
16  * details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with these librararies and programs; if not, write
20  * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
21  * Floor, Boston, MA 02110-1301 USA
22  */
23 /* $XConsortium: dce_acct_mgmt.c /main/5 1996/05/09 04:26:10 drk $ */
24
25 /*
26  * Copyright (c) 1992-1995, by Sun Microsystems, Inc.
27  * All rights reserved.
28  */
29
30 #ident  "@(#)dce_acct_mgmt.c 1.3     95/08/02 SMI"
31
32 #include <syslog.h>
33 #include <security/pam_appl.h>
34 #include <security/pam_modules.h>
35 #include <libintl.h>
36 #include <dce/sec_login.h>
37 #include <dce/dce_error.h>
38
39 #include "pam_impl.h"
40 #include "utils.h"
41
42 /*
43  * pam_sm_acct_mgmt       main account managment routine.
44  *                        XXX: The routine just prints out a warning message.
45  *                        It may need to force the user to change his/her
46  *                        passwd.
47  */
48
49 #include <security/pam_appl.h>
50
51 #define SECS_PER_HOUR   (60*60)
52 #define SECS_PER_DAY    (SECS_PER_HOUR * 24)
53
54 static void
55 do_warn(pam_handle_t *pamh, time_t cur, time_t t, char *desc);
56
57 static void
58 do_warn_passwd(pam_handle_t *pamh);
59
60 int
61 pam_sm_acct_mgmt(
62         pam_handle_t *pamh,
63         int     flags,
64         int     argc,
65         const char **argv)
66 {
67         dce_module_data_t       *dsd;
68         sec_login_net_info_t    net_info;
69         error_status_t          st;
70         time_t                  curtime;
71         int                     result = PAM_AUTH_ERR;
72         int                     i, debug = 0;
73         int                     warn = 1;
74         int                     allow_expired_passwd = 0;
75         int                     err;
76
77
78         for (i = 0; i < argc; i++) {
79                 if (strcmp(argv[i], "debug") == 0)
80                         debug = 1;
81                 else if (strcmp(argv[i], "allow_expired_passwd") == 0)
82                         allow_expired_passwd = 1;
83                 else if (strcmp(argv[i], "nowarn") == 0)
84                         warn = 0;
85                 else
86                         syslog(LOG_ERR,
87                                 "illegal DCE acct_mgmt option %s", argv[i]);
88         }
89
90         if (flags & PAM_SILENT) warn = 0;
91
92         if (debug) syslog(LOG_DEBUG, "DCE pam_sm_acct_mgmt");
93
94         if (pam_get_data(pamh, DCE_DATA, (void**)&dsd) != PAM_SUCCESS ||
95                 dsd == NULL) {
96                 return (PAM_AUTH_ERR);
97         }
98
99         if (dsd->auth_status != PAM_SUCCESS)
100                 return (dsd->auth_status);
101
102         if (dsd->auth_src == sec_login_auth_src_local) {
103                 /* we can't call sec_login_inquire_net_info on locally */
104                 /* authenticated contexts. Might want an option to dis-allow */
105                 /* them. For now we just allow them. */
106                 return (PAM_SUCCESS);
107         }
108
109         sec_login_inquire_net_info(dsd->login_context, &net_info, &st);
110
111         if (st != error_status_ok && st != sec_login_s_not_certified) {
112                 if (debug) {
113                         dce_error_string_t text;
114
115                         syslog(LOG_DEBUG, "sec_login_inquire_net_info: %s",
116                                 get_dce_error_message(st, text));
117                 }
118                 return (PAM_PERM_DENIED);
119         }
120
121         time(&curtime);
122
123         if (warn) {
124                 if (dsd->reset_passwd) {
125                         do_warn_passwd(pamh);
126                 } else {
127                         do_warn(pamh, curtime,
128                                 (time_t) net_info.passwd_expiration_date,
129                                                                 "passwd");
130                 }
131         }
132
133         if (warn) do_warn(pamh, curtime,
134                 (time_t) net_info.acct_expiration_date, "account");
135
136         result = PAM_SUCCESS;
137
138         if ((net_info.passwd_expiration_date &&
139                         net_info.passwd_expiration_date < curtime) ||
140                         dsd->reset_passwd) {
141
142                 result = PAM_AUTHTOKEN_REQD;
143
144                 dsd->passwd_expired = 1;
145         }
146
147         /*
148          * I assume an expired account is worse then an expired password,
149          * so if both the password and account are expired we want to
150          * return PAM_ACCT_EXPIRED.
151          */
152
153         if (net_info.acct_expiration_date &&
154                 net_info.acct_expiration_date < curtime) {
155                         result = PAM_ACCT_EXPIRED;
156         }
157
158         sec_login_free_net_info(&net_info);
159
160         return (result);
161 }
162
163 static void
164 do_warn(pam_handle_t *pamh, time_t cur, time_t t, char *desc)
165 {
166         char    messages[PAM_MAX_NUM_MSG][PAM_MAX_MSG_SIZE];
167
168         if (t == 0)
169                 return; /* unlimited */
170
171         if (cur > t) {
172                 sprintf(messages[0],
173                 PAM_MSG(pamh, 1, "Warning: Your DCE %s has expired.\n"), desc);
174         } else if ((cur + SECS_PER_DAY) > t) {
175                 int hours;
176
177                 hours = (t - cur) / (SECS_PER_HOUR);
178                 hours = hours ? hours : 1;
179                 sprintf(messages[0], PAM_MSG(pamh, 2,
180                 "Warning: Your DCE %s will expire within %d hour%s.\n"),
181                         desc, hours, (hours == 1) ? "" : "s");
182         } else if ((cur + (2*SECS_PER_DAY)) > t) {
183                 sprintf(messages[0], PAM_MSG(pamh, 3,
184                 "Warning: Your DCE %s will expire in 2 days.\n"), desc);
185         } else {
186                 return;
187         }
188         __pam_display_msg(pamh, PAM_ERROR_MSG, 1, messages, NULL);
189 }
190
191
192 static void
193 do_warn_passwd(pam_handle_t *pamh)
194 {
195         char    messages[PAM_MAX_NUM_MSG][PAM_MAX_MSG_SIZE];
196
197         sprintf(messages[0],
198                 PAM_MSG(pamh, 4, "Warning: Your DCE passwd has expired.\n"));
199
200         __pam_display_msg(pamh, PAM_ERROR_MSG, 1, messages, NULL);
201 }