1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate  * Copyright 2003 Sun Microsystems, Inc.  All rights reserved.
24*0Sstevel@tonic-gate  * Use is subject to license terms.
25*0Sstevel@tonic-gate  */
26*0Sstevel@tonic-gate 
27*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*0Sstevel@tonic-gate 
29*0Sstevel@tonic-gate #include <sys/types.h>
30*0Sstevel@tonic-gate #include <sys/time.h>
31*0Sstevel@tonic-gate #include <string.h>
32*0Sstevel@tonic-gate #include <thread.h>
33*0Sstevel@tonic-gate #include <unistd.h>
34*0Sstevel@tonic-gate #include <stdlib.h>
35*0Sstevel@tonic-gate #include <crypt.h>
36*0Sstevel@tonic-gate #include <pwd.h>
37*0Sstevel@tonic-gate #include <shadow.h>
38*0Sstevel@tonic-gate 
39*0Sstevel@tonic-gate #include <deflt.h>
40*0Sstevel@tonic-gate 
41*0Sstevel@tonic-gate #include "passwdutil.h"
42*0Sstevel@tonic-gate 
43*0Sstevel@tonic-gate #define	PWADMIN "/etc/default/passwd"
44*0Sstevel@tonic-gate 
45*0Sstevel@tonic-gate #define	MINWEEKS	-1
46*0Sstevel@tonic-gate #define	MAXWEEKS	-1
47*0Sstevel@tonic-gate #define	WARNWEEKS	-1
48*0Sstevel@tonic-gate 
49*0Sstevel@tonic-gate void
50*0Sstevel@tonic-gate free_pwd(struct passwd *pw)
51*0Sstevel@tonic-gate {
52*0Sstevel@tonic-gate 	if (pw->pw_name) free(pw->pw_name);
53*0Sstevel@tonic-gate 	if (pw->pw_passwd) free(pw->pw_passwd);
54*0Sstevel@tonic-gate 	if (pw->pw_gecos) free(pw->pw_gecos);
55*0Sstevel@tonic-gate 	if (pw->pw_dir) free(pw->pw_dir);
56*0Sstevel@tonic-gate 	if (pw->pw_shell) free(pw->pw_shell);
57*0Sstevel@tonic-gate 	free(pw);
58*0Sstevel@tonic-gate }
59*0Sstevel@tonic-gate 
60*0Sstevel@tonic-gate void
61*0Sstevel@tonic-gate free_spwd(struct spwd *spw)
62*0Sstevel@tonic-gate {
63*0Sstevel@tonic-gate 	if (spw->sp_namp) free(spw->sp_namp);
64*0Sstevel@tonic-gate 	if (spw->sp_pwdp) free(spw->sp_pwdp);
65*0Sstevel@tonic-gate 	free(spw);
66*0Sstevel@tonic-gate }
67*0Sstevel@tonic-gate 
68*0Sstevel@tonic-gate int
69*0Sstevel@tonic-gate dup_pw(struct passwd **d, struct passwd *s)
70*0Sstevel@tonic-gate {
71*0Sstevel@tonic-gate 	if (s == NULL) {
72*0Sstevel@tonic-gate 		*d = NULL;
73*0Sstevel@tonic-gate 		return (PWU_NOT_FOUND);
74*0Sstevel@tonic-gate 	}
75*0Sstevel@tonic-gate 	if ((*d = calloc(1, sizeof (**d))) == NULL)
76*0Sstevel@tonic-gate 		return (PWU_NOMEM);
77*0Sstevel@tonic-gate 
78*0Sstevel@tonic-gate 	if (s->pw_name) {
79*0Sstevel@tonic-gate 		if (((*d)->pw_name = strdup(s->pw_name)) == NULL)
80*0Sstevel@tonic-gate 			goto no_mem;
81*0Sstevel@tonic-gate 	}
82*0Sstevel@tonic-gate 	if (s->pw_passwd) {
83*0Sstevel@tonic-gate 		if (((*d)->pw_passwd = strdup(s->pw_passwd)) == NULL)
84*0Sstevel@tonic-gate 			goto no_mem;
85*0Sstevel@tonic-gate 	}
86*0Sstevel@tonic-gate 	(*d)->pw_uid = s->pw_uid;
87*0Sstevel@tonic-gate 	(*d)->pw_gid = s->pw_gid;
88*0Sstevel@tonic-gate 
89*0Sstevel@tonic-gate 	if (s->pw_gecos) {
90*0Sstevel@tonic-gate 		if (((*d)->pw_gecos = strdup(s->pw_gecos)) == NULL)
91*0Sstevel@tonic-gate 			goto no_mem;
92*0Sstevel@tonic-gate 	}
93*0Sstevel@tonic-gate 	if (s->pw_dir) {
94*0Sstevel@tonic-gate 		if (((*d)->pw_dir = strdup(s->pw_dir)) == NULL)
95*0Sstevel@tonic-gate 			goto no_mem;
96*0Sstevel@tonic-gate 	}
97*0Sstevel@tonic-gate 	if (s->pw_shell) {
98*0Sstevel@tonic-gate 		if (((*d)->pw_shell = strdup(s->pw_shell)) == NULL)
99*0Sstevel@tonic-gate 			goto no_mem;
100*0Sstevel@tonic-gate 	}
101*0Sstevel@tonic-gate 
102*0Sstevel@tonic-gate 	return (PWU_SUCCESS);
103*0Sstevel@tonic-gate 
104*0Sstevel@tonic-gate no_mem:
105*0Sstevel@tonic-gate 	free_pwd(*d);
106*0Sstevel@tonic-gate 	*d = NULL;
107*0Sstevel@tonic-gate 	return (PWU_NOMEM);
108*0Sstevel@tonic-gate }
109*0Sstevel@tonic-gate 
110*0Sstevel@tonic-gate int
111*0Sstevel@tonic-gate dup_spw(struct spwd **d, struct spwd *s)
112*0Sstevel@tonic-gate {
113*0Sstevel@tonic-gate 	if (s == NULL) {
114*0Sstevel@tonic-gate 		*d = NULL;
115*0Sstevel@tonic-gate 		return (PWU_NOT_FOUND);
116*0Sstevel@tonic-gate 	}
117*0Sstevel@tonic-gate 	if ((*d = calloc(1, sizeof (**d))) == NULL)
118*0Sstevel@tonic-gate 		return (PWU_NOMEM);
119*0Sstevel@tonic-gate 
120*0Sstevel@tonic-gate 	**d = *s;
121*0Sstevel@tonic-gate 
122*0Sstevel@tonic-gate 	if (s->sp_namp)
123*0Sstevel@tonic-gate 		if (((*d)->sp_namp = strdup(s->sp_namp)) == NULL)
124*0Sstevel@tonic-gate 			goto no_mem;
125*0Sstevel@tonic-gate 	if (s->sp_pwdp)
126*0Sstevel@tonic-gate 		if (((*d)->sp_pwdp = strdup(s->sp_pwdp)) == NULL)
127*0Sstevel@tonic-gate 			goto no_mem;
128*0Sstevel@tonic-gate 	return (PWU_SUCCESS);
129*0Sstevel@tonic-gate 
130*0Sstevel@tonic-gate no_mem:
131*0Sstevel@tonic-gate 	free_spwd(*d);
132*0Sstevel@tonic-gate 	return (PWU_NOMEM);
133*0Sstevel@tonic-gate }
134*0Sstevel@tonic-gate 
135*0Sstevel@tonic-gate /*
136*0Sstevel@tonic-gate  * read a value from the defaults file, and return it if it is
137*0Sstevel@tonic-gate  * a positive integer. If the value is not defined, or negative,
138*0Sstevel@tonic-gate  * return the supplied default value
139*0Sstevel@tonic-gate  */
140*0Sstevel@tonic-gate int
141*0Sstevel@tonic-gate def_getuint(char *name, int defvalue)
142*0Sstevel@tonic-gate {
143*0Sstevel@tonic-gate 	char *p;
144*0Sstevel@tonic-gate 	int val = -1;	/* -1 is a guard to catch undefined values */
145*0Sstevel@tonic-gate 
146*0Sstevel@tonic-gate 	if (p = defread(name))
147*0Sstevel@tonic-gate 		val = atoi(p);
148*0Sstevel@tonic-gate 
149*0Sstevel@tonic-gate 	return (val >= 0 ? val : defvalue);
150*0Sstevel@tonic-gate }
151*0Sstevel@tonic-gate 
152*0Sstevel@tonic-gate void
153*0Sstevel@tonic-gate turn_on_default_aging(struct spwd *spw)
154*0Sstevel@tonic-gate {
155*0Sstevel@tonic-gate 	int minweeks;
156*0Sstevel@tonic-gate 	int maxweeks;
157*0Sstevel@tonic-gate 	int warnweeks;
158*0Sstevel@tonic-gate 
159*0Sstevel@tonic-gate 	if (defopen(PWADMIN) != 0) {
160*0Sstevel@tonic-gate 		minweeks = MINWEEKS;
161*0Sstevel@tonic-gate 		maxweeks = MAXWEEKS;
162*0Sstevel@tonic-gate 		warnweeks = WARNWEEKS;
163*0Sstevel@tonic-gate 	} else {
164*0Sstevel@tonic-gate 		minweeks = def_getuint("MINWEEKS=", MINWEEKS);
165*0Sstevel@tonic-gate 		maxweeks = def_getuint("MAXWEEKS=", MAXWEEKS);
166*0Sstevel@tonic-gate 		warnweeks = def_getuint("WARNWEEKS=", WARNWEEKS);
167*0Sstevel@tonic-gate 		(void) defopen(NULL);
168*0Sstevel@tonic-gate 	}
169*0Sstevel@tonic-gate 
170*0Sstevel@tonic-gate 	/*
171*0Sstevel@tonic-gate 	 * The values specified in /etc/default/passwd are interpreted
172*0Sstevel@tonic-gate 	 * in a specific way. Special cases are
173*0Sstevel@tonic-gate 	 *   MINWEEKS==0 (results in sp_min = -1)
174*0Sstevel@tonic-gate 	 *   MAXWEEKS==0 (results in sp_max = default)
175*0Sstevel@tonic-gate 	 */
176*0Sstevel@tonic-gate 	spw->sp_min = 7 * minweeks;
177*0Sstevel@tonic-gate 	if (spw->sp_min <= 0)
178*0Sstevel@tonic-gate 		spw->sp_min = -1;
179*0Sstevel@tonic-gate 
180*0Sstevel@tonic-gate 	spw->sp_max = 7 * maxweeks;
181*0Sstevel@tonic-gate 	if (spw->sp_max == 0)
182*0Sstevel@tonic-gate 		spw->sp_max = 7 * MAXWEEKS;
183*0Sstevel@tonic-gate 	if (spw->sp_max < 0)
184*0Sstevel@tonic-gate 		spw->sp_max = -1;
185*0Sstevel@tonic-gate 
186*0Sstevel@tonic-gate 	spw->sp_warn = 7 * warnweeks;
187*0Sstevel@tonic-gate 	if (spw->sp_warn <= 0)
188*0Sstevel@tonic-gate 		spw->sp_warn = -1;
189*0Sstevel@tonic-gate }
190*0Sstevel@tonic-gate 
191*0Sstevel@tonic-gate /*
192*0Sstevel@tonic-gate  * open and read a value from the defaults file,
193*0Sstevel@tonic-gate  * return value found or default value if not found.
194*0Sstevel@tonic-gate  */
195*0Sstevel@tonic-gate int
196*0Sstevel@tonic-gate def_getint(char *name, int defvalue)
197*0Sstevel@tonic-gate {
198*0Sstevel@tonic-gate 	int	val;
199*0Sstevel@tonic-gate 
200*0Sstevel@tonic-gate 	if (defopen(PWADMIN) != 0)
201*0Sstevel@tonic-gate 		val = defvalue;
202*0Sstevel@tonic-gate 	else
203*0Sstevel@tonic-gate 		val = def_getuint(name, defvalue);
204*0Sstevel@tonic-gate 
205*0Sstevel@tonic-gate 	(void) defopen(NULL);
206*0Sstevel@tonic-gate 	return (val);
207*0Sstevel@tonic-gate }
208