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]
22 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
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
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.
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))
67 (void) snprintf(dblock
, sizeof (dblock
), "%s/authtok_check.lock", path
);
69 if ((lockfd
= open(dblock
, O_WRONLY
|O_CREAT
|O_EXCL
, 0400)) == -1) {
71 lockfd
= open(dblock
, O_WRONLY
);
74 syslog(LOG_ERR
, "pam_authtok_check::pam_sm_chauthtok: "
75 "can't open lockfile: %s", strerror(errno
));
82 flock
.l_type
= F_WRLCK
;
83 retval
= fcntl(lockfd
, F_SETLK
, &flock
);
85 (void) usleep(LOCK_WAIT
);
86 } while (retval
== -1 && ++retries
< LOCK_RETRIES
);
89 int errno_saved
= errno
;
90 syslog(LOG_ERR
, "pam_authtok_check::pam_sm_chauthtok: timeout "
91 "waiting for dictionary lock.");
101 * Release the database lock
107 flock
.l_type
= F_UNLCK
;
108 (void) fcntl(lockfd
, F_SETLK
, &flock
);
109 (void) close(lockfd
);
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
)
124 char dict_hwm
[PATH_MAX
];
125 char dict_pwd
[PATH_MAX
];
126 char dict_pwi
[PATH_MAX
];
129 (void) snprintf(dict_hwm
, sizeof (dict_hwm
), "%s/%s", path
,
131 (void) snprintf(dict_pwd
, sizeof (dict_pwd
), "%s/%s", path
,
133 (void) snprintf(dict_pwi
, sizeof (dict_pwi
), "%s/%s", path
,
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 */
145 return (NO_DICTDATABASE
);
147 (void) PWClose(dict
);
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
)
174 boolean_t update_needed
= B_FALSE
;
175 char dbase_pwd
[PATH_MAX
];
177 (void) snprintf(dbase_pwd
, sizeof (dbase_pwd
), "%s/%s", path
,
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
) {
193 "pam_authtok_check:update_dict_database: "
194 "dictionary \"%s\" not present.", buf
);
197 "pam_authtok_check:update_dict_database: "
198 "stat(%s) failed: %s", buf
,
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 ,");
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
;
223 * Since we actually rebuild the database, we need to remove
224 * the old database first.
227 return (build_dict_database(list
, path
));
234 * Build or update database, while holding the global lock.
237 make_dict_database(char *list
, char *path
)
241 if (lock_db(path
) == 0) {
242 if (database_present(path
) == NO_DICTDATABASE
)
243 r
= build_dict_database(list
, path
);
245 r
= update_dict_database(list
, path
);