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/autoconf.h>
37 * No pthreads, no mutex.
39 static int lrad_crypt_init = 0;
40 static pthread_mutex_t lrad_crypt_mutex;
43 #include <freeradius-devel/missing.h>
44 #include <freeradius-devel/libradius.h>
47 * performs a crypt password check in an thread-safe way.
49 * returns: 0 -- check succeeded
50 * -1 -- failed to crypt
53 int lrad_crypt_check(const char *key, const char *crypted)
60 * Ensure we're thread-safe, as crypt() isn't.
62 if (lrad_crypt_init == 0) {
63 pthread_mutex_init(&lrad_crypt_mutex, NULL);
67 pthread_mutex_lock(&lrad_crypt_mutex);
70 passwd = crypt(key, crypted);
73 * Got something, check it within the lock. This is
74 * faster than copying it to a local buffer, and the
75 * time spent within the lock is critical.
78 cmp = strcmp(crypted, passwd);
82 pthread_mutex_unlock(&lrad_crypt_mutex);