All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
crypt.c
Go to the documentation of this file.
1 /*
2  * crypt.c A thread-safe crypt wrapper
3  *
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.
8  *
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.
13  *
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
17  *
18  * Copyright 2000-2006 The FreeRADIUS server project
19  */
20 
21 RCSID("$Id: 0bef21d5c094f076a93308ac98486b76d6c3fa24 $")
22 
23 #include <freeradius-devel/libradius.h>
24 
25 #ifdef HAVE_CRYPT_H
26 #include <crypt.h>
27 #endif
28 
29 #ifdef HAVE_PTHREAD_H
30 #include <pthread.h>
31 
32 /*
33  * No pthreads, no mutex.
34  */
35 static bool fr_crypt_init = false;
36 static pthread_mutex_t fr_crypt_mutex;
37 
38 /*
39  * This is easier than ifdef's throughout the code.
40  */
41 # define PTHREAD_MUTEX_LOCK pthread_mutex_lock
42 # define PTHREAD_MUTEX_UNLOCK pthread_mutex_unlock
43 #else
44 # define PTHREAD_MUTEX_LOCK(_x)
45 # define PTHREAD_MUTEX_UNLOCK(_x)
46 #endif
47 
48 /*
49  * performs a crypt password check in an thread-safe way.
50  *
51  * returns: 0 -- check succeeded
52  * -1 -- failed to crypt
53  * 1 -- check failed
54  */
55 int fr_crypt_check(char const *key, char const *crypted)
56 {
57  char *passwd;
58  int cmp = 0;
59 
60 #ifdef HAVE_PTHREAD_H
61  /*
62  * Ensure we're thread-safe, as crypt() isn't.
63  */
64  if (fr_crypt_init == false) {
65  pthread_mutex_init(&fr_crypt_mutex, NULL);
66  fr_crypt_init = true;
67  }
68 
69  PTHREAD_MUTEX_LOCK(&fr_crypt_mutex);
70 #endif
71 
72  passwd = crypt(key, crypted);
73 
74  /*
75  * Got something, check it within the lock. This is
76  * faster than copying it to a local buffer, and the
77  * time spent within the lock is critical.
78  */
79  if (passwd) {
80  cmp = strcmp(crypted, passwd);
81  }
82 
83  PTHREAD_MUTEX_UNLOCK(&fr_crypt_mutex);
84 
85  /*
86  * Error.
87  */
88  if (!passwd) {
89  return -1;
90  }
91 
92  /*
93  * OK, return OK.
94  */
95  if (cmp == 0) {
96  return 0;
97  }
98 
99  /*
100  * Comparison failed.
101  */
102  return 1;
103 }
int fr_crypt_check(char const *key, char const *crypted)
Definition: crypt.c:55
#define pthread_mutex_init(_x, _y)
Definition: rlm_eap.h:75
#define PTHREAD_MUTEX_UNLOCK(_x)
Definition: crypt.c:45
char * crypt(UNUSED char *key, char *salt)
Definition: missing.c:35
#define PTHREAD_MUTEX_LOCK(_x)
Definition: crypt.c:44
Definition: pair.c:37
#define RCSID(id)
Definition: build.h:135