src/server/daemon/auth.h

Sun, 27 Nov 2022 10:07:37 +0100

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Sun, 27 Nov 2022 10:07:37 +0100
changeset 441
797aeb31a2c6
parent 415
d938228c382e
permissions
-rw-r--r--

fix listener ssl initialization

66
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1 /*
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
3 *
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
4 * Copyright 2013 Olaf Wintermann. All rights reserved.
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
5 *
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
6 * Redistribution and use in source and binary forms, with or without
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
7 * modification, are permitted provided that the following conditions are met:
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
8 *
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
9 * 1. Redistributions of source code must retain the above copyright
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
10 * notice, this list of conditions and the following disclaimer.
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
11 *
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
12 * 2. Redistributions in binary form must reproduce the above copyright
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
13 * notice, this list of conditions and the following disclaimer in the
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
14 * documentation and/or other materials provided with the distribution.
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
15 *
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
74babc0082b7 added authentication cache
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
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
26 * POSSIBILITY OF SUCH DAMAGE.
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
27 */
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
28
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
29 #ifndef AUTH_H
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
30 #define AUTH_H
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
31
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
32 #include <sys/types.h>
91
fac51f87def0 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 66
diff changeset
33 #include <inttypes.h>
66
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
34 #include "../public/auth.h"
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
35
415
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 400
diff changeset
36 #include <cx/map.h>
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 400
diff changeset
37 #include <cx/string.h>
254
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 91
diff changeset
38
66
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
39 #ifdef __cplusplus
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
40 extern "C" {
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
41 #endif
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
42
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
43 typedef struct {
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
44 User user;
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
45 char *authdb;
415
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 400
diff changeset
46 cxmutstr *groups;
66
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
47 size_t numgroups;
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
48 char *password;
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
49 uint32_t ref;
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
50 } CachedUser;
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
51
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
52 typedef struct user_cache_elm UserCacheElm;
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
53 struct user_cache_elm {
254
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 91
diff changeset
54 CachedUser *user;
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 91
diff changeset
55 UserCacheElm *next_user; // next elm in the cached user list
415
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 400
diff changeset
56 CxHashKey key; // key to access this element
254
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 91
diff changeset
57 size_t slot; // slot in the map
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 91
diff changeset
58 UserCacheElm *next_elm; // next element in this map slot
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 91
diff changeset
59 time_t created;
66
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
60 };
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
61
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
62 typedef struct {
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
63 UserCacheElm **map;
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
64 size_t size;
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
65 size_t count;
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
66 size_t max_users;
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
67 UserCacheElm *head;
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
68 UserCacheElm *trail;
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
69 } UserCache;
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
70
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
71 void auth_cache_init();
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
72
415
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 400
diff changeset
73 User* auth_cache_get(char *authdb, const char *user);
66
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
74 void auth_cache_add(
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
75 char *authdb,
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
76 User *user,
415
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 400
diff changeset
77 const char *password,
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 400
diff changeset
78 const char **groups,
66
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
79 size_t numgroups);
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
80
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
81 void auth_cache_remove_from_map(UserCacheElm *elm);
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
82
415
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 400
diff changeset
83 int cached_user_verify_password(CachedUser *user, const char *password);
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 400
diff changeset
84 int cached_user_check_group(CachedUser *user, const char *group);
66
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
85 void cached_user_unref(CachedUser *user);
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
86 void cached_user_delete(CachedUser *user);
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
87
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
88 #ifdef __cplusplus
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
89 }
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
90 #endif
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
91
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
92 #endif /* AUTH_H */
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
93

mercurial