xref: /titanic_52/usr/src/lib/passwdutil/utils.c (revision b9175c69691c8949bec97fb8f689b7d1efdb05bb)
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
5*b9175c69SKenjiro Tsuji  * Common Development and Distribution License (the "License").
6*b9175c69SKenjiro Tsuji  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22*b9175c69SKenjiro Tsuji  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate #include <sys/types.h>
277c478bd9Sstevel@tonic-gate #include <sys/time.h>
287c478bd9Sstevel@tonic-gate #include <string.h>
297c478bd9Sstevel@tonic-gate #include <thread.h>
307c478bd9Sstevel@tonic-gate #include <unistd.h>
317c478bd9Sstevel@tonic-gate #include <stdlib.h>
327c478bd9Sstevel@tonic-gate #include <crypt.h>
337c478bd9Sstevel@tonic-gate #include <pwd.h>
347c478bd9Sstevel@tonic-gate #include <shadow.h>
357c478bd9Sstevel@tonic-gate 
367c478bd9Sstevel@tonic-gate #include <deflt.h>
377c478bd9Sstevel@tonic-gate 
387c478bd9Sstevel@tonic-gate #include "passwdutil.h"
397c478bd9Sstevel@tonic-gate 
407c478bd9Sstevel@tonic-gate #define	PWADMIN "/etc/default/passwd"
417c478bd9Sstevel@tonic-gate 
427c478bd9Sstevel@tonic-gate #define	MINWEEKS	-1
437c478bd9Sstevel@tonic-gate #define	MAXWEEKS	-1
447c478bd9Sstevel@tonic-gate #define	WARNWEEKS	-1
457c478bd9Sstevel@tonic-gate 
4603c65128Swy83408 extern repops_t files_repops, nis_repops,
4703c65128Swy83408 	nisplus_repops, ldap_repops, nss_repops;
4803c65128Swy83408 
4903c65128Swy83408 repops_t *rops[REP_LAST+1] = {
5003c65128Swy83408 	NULL,
5103c65128Swy83408 	&files_repops,
5203c65128Swy83408 	&nis_repops,
5303c65128Swy83408 	NULL,
5403c65128Swy83408 	&nisplus_repops,
5503c65128Swy83408 	NULL,
5603c65128Swy83408 	NULL,
5703c65128Swy83408 	NULL,
5803c65128Swy83408 	&ldap_repops,
5903c65128Swy83408 	NULL,
6003c65128Swy83408 	NULL,
6103c65128Swy83408 	NULL,
6203c65128Swy83408 	NULL,
6303c65128Swy83408 	NULL,
6403c65128Swy83408 	NULL,
6503c65128Swy83408 	NULL,
6603c65128Swy83408 	&nss_repops,
6703c65128Swy83408 };
6803c65128Swy83408 
697c478bd9Sstevel@tonic-gate void
707c478bd9Sstevel@tonic-gate free_pwd(struct passwd *pw)
717c478bd9Sstevel@tonic-gate {
727c478bd9Sstevel@tonic-gate 	if (pw->pw_name) free(pw->pw_name);
737c478bd9Sstevel@tonic-gate 	if (pw->pw_passwd) free(pw->pw_passwd);
747c478bd9Sstevel@tonic-gate 	if (pw->pw_gecos) free(pw->pw_gecos);
757c478bd9Sstevel@tonic-gate 	if (pw->pw_dir) free(pw->pw_dir);
767c478bd9Sstevel@tonic-gate 	if (pw->pw_shell) free(pw->pw_shell);
777c478bd9Sstevel@tonic-gate 	free(pw);
787c478bd9Sstevel@tonic-gate }
797c478bd9Sstevel@tonic-gate 
807c478bd9Sstevel@tonic-gate void
817c478bd9Sstevel@tonic-gate free_spwd(struct spwd *spw)
827c478bd9Sstevel@tonic-gate {
837c478bd9Sstevel@tonic-gate 	if (spw->sp_namp) free(spw->sp_namp);
847c478bd9Sstevel@tonic-gate 	if (spw->sp_pwdp) free(spw->sp_pwdp);
857c478bd9Sstevel@tonic-gate 	free(spw);
867c478bd9Sstevel@tonic-gate }
877c478bd9Sstevel@tonic-gate 
887c478bd9Sstevel@tonic-gate int
897c478bd9Sstevel@tonic-gate dup_pw(struct passwd **d, struct passwd *s)
907c478bd9Sstevel@tonic-gate {
917c478bd9Sstevel@tonic-gate 	if (s == NULL) {
927c478bd9Sstevel@tonic-gate 		*d = NULL;
937c478bd9Sstevel@tonic-gate 		return (PWU_NOT_FOUND);
947c478bd9Sstevel@tonic-gate 	}
957c478bd9Sstevel@tonic-gate 	if ((*d = calloc(1, sizeof (**d))) == NULL)
967c478bd9Sstevel@tonic-gate 		return (PWU_NOMEM);
977c478bd9Sstevel@tonic-gate 
987c478bd9Sstevel@tonic-gate 	if (s->pw_name) {
997c478bd9Sstevel@tonic-gate 		if (((*d)->pw_name = strdup(s->pw_name)) == NULL)
1007c478bd9Sstevel@tonic-gate 			goto no_mem;
1017c478bd9Sstevel@tonic-gate 	}
1027c478bd9Sstevel@tonic-gate 	if (s->pw_passwd) {
1037c478bd9Sstevel@tonic-gate 		if (((*d)->pw_passwd = strdup(s->pw_passwd)) == NULL)
1047c478bd9Sstevel@tonic-gate 			goto no_mem;
1057c478bd9Sstevel@tonic-gate 	}
1067c478bd9Sstevel@tonic-gate 	(*d)->pw_uid = s->pw_uid;
1077c478bd9Sstevel@tonic-gate 	(*d)->pw_gid = s->pw_gid;
1087c478bd9Sstevel@tonic-gate 
1097c478bd9Sstevel@tonic-gate 	if (s->pw_gecos) {
1107c478bd9Sstevel@tonic-gate 		if (((*d)->pw_gecos = strdup(s->pw_gecos)) == NULL)
1117c478bd9Sstevel@tonic-gate 			goto no_mem;
1127c478bd9Sstevel@tonic-gate 	}
1137c478bd9Sstevel@tonic-gate 	if (s->pw_dir) {
1147c478bd9Sstevel@tonic-gate 		if (((*d)->pw_dir = strdup(s->pw_dir)) == NULL)
1157c478bd9Sstevel@tonic-gate 			goto no_mem;
1167c478bd9Sstevel@tonic-gate 	}
1177c478bd9Sstevel@tonic-gate 	if (s->pw_shell) {
1187c478bd9Sstevel@tonic-gate 		if (((*d)->pw_shell = strdup(s->pw_shell)) == NULL)
1197c478bd9Sstevel@tonic-gate 			goto no_mem;
1207c478bd9Sstevel@tonic-gate 	}
1217c478bd9Sstevel@tonic-gate 
1227c478bd9Sstevel@tonic-gate 	return (PWU_SUCCESS);
1237c478bd9Sstevel@tonic-gate 
1247c478bd9Sstevel@tonic-gate no_mem:
1257c478bd9Sstevel@tonic-gate 	free_pwd(*d);
1267c478bd9Sstevel@tonic-gate 	*d = NULL;
1277c478bd9Sstevel@tonic-gate 	return (PWU_NOMEM);
1287c478bd9Sstevel@tonic-gate }
1297c478bd9Sstevel@tonic-gate 
1307c478bd9Sstevel@tonic-gate int
1317c478bd9Sstevel@tonic-gate dup_spw(struct spwd **d, struct spwd *s)
1327c478bd9Sstevel@tonic-gate {
1337c478bd9Sstevel@tonic-gate 	if (s == NULL) {
1347c478bd9Sstevel@tonic-gate 		*d = NULL;
1357c478bd9Sstevel@tonic-gate 		return (PWU_NOT_FOUND);
1367c478bd9Sstevel@tonic-gate 	}
1377c478bd9Sstevel@tonic-gate 	if ((*d = calloc(1, sizeof (**d))) == NULL)
1387c478bd9Sstevel@tonic-gate 		return (PWU_NOMEM);
1397c478bd9Sstevel@tonic-gate 
1407c478bd9Sstevel@tonic-gate 	**d = *s;
1417c478bd9Sstevel@tonic-gate 
1427c478bd9Sstevel@tonic-gate 	if (s->sp_namp)
1437c478bd9Sstevel@tonic-gate 		if (((*d)->sp_namp = strdup(s->sp_namp)) == NULL)
1447c478bd9Sstevel@tonic-gate 			goto no_mem;
1457c478bd9Sstevel@tonic-gate 	if (s->sp_pwdp)
1467c478bd9Sstevel@tonic-gate 		if (((*d)->sp_pwdp = strdup(s->sp_pwdp)) == NULL)
1477c478bd9Sstevel@tonic-gate 			goto no_mem;
1487c478bd9Sstevel@tonic-gate 	return (PWU_SUCCESS);
1497c478bd9Sstevel@tonic-gate 
1507c478bd9Sstevel@tonic-gate no_mem:
1517c478bd9Sstevel@tonic-gate 	free_spwd(*d);
1527c478bd9Sstevel@tonic-gate 	return (PWU_NOMEM);
1537c478bd9Sstevel@tonic-gate }
1547c478bd9Sstevel@tonic-gate 
1557c478bd9Sstevel@tonic-gate /*
1567c478bd9Sstevel@tonic-gate  * read a value from the defaults file, and return it if it is
1577c478bd9Sstevel@tonic-gate  * a positive integer. If the value is not defined, or negative,
1587c478bd9Sstevel@tonic-gate  * return the supplied default value
1597c478bd9Sstevel@tonic-gate  */
1607c478bd9Sstevel@tonic-gate int
161*b9175c69SKenjiro Tsuji def_getuint(char *name, int defvalue, void *defp)
1627c478bd9Sstevel@tonic-gate {
1637c478bd9Sstevel@tonic-gate 	char *p;
1647c478bd9Sstevel@tonic-gate 	int val = -1;	/* -1 is a guard to catch undefined values */
1657c478bd9Sstevel@tonic-gate 
166*b9175c69SKenjiro Tsuji 	if ((p = defread_r(name, defp)) != NULL)
1677c478bd9Sstevel@tonic-gate 		val = atoi(p);
1687c478bd9Sstevel@tonic-gate 
1697c478bd9Sstevel@tonic-gate 	return (val >= 0 ? val : defvalue);
1707c478bd9Sstevel@tonic-gate }
1717c478bd9Sstevel@tonic-gate 
1727c478bd9Sstevel@tonic-gate void
1737c478bd9Sstevel@tonic-gate turn_on_default_aging(struct spwd *spw)
1747c478bd9Sstevel@tonic-gate {
1757c478bd9Sstevel@tonic-gate 	int minweeks;
1767c478bd9Sstevel@tonic-gate 	int maxweeks;
1777c478bd9Sstevel@tonic-gate 	int warnweeks;
178*b9175c69SKenjiro Tsuji 	void	*defp;
1797c478bd9Sstevel@tonic-gate 
180*b9175c69SKenjiro Tsuji 	if ((defp = defopen_r(PWADMIN)) == NULL) {
1817c478bd9Sstevel@tonic-gate 		minweeks = MINWEEKS;
1827c478bd9Sstevel@tonic-gate 		maxweeks = MAXWEEKS;
1837c478bd9Sstevel@tonic-gate 		warnweeks = WARNWEEKS;
1847c478bd9Sstevel@tonic-gate 	} else {
185*b9175c69SKenjiro Tsuji 		minweeks = def_getuint("MINWEEKS=", MINWEEKS, defp);
186*b9175c69SKenjiro Tsuji 		maxweeks = def_getuint("MAXWEEKS=", MAXWEEKS, defp);
187*b9175c69SKenjiro Tsuji 		warnweeks = def_getuint("WARNWEEKS=", WARNWEEKS, defp);
188*b9175c69SKenjiro Tsuji 		defclose_r(defp);
1897c478bd9Sstevel@tonic-gate 	}
1907c478bd9Sstevel@tonic-gate 
1917c478bd9Sstevel@tonic-gate 	/*
1927c478bd9Sstevel@tonic-gate 	 * The values specified in /etc/default/passwd are interpreted
1937c478bd9Sstevel@tonic-gate 	 * in a specific way. Special cases are
1947c478bd9Sstevel@tonic-gate 	 *   MINWEEKS==0 (results in sp_min = -1)
1957c478bd9Sstevel@tonic-gate 	 *   MAXWEEKS==0 (results in sp_max = default)
1967c478bd9Sstevel@tonic-gate 	 */
1977c478bd9Sstevel@tonic-gate 	spw->sp_min = 7 * minweeks;
1987c478bd9Sstevel@tonic-gate 	if (spw->sp_min <= 0)
1997c478bd9Sstevel@tonic-gate 		spw->sp_min = -1;
2007c478bd9Sstevel@tonic-gate 
2017c478bd9Sstevel@tonic-gate 	spw->sp_max = 7 * maxweeks;
2027c478bd9Sstevel@tonic-gate 	if (spw->sp_max == 0)
2037c478bd9Sstevel@tonic-gate 		spw->sp_max = 7 * MAXWEEKS;
2047c478bd9Sstevel@tonic-gate 	if (spw->sp_max < 0)
2057c478bd9Sstevel@tonic-gate 		spw->sp_max = -1;
2067c478bd9Sstevel@tonic-gate 
2077c478bd9Sstevel@tonic-gate 	spw->sp_warn = 7 * warnweeks;
2087c478bd9Sstevel@tonic-gate 	if (spw->sp_warn <= 0)
2097c478bd9Sstevel@tonic-gate 		spw->sp_warn = -1;
2107c478bd9Sstevel@tonic-gate }
2117c478bd9Sstevel@tonic-gate 
2127c478bd9Sstevel@tonic-gate /*
2137c478bd9Sstevel@tonic-gate  * open and read a value from the defaults file,
2147c478bd9Sstevel@tonic-gate  * return value found or default value if not found.
2157c478bd9Sstevel@tonic-gate  */
2167c478bd9Sstevel@tonic-gate int
2177c478bd9Sstevel@tonic-gate def_getint(char *name, int defvalue)
2187c478bd9Sstevel@tonic-gate {
2197c478bd9Sstevel@tonic-gate 	int	val;
220*b9175c69SKenjiro Tsuji 	void	*defp;
2217c478bd9Sstevel@tonic-gate 
222*b9175c69SKenjiro Tsuji 	if ((defp = defopen_r(PWADMIN)) == NULL) {
2237c478bd9Sstevel@tonic-gate 		val = defvalue;
224*b9175c69SKenjiro Tsuji 	} else {
225*b9175c69SKenjiro Tsuji 		val = def_getuint(name, defvalue, defp);
226*b9175c69SKenjiro Tsuji 		defclose_r(defp);
227*b9175c69SKenjiro Tsuji 	}
2287c478bd9Sstevel@tonic-gate 
2297c478bd9Sstevel@tonic-gate 	return (val);
2307c478bd9Sstevel@tonic-gate }
231