116 char *location = NULL; |
121 char *location = NULL; |
117 char *user = NULL; |
122 char *user = NULL; |
118 char *password = NULL; |
123 char *password = NULL; |
119 |
124 |
120 int res = 0; |
125 int res = 0; |
|
126 int ret = 0; |
121 if((res += readval(in, &id, FALSE)) == 1) { |
127 if((res += readval(in, &id, FALSE)) == 1) { |
|
128 if(index) { |
|
129 if((res += readval(in, &location, TRUE)) == 2) { |
|
130 pwdstore_put_index(p, id, location); |
|
131 ret = 1; |
|
132 } |
|
133 } else { |
|
134 if((res += readval(in, &user, FALSE)) == 2) { |
|
135 if((res += readval(in, &password, FALSE)) == 3) { |
|
136 pwdstore_put(p, id, user, password); |
|
137 ret = 1; |
|
138 } |
|
139 } |
|
140 } |
122 if((res += readval(in, &location, TRUE)) == 2) { |
141 if((res += readval(in, &location, TRUE)) == 2) { |
123 if(!index) { |
142 if(!index) { |
124 if((res += readval(in, &user, FALSE)) == 3) { |
143 if((res += readval(in, &user, FALSE)) == 3) { |
125 res += readval(in, &password, FALSE); |
144 res += readval(in, &password, FALSE); |
126 } |
145 } |
127 } |
146 } |
128 } |
147 } |
129 } |
|
130 |
|
131 int ret = 0; |
|
132 if((!index && res == 4) || (index && res == 2)) { |
|
133 pwdstore_put(p, id, location, user, password); |
|
134 ret = 1; |
|
135 } |
148 } |
136 |
149 |
137 if(id) free(id); |
150 if(id) free(id); |
138 if(location) free(location); |
151 if(location) free(location); |
139 if(user) free(user); |
152 if(user) free(user); |
249 } else { |
257 } else { |
250 return NULL; |
258 return NULL; |
251 } |
259 } |
252 } |
260 } |
253 |
261 |
254 void pwdstore_put(PwdStore *p, const char *id, const char *location, const char *username, const char *password) { |
262 void pwdstore_put(PwdStore *p, const char *id, const char *username, const char *password) { |
255 PwdEntry *entry = malloc(sizeof(PwdEntry)); |
263 PwdEntry *entry = malloc(sizeof(PwdEntry)); |
256 entry->id = strdup(id); |
264 entry->id = strdup(id); |
257 entry->location = location ? strdup(location) : NULL; |
265 entry->user = strdup(username); |
258 entry->user = username ? strdup(username) : NULL; |
266 entry->password = strdup(password); |
259 entry->password = password ? strdup(password) : NULL; |
|
260 ucx_map_cstr_put(p->ids, id, entry); |
267 ucx_map_cstr_put(p->ids, id, entry); |
261 |
268 } |
|
269 |
|
270 void pwdstore_put_index(PwdStore *p, const char *id, const char *location) { |
|
271 PwdIndexEntry *e = ucx_map_cstr_get(p->index, id); |
|
272 if(e) { |
|
273 return; |
|
274 } |
|
275 PwdIndexEntry *newentry = malloc(sizeof(PwdIndexEntry)); |
|
276 newentry->id = strdup(id); |
262 if(location) { |
277 if(location) { |
263 p->locations = ucx_list_append(p->locations, entry); |
278 newentry->location = strdup(location); |
|
279 p->locations = ucx_list_append(p->locations, newentry); |
|
280 } else { |
|
281 newentry->location = NULL; |
|
282 p->noloc = ucx_list_append(p->noloc, newentry); |
|
283 } |
|
284 ucx_map_cstr_put(p->index, id, newentry); |
|
285 } |
|
286 |
|
287 void write_index_entry(UcxBuffer *out, PwdIndexEntry *e) { |
|
288 uint32_t idlen = strlen(e->id); |
|
289 uint32_t locationlen = e->location ? strlen(e->location) : 0; |
|
290 uint32_t netidlen = htonl(idlen); |
|
291 uint32_t netlocationlen = htonl(locationlen); |
|
292 |
|
293 ucx_buffer_putc(out, 0); // type |
|
294 |
|
295 ucx_buffer_write(&netidlen, 1, sizeof(uint32_t), out); |
|
296 ucx_buffer_write(e->id, 1, idlen, out); |
|
297 ucx_buffer_write(&netlocationlen, 1, sizeof(uint32_t), out); |
|
298 if(e->location) { |
|
299 ucx_buffer_write(e->location, 1, locationlen, out); |
264 } |
300 } |
265 } |
301 } |
266 |
302 |
267 int pwdstore_store(PwdStore *p, const char *file) { |
303 int pwdstore_store(PwdStore *p, const char *file) { |
268 if(!p->key) { |
304 if(!p->key) { |
269 return 1; |
305 return 1; |
270 } |
306 } |
271 |
307 |
272 UcxBuffer *index = ucx_buffer_new(NULL, 2048, UCX_BUFFER_AUTOEXTEND); |
308 UcxBuffer *index = ucx_buffer_new(NULL, 2048, UCX_BUFFER_AUTOEXTEND); |
273 UcxBuffer *content = ucx_buffer_new(NULL, 2048, UCX_BUFFER_AUTOEXTEND); |
309 UcxBuffer *content = ucx_buffer_new(NULL, 2048, UCX_BUFFER_AUTOEXTEND); |
|
310 |
|
311 // create index |
|
312 UCX_FOREACH(elm, p->noloc) { |
|
313 PwdIndexEntry *e = elm->data; |
|
314 write_index_entry(index, e); |
|
315 } |
|
316 UCX_FOREACH(elm, p->locations) { |
|
317 PwdIndexEntry *e = elm->data; |
|
318 write_index_entry(index, e); |
|
319 } |
274 |
320 |
275 UcxMapIterator i = ucx_map_iterator(p->ids); |
321 UcxMapIterator i = ucx_map_iterator(p->ids); |
276 PwdEntry *value; |
322 PwdEntry *value; |
277 UCX_MAP_FOREACH(key, value, i) { |
323 UCX_MAP_FOREACH(key, value, i) { |
278 if(!value->id || !value->user || !value->password) { |
324 if(!value->id || !value->user || !value->password) { |
279 continue; |
325 continue; |
280 } |
326 } |
281 |
327 |
282 uint32_t idlen = strlen(value->id); |
328 uint32_t idlen = strlen(value->id); |
283 uint32_t locationlen = value->location ? strlen(value->location) : 0; |
|
284 uint32_t ulen = strlen(value->user); |
329 uint32_t ulen = strlen(value->user); |
285 uint32_t plen = strlen(value->password); |
330 uint32_t plen = strlen(value->password); |
286 uint32_t netidlen = htonl(idlen); |
331 uint32_t netidlen = htonl(idlen); |
287 uint32_t netlocationlen = htonl(locationlen); |
|
288 uint32_t netulen = htonl(ulen); |
332 uint32_t netulen = htonl(ulen); |
289 uint32_t netplen = htonl(plen); |
333 uint32_t netplen = htonl(plen); |
290 |
|
291 // index buffer |
|
292 ucx_buffer_putc(index, 0); // type |
|
293 |
|
294 ucx_buffer_write(&netidlen, 1, sizeof(uint32_t), index); |
|
295 ucx_buffer_write(value->id, 1, idlen, index); |
|
296 ucx_buffer_write(&netlocationlen, 1, sizeof(uint32_t), index); |
|
297 if(value->location) { |
|
298 ucx_buffer_write(value->location, 1, locationlen, index); |
|
299 } |
|
300 |
334 |
301 // content buffer |
335 // content buffer |
302 ucx_buffer_putc(content, 0); // type |
336 ucx_buffer_putc(content, 0); // type |
303 |
337 |
304 ucx_buffer_write(&netidlen, 1, sizeof(uint32_t), content); |
338 ucx_buffer_write(&netidlen, 1, sizeof(uint32_t), content); |
305 ucx_buffer_write(value->id, 1, idlen, content); |
339 ucx_buffer_write(value->id, 1, idlen, content); |
306 ucx_buffer_write(&netlocationlen, 1, sizeof(uint32_t), content); |
|
307 if(value->location) { |
|
308 ucx_buffer_write(value->location, 1, locationlen, content); |
|
309 } |
|
310 ucx_buffer_write(&netulen, 1, sizeof(uint32_t), content); |
340 ucx_buffer_write(&netulen, 1, sizeof(uint32_t), content); |
311 ucx_buffer_write(value->user, 1, ulen, content); |
341 ucx_buffer_write(value->user, 1, ulen, content); |
312 ucx_buffer_write(&netplen, 1, sizeof(uint32_t), content); |
342 ucx_buffer_write(&netplen, 1, sizeof(uint32_t), content); |
313 ucx_buffer_write(value->password, 1, plen, content); |
343 ucx_buffer_write(value->password, 1, plen, content); |
314 } |
344 } |