xref: /illumos-gate/usr/src/lib/passwdutil/utils.c (revision 36e852a1)
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
5b9175c69SKenjiro Tsuji  * Common Development and Distribution License (the "License").
6b9175c69SKenjiro 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 /*
22b9175c69SKenjiro 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 
46*36e852a1SRaja Andra extern repops_t files_repops, nis_repops, ldap_repops, nss_repops;
4703c65128Swy 
4803c65128Swy repops_t *rops[REP_LAST+1] = {
4903c65128Swy 	NULL,
5003c65128Swy 	&files_repops,
5103c65128Swy 	&nis_repops,
5203c65128Swy 	NULL,
5303c65128Swy 	&ldap_repops,
5403c65128Swy 	NULL,
5503c65128Swy 	NULL,
5603c65128Swy 	NULL,
5703c65128Swy 	&nss_repops,
5803c65128Swy };
5903c65128Swy 
607c478bd9Sstevel@tonic-gate void
free_pwd(struct passwd * pw)617c478bd9Sstevel@tonic-gate free_pwd(struct passwd *pw)
627c478bd9Sstevel@tonic-gate {
637c478bd9Sstevel@tonic-gate 	if (pw->pw_name) free(pw->pw_name);
647c478bd9Sstevel@tonic-gate 	if (pw->pw_passwd) free(pw->pw_passwd);
657c478bd9Sstevel@tonic-gate 	if (pw->pw_gecos) free(pw->pw_gecos);
667c478bd9Sstevel@tonic-gate 	if (pw->pw_dir) free(pw->pw_dir);
677c478bd9Sstevel@tonic-gate 	if (pw->pw_shell) free(pw->pw_shell);
687c478bd9Sstevel@tonic-gate 	free(pw);
697c478bd9Sstevel@tonic-gate }
707c478bd9Sstevel@tonic-gate 
717c478bd9Sstevel@tonic-gate void
free_spwd(struct spwd * spw)727c478bd9Sstevel@tonic-gate free_spwd(struct spwd *spw)
737c478bd9Sstevel@tonic-gate {
747c478bd9Sstevel@tonic-gate 	if (spw->sp_namp) free(spw->sp_namp);
757c478bd9Sstevel@tonic-gate 	if (spw->sp_pwdp) free(spw->sp_pwdp);
767c478bd9Sstevel@tonic-gate 	free(spw);
777c478bd9Sstevel@tonic-gate }
787c478bd9Sstevel@tonic-gate 
797c478bd9Sstevel@tonic-gate int
dup_pw(struct passwd ** d,struct passwd * s)807c478bd9Sstevel@tonic-gate dup_pw(struct passwd **d, struct passwd *s)
817c478bd9Sstevel@tonic-gate {
827c478bd9Sstevel@tonic-gate 	if (s == NULL) {
837c478bd9Sstevel@tonic-gate 		*d = NULL;
847c478bd9Sstevel@tonic-gate 		return (PWU_NOT_FOUND);
857c478bd9Sstevel@tonic-gate 	}
867c478bd9Sstevel@tonic-gate 	if ((*d = calloc(1, sizeof (**d))) == NULL)
877c478bd9Sstevel@tonic-gate 		return (PWU_NOMEM);
887c478bd9Sstevel@tonic-gate 
897c478bd9Sstevel@tonic-gate 	if (s->pw_name) {
907c478bd9Sstevel@tonic-gate 		if (((*d)->pw_name = strdup(s->pw_name)) == NULL)
917c478bd9Sstevel@tonic-gate 			goto no_mem;
927c478bd9Sstevel@tonic-gate 	}
937c478bd9Sstevel@tonic-gate 	if (s->pw_passwd) {
947c478bd9Sstevel@tonic-gate 		if (((*d)->pw_passwd = strdup(s->pw_passwd)) == NULL)
957c478bd9Sstevel@tonic-gate 			goto no_mem;
967c478bd9Sstevel@tonic-gate 	}
977c478bd9Sstevel@tonic-gate 	(*d)->pw_uid = s->pw_uid;
987c478bd9Sstevel@tonic-gate 	(*d)->pw_gid = s->pw_gid;
997c478bd9Sstevel@tonic-gate 
1007c478bd9Sstevel@tonic-gate 	if (s->pw_gecos) {
1017c478bd9Sstevel@tonic-gate 		if (((*d)->pw_gecos = strdup(s->pw_gecos)) == NULL)
1027c478bd9Sstevel@tonic-gate 			goto no_mem;
1037c478bd9Sstevel@tonic-gate 	}
1047c478bd9Sstevel@tonic-gate 	if (s->pw_dir) {
1057c478bd9Sstevel@tonic-gate 		if (((*d)->pw_dir = strdup(s->pw_dir)) == NULL)
1067c478bd9Sstevel@tonic-gate 			goto no_mem;
1077c478bd9Sstevel@tonic-gate 	}
1087c478bd9Sstevel@tonic-gate 	if (s->pw_shell) {
1097c478bd9Sstevel@tonic-gate 		if (((*d)->pw_shell = strdup(s->pw_shell)) == NULL)
1107c478bd9Sstevel@tonic-gate 			goto no_mem;
1117c478bd9Sstevel@tonic-gate 	}
1127c478bd9Sstevel@tonic-gate 
1137c478bd9Sstevel@tonic-gate 	return (PWU_SUCCESS);
1147c478bd9Sstevel@tonic-gate 
1157c478bd9Sstevel@tonic-gate no_mem:
1167c478bd9Sstevel@tonic-gate 	free_pwd(*d);
1177c478bd9Sstevel@tonic-gate 	*d = NULL;
1187c478bd9Sstevel@tonic-gate 	return (PWU_NOMEM);
1197c478bd9Sstevel@tonic-gate }
1207c478bd9Sstevel@tonic-gate 
1217c478bd9Sstevel@tonic-gate int
dup_spw(struct spwd ** d,struct spwd * s)1227c478bd9Sstevel@tonic-gate dup_spw(struct spwd **d, struct spwd *s)
1237c478bd9Sstevel@tonic-gate {
1247c478bd9Sstevel@tonic-gate 	if (s == NULL) {
1257c478bd9Sstevel@tonic-gate 		*d = NULL;
1267c478bd9Sstevel@tonic-gate 		return (PWU_NOT_FOUND);
1277c478bd9Sstevel@tonic-gate 	}
1287c478bd9Sstevel@tonic-gate 	if ((*d = calloc(1, sizeof (**d))) == NULL)
1297c478bd9Sstevel@tonic-gate 		return (PWU_NOMEM);
1307c478bd9Sstevel@tonic-gate 
1317c478bd9Sstevel@tonic-gate 	**d = *s;
1327c478bd9Sstevel@tonic-gate 
1337c478bd9Sstevel@tonic-gate 	if (s->sp_namp)
1347c478bd9Sstevel@tonic-gate 		if (((*d)->sp_namp = strdup(s->sp_namp)) == NULL)
1357c478bd9Sstevel@tonic-gate 			goto no_mem;
1367c478bd9Sstevel@tonic-gate 	if (s->sp_pwdp)
1377c478bd9Sstevel@tonic-gate 		if (((*d)->sp_pwdp = strdup(s->sp_pwdp)) == NULL)
1387c478bd9Sstevel@tonic-gate 			goto no_mem;
1397c478bd9Sstevel@tonic-gate 	return (PWU_SUCCESS);
1407c478bd9Sstevel@tonic-gate 
1417c478bd9Sstevel@tonic-gate no_mem:
1427c478bd9Sstevel@tonic-gate 	free_spwd(*d);
1437c478bd9Sstevel@tonic-gate 	return (PWU_NOMEM);
1447c478bd9Sstevel@tonic-gate }
1457c478bd9Sstevel@tonic-gate 
1467c478bd9Sstevel@tonic-gate /*
1477c478bd9Sstevel@tonic-gate  * read a value from the defaults file, and return it if it is
1487c478bd9Sstevel@tonic-gate  * a positive integer. If the value is not defined, or negative,
1497c478bd9Sstevel@tonic-gate  * return the supplied default value
1507c478bd9Sstevel@tonic-gate  */
1517c478bd9Sstevel@tonic-gate int
def_getuint(char * name,int defvalue,void * defp)152b9175c69SKenjiro Tsuji def_getuint(char *name, int defvalue, void *defp)
1537c478bd9Sstevel@tonic-gate {
1547c478bd9Sstevel@tonic-gate 	char *p;
1557c478bd9Sstevel@tonic-gate 	int val = -1;	/* -1 is a guard to catch undefined values */
1567c478bd9Sstevel@tonic-gate 
157b9175c69SKenjiro Tsuji 	if ((p = defread_r(name, defp)) != NULL)
1587c478bd9Sstevel@tonic-gate 		val = atoi(p);
1597c478bd9Sstevel@tonic-gate 
1607c478bd9Sstevel@tonic-gate 	return (val >= 0 ? val : defvalue);
1617c478bd9Sstevel@tonic-gate }
1627c478bd9Sstevel@tonic-gate 
1637c478bd9Sstevel@tonic-gate void
turn_on_default_aging(struct spwd * spw)1647c478bd9Sstevel@tonic-gate turn_on_default_aging(struct spwd *spw)
1657c478bd9Sstevel@tonic-gate {
1667c478bd9Sstevel@tonic-gate 	int minweeks;
1677c478bd9Sstevel@tonic-gate 	int maxweeks;
1687c478bd9Sstevel@tonic-gate 	int warnweeks;
169b9175c69SKenjiro Tsuji 	void	*defp;
1707c478bd9Sstevel@tonic-gate 
171b9175c69SKenjiro Tsuji 	if ((defp = defopen_r(PWADMIN)) == NULL) {
1727c478bd9Sstevel@tonic-gate 		minweeks = MINWEEKS;
1737c478bd9Sstevel@tonic-gate 		maxweeks = MAXWEEKS;
1747c478bd9Sstevel@tonic-gate 		warnweeks = WARNWEEKS;
1757c478bd9Sstevel@tonic-gate 	} else {
176b9175c69SKenjiro Tsuji 		minweeks = def_getuint("MINWEEKS=", MINWEEKS, defp);
177b9175c69SKenjiro Tsuji 		maxweeks = def_getuint("MAXWEEKS=", MAXWEEKS, defp);
178b9175c69SKenjiro Tsuji 		warnweeks = def_getuint("WARNWEEKS=", WARNWEEKS, defp);
179b9175c69SKenjiro Tsuji 		defclose_r(defp);
1807c478bd9Sstevel@tonic-gate 	}
1817c478bd9Sstevel@tonic-gate 
1827c478bd9Sstevel@tonic-gate 	/*
1837c478bd9Sstevel@tonic-gate 	 * The values specified in /etc/default/passwd are interpreted
1847c478bd9Sstevel@tonic-gate 	 * in a specific way. Special cases are
1857c478bd9Sstevel@tonic-gate 	 *   MINWEEKS==0 (results in sp_min = -1)
1867c478bd9Sstevel@tonic-gate 	 *   MAXWEEKS==0 (results in sp_max = default)
1877c478bd9Sstevel@tonic-gate 	 */
1887c478bd9Sstevel@tonic-gate 	spw->sp_min = 7 * minweeks;
1897c478bd9Sstevel@tonic-gate 	if (spw->sp_min <= 0)
1907c478bd9Sstevel@tonic-gate 		spw->sp_min = -1;
1917c478bd9Sstevel@tonic-gate 
1927c478bd9Sstevel@tonic-gate 	spw->sp_max = 7 * maxweeks;
1937c478bd9Sstevel@tonic-gate 	if (spw->sp_max == 0)
1947c478bd9Sstevel@tonic-gate 		spw->sp_max = 7 * MAXWEEKS;
1957c478bd9Sstevel@tonic-gate 	if (spw->sp_max < 0)
1967c478bd9Sstevel@tonic-gate 		spw->sp_max = -1;
1977c478bd9Sstevel@tonic-gate 
1987c478bd9Sstevel@tonic-gate 	spw->sp_warn = 7 * warnweeks;
1997c478bd9Sstevel@tonic-gate 	if (spw->sp_warn <= 0)
2007c478bd9Sstevel@tonic-gate 		spw->sp_warn = -1;
2017c478bd9Sstevel@tonic-gate }
2027c478bd9Sstevel@tonic-gate 
2037c478bd9Sstevel@tonic-gate /*
2047c478bd9Sstevel@tonic-gate  * open and read a value from the defaults file,
2057c478bd9Sstevel@tonic-gate  * return value found or default value if not found.
2067c478bd9Sstevel@tonic-gate  */
2077c478bd9Sstevel@tonic-gate int
def_getint(char * name,int defvalue)2087c478bd9Sstevel@tonic-gate def_getint(char *name, int defvalue)
2097c478bd9Sstevel@tonic-gate {
2107c478bd9Sstevel@tonic-gate 	int	val;
211b9175c69SKenjiro Tsuji 	void	*defp;
2127c478bd9Sstevel@tonic-gate 
213b9175c69SKenjiro Tsuji 	if ((defp = defopen_r(PWADMIN)) == NULL) {
2147c478bd9Sstevel@tonic-gate 		val = defvalue;
215b9175c69SKenjiro Tsuji 	} else {
216b9175c69SKenjiro Tsuji 		val = def_getuint(name, defvalue, defp);
217b9175c69SKenjiro Tsuji 		defclose_r(defp);
218b9175c69SKenjiro Tsuji 	}
2197c478bd9Sstevel@tonic-gate 
2207c478bd9Sstevel@tonic-gate 	return (val);
2217c478bd9Sstevel@tonic-gate }
222