| 1 | /* |
| 2 | * Copyright (C) 2004 Dizzy |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU General Public License |
| 6 | * as published by the Free Software Foundation; either version 2 |
| 7 | * of the License, or (at your option) any later version. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 17 | */ |
| 18 | |
| 19 | #include "common/setup_before.h" |
| 20 | #define ATTRGROUP_INTERNAL_ACCESS |
| 21 | #include "attrgroup.h" |
| 22 | |
| 23 | #include <cassert> |
| 24 | #include <cstring> |
| 25 | #include <cstdlib> |
| 26 | #include <string> |
| 27 | |
| 28 | #include "common/eventlog.h" |
| 29 | #include "common/flags.h" |
| 30 | #include "common/xalloc.h" |
| 31 | #include "compat/strcasecmp.h" |
| 32 | #include "compat/strncasecmp.h" |
| 33 | #include "attr.h" |
| 34 | #include "attrlayer.h" |
| 35 | #include "storage.h" |
| 36 | #include "prefs.h" |
| 37 | #include "server.h" |
| 38 | #include "connection.h" |
| 39 | #include "common/setup_after.h" |
| 40 | |
| 41 | |
| 42 | namespace pvpgn |
| 43 | { |
| 44 | |
| 45 | namespace bnetd |
| 46 | { |
| 47 | static const char * key_get_tab(const char *key); |
| 48 | |
| 49 | static inline void attrgroup_set_accessed(t_attrgroup *attrgroup) |
| 50 | { |
| 51 | FLAG_SET(&attrgroup->flags, ATTRGROUP_FLAG_ACCESSED); |
| 52 | attrgroup->lastaccess = now; |
| 53 | attrlayer_accessed(attrgroup); |
| 54 | } |
| 55 | |
| 56 | static inline void attrgroup_clear_accessed(t_attrgroup *attrgroup) |
| 57 | { |
| 58 | FLAG_CLEAR(&attrgroup->flags, ATTRGROUP_FLAG_ACCESSED); |
| 59 | } |
| 60 | |
| 61 | static inline void attrgroup_set_dirty(t_attrgroup *attrgroup) |
| 62 | { |
| 63 | if (FLAG_ISSET(attrgroup->flags, ATTRGROUP_FLAG_DIRTY)) return; |
| 64 | |
| 65 | attrgroup->dirtytime = now; |
| 66 | FLAG_SET(&attrgroup->flags, ATTRGROUP_FLAG_DIRTY); |
| 67 | attrlayer_add_dirtylist(&attrgroup->dirtylist); |
| 68 | } |
| 69 | |
| 70 | static inline void attrgroup_clear_dirty(t_attrgroup *attrgroup) |
| 71 | { |
| 72 | if (!FLAG_ISSET(attrgroup->flags, ATTRGROUP_FLAG_DIRTY)) return; |
| 73 | |
| 74 | FLAG_CLEAR(&attrgroup->flags, ATTRGROUP_FLAG_DIRTY); |
| 75 | attrlayer_del_dirtylist(&attrgroup->dirtylist); |
| 76 | |
| 77 | t_attr *attr; |
| 78 | t_hlist *curr; |
| 79 | // clear dirty flag on each attribute |
| 80 | hlist_for_each(curr, (t_hlist*)attrgroup) |
| 81 | { |
| 82 | attr = hlist_entry(curr, t_attr, link); |
| 83 | attr_clear_dirty(attr); |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | static inline void attrgroup_set_loaded(t_attrgroup *attrgroup) |
| 88 | { |
| 89 | if (FLAG_ISSET(attrgroup->flags, ATTRGROUP_FLAG_LOADED)) return; |
| 90 | FLAG_SET(&attrgroup->flags, ATTRGROUP_FLAG_LOADED); |
| 91 | |
| 92 | attrlayer_add_loadedlist(&attrgroup->loadedlist); |
| 93 | } |
| 94 | |
| 95 | static inline void attrgroup_clear_loaded(t_attrgroup *attrgroup) |
| 96 | { |
| 97 | if (!FLAG_ISSET(attrgroup->flags, ATTRGROUP_FLAG_LOADED)) return; |
| 98 | |
| 99 | /* clear this because they are not valid if attrgroup is unloaded */ |
| 100 | attrgroup_clear_dirty(attrgroup); |
| 101 | attrgroup_clear_accessed(attrgroup); |
| 102 | |
| 103 | FLAG_CLEAR(&attrgroup->flags, ATTRGROUP_FLAG_LOADED); |
| 104 | attrlayer_del_loadedlist(&attrgroup->loadedlist); |
| 105 | |
| 106 | #ifdef WITH_SQL |
| 107 | for (std::vector<const char *>::iterator it = attrgroup->loadedtabs->begin(); it != attrgroup->loadedtabs->end(); ++it) |
| 108 | xfree((void*)(*it)); |
| 109 | |
| 110 | // clear loaded tabs |
| 111 | attrgroup->loadedtabs->clear(); |
| 112 | #endif |
| 113 | } |
| 114 | |
| 115 | static t_attrgroup * attrgroup_create(void) |
| 116 | { |
| 117 | t_attrgroup *attrgroup; |
| 118 | |
| 119 | attrgroup = (t_attrgroup*)xmalloc(sizeof(t_attrgroup)); |
| 120 | |
| 121 | hlist_init(&attrgroup->list); |
| 122 | attrgroup->storage = NULL; |
| 123 | attrgroup->flags = ATTRGROUP_FLAG_NONE; |
| 124 | attrgroup->lastaccess = 0; |
| 125 | attrgroup->dirtytime = 0; |
| 126 | elist_init(&attrgroup->loadedlist); |
| 127 | elist_init(&attrgroup->dirtylist); |
| 128 | #ifdef WITH_SQL |
| 129 | attrgroup->loadedtabs = new std::vector<const char*>(); |
| 130 | #endif |
| 131 | return attrgroup; |
| 132 | } |
| 133 | |
| 134 | extern t_attrgroup * attrgroup_create_storage(t_storage_info *storage) |
| 135 | { |
| 136 | t_attrgroup *attrgroup; |
| 137 | |
| 138 | attrgroup = attrgroup_create(); |
| 139 | attrgroup->storage = storage; |
| 140 | |
| 141 | return attrgroup; |
| 142 | } |
| 143 | |
| 144 | extern t_attrgroup * attrgroup_create_newuser(const char *name) |
| 145 | { |
| 146 | t_attrgroup *attrgroup; |
| 147 | t_storage_info *stmp; |
| 148 | |
| 149 | stmp = storage->create_account(name); |
| 150 | if (!stmp) { |
| 151 | eventlog(eventlog_level_error, __FUNCTION__, "failed to add user '{}' to storage", name); |
| 152 | return NULL; |
| 153 | } |
| 154 | |
| 155 | attrgroup = attrgroup_create_storage(stmp); |
| 156 | |
| 157 | /* new accounts are born loaded */ |
| 158 | attrgroup_set_loaded(attrgroup); |
| 159 | |
| 160 | return attrgroup; |
| 161 | } |
| 162 | |
| 163 | extern t_attrgroup * attrgroup_create_nameuid(const char *name, unsigned uid) |
| 164 | { |
| 165 | t_storage_info *info; |
| 166 | t_attrgroup *attrgroup; |
| 167 | |
| 168 | info = storage->read_account(name, uid); |
| 169 | if (!info) return NULL; |
| 170 | |
| 171 | attrgroup = attrgroup_create_storage(info); |
| 172 | |
| 173 | return attrgroup; |
| 174 | } |
| 175 | |
| 176 | extern int attrgroup_destroy(t_attrgroup *attrgroup) |
| 177 | { |
| 178 | if (!attrgroup) { |
| 179 | eventlog(eventlog_level_error, __FUNCTION__, "got NULL attrgroup"); |
| 180 | return -1; |
| 181 | } |
| 182 | |
| 183 | attrgroup_unload(attrgroup); |
| 184 | if (attrgroup->storage) storage->free_info(attrgroup->storage); |
| 185 | xfree(attrgroup); |
| 186 | |
| 187 | return 0; |
| 188 | } |
| 189 | |
| 190 | extern int attrgroup_save(t_attrgroup *attrgroup, int flags) |
| 191 | { |
| 192 | if (!attrgroup) { |
| 193 | eventlog(eventlog_level_error, __FUNCTION__, "got NULL attrgroup"); |
| 194 | return -1; |
| 195 | } |
| 196 | |
| 197 | if (!FLAG_ISSET(attrgroup->flags, ATTRGROUP_FLAG_LOADED)) |
| 198 | return 0; |
| 199 | |
| 200 | if (!FLAG_ISSET(attrgroup->flags, ATTRGROUP_FLAG_DIRTY)) |
| 201 | return 0; |
| 202 | |
| 203 | if (!FLAG_ISSET(flags, FS_FORCE) && now - attrgroup->dirtytime < prefs_get_user_sync_timer()) |
| 204 | return 0; |
| 205 | |
| 206 | assert(attrgroup->storage); |
| 207 | |
| 208 | storage->write_attrs(attrgroup->storage, &attrgroup->list); |
| 209 | attrgroup_clear_dirty(attrgroup); |
| 210 | |
| 211 | return 1; |
| 212 | } |
| 213 | |
| 214 | extern int attrgroup_flush(t_attrgroup *attrgroup, int flags) |
| 215 | { |
| 216 | t_attr *attr; |
| 217 | t_hlist *curr, *save; |
| 218 | |
| 219 | if (!attrgroup) { |
| 220 | eventlog(eventlog_level_error, __FUNCTION__, "got NULL attrgroup"); |
| 221 | return -1; |
| 222 | } |
| 223 | |
| 224 | if (!FLAG_ISSET(attrgroup->flags, ATTRGROUP_FLAG_LOADED)) |
| 225 | return 0; |
| 226 | |
| 227 | if (!FLAG_ISSET(flags, FS_FORCE) && |
| 228 | FLAG_ISSET(attrgroup->flags, ATTRGROUP_FLAG_ACCESSED) && |
| 229 | now - attrgroup->lastaccess < prefs_get_user_flush_timer()) |
| 230 | return 0; |
| 231 | |
| 232 | assert(attrgroup->storage); |
| 233 | unsigned int uid = *((unsigned int *)attrgroup->storage); |
| 234 | t_storage_info *defacct = storage->get_defacct(); |
| 235 | unsigned int defuid = *((unsigned int *)defacct); |
| 236 | |
| 237 | // do not flush default account |
| 238 | if (uid == defuid) |
| 239 | { |
| 240 | storage->free_info(defacct); |
| 241 | return 2; |
| 242 | } |
| 243 | |
| 244 | // do not flush online users (but flush if FORCE!) |
| 245 | if (!prefs_get_user_flush_connected() && !FLAG_ISSET(flags, FS_FORCE)) |
| 246 | { |
| 247 | if (const char * username = attrgroup_get_attr(attrgroup, "BNET\\acct\\username")) |
| 248 | if (t_connection * c = connlist_find_connection_by_accountname(username)) |
| 249 | { |
| 250 | storage->free_info(defacct); |
| 251 | return 2; |
| 252 | } |
| 253 | } |
| 254 | |
| 255 | /* sync data to disk if dirty */ |
| 256 | attrgroup_save(attrgroup, FS_FORCE); |
| 257 | |
| 258 | hlist_for_each_safe(curr, &attrgroup->list, save) { |
| 259 | attr = hlist_entry(curr, t_attr, link); |
| 260 | attr_destroy(attr); |
| 261 | } |
| 262 | hlist_init(&attrgroup->list); /* reset list */ |
| 263 | |
| 264 | attrgroup_clear_loaded(attrgroup); |
| 265 | |
| 266 | storage->free_info(defacct); |
| 267 | |
| 268 | return 1; |
| 269 | } |
| 270 | |
| 271 | static int _cb_load_attr(const char *key, const char *val, void *data) |
| 272 | { |
| 273 | t_attrgroup *attrgroup = (t_attrgroup *)data; |
| 274 | |
| 275 | #ifdef WITH_SQL |
| 276 | if (strcmp(prefs_get_storage_path(), "sql") == 0) |
| 277 | { |
| 278 | const char *tab = key_get_tab(key); |
| 279 | |
| 280 | bool is_found = false; |
| 281 | for (std::vector<const char *>::iterator it = attrgroup->loadedtabs->begin(); it != attrgroup->loadedtabs->end(); ++it) |
| 282 | { |
| 283 | if (strcmp(tab, *it) == 0) |
| 284 | is_found = true; |
| 285 | } |
| 286 | // add a tab if it's not found |
| 287 | if (!is_found) |
| 288 | attrgroup->loadedtabs->push_back(tab); |
| 289 | else |
| 290 | xfree((void*)tab); |
| 291 | } |
| 292 | #endif |
| 293 | // set loaded attribute without a dirty flag |
| 294 | return attrgroup_set_attr(attrgroup, key, val, false); |
| 295 | } |
| 296 | |
| 297 | extern int attrgroup_load(t_attrgroup *attrgroup, const char *tab) |
| 298 | { |
| 299 | assert(attrgroup); |
| 300 | assert(attrgroup->storage); |
| 301 | |
| 302 | if (FLAG_ISSET(attrgroup->flags, ATTRGROUP_FLAG_LOADED)) |
| 303 | { |
| 304 | #ifdef WITH_SQL |
| 305 | if (strcmp(prefs_get_storage_path(), "sql") == 0) |
| 306 | { |
| 307 | // find a tab |
| 308 | for (std::vector<const char *>::iterator it = attrgroup->loadedtabs->begin(); it != attrgroup->loadedtabs->end(); ++it) |
| 309 | if (strcmp(tab, *it) == 0) |
| 310 | return 0; |
| 311 | } |
| 312 | else |
| 313 | #endif |
| 314 | return 0; /* already done */ |
| 315 | } |
| 316 | else |
| 317 | { |
| 318 | if (FLAG_ISSET(attrgroup->flags, ATTRGROUP_FLAG_DIRTY)) { /* if not loaded, how dirty ? */ |
| 319 | eventlog(eventlog_level_error, __FUNCTION__, "can't load modified account"); |
| 320 | return -1; |
| 321 | } |
| 322 | } |
| 323 | |
| 324 | attrgroup_set_loaded(attrgroup); |
| 325 | if (storage->read_attrs(attrgroup->storage, _cb_load_attr, attrgroup, tab)) { |
| 326 | eventlog(eventlog_level_error, __FUNCTION__, "got error loading attributes"); |
| 327 | return -1; |
| 328 | } |
| 329 | |
| 330 | return 0; |
| 331 | } |
| 332 | |
| 333 | extern int attrgroup_unload(t_attrgroup *attrgroup) |
| 334 | { |
| 335 | attrgroup_flush(attrgroup, FS_FORCE); |
| 336 | |
| 337 | return 0; |
| 338 | } |
| 339 | |
| 340 | typedef struct { |
| 341 | void *data; |
| 342 | t_attr_cb cb; |
| 343 | } t_attr_cb_data; |
| 344 | |
| 345 | static int _cb_read_accounts(t_storage_info *info, void *data) |
| 346 | { |
| 347 | t_attrgroup *attrgroup; |
| 348 | t_attr_cb_data *cbdata = (t_attr_cb_data*)data; |
| 349 | |
| 350 | attrgroup = attrgroup_create_storage(info); |
| 351 | return cbdata->cb(attrgroup, cbdata->data); |
| 352 | } |
| 353 | |
| 354 | extern int attrgroup_read_accounts(int flag, t_attr_cb cb, void *data) |
| 355 | { |
| 356 | t_attr_cb_data cbdata; |
| 357 | |
| 358 | cbdata.cb = cb; |
| 359 | cbdata.data = data; |
| 360 | |
| 361 | return storage->read_accounts(flag, _cb_read_accounts, &cbdata); |
| 362 | } |
| 363 | |
| 364 | static const char *attrgroup_escape_key(const char *key) |
| 365 | { |
| 366 | const char *newkey, *newkey2; |
| 367 | char *tmp; |
| 368 | |
| 369 | newkey = key; |
| 370 | |
| 371 | if (!strncasecmp(key, "DynKey", 6)) { |
| 372 | /* OLD COMMENT, MIGHT NOT BE VALID ANYMORE |
| 373 | * Recent Starcraft clients seems to query DynKey\*\1\rank instead of |
| 374 | * Record\*\1\rank. So replace Dynkey with Record for key lookup. |
| 375 | */ |
| 376 | tmp = xstrdup(key); |
| 377 | std::strncpy(tmp, "Record", 6); |
| 378 | newkey = tmp; |
| 379 | } |
| 380 | else if (!std::strncmp(key, "Star", 4)) { |
| 381 | /* OLD COMMENT |
| 382 | * Starcraft clients query Star instead of STAR on logon screen. |
| 383 | */ |
| 384 | tmp = xstrdup(key); |
| 385 | std::strncpy(tmp, "STAR", 4); |
| 386 | newkey = tmp; |
| 387 | } |
| 388 | |
| 389 | if (newkey != key) { |
| 390 | newkey2 = storage->escape_key(newkey); |
| 391 | if (newkey2 != newkey) { |
| 392 | xfree((void*)newkey); |
| 393 | newkey = newkey2; |
| 394 | } |
| 395 | } |
| 396 | else newkey = storage->escape_key(key); |
| 397 | |
| 398 | return newkey; |
| 399 | } |
| 400 | |
| 401 | static t_attr *attrgroup_find_attr(t_attrgroup *attrgroup, const char *pkey[], int escape) |
| 402 | { |
| 403 | const char *val; |
| 404 | t_hlist *curr, *last, *last2; |
| 405 | t_attr *attr; |
| 406 | |
| 407 | assert(attrgroup); |
| 408 | assert(pkey); |
| 409 | assert(*pkey); |
| 410 | |
| 411 | /* only if the callers tell us to */ |
| 412 | if (escape) *pkey = attrgroup_escape_key(*pkey); |
| 413 | |
| 414 | const char * tab = key_get_tab(*pkey); |
| 415 | /* trigger loading of attributes if not loaded already */ |
| 416 | if (attrgroup_load(attrgroup, tab)) |
| 417 | return NULL; /* eventlog happens earlier */ |
| 418 | xfree((void*)tab); |
| 419 | |
| 420 | /* we are doing attribute lookup so we are accessing it */ |
| 421 | attrgroup_set_accessed(attrgroup); |
| 422 | |
| 423 | last = &attrgroup->list; |
| 424 | last2 = NULL; |
| 425 | |
| 426 | hlist_for_each(curr, &attrgroup->list) { |
| 427 | attr = hlist_entry(curr, t_attr, link); |
| 428 | |
| 429 | if (!strcasecmp(attr_get_key(attr), *pkey)) { |
| 430 | val = attr_get_val(attr); |
| 431 | /* key found, promote it so it's found faster next time */ |
| 432 | hlist_promote(curr, last, last2); |
| 433 | break; |
| 434 | } |
| 435 | |
| 436 | last2 = last; last = curr; |
| 437 | } |
| 438 | |
| 439 | if (curr == &attrgroup->list) { /* no key found in cached list */ |
| 440 | attr = (t_attr*)storage->read_attr(attrgroup->storage, *pkey); |
| 441 | if (attr) hlist_add(&attrgroup->list, &attr->link); |
| 442 | } |
| 443 | |
| 444 | /* "attr" here can either have a proper value found in the cached list, or |
| 445 | * a value returned by storage->read_attr, or NULL */ |
| 446 | return attr; |
| 447 | } |
| 448 | |
| 449 | /* low-level get attr, receives a flag to tell if it needs to escape key */ |
| 450 | static const char *attrgroup_get_attrlow(t_attrgroup *attrgroup, const char *key, int escape) |
| 451 | { |
| 452 | const char *val = NULL; |
| 453 | const char *newkey = key; |
| 454 | t_attr *attr; |
| 455 | |
| 456 | /* no need to check for attrgroup, key */ |
| 457 | |
| 458 | attr = attrgroup_find_attr(attrgroup, &newkey, escape); |
| 459 | |
| 460 | // if attribute found |
| 461 | if (attr) |
| 462 | val = attr_get_val(attr); |
| 463 | |
| 464 | // if attribute is null then return default attribute value |
| 465 | if (!val && attrgroup != attrlayer_get_defattrgroup()) |
| 466 | val = attrgroup_get_attrlow(attrlayer_get_defattrgroup(), newkey, 0); |
| 467 | |
| 468 | if (newkey != key) xfree((void*)newkey); |
| 469 | |
| 470 | return val; |
| 471 | } |
| 472 | |
| 473 | extern const char *attrgroup_get_attr(t_attrgroup *attrgroup, const char *key) |
| 474 | { |
| 475 | if (!attrgroup) { |
| 476 | eventlog(eventlog_level_error, __FUNCTION__, "got NULL attrgroup"); |
| 477 | return NULL; |
| 478 | } |
| 479 | |
| 480 | if (!key) { |
| 481 | eventlog(eventlog_level_error, __FUNCTION__, "got NULL key"); |
| 482 | return NULL; |
| 483 | } |
| 484 | |
| 485 | return attrgroup_get_attrlow(attrgroup, key, 1); |
| 486 | } |
| 487 | |
| 488 | extern int attrgroup_set_attr(t_attrgroup *attrgroup, const char *key, const char *val, bool set_dirty) |
| 489 | { |
| 490 | t_attr *attr; |
| 491 | const char *newkey = key; |
| 492 | |
| 493 | if (!attrgroup) { |
| 494 | eventlog(eventlog_level_error, __FUNCTION__, "got NULL attrgroup"); |
| 495 | return -1; |
| 496 | } |
| 497 | |
| 498 | if (!key) { |
| 499 | eventlog(eventlog_level_error, __FUNCTION__, "got NULL key"); |
| 500 | return -1; |
| 501 | } |
| 502 | |
| 503 | attr = attrgroup_find_attr(attrgroup, &newkey, 1); |
| 504 | |
| 505 | if (attr) { |
| 506 | if (attr_get_val(attr) == val || |
| 507 | (attr_get_val(attr) && val && !std::strcmp(attr_get_val(attr), val))) |
| 508 | goto out; /* no need to modify anything, values are the same */ |
| 509 | |
| 510 | /* new value for existent key, replace the old one */ |
| 511 | attr_set_val(attr, val); |
| 512 | } |
| 513 | else { /* unknown key so add new attr */ |
| 514 | attr = attr_create(newkey, val); |
| 515 | hlist_add(&attrgroup->list, &attr->link); |
| 516 | } |
| 517 | |
| 518 | /* we have modified this attr and attrgroup */ |
| 519 | if (set_dirty) |
| 520 | { |
| 521 | attr_set_dirty(attr); |
| 522 | attrgroup_set_dirty(attrgroup); |
| 523 | } |
| 524 | |
| 525 | out: |
| 526 | if (newkey != key) xfree((void*)newkey); |
| 527 | |
| 528 | return 0; |
| 529 | } |
| 530 | |
| 531 | // extract tab name from key |
| 532 | static const char * key_get_tab(const char *key) |
| 533 | { |
| 534 | std::string str = std::string(key); |
| 535 | std::size_t pos = str.find("_"); |
| 536 | std::string find = str.substr(0, pos); |
| 537 | return xstrdup(find.c_str()); |
| 538 | } |
| 539 | |
| 540 | |
| 541 | } |
| 542 | |
| 543 | } |