2 * crypt.c A thread-safe crypt wrapper
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
18 * Copyright 2000-2006 The FreeRADIUS server project
21 #include <freeradius-devel/ident.h>
24 #include <freeradius-devel/libradius.h>
34 * No pthreads, no mutex.
36 static int fr_crypt_init = 0;
37 static pthread_mutex_t fr_crypt_mutex;
42 * performs a crypt password check in an thread-safe way.
44 * returns: 0 -- check succeeded
45 * -1 -- failed to crypt
48 int fr_crypt_check(const char *key, const char *crypted)
55 * Ensure we're thread-safe, as crypt() isn't.
57 if (fr_crypt_init == 0) {
58 pthread_mutex_init(&fr_crypt_mutex, NULL);
62 pthread_mutex_lock(&fr_crypt_mutex);
65 passwd = crypt(key, crypted);
68 * Got something, check it within the lock. This is
69 * faster than copying it to a local buffer, and the
70 * time spent within the lock is critical.
73 cmp = strcmp(crypted, passwd);
77 pthread_mutex_unlock(&fr_crypt_mutex);