xref: /onnv-gate/usr/src/lib/pam_modules/authtok_check/dict.c (revision 9798:c54abbfdeb61)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
55890Sjjj  * Common Development and Distribution License (the "License").
65890Sjjj  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
22*9798SJoep.Vesseur@Sun.COM  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate #include <sys/stat.h>
270Sstevel@tonic-gate #include <stdio.h>
280Sstevel@tonic-gate #include <syslog.h>
290Sstevel@tonic-gate #include <fcntl.h>
300Sstevel@tonic-gate #include <time.h>
310Sstevel@tonic-gate #include <errno.h>
320Sstevel@tonic-gate #include <signal.h>
330Sstevel@tonic-gate #include "packer.h"
340Sstevel@tonic-gate 
350Sstevel@tonic-gate static int lockfd = -1;
360Sstevel@tonic-gate static struct flock flock = { 0, 0, 0, 0, 0, 0 };
370Sstevel@tonic-gate 
380Sstevel@tonic-gate char dblock[PATH_MAX];
390Sstevel@tonic-gate 
405890Sjjj #define	LOCK_WAIT	1000000
415890Sjjj #define	LOCK_RETRIES	60
420Sstevel@tonic-gate 
430Sstevel@tonic-gate /*
440Sstevel@tonic-gate  * lock_db()
450Sstevel@tonic-gate  *
460Sstevel@tonic-gate  * Create a lockfile to prevent simultaneous access to the database
470Sstevel@tonic-gate  * creation routines. We set a timeout to LOCK_WAIT seconds. If we
485890Sjjj  * haven't obtained a lock after LOCK_RETIRES attempts, we bail out.
490Sstevel@tonic-gate  *
500Sstevel@tonic-gate  * returns 0 on succes, -1 on (lock) failure.
510Sstevel@tonic-gate  * side effect: the directory "path" will be created if it didn't exist.
520Sstevel@tonic-gate  */
530Sstevel@tonic-gate int
lock_db(char * path)540Sstevel@tonic-gate lock_db(char *path)
550Sstevel@tonic-gate {
560Sstevel@tonic-gate 	int retval;
570Sstevel@tonic-gate 	struct stat st;
585890Sjjj 	int retries = 0;
590Sstevel@tonic-gate 
600Sstevel@tonic-gate 	/* create directory "path" if it doesn't exist */
610Sstevel@tonic-gate 	if (stat(path, &st) == -1) {
620Sstevel@tonic-gate 		if (errno != ENOENT ||
630Sstevel@tonic-gate 		    (mkdir(path, 0755) == -1 || chmod(path, 0755) == -1))
640Sstevel@tonic-gate 			return (-1);
650Sstevel@tonic-gate 	}
660Sstevel@tonic-gate 
670Sstevel@tonic-gate 	(void) snprintf(dblock, sizeof (dblock), "%s/authtok_check.lock", path);
680Sstevel@tonic-gate 
690Sstevel@tonic-gate 	if ((lockfd = open(dblock, O_WRONLY|O_CREAT|O_EXCL, 0400)) == -1) {
700Sstevel@tonic-gate 		if (errno == EEXIST)
710Sstevel@tonic-gate 			lockfd = open(dblock, O_WRONLY);
720Sstevel@tonic-gate 		if (lockfd == -1) {
730Sstevel@tonic-gate 			int olderrno = errno;
740Sstevel@tonic-gate 			syslog(LOG_ERR, "pam_authtok_check::pam_sm_chauthtok: "
750Sstevel@tonic-gate 			    "can't open lockfile: %s", strerror(errno));
760Sstevel@tonic-gate 			errno = olderrno;
770Sstevel@tonic-gate 			return (-1);
780Sstevel@tonic-gate 		}
790Sstevel@tonic-gate 	}
800Sstevel@tonic-gate 
815890Sjjj 	do {
825890Sjjj 		flock.l_type = F_WRLCK;
835890Sjjj 		retval = fcntl(lockfd, F_SETLK, &flock);
845890Sjjj 		if (retval == -1)
855890Sjjj 			(void) usleep(LOCK_WAIT);
865890Sjjj 	} while (retval == -1 && ++retries < LOCK_RETRIES);
870Sstevel@tonic-gate 
885890Sjjj 	if (retval == -1) {
895890Sjjj 		int errno_saved = errno;
900Sstevel@tonic-gate 		syslog(LOG_ERR, "pam_authtok_check::pam_sm_chauthtok: timeout "
910Sstevel@tonic-gate 		    "waiting for dictionary lock.");
925890Sjjj 		errno = errno_saved;
930Sstevel@tonic-gate 	}
940Sstevel@tonic-gate 
950Sstevel@tonic-gate 	return (retval);
960Sstevel@tonic-gate }
970Sstevel@tonic-gate 
980Sstevel@tonic-gate /*
990Sstevel@tonic-gate  * unlock_db()
1000Sstevel@tonic-gate  *
1010Sstevel@tonic-gate  * Release the database lock
1020Sstevel@tonic-gate  */
1030Sstevel@tonic-gate void
unlock_db(void)1040Sstevel@tonic-gate unlock_db(void)
1050Sstevel@tonic-gate {
1060Sstevel@tonic-gate 	if (lockfd != -1) {
1070Sstevel@tonic-gate 		flock.l_type = F_UNLCK;
1080Sstevel@tonic-gate 		(void) fcntl(lockfd, F_SETLK, &flock);
1090Sstevel@tonic-gate 		(void) close(lockfd);
1100Sstevel@tonic-gate 		lockfd = -1;
1110Sstevel@tonic-gate 	}
1120Sstevel@tonic-gate }
1130Sstevel@tonic-gate 
1140Sstevel@tonic-gate /*
1150Sstevel@tonic-gate  * database_present()
1160Sstevel@tonic-gate  *
1170Sstevel@tonic-gate  * returns 0 if the database files are found, and the database size is
118*9798SJoep.Vesseur@Sun.COM  * greater than 0 and the database version matches the current version.
1190Sstevel@tonic-gate  */
1200Sstevel@tonic-gate int
database_present(char * path)1210Sstevel@tonic-gate database_present(char *path)
1220Sstevel@tonic-gate {
1230Sstevel@tonic-gate 	struct stat st;
1240Sstevel@tonic-gate 	char dict_hwm[PATH_MAX];
1250Sstevel@tonic-gate 	char dict_pwd[PATH_MAX];
1260Sstevel@tonic-gate 	char dict_pwi[PATH_MAX];
127*9798SJoep.Vesseur@Sun.COM 	PWDICT *dict;
1280Sstevel@tonic-gate 
1290Sstevel@tonic-gate 	(void) snprintf(dict_hwm, sizeof (dict_hwm), "%s/%s", path,
1300Sstevel@tonic-gate 	    DICT_DATABASE_HWM);
1310Sstevel@tonic-gate 	(void) snprintf(dict_pwd, sizeof (dict_pwd), "%s/%s", path,
1320Sstevel@tonic-gate 	    DICT_DATABASE_PWD);
1330Sstevel@tonic-gate 	(void) snprintf(dict_pwi, sizeof (dict_pwi), "%s/%s", path,
1340Sstevel@tonic-gate 	    DICT_DATABASE_PWI);
1350Sstevel@tonic-gate 
1360Sstevel@tonic-gate 	if (stat(dict_hwm, &st) == -1 ||
1370Sstevel@tonic-gate 	    (stat(dict_pwd, &st) == -1 || st.st_size == 0) ||
1380Sstevel@tonic-gate 	    stat(dict_pwi, &st) == -1)
1390Sstevel@tonic-gate 		return (NO_DICTDATABASE);
1400Sstevel@tonic-gate 
141*9798SJoep.Vesseur@Sun.COM 	/* verify database version number by trying to open it */
142*9798SJoep.Vesseur@Sun.COM 	if ((dict = PWOpen(path, "r")) == NULL) {
143*9798SJoep.Vesseur@Sun.COM 		/* the files are there, but an outdated version */
144*9798SJoep.Vesseur@Sun.COM 		PWRemove(path);
145*9798SJoep.Vesseur@Sun.COM 		return (NO_DICTDATABASE);
146*9798SJoep.Vesseur@Sun.COM 	}
147*9798SJoep.Vesseur@Sun.COM 	(void) PWClose(dict);
1480Sstevel@tonic-gate 	return (0);
1490Sstevel@tonic-gate }
1500Sstevel@tonic-gate 
1510Sstevel@tonic-gate /*
1520Sstevel@tonic-gate  * build_dict_database(list, char *path)
1530Sstevel@tonic-gate  *
1540Sstevel@tonic-gate  * Create the Crack Dictionary Database based on the list of sources
1550Sstevel@tonic-gate  * dictionaries specified in "list". Store the database in "path".
1560Sstevel@tonic-gate  */
1570Sstevel@tonic-gate int
build_dict_database(char * list,char * path)1580Sstevel@tonic-gate build_dict_database(char *list, char *path)
1590Sstevel@tonic-gate {
1600Sstevel@tonic-gate 	return (packer(list, path) == -1 ? DICTDATABASE_BUILD_ERR : 0);
1610Sstevel@tonic-gate }
1620Sstevel@tonic-gate 
1630Sstevel@tonic-gate /*
1640Sstevel@tonic-gate  * Rebuild the database in "path" if the database is older than one of the
1650Sstevel@tonic-gate  * files listed in "list", or older than the config-file PWADMIN.
1660Sstevel@tonic-gate  */
1670Sstevel@tonic-gate int
update_dict_database(char * list,char * path)1680Sstevel@tonic-gate update_dict_database(char *list, char *path)
1690Sstevel@tonic-gate {
1700Sstevel@tonic-gate 	struct stat st_db;
1710Sstevel@tonic-gate 	struct stat st_file;
1720Sstevel@tonic-gate 	char *buf;
1730Sstevel@tonic-gate 	char *listcopy;
1740Sstevel@tonic-gate 	boolean_t update_needed = B_FALSE;
1750Sstevel@tonic-gate 	char dbase_pwd[PATH_MAX];
1760Sstevel@tonic-gate 
1770Sstevel@tonic-gate 	(void) snprintf(dbase_pwd, sizeof (dbase_pwd), "%s/%s", path,
1780Sstevel@tonic-gate 	    DICT_DATABASE_PWD);
1790Sstevel@tonic-gate 
1800Sstevel@tonic-gate 	if (stat(dbase_pwd, &st_db) == -1)
1810Sstevel@tonic-gate 		return (DICTFILE_ERR);
1820Sstevel@tonic-gate 
1830Sstevel@tonic-gate 	if ((listcopy = strdup(list)) == NULL)
1840Sstevel@tonic-gate 		return (DICTFILE_ERR);
1850Sstevel@tonic-gate 
1860Sstevel@tonic-gate 	buf = strtok(listcopy,  "\t ,");
1870Sstevel@tonic-gate 
1880Sstevel@tonic-gate 	/* Compare mtime of each listed dictionary against DB mtime */
1890Sstevel@tonic-gate 	while (update_needed == B_FALSE && buf != NULL) {
1900Sstevel@tonic-gate 		if (stat(buf, &st_file) == -1) {
1910Sstevel@tonic-gate 			if (errno == ENOENT) {
1920Sstevel@tonic-gate 				syslog(LOG_ERR,
1930Sstevel@tonic-gate 				    "pam_authtok_check:update_dict_database: "
1940Sstevel@tonic-gate 				    "dictionary \"%s\" not present.", buf);
1950Sstevel@tonic-gate 			} else {
1960Sstevel@tonic-gate 				syslog(LOG_ERR,
1970Sstevel@tonic-gate 				    "pam_authtok_check:update_dict_database: "
1980Sstevel@tonic-gate 				    "stat(%s) failed: %s", buf,
1990Sstevel@tonic-gate 				    strerror(errno));
2000Sstevel@tonic-gate 			}
2010Sstevel@tonic-gate 			free(listcopy);
2020Sstevel@tonic-gate 			return (DICTFILE_ERR);
2030Sstevel@tonic-gate 		}
2040Sstevel@tonic-gate 		if (st_db.st_mtime < st_file.st_mtime)
2050Sstevel@tonic-gate 			update_needed = B_TRUE;	/* database out of date */
2060Sstevel@tonic-gate 		buf = strtok(NULL, "\t ,");
2070Sstevel@tonic-gate 	}
2080Sstevel@tonic-gate 
2090Sstevel@tonic-gate 	free(listcopy);
2100Sstevel@tonic-gate 
2110Sstevel@tonic-gate 	/*
2120Sstevel@tonic-gate 	 * If /etc/default/passwd is updated, generate the database.
2130Sstevel@tonic-gate 	 * Because this is the only way we can check if files have been
2140Sstevel@tonic-gate 	 * added/deleted from DICTIONLIST.
2150Sstevel@tonic-gate 	 * Drawback: the database will also be generated when other
2160Sstevel@tonic-gate 	 * tunables are changed.
2170Sstevel@tonic-gate 	 */
2180Sstevel@tonic-gate 	if (stat(PWADMIN, &st_file) != -1 && st_db.st_mtime < st_file.st_mtime)
2190Sstevel@tonic-gate 		update_needed = B_TRUE;
2200Sstevel@tonic-gate 
2210Sstevel@tonic-gate 	if (update_needed) {
2220Sstevel@tonic-gate 		/*
2230Sstevel@tonic-gate 		 * Since we actually rebuild the database, we need to remove
2240Sstevel@tonic-gate 		 * the old database first.
2250Sstevel@tonic-gate 		 */
2260Sstevel@tonic-gate 		PWRemove(path);
2270Sstevel@tonic-gate 		return (build_dict_database(list, path));
2280Sstevel@tonic-gate 	}
2290Sstevel@tonic-gate 
2300Sstevel@tonic-gate 	return (0);
2310Sstevel@tonic-gate }
2320Sstevel@tonic-gate 
2330Sstevel@tonic-gate /*
2340Sstevel@tonic-gate  * Build or update database, while holding the global lock.
2350Sstevel@tonic-gate  */
2360Sstevel@tonic-gate int
make_dict_database(char * list,char * path)2370Sstevel@tonic-gate make_dict_database(char *list, char *path)
2380Sstevel@tonic-gate {
2390Sstevel@tonic-gate 	int r = -1;
2400Sstevel@tonic-gate 
2410Sstevel@tonic-gate 	if (lock_db(path) == 0) {
2420Sstevel@tonic-gate 		if (database_present(path) == NO_DICTDATABASE)
2430Sstevel@tonic-gate 			r = build_dict_database(list, path);
2440Sstevel@tonic-gate 		else
2450Sstevel@tonic-gate 			r = update_dict_database(list, path);
2460Sstevel@tonic-gate 		unlock_db();
2470Sstevel@tonic-gate 	}
2480Sstevel@tonic-gate 	return (r);
2490Sstevel@tonic-gate }
250