dav/pwd.c

changeset 489
fb69eae42ef0
parent 488
29b979ca8750
child 515
2465dd550bb5
equal deleted inserted replaced
488:29b979ca8750 489:fb69eae42ef0
93 uint32_t length = 0; 93 uint32_t length = 0;
94 if(ucx_buffer_read(&length, 1, sizeof(uint32_t), in) != sizeof(uint32_t)) { 94 if(ucx_buffer_read(&length, 1, sizeof(uint32_t), in) != sizeof(uint32_t)) {
95 return 0; 95 return 0;
96 } 96 }
97 length = ntohl(length); 97 length = ntohl(length);
98 if((length == 0 && !allowzero) || length > PWDSTORE_MAX_LEN) { 98 if(length == 0) {
99 if(allowzero) {
100 return 1;
101 } else {
102 return 0;
103 }
104 }
105 if(length > PWDSTORE_MAX_LEN) {
99 return 0; 106 return 0;
100 } 107 }
101 108
102 char *value = malloc(length + 1); 109 char *value = malloc(length + 1);
103 value[length] = 0; 110 value[length] = 0;
108 115
109 *val = value; 116 *val = value;
110 return 1; 117 return 1;
111 } 118 }
112 119
113 static int read_pwdentry(PwdStore *p, UcxBuffer *in, int index) { 120 static int read_indexentry(PwdStore *p, UcxBuffer *in) {
121 int type = ucx_buffer_getc(in);
122 if(type == EOF || type != 0) {
123 // only type 0 supported yet
124 return 0;
125 }
126
127 char *id = NULL;
128 UcxList *locations = NULL;
129
130 int ret = 0;
131 if(readval(in, &id, FALSE)) {
132 ret = 1;
133 char *location = NULL;
134 while((ret = readval(in, &location, TRUE)) == 1) {
135 if(!location) {
136 break;
137 }
138 locations = ucx_list_append(locations, location);
139 }
140 }
141
142 if(ret) {
143 pwdstore_put_index(p, id, locations);
144 } else {
145 if(id) free(id);
146 ucx_list_free_content(locations, free);
147 }
148
149 return ret;
150 }
151
152 static int read_pwdentry(PwdStore *p, UcxBuffer *in) {
114 int type = ucx_buffer_getc(in); 153 int type = ucx_buffer_getc(in);
115 if(type == EOF || type != 0) { 154 if(type == EOF || type != 0) {
116 // only type 0 supported yet 155 // only type 0 supported yet
117 return 0; 156 return 0;
118 } 157 }
120 char *id = NULL; 159 char *id = NULL;
121 char *location = NULL; 160 char *location = NULL;
122 char *user = NULL; 161 char *user = NULL;
123 char *password = NULL; 162 char *password = NULL;
124 163
125 int res = 0;
126 int ret = 0; 164 int ret = 0;
127 if((res += readval(in, &id, FALSE)) == 1) { 165 if(readval(in, &id, FALSE)) {
128 if(index) { 166 if(readval(in, &user, FALSE)) {
129 if((res += readval(in, &location, TRUE)) == 2) { 167 if(readval(in, &password, FALSE)) {
130 pwdstore_put_index(p, id, location); 168 pwdstore_put(p, id, user, password);
131 ret = 1; 169 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 }
141 if((res += readval(in, &location, TRUE)) == 2) {
142 if(!index) {
143 if((res += readval(in, &user, FALSE)) == 3) {
144 res += readval(in, &password, FALSE);
145 }
146 } 170 }
147 } 171 }
148 } 172 }
149 173
150 if(id) free(id); 174 if(id) free(id);
151 if(location) free(location); 175 if(location) free(location);
152 if(user) free(user); 176 if(user) free(user);
153 if(password) free(password); 177 if(password) free(password);
154 178
155 return ret; 179 return ret;
156
157 } 180 }
158 181
159 int pwdstore_getindex(PwdStore *s) { 182 int pwdstore_getindex(PwdStore *s) {
160 uint32_t netindexlen; 183 uint32_t netindexlen;
161 s->content->pos = PWDS_HEADER_SIZE - sizeof(uint32_t); 184 s->content->pos = PWDS_HEADER_SIZE - sizeof(uint32_t);
171 } 194 }
172 s->encoffset += indexlen; 195 s->encoffset += indexlen;
173 196
174 UcxBuffer *index = ucx_buffer_new(s->content->space+PWDS_HEADER_SIZE, indexlen, 0); 197 UcxBuffer *index = ucx_buffer_new(s->content->space+PWDS_HEADER_SIZE, indexlen, 0);
175 index->size = indexlen; 198 index->size = indexlen;
176 while(read_pwdentry(s, index, 1)) {} 199 while(read_indexentry(s, index)) {}
177 200
178 ucx_buffer_free(index); 201 ucx_buffer_free(index);
179 202
180 return 0; 203 return 0;
181 } 204 }
197 ucx_buffer_free(enc); 220 ucx_buffer_free(enc);
198 if(!content) { 221 if(!content) {
199 return 1; 222 return 1;
200 } 223 }
201 224
202 while(read_pwdentry(p, content, 0)) {} 225 while(read_pwdentry(p, content)) {}
203 226
204 ucx_buffer_free(content); 227 ucx_buffer_free(content);
205 228
206 return 0; 229 return 0;
207 } 230 }
265 entry->user = strdup(username); 288 entry->user = strdup(username);
266 entry->password = strdup(password); 289 entry->password = strdup(password);
267 ucx_map_cstr_put(p->ids, id, entry); 290 ucx_map_cstr_put(p->ids, id, entry);
268 } 291 }
269 292
270 void pwdstore_put_index(PwdStore *p, const char *id, const char *location) { 293 void pwdstore_put_index(PwdStore *p, char *id, UcxList *locations) {
271 PwdIndexEntry *e = ucx_map_cstr_get(p->index, id); 294 PwdIndexEntry *e = ucx_map_cstr_get(p->index, id);
272 if(e) { 295 if(e) {
273 return; 296 return;
274 } 297 }
275 PwdIndexEntry *newentry = malloc(sizeof(PwdIndexEntry)); 298 PwdIndexEntry *newentry = malloc(sizeof(PwdIndexEntry));
276 newentry->id = strdup(id); 299 newentry->id = id;
277 if(location) { 300 if(locations) {
278 newentry->location = strdup(location); 301 newentry->locations = locations;
279 p->locations = ucx_list_append(p->locations, newentry); 302 p->locations = ucx_list_append(p->locations, newentry);
280 } else { 303 } else {
281 newentry->location = NULL; 304 newentry->locations = NULL;
282 p->noloc = ucx_list_append(p->noloc, newentry); 305 p->noloc = ucx_list_append(p->noloc, newentry);
283 } 306 }
284 ucx_map_cstr_put(p->index, id, newentry); 307 ucx_map_cstr_put(p->index, id, newentry);
285 } 308 }
286 309
287 void write_index_entry(UcxBuffer *out, PwdIndexEntry *e) { 310 void write_index_entry(UcxBuffer *out, PwdIndexEntry *e) {
288 uint32_t idlen = strlen(e->id); 311 uint32_t idlen = strlen(e->id);
289 uint32_t locationlen = e->location ? strlen(e->location) : 0;
290 uint32_t netidlen = htonl(idlen); 312 uint32_t netidlen = htonl(idlen);
291 uint32_t netlocationlen = htonl(locationlen);
292 313
293 ucx_buffer_putc(out, 0); // type 314 ucx_buffer_putc(out, 0); // type
294 315
295 ucx_buffer_write(&netidlen, 1, sizeof(uint32_t), out); 316 ucx_buffer_write(&netidlen, 1, sizeof(uint32_t), out);
296 ucx_buffer_write(e->id, 1, idlen, out); 317 ucx_buffer_write(e->id, 1, idlen, out);
297 ucx_buffer_write(&netlocationlen, 1, sizeof(uint32_t), out); 318
298 if(e->location) { 319 UCX_FOREACH(elm, e->locations) {
299 ucx_buffer_write(e->location, 1, locationlen, out); 320 char *location = elm->data;
300 } 321 uint32_t locationlen = strlen(location);
322 uint32_t netlocationlen = htonl(locationlen);
323
324 ucx_buffer_write(&netlocationlen, 1, sizeof(uint32_t), out);
325 ucx_buffer_write(location, 1, locationlen, out);
326 }
327
328 uint32_t terminate = 0;
329 ucx_buffer_write(&terminate, 1, sizeof(uint32_t), out);
301 } 330 }
302 331
303 int pwdstore_store(PwdStore *p, const char *file) { 332 int pwdstore_store(PwdStore *p, const char *file) {
304 if(!p->key) { 333 if(!p->key) {
305 return 1; 334 return 1;

mercurial