xref: /titanic_52/usr/src/lib/passwdutil/utils.c (revision 03c6512857753c00f62e52595dc5def4dd3546e1)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
227c478bd9Sstevel@tonic-gate /*
23*03c65128Swy83408  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate #include <sys/types.h>
307c478bd9Sstevel@tonic-gate #include <sys/time.h>
317c478bd9Sstevel@tonic-gate #include <string.h>
327c478bd9Sstevel@tonic-gate #include <thread.h>
337c478bd9Sstevel@tonic-gate #include <unistd.h>
347c478bd9Sstevel@tonic-gate #include <stdlib.h>
357c478bd9Sstevel@tonic-gate #include <crypt.h>
367c478bd9Sstevel@tonic-gate #include <pwd.h>
377c478bd9Sstevel@tonic-gate #include <shadow.h>
387c478bd9Sstevel@tonic-gate 
397c478bd9Sstevel@tonic-gate #include <deflt.h>
407c478bd9Sstevel@tonic-gate 
417c478bd9Sstevel@tonic-gate #include "passwdutil.h"
427c478bd9Sstevel@tonic-gate 
437c478bd9Sstevel@tonic-gate #define	PWADMIN "/etc/default/passwd"
447c478bd9Sstevel@tonic-gate 
457c478bd9Sstevel@tonic-gate #define	MINWEEKS	-1
467c478bd9Sstevel@tonic-gate #define	MAXWEEKS	-1
477c478bd9Sstevel@tonic-gate #define	WARNWEEKS	-1
487c478bd9Sstevel@tonic-gate 
49*03c65128Swy83408 extern repops_t files_repops, nis_repops,
50*03c65128Swy83408 	nisplus_repops, ldap_repops, nss_repops;
51*03c65128Swy83408 
52*03c65128Swy83408 repops_t *rops[REP_LAST+1] = {
53*03c65128Swy83408 	NULL,
54*03c65128Swy83408 	&files_repops,
55*03c65128Swy83408 	&nis_repops,
56*03c65128Swy83408 	NULL,
57*03c65128Swy83408 	&nisplus_repops,
58*03c65128Swy83408 	NULL,
59*03c65128Swy83408 	NULL,
60*03c65128Swy83408 	NULL,
61*03c65128Swy83408 	&ldap_repops,
62*03c65128Swy83408 	NULL,
63*03c65128Swy83408 	NULL,
64*03c65128Swy83408 	NULL,
65*03c65128Swy83408 	NULL,
66*03c65128Swy83408 	NULL,
67*03c65128Swy83408 	NULL,
68*03c65128Swy83408 	NULL,
69*03c65128Swy83408 	&nss_repops,
70*03c65128Swy83408 };
71*03c65128Swy83408 
727c478bd9Sstevel@tonic-gate void
737c478bd9Sstevel@tonic-gate free_pwd(struct passwd *pw)
747c478bd9Sstevel@tonic-gate {
757c478bd9Sstevel@tonic-gate 	if (pw->pw_name) free(pw->pw_name);
767c478bd9Sstevel@tonic-gate 	if (pw->pw_passwd) free(pw->pw_passwd);
777c478bd9Sstevel@tonic-gate 	if (pw->pw_gecos) free(pw->pw_gecos);
787c478bd9Sstevel@tonic-gate 	if (pw->pw_dir) free(pw->pw_dir);
797c478bd9Sstevel@tonic-gate 	if (pw->pw_shell) free(pw->pw_shell);
807c478bd9Sstevel@tonic-gate 	free(pw);
817c478bd9Sstevel@tonic-gate }
827c478bd9Sstevel@tonic-gate 
837c478bd9Sstevel@tonic-gate void
847c478bd9Sstevel@tonic-gate free_spwd(struct spwd *spw)
857c478bd9Sstevel@tonic-gate {
867c478bd9Sstevel@tonic-gate 	if (spw->sp_namp) free(spw->sp_namp);
877c478bd9Sstevel@tonic-gate 	if (spw->sp_pwdp) free(spw->sp_pwdp);
887c478bd9Sstevel@tonic-gate 	free(spw);
897c478bd9Sstevel@tonic-gate }
907c478bd9Sstevel@tonic-gate 
917c478bd9Sstevel@tonic-gate int
927c478bd9Sstevel@tonic-gate dup_pw(struct passwd **d, struct passwd *s)
937c478bd9Sstevel@tonic-gate {
947c478bd9Sstevel@tonic-gate 	if (s == NULL) {
957c478bd9Sstevel@tonic-gate 		*d = NULL;
967c478bd9Sstevel@tonic-gate 		return (PWU_NOT_FOUND);
977c478bd9Sstevel@tonic-gate 	}
987c478bd9Sstevel@tonic-gate 	if ((*d = calloc(1, sizeof (**d))) == NULL)
997c478bd9Sstevel@tonic-gate 		return (PWU_NOMEM);
1007c478bd9Sstevel@tonic-gate 
1017c478bd9Sstevel@tonic-gate 	if (s->pw_name) {
1027c478bd9Sstevel@tonic-gate 		if (((*d)->pw_name = strdup(s->pw_name)) == NULL)
1037c478bd9Sstevel@tonic-gate 			goto no_mem;
1047c478bd9Sstevel@tonic-gate 	}
1057c478bd9Sstevel@tonic-gate 	if (s->pw_passwd) {
1067c478bd9Sstevel@tonic-gate 		if (((*d)->pw_passwd = strdup(s->pw_passwd)) == NULL)
1077c478bd9Sstevel@tonic-gate 			goto no_mem;
1087c478bd9Sstevel@tonic-gate 	}
1097c478bd9Sstevel@tonic-gate 	(*d)->pw_uid = s->pw_uid;
1107c478bd9Sstevel@tonic-gate 	(*d)->pw_gid = s->pw_gid;
1117c478bd9Sstevel@tonic-gate 
1127c478bd9Sstevel@tonic-gate 	if (s->pw_gecos) {
1137c478bd9Sstevel@tonic-gate 		if (((*d)->pw_gecos = strdup(s->pw_gecos)) == NULL)
1147c478bd9Sstevel@tonic-gate 			goto no_mem;
1157c478bd9Sstevel@tonic-gate 	}
1167c478bd9Sstevel@tonic-gate 	if (s->pw_dir) {
1177c478bd9Sstevel@tonic-gate 		if (((*d)->pw_dir = strdup(s->pw_dir)) == NULL)
1187c478bd9Sstevel@tonic-gate 			goto no_mem;
1197c478bd9Sstevel@tonic-gate 	}
1207c478bd9Sstevel@tonic-gate 	if (s->pw_shell) {
1217c478bd9Sstevel@tonic-gate 		if (((*d)->pw_shell = strdup(s->pw_shell)) == NULL)
1227c478bd9Sstevel@tonic-gate 			goto no_mem;
1237c478bd9Sstevel@tonic-gate 	}
1247c478bd9Sstevel@tonic-gate 
1257c478bd9Sstevel@tonic-gate 	return (PWU_SUCCESS);
1267c478bd9Sstevel@tonic-gate 
1277c478bd9Sstevel@tonic-gate no_mem:
1287c478bd9Sstevel@tonic-gate 	free_pwd(*d);
1297c478bd9Sstevel@tonic-gate 	*d = NULL;
1307c478bd9Sstevel@tonic-gate 	return (PWU_NOMEM);
1317c478bd9Sstevel@tonic-gate }
1327c478bd9Sstevel@tonic-gate 
1337c478bd9Sstevel@tonic-gate int
1347c478bd9Sstevel@tonic-gate dup_spw(struct spwd **d, struct spwd *s)
1357c478bd9Sstevel@tonic-gate {
1367c478bd9Sstevel@tonic-gate 	if (s == NULL) {
1377c478bd9Sstevel@tonic-gate 		*d = NULL;
1387c478bd9Sstevel@tonic-gate 		return (PWU_NOT_FOUND);
1397c478bd9Sstevel@tonic-gate 	}
1407c478bd9Sstevel@tonic-gate 	if ((*d = calloc(1, sizeof (**d))) == NULL)
1417c478bd9Sstevel@tonic-gate 		return (PWU_NOMEM);
1427c478bd9Sstevel@tonic-gate 
1437c478bd9Sstevel@tonic-gate 	**d = *s;
1447c478bd9Sstevel@tonic-gate 
1457c478bd9Sstevel@tonic-gate 	if (s->sp_namp)
1467c478bd9Sstevel@tonic-gate 		if (((*d)->sp_namp = strdup(s->sp_namp)) == NULL)
1477c478bd9Sstevel@tonic-gate 			goto no_mem;
1487c478bd9Sstevel@tonic-gate 	if (s->sp_pwdp)
1497c478bd9Sstevel@tonic-gate 		if (((*d)->sp_pwdp = strdup(s->sp_pwdp)) == NULL)
1507c478bd9Sstevel@tonic-gate 			goto no_mem;
1517c478bd9Sstevel@tonic-gate 	return (PWU_SUCCESS);
1527c478bd9Sstevel@tonic-gate 
1537c478bd9Sstevel@tonic-gate no_mem:
1547c478bd9Sstevel@tonic-gate 	free_spwd(*d);
1557c478bd9Sstevel@tonic-gate 	return (PWU_NOMEM);
1567c478bd9Sstevel@tonic-gate }
1577c478bd9Sstevel@tonic-gate 
1587c478bd9Sstevel@tonic-gate /*
1597c478bd9Sstevel@tonic-gate  * read a value from the defaults file, and return it if it is
1607c478bd9Sstevel@tonic-gate  * a positive integer. If the value is not defined, or negative,
1617c478bd9Sstevel@tonic-gate  * return the supplied default value
1627c478bd9Sstevel@tonic-gate  */
1637c478bd9Sstevel@tonic-gate int
1647c478bd9Sstevel@tonic-gate def_getuint(char *name, int defvalue)
1657c478bd9Sstevel@tonic-gate {
1667c478bd9Sstevel@tonic-gate 	char *p;
1677c478bd9Sstevel@tonic-gate 	int val = -1;	/* -1 is a guard to catch undefined values */
1687c478bd9Sstevel@tonic-gate 
1697c478bd9Sstevel@tonic-gate 	if (p = defread(name))
1707c478bd9Sstevel@tonic-gate 		val = atoi(p);
1717c478bd9Sstevel@tonic-gate 
1727c478bd9Sstevel@tonic-gate 	return (val >= 0 ? val : defvalue);
1737c478bd9Sstevel@tonic-gate }
1747c478bd9Sstevel@tonic-gate 
1757c478bd9Sstevel@tonic-gate void
1767c478bd9Sstevel@tonic-gate turn_on_default_aging(struct spwd *spw)
1777c478bd9Sstevel@tonic-gate {
1787c478bd9Sstevel@tonic-gate 	int minweeks;
1797c478bd9Sstevel@tonic-gate 	int maxweeks;
1807c478bd9Sstevel@tonic-gate 	int warnweeks;
1817c478bd9Sstevel@tonic-gate 
1827c478bd9Sstevel@tonic-gate 	if (defopen(PWADMIN) != 0) {
1837c478bd9Sstevel@tonic-gate 		minweeks = MINWEEKS;
1847c478bd9Sstevel@tonic-gate 		maxweeks = MAXWEEKS;
1857c478bd9Sstevel@tonic-gate 		warnweeks = WARNWEEKS;
1867c478bd9Sstevel@tonic-gate 	} else {
1877c478bd9Sstevel@tonic-gate 		minweeks = def_getuint("MINWEEKS=", MINWEEKS);
1887c478bd9Sstevel@tonic-gate 		maxweeks = def_getuint("MAXWEEKS=", MAXWEEKS);
1897c478bd9Sstevel@tonic-gate 		warnweeks = def_getuint("WARNWEEKS=", WARNWEEKS);
1907c478bd9Sstevel@tonic-gate 		(void) defopen(NULL);
1917c478bd9Sstevel@tonic-gate 	}
1927c478bd9Sstevel@tonic-gate 
1937c478bd9Sstevel@tonic-gate 	/*
1947c478bd9Sstevel@tonic-gate 	 * The values specified in /etc/default/passwd are interpreted
1957c478bd9Sstevel@tonic-gate 	 * in a specific way. Special cases are
1967c478bd9Sstevel@tonic-gate 	 *   MINWEEKS==0 (results in sp_min = -1)
1977c478bd9Sstevel@tonic-gate 	 *   MAXWEEKS==0 (results in sp_max = default)
1987c478bd9Sstevel@tonic-gate 	 */
1997c478bd9Sstevel@tonic-gate 	spw->sp_min = 7 * minweeks;
2007c478bd9Sstevel@tonic-gate 	if (spw->sp_min <= 0)
2017c478bd9Sstevel@tonic-gate 		spw->sp_min = -1;
2027c478bd9Sstevel@tonic-gate 
2037c478bd9Sstevel@tonic-gate 	spw->sp_max = 7 * maxweeks;
2047c478bd9Sstevel@tonic-gate 	if (spw->sp_max == 0)
2057c478bd9Sstevel@tonic-gate 		spw->sp_max = 7 * MAXWEEKS;
2067c478bd9Sstevel@tonic-gate 	if (spw->sp_max < 0)
2077c478bd9Sstevel@tonic-gate 		spw->sp_max = -1;
2087c478bd9Sstevel@tonic-gate 
2097c478bd9Sstevel@tonic-gate 	spw->sp_warn = 7 * warnweeks;
2107c478bd9Sstevel@tonic-gate 	if (spw->sp_warn <= 0)
2117c478bd9Sstevel@tonic-gate 		spw->sp_warn = -1;
2127c478bd9Sstevel@tonic-gate }
2137c478bd9Sstevel@tonic-gate 
2147c478bd9Sstevel@tonic-gate /*
2157c478bd9Sstevel@tonic-gate  * open and read a value from the defaults file,
2167c478bd9Sstevel@tonic-gate  * return value found or default value if not found.
2177c478bd9Sstevel@tonic-gate  */
2187c478bd9Sstevel@tonic-gate int
2197c478bd9Sstevel@tonic-gate def_getint(char *name, int defvalue)
2207c478bd9Sstevel@tonic-gate {
2217c478bd9Sstevel@tonic-gate 	int	val;
2227c478bd9Sstevel@tonic-gate 
2237c478bd9Sstevel@tonic-gate 	if (defopen(PWADMIN) != 0)
2247c478bd9Sstevel@tonic-gate 		val = defvalue;
2257c478bd9Sstevel@tonic-gate 	else
2267c478bd9Sstevel@tonic-gate 		val = def_getuint(name, defvalue);
2277c478bd9Sstevel@tonic-gate 
2287c478bd9Sstevel@tonic-gate 	(void) defopen(NULL);
2297c478bd9Sstevel@tonic-gate 	return (val);
2307c478bd9Sstevel@tonic-gate }
231