Changeset 8e6ecfe for storage_xml.c
- Timestamp:
- 2016-03-25T18:07:53Z (9 years ago)
- Branches:
- master
- Children:
- a6005da
- Parents:
- 446a23e
- git-author:
- Dennis Kaarsemaker <dennis@…> (23-02-16 18:41:34)
- git-committer:
- Dennis Kaarsemaker <dennis@…> (25-03-16 18:07:53)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
storage_xml.c
r446a23e r8e6ecfe 34 34 35 35 typedef enum { 36 XML_PASS_CHECK_ONLY = -1, 37 XML_PASS_UNKNOWN = 0, 38 XML_PASS_WRONG, 39 XML_PASS_OK 40 } xml_pass_st; 36 XML_PASS_CHECK = 0, 37 XML_LOAD 38 } xml_action; 41 39 42 40 /* To make it easier later when extending the format: */ … … 121 119 if (!handle || !pass_b64 || !protocol || !prpl) { 122 120 return XT_ABORT; 123 } else if ((pass_len = base64_decode(pass_b64, (unsigned char **) &pass_cr)) && 124 arc_decode(pass_cr, pass_len, &password, xd->given_pass) >= 0) { 125 acc = account_add(xd->irc->b, prpl, handle, password); 126 if (server) { 127 set_setstr(&acc->set, "server", server); 128 } 129 if (autoconnect) { 130 set_setstr(&acc->set, "auto_connect", autoconnect); 131 } 132 if (tag) { 133 set_setstr(&acc->set, "tag", tag); 134 } 135 if (local) { 136 acc->flags |= ACC_FLAG_LOCAL; 137 } 138 if (locked && !g_strcasecmp(locked, "true")) { 139 acc->flags |= ACC_FLAG_LOCKED; 140 } 121 } 122 123 base64_decode(pass_b64, (unsigned char **) &pass_cr); 124 if (xd->irc->auth_backend) { 125 password = g_strdup((char *)pass_cr); 141 126 } else { 142 g_free(pass_cr); 143 g_free(password); 144 return XT_ABORT; 127 pass_len = arc_decode(pass_cr, pass_len, &password, xd->given_pass); 128 if (pass_len < 0) { 129 g_free(pass_cr); 130 g_free(password); 131 return XT_ABORT; 132 } 133 } 134 135 acc = account_add(xd->irc->b, prpl, handle, password); 136 if (server) { 137 set_setstr(&acc->set, "server", server); 138 } 139 if (autoconnect) { 140 set_setstr(&acc->set, "auto_connect", autoconnect); 141 } 142 if (tag) { 143 set_setstr(&acc->set, "tag", tag); 144 } 145 if (local) { 146 acc->flags |= ACC_FLAG_LOCAL; 147 } 148 if (locked && !g_strcasecmp(locked, "true")) { 149 acc->flags |= ACC_FLAG_LOCKED; 145 150 } 146 151 … … 198 203 }; 199 204 200 static storage_status_t xml_load_real(irc_t *irc, const char *my_nick, const char *password, xml_ pass_staction)205 static storage_status_t xml_load_real(irc_t *irc, const char *my_nick, const char *password, xml_action action) 201 206 { 202 207 struct xml_parsedata xd[1]; … … 240 245 } 241 246 242 {247 if (action == XML_PASS_CHECK) { 243 248 char *nick = xt_find_attr(node, "nick"); 244 249 char *pass = xt_find_attr(node, "password"); 245 246 if (!nick || !pass) { 250 char *backend = xt_find_attr(node, "auth_backend"); 251 252 if (!nick || !(pass || backend)) { 247 253 goto error; 254 } 255 256 if (backend) { 257 g_free(xd->irc->auth_backend); 258 xd->irc->auth_backend = g_strdup(backend); 259 ret = STORAGE_CHECK_BACKEND; 248 260 } else if ((st = md5_verify_password(xd->given_pass, pass)) != 0) { 249 261 ret = STORAGE_INVALID_PASSWORD; 250 goto error; 251 } 252 } 253 254 if (action == XML_PASS_CHECK_ONLY) { 255 ret = STORAGE_OK; 256 goto error; 257 } 258 259 /* DO NOT call xt_handle() before verifying the password! */ 262 } else { 263 ret = STORAGE_OK; 264 } 265 goto error; 266 } 267 260 268 if (xt_handle(xp, NULL, 1) == XT_HANDLED) { 261 269 ret = STORAGE_OK; … … 272 280 static storage_status_t xml_load(irc_t *irc, const char *password) 273 281 { 274 return xml_load_real(irc, irc->user->nick, password, XML_ PASS_UNKNOWN);275 } 276 277 static storage_status_t xml_check_pass( const char *my_nick, const char *password)278 { 279 return xml_load_real( NULL, my_nick, password, XML_PASS_CHECK_ONLY);282 return xml_load_real(irc, irc->user->nick, password, XML_LOAD); 283 } 284 285 static storage_status_t xml_check_pass(irc_t *irc, const char *my_nick, const char *password) 286 { 287 return xml_load_real(irc, my_nick, password, XML_PASS_CHECK); 280 288 } 281 289 … … 292 300 struct xt_node *root, *cur; 293 301 294 /* Generate a salted md5sum of the password. Use 5 bytes for the salt295 (to prevent dictionary lookups of passwords) to end up with a 21-296 byte password hash, more convenient for base64 encoding. */297 random_bytes(pass_md5 + 16, 5);298 md5_init(&md5_state);299 md5_append(&md5_state, (md5_byte_t *) irc->password, strlen(irc->password));300 md5_append(&md5_state, pass_md5 + 16, 5); /* Add the salt. */301 md5_finish(&md5_state, pass_md5);302 /* Save the hash in base64-encoded form. */303 pass_buf = base64_encode(pass_md5, 21);304 305 302 root = cur = xt_new_node("user", NULL, NULL); 303 if (irc->auth_backend) { 304 xt_add_attr(cur, "auth_backend", irc->auth_backend); 305 } else { 306 /* Generate a salted md5sum of the password. Use 5 bytes for the salt 307 (to prevent dictionary lookups of passwords) to end up with a 21- 308 byte password hash, more convenient for base64 encoding. */ 309 random_bytes(pass_md5 + 16, 5); 310 md5_init(&md5_state); 311 md5_append(&md5_state, (md5_byte_t *) irc->password, strlen(irc->password)); 312 md5_append(&md5_state, pass_md5 + 16, 5); /* Add the salt. */ 313 md5_finish(&md5_state, pass_md5); 314 /* Save the hash in base64-encoded form. */ 315 pass_buf = base64_encode(pass_md5, 21); 316 xt_add_attr(cur, "password", pass_buf); 317 g_free(pass_buf); 318 } 319 306 320 xt_add_attr(cur, "nick", irc->user->nick); 307 xt_add_attr(cur, "password", pass_buf);308 321 xt_add_attr(cur, "version", XML_FORMAT_VERSION); 309 310 g_free(pass_buf);311 322 312 323 xml_generate_settings(cur, &irc->b->set); … … 319 330 int pass_len; 320 331 321 pass_len = arc_encode(acc->pass, strlen(acc->pass), (unsigned char **) &pass_cr, irc->password, 12); 322 pass_b64 = base64_encode(pass_cr, pass_len); 323 g_free(pass_cr); 332 if(irc->auth_backend) { 333 /* If we don't "own" the password, it may change without us 334 * knowing, so we cannot encrypt the data, as we then may not be 335 * able to decrypt it */ 336 pass_b64 = base64_encode((unsigned char *)acc->pass, strlen(acc->pass)); 337 } else { 338 pass_len = arc_encode(acc->pass, strlen(acc->pass), (unsigned char **) &pass_cr, irc->password, 12); 339 pass_b64 = base64_encode(pass_cr, pass_len); 340 g_free(pass_cr); 341 } 324 342 325 343 cur = xt_new_node("account", NULL, NULL); … … 440 458 441 459 442 static storage_status_t xml_remove(const char *nick , const char *password)460 static storage_status_t xml_remove(const char *nick) 443 461 { 444 462 char s[512], *lc; 445 storage_status_t status;446 447 status = xml_check_pass(nick, password);448 if (status != STORAGE_OK) {449 return status;450 }451 463 452 464 lc = g_strdup(nick);
Note: See TracChangeset
for help on using the changeset viewer.