xref: /openbsd-src/lib/libutil/check_expire.c (revision 15d1ec5d30ceebc4c1addb2e30309472c3d7f876)
1 /*	$OpenBSD: check_expire.c,v 1.7 2003/09/02 16:55:32 markus Exp $	*/
2 
3 /*
4  * Copyright (c) 1997 Berkeley Software Design, Inc. All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
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. All advertising materials mentioning features or use of this software
15  *    must display the following acknowledgement:
16  *	This product includes software developed by Berkeley Software Design,
17  *	Inc.
18  * 4. The name of Berkeley Software Design, Inc.  may not be used to endorse
19  *    or promote products derived from this software without specific prior
20  *    written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY BERKELEY SOFTWARE DESIGN, INC. ``AS IS'' AND
23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED.  IN NO EVENT SHALL BERKELEY SOFTWARE DESIGN, INC. BE LIABLE
26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  *
34  *	BSDI $From: check_expire.c,v 2.1 1997/08/08 18:38:25 prb Exp $
35  */
36 
37 #include <sys/types.h>
38 
39 #include <errno.h>
40 #include <fcntl.h>
41 #include <pwd.h>
42 #include <signal.h>
43 #include <stdio.h>
44 #include <stdlib.h>
45 #include <string.h>
46 #include <syslog.h>
47 #include <time.h>
48 #include <tzfile.h>
49 #include <login_cap.h>
50 #include <bsd_auth.h>
51 
52 #include "util.h"
53 
54 static char *pwd_update(struct passwd *);
55 
56 int
57 login_check_expire(back, pwd, class, lastchance)
58 	FILE *back;
59 	struct passwd *pwd;
60 	char *class;
61 	int lastchance;
62 {
63 	auth_session_t *as;
64 	login_cap_t *lc;
65 	quad_t dead, expire, warn;
66 	char *p;
67 
68 	if ((as = auth_open()) == NULL) {
69 		syslog(LOG_ERR, "failed to create auth session");
70 		return (1);
71 	}
72 	if (auth_setpwd(as, pwd) < 0) {
73 		syslog(LOG_ERR, "failed to set pwd entry in auth session");
74 		return (1);
75 	}
76 
77 	expire = auth_check_change(as);
78 	auth_close(as);
79 
80 	if (expire != 0) {
81 		fprintf(back, BI_VALUE " expire %qd\n", expire);
82 
83 		if (class == NULL)
84 			class = pwd->pw_class;
85 
86 		if ((lc = login_getclass(class)) == NULL) {
87 			dead = 0;
88 			warn = 0;
89 		} else {
90 			dead = login_getcaptime(lc, "password-dead", 0, 0);
91 			warn = login_getcaptime(lc, "password-warn",
92 			    2 * DAYSPERWEEK * SECSPERDAY,
93 			    2 * DAYSPERWEEK * SECSPERDAY);
94 			if (dead < 0) {
95 				syslog(LOG_ERR, "class %s password-dead is %qd",
96 					lc->lc_class, dead);
97 				dead = 0;
98 			}
99 			if (warn < 0) {
100 				syslog(LOG_ERR, "class %s password-warn is %qd",
101 					lc->lc_class, warn);
102 				warn = 0;
103 			}
104 		}
105 		login_close(lc);
106 
107 		/*
108 		 * If their password is dead (expired longer than
109 		 * password-dead) then just reject them.  If it is
110 		 * expired but not dead yet, reject them with a
111 		 * PWEXPIRED so login knows they can still sort of
112 		 * get in.
113 		 */
114 		if (expire < -dead) {
115 			syslog(LOG_WARNING, "%s: dead password", pwd->pw_name);
116 			fprintf(back, BI_VALUE
117 			    " errormsg Your password has expired\n");
118 			fprintf(back, BI_REJECT "\n");
119 			return (1);
120 		}
121 		if (expire < 0) {
122 			if (lastchance) {
123 				struct passwd *npwd;
124 
125 				endpwent();
126 
127 				/*
128 				 * Only let them play this game once.
129 				 * Set their password change time to 1.
130 				 * This will most certainly cause any
131 				 * expired password to be dead, as well.
132 				 */
133 				npwd = pw_dup(pwd);
134 				npwd->pw_change = 1;
135 				p = pwd_update(npwd);
136 				memset(npwd->pw_passwd, 0,
137 				    strlen(npwd->pw_passwd));
138 				free(npwd);
139 				if (p != NULL) {
140 					fprintf(back, BI_VALUE " errormsg %s",
141 					    auth_mkvalue(p));
142 					fprintf(back, BI_REJECT "\n");
143 					return (1);
144 				}
145 			}
146 			syslog(LOG_WARNING, "%s: expired password", pwd->pw_name);
147 			fprintf(back, BI_VALUE
148 			    " errormsg Your password has expired\n");
149 			fprintf(back, BI_PWEXPIRED "\n");
150 			return (1);
151 		}
152 
153 		/*
154 		 * If their password is not expired but is about to expire
155 		 * then warn them.
156 		 */
157 		if (expire <= warn) {
158 			fprintf(back, BI_VALUE
159 			    " warnmsg Your password expires on %s\n",
160 			    ctime(&pwd->pw_change));
161 		}
162 	}
163 	return (0);
164 }
165 
166 static char *
167 pwd_update(pwd)
168 	struct passwd *pwd;
169 {
170 	int tfd, pfd;
171 
172 	pw_init();
173 	tfd = pw_lock(0);
174 	if (tfd < 0) {
175 		if (errno == EEXIST)
176 			return("the passwd file is busy.");
177 		else
178 			return("can't open passwd temp file");
179 	}
180 
181 	pfd = open(_PATH_MASTERPASSWD, O_RDONLY, 0);
182 	if (pfd < 0 || fcntl(pfd, F_SETFD, 1) == -1) {
183 		pw_abort();
184 		return(strerror(errno));
185 	}
186 
187 	pw_copy(pfd, tfd, pwd);
188 	if (pw_mkdb(pwd->pw_name, 0) < 0) {
189 		pw_abort();
190 		return("unable to update password database");
191 	}
192 
193 	return(NULL);
194 }
195