41 #include "../util/atomic.h" |
41 #include "../util/atomic.h" |
42 #include "../util/util.h" |
42 #include "../util/util.h" |
43 |
43 |
44 #include "keyfile_auth.h" |
44 #include "keyfile_auth.h" |
45 |
45 |
46 Keyfile* keyfile_new() { |
46 Keyfile* keyfile_new(UcxAllocator *a) { |
47 Keyfile *keyfile = malloc(sizeof(Keyfile)); |
47 Keyfile *keyfile = alcalloc(a, 1, sizeof(Keyfile)); |
|
48 if(!keyfile) { |
|
49 return NULL; |
|
50 } |
48 keyfile->authdb.get_user = keyfile_get_user; |
51 keyfile->authdb.get_user = keyfile_get_user; |
49 keyfile->authdb.use_cache = 0; |
52 keyfile->authdb.use_cache = 0; |
50 keyfile->users = ucx_map_new(16); |
53 keyfile->users = ucx_map_new_a(a, 16); |
51 keyfile->ref = 1; |
|
52 return keyfile; |
54 return keyfile; |
53 } |
55 } |
54 |
56 |
55 void keyfile_ref(Keyfile *keyfile) { |
57 int keyfile_add_user( |
56 ws_atomic_inc32(&keyfile->ref); |
|
57 } |
|
58 |
|
59 void keyfile_unref(Keyfile *keyfile) { |
|
60 uint32_t ref = ws_atomic_dec32(&keyfile->ref); |
|
61 if(ref == 0) { |
|
62 UcxMapIterator i = ucx_map_iterator(keyfile->users); |
|
63 KeyfileUser *user; |
|
64 UCX_MAP_FOREACH(key, user, i) { |
|
65 free(user->user.name); |
|
66 free(user->hash); |
|
67 for(int n=0;n<user->numgroups;n++) { |
|
68 free(user->groups[n].ptr); |
|
69 } |
|
70 free(user->groups); |
|
71 } |
|
72 ucx_map_free(keyfile->users); |
|
73 |
|
74 free(keyfile->authdb.name); |
|
75 free(keyfile); |
|
76 } |
|
77 } |
|
78 |
|
79 void keyfile_add_user( |
|
80 Keyfile *keyfile, |
58 Keyfile *keyfile, |
81 sstr_t name, |
59 sstr_t name, |
82 enum KeyfileHashType hash_type, |
60 enum KeyfileHashType hash_type, |
83 sstr_t hash, |
61 sstr_t hash, |
84 sstr_t *groups, |
62 sstr_t *groups, |
85 size_t ngroups) |
63 size_t ngroups) |
86 { |
64 { |
|
65 UcxAllocator *a = keyfile->users->allocator; |
|
66 |
87 if(hash.length < 12) { |
67 if(hash.length < 12) { |
88 // hash too short |
68 // hash too short |
89 // TODO: log |
69 // TODO: log |
90 return; |
70 return -1; |
91 } |
71 } |
92 |
72 |
93 KeyfileUser *user = malloc(sizeof(KeyfileUser)); |
73 KeyfileUser *user = almalloc(a, sizeof(KeyfileUser)); |
94 user->user.name = sstrdup(name).ptr; |
74 user->user.name = sstrdup_a(a, name).ptr; |
95 user->user.uid = -1; |
75 user->user.uid = -1; |
96 user->user.gid = -1; |
76 user->user.gid = -1; |
97 user->user.verify_password = keyfile_user_verify_password; |
77 user->user.verify_password = keyfile_user_verify_password; |
98 user->user.check_group = keyfile_user_check_group; |
78 user->user.check_group = keyfile_user_check_group; |
99 user->user.free = keyfile_user_free; |
79 user->user.free = keyfile_user_free; |
100 |
80 |
101 user->hash_type = hash_type; |
81 user->hash_type = hash_type; |
102 user->hash = malloc(hash.length + 1); |
82 user->hash = almalloc(a, hash.length + 1); |
|
83 |
|
84 if(!user->user.name || !user->hash) { |
|
85 return -1; |
|
86 } |
|
87 |
103 user->hashlen = util_base64decode(hash.ptr, hash.length, user->hash); |
88 user->hashlen = util_base64decode(hash.ptr, hash.length, user->hash); |
104 |
89 |
105 user->groups = calloc(ngroups, sizeof(sstr_t)); |
90 if(ngroups > 0) { |
106 for(int i=0;i<ngroups;i++) { |
91 user->groups = alcalloc(a, ngroups, sizeof(sstr_t)); |
107 user->groups[i] = sstrdup(groups[i]); |
92 if(!user->groups) { |
|
93 return -1; |
|
94 } |
|
95 for(int i=0;i<ngroups;i++) { |
|
96 user->groups[i] = sstrdup_a(a, groups[i]); |
|
97 } |
|
98 |
|
99 } else { |
|
100 user->groups = NULL; |
108 } |
101 } |
109 |
102 |
110 // add to keyfile |
103 // add to keyfile |
111 ucx_map_sstr_put(keyfile->users, name, user); |
104 return ucx_map_sstr_put(keyfile->users, name, user); |
112 } |
105 } |
113 |
106 |
114 // authdb functions |
107 // authdb functions |
115 |
108 |
116 User* keyfile_get_user(AuthDB *db, char *user) { |
109 User* keyfile_get_user(AuthDB *db, char *user) { |