src/server/daemon/auth.c

Thu, 11 Jul 2013 14:21:23 +0200

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Thu, 11 Jul 2013 14:21:23 +0200
changeset 87
bdec069d2239
parent 79
f48cea237ec3
child 91
fac51f87def0
permissions
-rw-r--r--

fixed pathcheck behavior

38
d07810b02147 added ldap authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1 /*
d07810b02147 added ldap authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
d07810b02147 added ldap authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
3 *
44
3da1f7b6847f added some error messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 38
diff changeset
4 * Copyright 2013 Olaf Wintermann. All rights reserved.
38
d07810b02147 added ldap authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
5 *
d07810b02147 added ldap authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
6 * Redistribution and use in source and binary forms, with or without
d07810b02147 added ldap authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
7 * modification, are permitted provided that the following conditions are met:
d07810b02147 added ldap authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
8 *
d07810b02147 added ldap authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
9 * 1. Redistributions of source code must retain the above copyright
d07810b02147 added ldap authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
10 * notice, this list of conditions and the following disclaimer.
d07810b02147 added ldap authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
11 *
d07810b02147 added ldap authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
12 * 2. Redistributions in binary form must reproduce the above copyright
d07810b02147 added ldap authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
13 * notice, this list of conditions and the following disclaimer in the
d07810b02147 added ldap authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
14 * documentation and/or other materials provided with the distribution.
d07810b02147 added ldap authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
15 *
d07810b02147 added ldap authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
d07810b02147 added ldap authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
d07810b02147 added ldap authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
d07810b02147 added ldap authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
d07810b02147 added ldap authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
d07810b02147 added ldap authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
d07810b02147 added ldap authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
d07810b02147 added ldap authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
d07810b02147 added ldap authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
d07810b02147 added ldap authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
d07810b02147 added ldap authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
26 * POSSIBILITY OF SUCH DAMAGE.
d07810b02147 added ldap authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
27 */
d07810b02147 added ldap authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
28
d07810b02147 added ldap authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
29 #include <stdio.h>
d07810b02147 added ldap authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
30 #include <stdlib.h>
66
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
31 #include <pthread.h>
38
d07810b02147 added ldap authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
32
d07810b02147 added ldap authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
33 #include "../ucx/map.h"
66
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
34 #include "../util/atomic.h"
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
35 #include "auth.h"
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
36
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
37 static pthread_mutex_t auth_cache_mutex = PTHREAD_MUTEX_INITIALIZER;
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
38 static UserCache cache;
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
39
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
40 void auth_cache_init() {
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
41 printf("auth_cache_init\n");
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
42 // TODO: config parameters
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
43 //pthread_mutex_init(&auth_cache_mutex, NULL);
77
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 67
diff changeset
44 cache.map = calloc(80, sizeof(UserCacheElm));
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 67
diff changeset
45 cache.size = 80;
66
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
46 cache.count = 0;
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
47 cache.max_users = 64;
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
48 cache.head = NULL;
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
49 cache.trail = NULL;
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
50 }
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
51
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
52 User* auth_cache_get(char *authdb, char *user) {
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
53 //printf("auth_cache_get: %s\n", user);
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
54 /*
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
55 * create the key to access the map
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
56 * key: authdb\0user
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
57 */
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
58 size_t authdblen = strlen(authdb);
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
59 size_t userlen = strlen(user);
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
60
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
61 size_t keylen = authdblen + userlen + 1;
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
62 char *key = malloc(keylen);
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
63 memcpy(key, authdb, authdblen);
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
64 key[authdblen] = 0;
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
65 memcpy(key + authdblen + 1, user, userlen);
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
66
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
67 UcxKey mapkey = ucx_key(key, keylen);
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
68
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
69 // get cached user from map
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
70 time_t now = time(NULL);
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
71 size_t slot = mapkey.hash%cache.size;
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
72
67
50505dc3f8a6 dynamic thread pool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 66
diff changeset
73 User *u = NULL;
66
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
74 pthread_mutex_lock(&auth_cache_mutex);
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
75
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
76 UserCacheElm *elm = cache.map[slot];
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
77 while(elm && elm->key.hash != mapkey.hash) {
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
78 elm = elm->next_elm;
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
79 }
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
80 // if we have an elm, the hash is correct
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
81 if(elm) {
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
82 // compare the key data to be sure it is the correct user
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
83 int n = (mapkey.len > elm->key.len) ? elm->key.len : mapkey.len;
67
50505dc3f8a6 dynamic thread pool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 66
diff changeset
84 if (!memcmp(elm->key.data, mapkey.data, n)) {
50505dc3f8a6 dynamic thread pool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 66
diff changeset
85 // elm is now the correct UserCacheElm
50505dc3f8a6 dynamic thread pool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 66
diff changeset
86 // TODO: use configuration for expire time
50505dc3f8a6 dynamic thread pool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 66
diff changeset
87 if(now - elm->created > 120) {
50505dc3f8a6 dynamic thread pool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 66
diff changeset
88 // cached user expired
50505dc3f8a6 dynamic thread pool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 66
diff changeset
89 // remove all users from the list from the first to this one
50505dc3f8a6 dynamic thread pool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 66
diff changeset
90 UserCacheElm *e = cache.head;
50505dc3f8a6 dynamic thread pool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 66
diff changeset
91 while(e) {
50505dc3f8a6 dynamic thread pool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 66
diff changeset
92 if(e == elm) {
50505dc3f8a6 dynamic thread pool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 66
diff changeset
93 break;
50505dc3f8a6 dynamic thread pool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 66
diff changeset
94 }
50505dc3f8a6 dynamic thread pool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 66
diff changeset
95 UserCacheElm *n = e->next_user;
50505dc3f8a6 dynamic thread pool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 66
diff changeset
96 auth_cache_remove_from_map(e);
50505dc3f8a6 dynamic thread pool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 66
diff changeset
97 e = n;
50505dc3f8a6 dynamic thread pool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 66
diff changeset
98 }
50505dc3f8a6 dynamic thread pool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 66
diff changeset
99 cache.head = elm->next_user;
50505dc3f8a6 dynamic thread pool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 66
diff changeset
100 if(cache.trail == elm) {
50505dc3f8a6 dynamic thread pool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 66
diff changeset
101 cache.trail = NULL;
50505dc3f8a6 dynamic thread pool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 66
diff changeset
102 }
50505dc3f8a6 dynamic thread pool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 66
diff changeset
103 auth_cache_remove_from_map(elm);
50505dc3f8a6 dynamic thread pool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 66
diff changeset
104 u = NULL;
50505dc3f8a6 dynamic thread pool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 66
diff changeset
105 } else {
50505dc3f8a6 dynamic thread pool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 66
diff changeset
106 u = (User*)elm->user;
50505dc3f8a6 dynamic thread pool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 66
diff changeset
107 }
66
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
108 }
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
109 }
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
110
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
111 pthread_mutex_unlock(&auth_cache_mutex);
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
112 free(key);
67
50505dc3f8a6 dynamic thread pool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 66
diff changeset
113 return u;
66
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
114 }
38
d07810b02147 added ldap authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
115
66
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
116 void auth_cache_add(
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
117 char *authdb,
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
118 User *user,
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
119 char *password,
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
120 char **groups,
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
121 size_t numgroups)
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
122 {
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
123 //printf("auth_cache_add: %s\n", user->name);
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
124 /*
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
125 * this function does not check, if the user is already in the map
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
126 * use it only after auth_cache_get
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
127 */
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
128
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
129 CachedUser *cusr = malloc(sizeof(CachedUser));
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
130 cusr->user.name = strdup(user->name);
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
131 cusr->user.uid = user->uid;
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
132 cusr->user.gid = user->gid;
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
133 cusr->user.verify_password =
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
134 (user_verify_passwd_f)cached_user_verify_password;
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
135 cusr->user.check_group = (user_check_group_f)cached_user_check_group;
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
136 cusr->user.free = (user_free_f)cached_user_unref;
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
137
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
138 cusr->authdb = strdup(authdb);
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
139 cusr->password = strdup(password);
79
f48cea237ec3 fixed some memory leaks
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
140 cusr->groups = numgroups ? calloc(numgroups, sizeof(sstr_t)) : NULL;
66
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
141 cusr->numgroups = numgroups;
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
142 for(int i=0;i<numgroups;i++) {
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
143 cusr->groups[i] = sstrdup(sstr(groups[i]));
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
144 }
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
145 cusr->ref = 1;
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
146
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
147 /*
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
148 * add the user to the auth cache
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
149 * the auth cache is a list of all cached users
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
150 */
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
151
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
152 // create list element
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
153 time_t now = time(NULL);
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
154 UserCacheElm *elm = malloc(sizeof(UserCacheElm));
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
155 elm->user = cusr;
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
156 elm->created = now;
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
157 elm->next_elm = NULL;
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
158 elm->next_user = NULL;
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
159
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
160 // create map key
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
161 size_t authdblen = strlen(authdb);
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
162 size_t userlen = strlen(user->name);
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
163 size_t keylen = authdblen + userlen + 1;
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
164 char *key = malloc(keylen);
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
165 memcpy(key, authdb, authdblen);
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
166 key[authdblen] = 0;
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
167 memcpy(key + authdblen + 1, user->name, userlen);
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
168 UcxKey mapkey = ucx_key(key, keylen);
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
169
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
170 elm->key = mapkey;
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
171 elm->slot = mapkey.hash%cache.size;
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
172
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
173 // add user to list and map
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
174 pthread_mutex_lock(&auth_cache_mutex);
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
175
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
176 // remove the first cached user if expired or the cache is full
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
177 if(cache.head &&
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
178 (cache.count >= cache.max_users || now-cache.head->created > 120))
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
179 {
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
180 UserCacheElm *first = cache.head;
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
181 cache.head = first->next_user;
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
182 if(!cache.head) {
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
183 cache.trail = NULL;
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
184 }
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
185 auth_cache_remove_from_map(first);
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
186 }
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
187
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
188 // add to map
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
189 UserCacheElm *prevelm = cache.map[elm->slot];
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
190 if(prevelm) {
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
191 for(;;) {
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
192 if(!prevelm->next_elm) {
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
193 break;
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
194 }
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
195 prevelm = prevelm->next_elm;
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
196 }
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
197 }
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
198 if(prevelm) {
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
199 prevelm->next_elm = elm;
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
200 } else {
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
201 cache.map[elm->slot] = elm;
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
202 }
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
203
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
204 // add to list
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
205 if(cache.head) {
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
206 cache.trail->next_user = elm;
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
207 cache.trail = elm;
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
208 } else {
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
209 cache.head = elm;
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
210 cache.trail = elm;
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
211 }
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
212
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
213 cache.count++;
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
214
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
215 pthread_mutex_unlock(&auth_cache_mutex);
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
216 }
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
217
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
218 void auth_cache_remove_from_map(UserCacheElm *elm) {
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
219 UserCacheElm *prevelm = NULL;
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
220 UserCacheElm *e = cache.map[elm->slot];
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
221 while(e) {
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
222 if(e == elm) {
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
223 break;
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
224 } else {
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
225 prevelm = e;
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
226 }
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
227 e = e->next_elm;
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
228 }
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
229 if(prevelm) {
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
230 prevelm->next_elm = elm->next_elm;
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
231 } else {
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
232 cache.map[elm->slot] = elm->next_elm;
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
233 }
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
234
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
235 free(elm->key.data);
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
236 cached_user_unref(elm->user);
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
237 free(elm);
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
238
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
239 cache.count--;
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
240 }
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
241
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
242 int cached_user_verify_password(CachedUser *user, char *password) {
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
243 if(!strcmp(user->password, password)) {
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
244 return 1;
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
245 } else {
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
246 return 0;
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
247 }
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
248 }
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
249
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
250 int cached_user_check_group(CachedUser *user, char *group) {
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
251 sstr_t grp = sstr(group);
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
252 for(int i=0;i<user->numgroups;i++) {
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
253 if(!sstrcmp(user->groups[i], grp)) {
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
254 return 1;
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
255 }
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
256 }
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
257 return 0;
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
258 }
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
259
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
260 void cached_user_unref(CachedUser *user) {
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
261 uint32_t ref = ws_atomic_dec32(&user->ref);
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
262 if(ref == 0) {
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
263 cached_user_delete(user);
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
264 }
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
265 }
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
266
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
267 void cached_user_delete(CachedUser *user) {
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
268 free(user->user.name);
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
269 free(user->authdb);
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
270 free(user->password);
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
271 free(user->groups);
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
272 free(user);
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
273 }
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
274
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
275
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
276 /*
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
277 * public API
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
278 * from public/auth.h
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
279 */
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
280
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
281 User* authdb_get_user(AuthDB *db, char *user) {
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
282 if(db->use_cache) {
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
283 User *u = auth_cache_get(db->name, user);
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
284 if(u) {
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
285 return u;
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
286 }
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
287 }
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
288 return db->get_user(db, user);
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
289 }
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
290
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
291 User* authdb_get_and_verify(AuthDB *db, char *user, char *password, int *pw) {
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
292 User *u = NULL;
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
293 // try getting the user from the cache
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
294 if(db->use_cache) {
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
295 u = auth_cache_get(db->name, user);
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
296 if(u) {
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
297 if(u->verify_password(u, password)) {
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
298 *pw = 1;
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
299 } else {
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
300 *pw = 0;
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
301 u->free(u);
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
302 u = NULL;
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
303 }
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
304 return u;
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
305 }
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
306 }
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
307 // user not cached
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
308 u = db->get_user(db, user);
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
309 if(u) {
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
310 if(u->verify_password(u, password)) {
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
311 if(db->use_cache) {
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
312 auth_cache_add(db->name, u, password, NULL, 0);
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
313 }
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
314 *pw = 1;
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
315 } else {
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
316 *pw = 0;
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
317 u->free(u);
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
318 u = NULL;
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
319 }
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
320 }
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
321 return u;
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
322 }

mercurial