dmake: do not set MAKEFLAGS=k
[unleashed/tickless.git] / usr / src / lib / pam_modules / authtok_check / dict.c
blob39a370526bfff794ed7f5e3bf9a6abdd8fe767c0
1 /*
2 * CDDL HEADER START
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
19 * CDDL HEADER END
22 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
26 #include <sys/stat.h>
27 #include <stdio.h>
28 #include <syslog.h>
29 #include <fcntl.h>
30 #include <time.h>
31 #include <errno.h>
32 #include <signal.h>
33 #include "packer.h"
35 static int lockfd = -1;
36 static struct flock flock = { 0, 0, 0, 0, 0, 0 };
38 char dblock[PATH_MAX];
40 #define LOCK_WAIT 1000000
41 #define LOCK_RETRIES 60
44 * lock_db()
46 * Create a lockfile to prevent simultaneous access to the database
47 * creation routines. We set a timeout to LOCK_WAIT seconds. If we
48 * haven't obtained a lock after LOCK_RETIRES attempts, we bail out.
50 * returns 0 on succes, -1 on (lock) failure.
51 * side effect: the directory "path" will be created if it didn't exist.
53 int
54 lock_db(char *path)
56 int retval;
57 struct stat st;
58 int retries = 0;
60 /* create directory "path" if it doesn't exist */
61 if (stat(path, &st) == -1) {
62 if (errno != ENOENT ||
63 (mkdir(path, 0755) == -1 || chmod(path, 0755) == -1))
64 return (-1);
67 (void) snprintf(dblock, sizeof (dblock), "%s/authtok_check.lock", path);
69 if ((lockfd = open(dblock, O_WRONLY|O_CREAT|O_EXCL, 0400)) == -1) {
70 if (errno == EEXIST)
71 lockfd = open(dblock, O_WRONLY);
72 if (lockfd == -1) {
73 int olderrno = errno;
74 syslog(LOG_ERR, "pam_authtok_check::pam_sm_chauthtok: "
75 "can't open lockfile: %s", strerror(errno));
76 errno = olderrno;
77 return (-1);
81 do {
82 flock.l_type = F_WRLCK;
83 retval = fcntl(lockfd, F_SETLK, &flock);
84 if (retval == -1)
85 (void) usleep(LOCK_WAIT);
86 } while (retval == -1 && ++retries < LOCK_RETRIES);
88 if (retval == -1) {
89 int errno_saved = errno;
90 syslog(LOG_ERR, "pam_authtok_check::pam_sm_chauthtok: timeout "
91 "waiting for dictionary lock.");
92 errno = errno_saved;
95 return (retval);
99 * unlock_db()
101 * Release the database lock
103 void
104 unlock_db(void)
106 if (lockfd != -1) {
107 flock.l_type = F_UNLCK;
108 (void) fcntl(lockfd, F_SETLK, &flock);
109 (void) close(lockfd);
110 lockfd = -1;
115 * database_present()
117 * returns 0 if the database files are found, and the database size is
118 * greater than 0 and the database version matches the current version.
121 database_present(char *path)
123 struct stat st;
124 char dict_hwm[PATH_MAX];
125 char dict_pwd[PATH_MAX];
126 char dict_pwi[PATH_MAX];
127 PWDICT *dict;
129 (void) snprintf(dict_hwm, sizeof (dict_hwm), "%s/%s", path,
130 DICT_DATABASE_HWM);
131 (void) snprintf(dict_pwd, sizeof (dict_pwd), "%s/%s", path,
132 DICT_DATABASE_PWD);
133 (void) snprintf(dict_pwi, sizeof (dict_pwi), "%s/%s", path,
134 DICT_DATABASE_PWI);
136 if (stat(dict_hwm, &st) == -1 ||
137 (stat(dict_pwd, &st) == -1 || st.st_size == 0) ||
138 stat(dict_pwi, &st) == -1)
139 return (NO_DICTDATABASE);
141 /* verify database version number by trying to open it */
142 if ((dict = PWOpen(path, "r")) == NULL) {
143 /* the files are there, but an outdated version */
144 PWRemove(path);
145 return (NO_DICTDATABASE);
147 (void) PWClose(dict);
148 return (0);
152 * build_dict_database(list, char *path)
154 * Create the Crack Dictionary Database based on the list of sources
155 * dictionaries specified in "list". Store the database in "path".
158 build_dict_database(char *list, char *path)
160 return (packer(list, path) == -1 ? DICTDATABASE_BUILD_ERR : 0);
164 * Rebuild the database in "path" if the database is older than one of the
165 * files listed in "list", or older than the config-file PWADMIN.
168 update_dict_database(char *list, char *path)
170 struct stat st_db;
171 struct stat st_file;
172 char *buf;
173 char *listcopy;
174 boolean_t update_needed = B_FALSE;
175 char dbase_pwd[PATH_MAX];
177 (void) snprintf(dbase_pwd, sizeof (dbase_pwd), "%s/%s", path,
178 DICT_DATABASE_PWD);
180 if (stat(dbase_pwd, &st_db) == -1)
181 return (DICTFILE_ERR);
183 if ((listcopy = strdup(list)) == NULL)
184 return (DICTFILE_ERR);
186 buf = strtok(listcopy, "\t ,");
188 /* Compare mtime of each listed dictionary against DB mtime */
189 while (update_needed == B_FALSE && buf != NULL) {
190 if (stat(buf, &st_file) == -1) {
191 if (errno == ENOENT) {
192 syslog(LOG_ERR,
193 "pam_authtok_check:update_dict_database: "
194 "dictionary \"%s\" not present.", buf);
195 } else {
196 syslog(LOG_ERR,
197 "pam_authtok_check:update_dict_database: "
198 "stat(%s) failed: %s", buf,
199 strerror(errno));
201 free(listcopy);
202 return (DICTFILE_ERR);
204 if (st_db.st_mtime < st_file.st_mtime)
205 update_needed = B_TRUE; /* database out of date */
206 buf = strtok(NULL, "\t ,");
209 free(listcopy);
212 * If /etc/default/passwd is updated, generate the database.
213 * Because this is the only way we can check if files have been
214 * added/deleted from DICTIONLIST.
215 * Drawback: the database will also be generated when other
216 * tunables are changed.
218 if (stat(PWADMIN, &st_file) != -1 && st_db.st_mtime < st_file.st_mtime)
219 update_needed = B_TRUE;
221 if (update_needed) {
223 * Since we actually rebuild the database, we need to remove
224 * the old database first.
226 PWRemove(path);
227 return (build_dict_database(list, path));
230 return (0);
234 * Build or update database, while holding the global lock.
237 make_dict_database(char *list, char *path)
239 int r = -1;
241 if (lock_db(path) == 0) {
242 if (database_present(path) == NO_DICTDATABASE)
243 r = build_dict_database(list, path);
244 else
245 r = update_dict_database(list, path);
246 unlock_db();
248 return (r);