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