xref: /dflybsd-src/crypto/openssh/auth-shadow.c (revision ba1276acd1c8c22d225b1bcf370a14c878644f44)
1*ba1276acSMatthew Dillon /*
2*ba1276acSMatthew Dillon  * Copyright (c) 2004 Darren Tucker.  All rights reserved.
3*ba1276acSMatthew Dillon  *
4*ba1276acSMatthew Dillon  * Redistribution and use in source and binary forms, with or without
5*ba1276acSMatthew Dillon  * modification, are permitted provided that the following conditions
6*ba1276acSMatthew Dillon  * are met:
7*ba1276acSMatthew Dillon  * 1. Redistributions of source code must retain the above copyright
8*ba1276acSMatthew Dillon  *    notice, this list of conditions and the following disclaimer.
9*ba1276acSMatthew Dillon  * 2. Redistributions in binary form must reproduce the above copyright
10*ba1276acSMatthew Dillon  *    notice, this list of conditions and the following disclaimer in the
11*ba1276acSMatthew Dillon  *    documentation and/or other materials provided with the distribution.
12*ba1276acSMatthew Dillon  *
13*ba1276acSMatthew Dillon  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
14*ba1276acSMatthew Dillon  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
15*ba1276acSMatthew Dillon  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
16*ba1276acSMatthew Dillon  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
17*ba1276acSMatthew Dillon  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
18*ba1276acSMatthew Dillon  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
19*ba1276acSMatthew Dillon  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
20*ba1276acSMatthew Dillon  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21*ba1276acSMatthew Dillon  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
22*ba1276acSMatthew Dillon  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23*ba1276acSMatthew Dillon  */
24*ba1276acSMatthew Dillon 
25*ba1276acSMatthew Dillon #include "includes.h"
26*ba1276acSMatthew Dillon 
27*ba1276acSMatthew Dillon #if defined(USE_SHADOW) && defined(HAS_SHADOW_EXPIRE)
28*ba1276acSMatthew Dillon #include <shadow.h>
29*ba1276acSMatthew Dillon #include <stdarg.h>
30*ba1276acSMatthew Dillon #include <string.h>
31*ba1276acSMatthew Dillon #include <time.h>
32*ba1276acSMatthew Dillon 
33*ba1276acSMatthew Dillon #include "hostfile.h"
34*ba1276acSMatthew Dillon #include "auth.h"
35*ba1276acSMatthew Dillon #include "sshbuf.h"
36*ba1276acSMatthew Dillon #include "ssherr.h"
37*ba1276acSMatthew Dillon #include "log.h"
38*ba1276acSMatthew Dillon 
39*ba1276acSMatthew Dillon #ifdef DAY
40*ba1276acSMatthew Dillon # undef DAY
41*ba1276acSMatthew Dillon #endif
42*ba1276acSMatthew Dillon #define DAY	(24L * 60 * 60) /* 1 day in seconds */
43*ba1276acSMatthew Dillon 
44*ba1276acSMatthew Dillon extern struct sshbuf *loginmsg;
45*ba1276acSMatthew Dillon 
46*ba1276acSMatthew Dillon /*
47*ba1276acSMatthew Dillon  * For the account and password expiration functions, we assume the expiry
48*ba1276acSMatthew Dillon  * occurs the day after the day specified.
49*ba1276acSMatthew Dillon  */
50*ba1276acSMatthew Dillon 
51*ba1276acSMatthew Dillon /*
52*ba1276acSMatthew Dillon  * Check if specified account is expired.  Returns 1 if account is expired,
53*ba1276acSMatthew Dillon  * 0 otherwise.
54*ba1276acSMatthew Dillon  */
55*ba1276acSMatthew Dillon int
auth_shadow_acctexpired(struct spwd * spw)56*ba1276acSMatthew Dillon auth_shadow_acctexpired(struct spwd *spw)
57*ba1276acSMatthew Dillon {
58*ba1276acSMatthew Dillon 	time_t today;
59*ba1276acSMatthew Dillon 	long long daysleft;
60*ba1276acSMatthew Dillon 	int r;
61*ba1276acSMatthew Dillon 
62*ba1276acSMatthew Dillon 	today = time(NULL) / DAY;
63*ba1276acSMatthew Dillon 	daysleft = spw->sp_expire - today;
64*ba1276acSMatthew Dillon 	debug3("%s: today %lld sp_expire %lld days left %lld", __func__,
65*ba1276acSMatthew Dillon 	    (long long)today, (long long)spw->sp_expire, daysleft);
66*ba1276acSMatthew Dillon 
67*ba1276acSMatthew Dillon 	if (spw->sp_expire == -1) {
68*ba1276acSMatthew Dillon 		debug3("account expiration disabled");
69*ba1276acSMatthew Dillon 	} else if (daysleft < 0) {
70*ba1276acSMatthew Dillon 		logit("Account %.100s has expired", spw->sp_namp);
71*ba1276acSMatthew Dillon 		return 1;
72*ba1276acSMatthew Dillon 	} else if (daysleft <= spw->sp_warn) {
73*ba1276acSMatthew Dillon 		debug3("account will expire in %lld days", daysleft);
74*ba1276acSMatthew Dillon 		if ((r = sshbuf_putf(loginmsg,
75*ba1276acSMatthew Dillon 		    "Your account will expire in %lld day%s.\n", daysleft,
76*ba1276acSMatthew Dillon 		    daysleft == 1 ? "" : "s")) != 0)
77*ba1276acSMatthew Dillon 			fatal("%s: buffer error: %s", __func__, ssh_err(r));
78*ba1276acSMatthew Dillon 	}
79*ba1276acSMatthew Dillon 
80*ba1276acSMatthew Dillon 	return 0;
81*ba1276acSMatthew Dillon }
82*ba1276acSMatthew Dillon 
83*ba1276acSMatthew Dillon /*
84*ba1276acSMatthew Dillon  * Checks password expiry for platforms that use shadow passwd files.
85*ba1276acSMatthew Dillon  * Returns: 1 = password expired, 0 = password not expired
86*ba1276acSMatthew Dillon  */
87*ba1276acSMatthew Dillon int
auth_shadow_pwexpired(Authctxt * ctxt)88*ba1276acSMatthew Dillon auth_shadow_pwexpired(Authctxt *ctxt)
89*ba1276acSMatthew Dillon {
90*ba1276acSMatthew Dillon 	struct spwd *spw = NULL;
91*ba1276acSMatthew Dillon 	const char *user = ctxt->pw->pw_name;
92*ba1276acSMatthew Dillon 	time_t today;
93*ba1276acSMatthew Dillon 	int r, daysleft, disabled = 0;
94*ba1276acSMatthew Dillon 
95*ba1276acSMatthew Dillon 	if ((spw = getspnam((char *)user)) == NULL) {
96*ba1276acSMatthew Dillon 		error("Could not get shadow information for %.100s", user);
97*ba1276acSMatthew Dillon 		return 0;
98*ba1276acSMatthew Dillon 	}
99*ba1276acSMatthew Dillon 
100*ba1276acSMatthew Dillon 	today = time(NULL) / DAY;
101*ba1276acSMatthew Dillon 	debug3_f("today %lld sp_lstchg %lld sp_max %lld", (long long)today,
102*ba1276acSMatthew Dillon 	    (long long)spw->sp_lstchg, (long long)spw->sp_max);
103*ba1276acSMatthew Dillon 
104*ba1276acSMatthew Dillon #if defined(__hpux) && !defined(HAVE_SECUREWARE)
105*ba1276acSMatthew Dillon 	if (iscomsec()) {
106*ba1276acSMatthew Dillon 		struct pr_passwd *pr;
107*ba1276acSMatthew Dillon 
108*ba1276acSMatthew Dillon 		pr = getprpwnam((char *)user);
109*ba1276acSMatthew Dillon 
110*ba1276acSMatthew Dillon 		/* Test for Trusted Mode expiry disabled */
111*ba1276acSMatthew Dillon 		if (pr != NULL && pr->ufld.fd_min == 0 &&
112*ba1276acSMatthew Dillon 		    pr->ufld.fd_lifetime == 0 && pr->ufld.fd_expire == 0 &&
113*ba1276acSMatthew Dillon 		    pr->ufld.fd_pw_expire_warning == 0 &&
114*ba1276acSMatthew Dillon 		    pr->ufld.fd_schange != 0)
115*ba1276acSMatthew Dillon 			disabled = 1;
116*ba1276acSMatthew Dillon 	}
117*ba1276acSMatthew Dillon #endif
118*ba1276acSMatthew Dillon 
119*ba1276acSMatthew Dillon 	/* TODO: check sp_inact */
120*ba1276acSMatthew Dillon 	daysleft = spw->sp_lstchg + spw->sp_max - today;
121*ba1276acSMatthew Dillon 	if (disabled) {
122*ba1276acSMatthew Dillon 		debug3("password expiration disabled");
123*ba1276acSMatthew Dillon 	} else if (spw->sp_lstchg == 0) {
124*ba1276acSMatthew Dillon 		logit("User %.100s password has expired (root forced)", user);
125*ba1276acSMatthew Dillon 		return 1;
126*ba1276acSMatthew Dillon 	} else if (spw->sp_max == -1) {
127*ba1276acSMatthew Dillon 		debug3("password expiration disabled");
128*ba1276acSMatthew Dillon 	} else if (daysleft < 0) {
129*ba1276acSMatthew Dillon 		logit("User %.100s password has expired (password aged)", user);
130*ba1276acSMatthew Dillon 		return 1;
131*ba1276acSMatthew Dillon 	} else if (daysleft <= spw->sp_warn) {
132*ba1276acSMatthew Dillon 		debug3("password will expire in %d days", daysleft);
133*ba1276acSMatthew Dillon 		if ((r = sshbuf_putf(loginmsg,
134*ba1276acSMatthew Dillon 		    "Your password will expire in %d day%s.\n", daysleft,
135*ba1276acSMatthew Dillon 		    daysleft == 1 ? "" : "s")) != 0)
136*ba1276acSMatthew Dillon 			fatal("%s: buffer error: %s", __func__, ssh_err(r));
137*ba1276acSMatthew Dillon 	}
138*ba1276acSMatthew Dillon 
139*ba1276acSMatthew Dillon 	return 0;
140*ba1276acSMatthew Dillon }
141*ba1276acSMatthew Dillon #endif	/* USE_SHADOW && HAS_SHADOW_EXPIRE */
142