Debian Codegraph @

server/account.c

raw symbols SCIP
1 /*
2 * Crossfire -- cooperative multi-player graphical RPG and adventure game
3 *
4 * Copyright (c) 1999-2014 Mark Wedel and the Crossfire Development Team
5 * Copyright (c) 1992 Frank Tore Johansen
6 *
7 * Crossfire is free software and comes with ABSOLUTELY NO WARRANTY. You are
8 * welcome to redistribute it under certain conditions. For details, please
9 * see COPYING and LICENSE.
10 *
11 * The authors can be reached via e-mail at <[email protected]>.
12 */
13
14 /**
15 * @file
16 * This file contains account management logic - creation, deletion,
17 * log in verification, as well as associating player files with the
18 * account.
19 *
20 * The code in this file mostly assumes that the number of account operations
21 * will be low - for example, we load up all acounts in a linked list, and save
22 * them all out. If we have 10,000 accounts, this would be very slow. But for
23 * a few hundred accounts, should be plenty fast on most any hardware.
24 *
25 * Likewise, the account structure is not exposed outside this file - this means
26 * that account access/updates is done through the account name. This adds some
27 * extra overhead in that code has to search the linked list, but for a relatively
28 * low number of entries, and relatively infrequent updates, this should not be much
29 * of an issue.
30 *
31 * Since the accounts file is updated in several places in this file,
32 * it is documented here. The file is a list of accounts, one line
33 * per account, with fields separted by colons, eg:
34 *
35 * Account name:Password:Account last used:Characters (semicolon separated):expansion
36 * [email protected]:mypassword:1260686494:Mark;MarkW;:
37 *
38 * none of the the fields listed could contain colons, and player names can not contain
39 * semicolon, as that is used to separate those. In addition, everything is limited to
40 * printable characters.
41 * The accounts file is designed to be human readable, even if it is not expected that
42 * a human will read it.
43 * Passwords are hashed using crypt(3) in traditional mode, unless you're on
44 * FreeBSD or Windows in which case passwords are stored in plaintext due to
45 * legacy reasons.
46 * expansion (last field) is there for possible expansion. There may be desire to store
47 * bits of information there. An example might be dm=1. But it is there for expansion.
48 */
49
50 #include "global.h"
51
52 #include <ctype.h>
53 #include <errno.h>
54 #include <stdlib.h>
55 #include <string.h>
56
57 #include "object.h"
58 #include "sproto.h"
59
60 #include "output_file.h"
61
62 /** Number of fields in the accounts file. These are colon seperated */
63 #define NUM_ACCOUNT_FIELDS 6
64
65 /**
66 * Structure that holds account data. This is basically in game representation
67 * of the data store in the file above.
68 * Note that there is no field here for the expansion area - if that gets used,
69 * then almost certainly the values will be extracted and stored into new fields
70 * in this structure, and not just a character that contains the data in an
71 * unprocessed form, eg, a int dm field could get added.
72 * all char* data here is from strdup_local(), and thus the shared string
73 * comparisons/functions should not be used on it.
74 */
75
76 typedef struct account_struct {
77 char *name; /**< Account name */
78 char *password; /**< Password for this account */
79 time_t last_login; /**< Last time this account was logged in */
80 int num_characters; /**< Number of characters on this account */
81 char *character_names[MAX_CHARACTERS_PER_ACCOUNT+1];
82 /**< Character names associated with this account
83 +1 added to allow for NULL termination */
84 time_t created; /**< When character was created */
85 struct account_struct *next; /**< Next in list */
86 } account_struct;
87
88 /**
89 * list of all accounts.
90 */
91 static account_struct *accounts=NULL;
92
93 /**
94 * Whether the account information was loaded or not.
95 * Because accounts_save() is called in cleanup(), we don't want
96 * programs using this library to erase the server's account information,
97 * since it is NOT loaded by default.
98 */
99 static int accounts_loaded = 0;
100
101 /**
102 * Name of the accounts file. I can not ever see a reason why this
103 * name would not work, but may as well still make it easy to change it.
104 */
105 #define ACCOUNT_FILE "accounts"
106
107 /**
108 * This is used purely by the test harness - by clearing the accounts,
109 * it can then verify that the data is added is loaded back into memory
110 * properly. As such, we don't worry about memory cleanup, etc.
111 */
112 void accounts_clear(void) {
113 accounts = NULL;
114 accounts_loaded = 0;
115 }
116
117 /**
118 * This loads all the account entries into memory. It is presumed to only
119 * be called once during the program startup.
120 */
121 void accounts_load(void) {
122 char fname[MAX_BUF], buf[VERY_BIG_BUF];
123 FILE *fp;
124 account_struct *ac, *last=NULL;
125 int fields=0;
126
127 if (accounts != NULL) {
128 LOG(llevError, "account_load_entries(): Called when accounts has been set.\n");
129 return;
130 }
131 snprintf(fname, MAX_BUF,"%s/%s", settings.localdir, ACCOUNT_FILE);
132 fp=fopen(fname,"r");
133 if (!fp) {
134 /* This may not in fact be a critical error - on a new server,
135 * the accounts file may not yet exist.
136 */
137 LOG(llevInfo,"Warning: Unable to open %s [%s]\n", fname, strerror(errno));
138 return;
139 }
140 while (fgets(buf, VERY_BIG_BUF, fp)) {
141 char *tmp[NUM_ACCOUNT_FIELDS], *cp;
142 int result, i;
143
144 /* Ignore any comment lines */
145 if (buf[0] == '#') continue;
146
147 /* remove newline */
148 cp = strchr(buf, '\n');
149 if (cp) *cp='\0';
150
151 fields = split_string(buf, tmp, NUM_ACCOUNT_FIELDS, ':');
152
153 ac = malloc(sizeof(account_struct));
154 ac->name = strdup_local(tmp[0]);
155 ac->password = strdup_local(tmp[1]);
156 ac->last_login = strtoul(tmp[2], (char**)NULL, 10);
157
158 /* While probably no one was using this code before this
159 * field was added, this provides a nice example of handling
160 * additional fields.
161 */
162 if (fields>4) ac->created = strtoul(tmp[4], (char**)NULL, 10);
163 else
164 ac->created = ac->last_login;
165
166 ac->next = NULL;
167
168 /* If this is a blank field, nothing to do */
169 if (tmp[3][0] == 0) {
170 ac->num_characters = 0;
171 for (i=0; i <= MAX_CHARACTERS_PER_ACCOUNT; i++)
172 ac->character_names[i] = NULL;
173 } else {
174 /* count up how many semicolons - this is the character
175 * seperator. We start at one, because these are seperators,
176 * so there will be one more name than seperators.
177 */
178 ac->num_characters=1;
179 for (cp = tmp[3]; *cp != '\0'; cp++) {
180 if (*cp == ';') ac->num_characters++;
181 }
182
183 result = split_string(tmp[3], ac->character_names, ac->num_characters, ';');
184 /* This should never happen, but check for it. Even if we do get it, not necessarily
185 * a critical error - this is why we use calloc above.
186 */
187 if (result != ac->num_characters) {
188 LOG(llevError, "account_load_entries: split_string found different number of characters: %d != %d\n",
189 result, ac->num_characters);
190 }
191
192 if (ac->num_characters > MAX_CHARACTERS_PER_ACCOUNT) {
193 LOG(llevError,"account_load_entries: Too many characters set for account %s - truncating to %d\n",
194 ac->name, MAX_CHARACTERS_PER_ACCOUNT);
195 ac->num_characters = MAX_CHARACTERS_PER_ACCOUNT;
196 }
197
198 /* The string data that the names are stored in is currently temporary data
199 * that will go away, so we need to allocate some permanent data
200 * now. Also, if we have a NULL value, means we got the error above -
201 * NULL values would only be at the end of the split, so just reduce
202 * the character count.
203 */
204 for (i=0; i<ac->num_characters; i++) {
205 if (ac->character_names[i] != NULL) {
206 ac->character_names[i] = strdup_local(ac->character_names[i]);
207 } else {
208 ac->num_characters = i;
209 break;
210 }
211 }
212 /* NULL terminate - in that way, we can just return ac->character_names to
213 * callers that want to know all characters associated with an account,
214 * and it can just iterate until it gets the NULL terminator.
215 * For safety, just set all remaining values to NULL
216 */
217 while (i <= MAX_CHARACTERS_PER_ACCOUNT) {
218 ac->character_names[i] = NULL;
219 i++;
220 }
221 }
222
223 /* We tack on to the end of the list - in this way,
224 * the order of the file remains the same.
225 */
226 if (last)
227 last->next = ac;
228 else
229 accounts = ac;
230 last = ac;
231
232 }
233 fclose(fp);
234 accounts_loaded = 1;
235 }
236
237 /**
238 * This writes a single account entry to the given filepointer.
239 * it is used both when saving all accounts, or if we just need
240 * to append a new account to the end of the file.
241 * @param fp
242 * file pointer to write to.
243 * @param ac
244 * account structure to write out.
245 */
246 static void account_write_entry(FILE *fp, account_struct *ac)
247 {
248 int i;
249
250 fprintf(fp,"%s:%s:%u:", ac->name, ac->password, (uint32_t)ac->last_login);
251 for (i=0; i<ac->num_characters; i++) {
252 if (i != 0)
253 fprintf(fp,";%s", ac->character_names[i]);
254 else
255 fprintf(fp,"%s", ac->character_names[i]);
256 }
257 fprintf(fp,":%u:\n", (uint32_t) ac->created);
258 }
259
260
261 /**
262 * Save all the account information. Since the data is stored in a flat
263 * file, if an update is needed for an account, the only way to save updates
264 * is to save all the data - there isn't an easy way to just add another player
265 * name for an account.
266 */
267 void accounts_save(void)
268 {
269 char fname[MAX_BUF];
270 FILE *fp;
271 OutputFile of;
272 account_struct *ac;
273
274 if (accounts_loaded == 0)
275 return;
276
277 snprintf(fname, MAX_BUF,"%s/%s", settings.localdir, ACCOUNT_FILE);
278
279 fp = of_open(&of, fname);
280 if (fp == NULL)
281 return;
282
283 fprintf(fp, "# IMPORTANT: Do not edit this file while the server is running. This file is\n"
284 "# only read when the server starts, and any changes will be overwritten when\n"
285 "# the server exits.\n");
286 fprintf(fp, "# Format:\n");
287 fprintf(fp, "# Account name:Password:Account last used:Characters (semicolon separated):created:expansion\n");
288 for (ac=accounts; ac; ac=ac->next) {
289 /* Don't write out accounts with no characters associated unless the
290 * account is at least a day old.
291 */
292 // 86400 seconds in a day, so no reason to make the game have to recalculate this all the time
293 // SilverNexus 2014-06-12
294 if (ac->num_characters || (ac->created > (time(NULL) - 86400)))
295 account_write_entry(fp, ac);
296 }
297 of_close(&of);
298 }
299
300 /**
301 * Checks the existing accounts, and see if this account exists.
302 * This can also be used to get the official name for the account
303 * (eg, as user first entered, so Mark instead of mARk)
304 * @param account_name
305 * account name we are looking for.
306 * @return
307 * returns official name on match, NULL on no match.
308 */
309 const char *account_exists(const char *account_name)
310 {
311 account_struct *ac;
312
313 for (ac=accounts; ac; ac=ac->next) {
314 if (!strcasecmp(ac->name, account_name)) return ac->name;
315 }
316 return NULL;
317 }
318
319 /**
320 * Check if the given account exists, and whether the password is correct.
321 *
322 * Note - if we do get a match, we update the last_login value - it is
323 * presumed that if someone knows the right accountname/password, that
324 * the account is effectively getting logged in.
325 * @param account_name
326 * account name we are looking for.
327 * @param account_password
328 * password for the account. This is the unencrypted password (password as entered
329 * by user)
330 * @return
331 * 0 if no match/wrong password, 1 if a match is found and password matches.
332 */
333 int account_login(const char *account_name, const char *account_password) {
334 account_struct *ac;
335
336 for (ac=accounts; ac; ac=ac->next) {
337 /* Look for a matching account name and check the password. */
338 if (!strcasecmp(ac->name, account_name)) {
339 if (check_password(account_password, ac->password)) {
340 ac->last_login = time(NULL);
341 return 1;
342 } else {
343 return 0;
344 }
345 }
346 }
347 return 0;
348 }
349
350 /**
351 * Checks a string to make sure it does not have any invalid characters.
352 * We are fairly permissive on character here.
353 * String must start with alphanumeric
354 * String must not contain :;/'[
355 * String can not end if a space
356 * This string will get used for file/directory names, but so long
357 * as as characters are not included that are not illegal, one can still
358 * manipulate the file/directory by putting it in quotes. If one starts
359 * to block all characters that may get interperted by shells, a large
360 * number of characters now get blocked (|*]()!, etc), and deciding
361 * which should be allowed and not just becomes a judgement call, so easier
362 * to just allow all and have the user put it in quotes.
363 *
364 * @param str
365 * string to check
366 * @return
367 * 0 if ok, 1 if we have an invalid character, 2 if string is too long.
368 */
369 int account_check_string(const char *str)
370 {
371 const char *cp = str;
372
373 /* Require first character to be letter or number */
374 if (!isalnum(*str)) return 1;
375 for (; *str != '\0'; ++str) {
376 if (!isprint(*str)) return 1;
377 switch (*str){
378 case ':':
379 case ';':
380 case '/':
381 case '\'':
382 case '[':
383 return 1;
384 }
385 }
386 /* Don't allow space characters at end of string. */
387 if (isspace(*(str-1))) return 1;
388 if ((str - cp) > MAX_NAME) return 2;
389 return 0;
390 }
391
392
393 /**
394 * Adds an account. It does error checking, but the caller might
395 * want to do checking before getting here.
396 * @param account_name
397 * account name we are adding
398 * @param account_password
399 * password for the account. This is the unencrypted password (password as entered
400 * by user)
401 * @retval 0
402 * account added successfully.
403 * @retval 1
404 * name or password has invalid character.
405 * @retval 2
406 * account already exists.
407 */
408 int account_new(const char *account_name, const char *account_password) {
409 account_struct *ac;
410
411 // Check password for invalid characters because newhash() may just
412 // return the string in plaintext.
413 if (account_check_string(account_name) || account_check_string(account_password))
414 return 1;
415
416 if (account_exists(account_name)) return 2;
417
418 ac = malloc(sizeof(account_struct));
419 ac->name = strdup_local(account_name);
420 ac->password = strdup_local(newhash(account_password));
421 ac->last_login = time(NULL);
422 ac->created = ac->last_login;
423 ac->num_characters = 0;
424
425 memset(ac->character_names, 0, MAX_CHARACTERS_PER_ACCOUNT+1 * sizeof(char*));
426
427 /* We put this at the top of the list. This means recent accounts will be at
428 * the top of the file, which is likely a good thing.
429 * We don't do a save right now. When the player associates a character with
430 * the account, we will save them - until that point, not too much reason
431 * to save this out. Note it is still possible for this to get saved out if
432 * another player does something that forces writing out of the accounts file.
433 */
434 ac->next = accounts;
435 accounts = ac;
436
437 /* mark that accounts should be saved through accounts_save(). */
438 accounts_loaded = 1;
439
440 return 0;
441 }
442
443 /**
444 * Adds a player name to an account. Player corresponds to the names used
445 * in the pl object, not the person sitting at the computer. This function
446 * presumes that the caller has done the work to verify that the player
447 * does in fact exist, and does any related work to update the player. What this
448 * function does is simply update the account structure.
449 * @param account_name
450 * name of the account we are adding this player to.
451 * @param player_name
452 * name of this player.
453 * @retval 0
454 * player name added successfully.
455 * @retval 1
456 * could not find account of that name.
457 * @retval 2
458 * number of characters on this account has reached a maximum.
459 */
460
461 int account_link(const char *account_name, const char *player_name) {
462 account_struct *ac;
463
464 for (ac=accounts; ac; ac=ac->next) {
465 if (!strcasecmp(ac->name, account_name)) break;
466 }
467 if (ac == NULL) return 1;
468
469 if (ac->num_characters >= MAX_CHARACTERS_PER_ACCOUNT) return 2;
470
471 ac->character_names[ac->num_characters] = strdup_local(player_name);
472 ac->num_characters++;
473 /* NULL terminate, as per notes above. In theory not necessary since data
474 * is all set to NULL */
475 ac->character_names[ac->num_characters] = NULL;
476 return 0;
477 }
478
479 /**
480 * Removes a player name from an account. Player corresponds to the names used
481 * in the pl object, not the person sitting at the computer. This function
482 * presumes that the caller has done the work to verify that the player
483 * does in fact exist, and does any related work to update the player. What this
484 * function does is simply update the account structure.
485 * @param account_name
486 * name of the account we are removing this player from.
487 * @param player_name
488 * name of this player.
489 * @retval 0
490 * player name removed successfully.
491 * @retval 1
492 * could not find account of that name.
493 * @retval 2
494 * player of this name not on account.
495 */
496
497 int account_remove_player(const char *account_name, const char *player_name) {
498 account_struct *ac;
499 int i, match=0;
500
501 if (account_name == NULL)
502 return 0;
503
504 for (ac=accounts; ac; ac=ac->next) {
505 if (!strcasecmp(ac->name, account_name)) break;
506 }
507 if (ac == NULL) return 1;
508
509 /* Try to find the character name. Once we find it, we set match, and
510 * then move the remain character names down by one. The array is
511 * always null terminated, so this also makes sure we copy the null down.
512 */
513 for (i=0; i<ac->num_characters; i++) {
514 if (!strcmp(ac->character_names[i], player_name)) {
515 free(ac->character_names[i]);
516 match=1;
517 }
518 if (match == 1) {
519 ac->character_names[i] = ac->character_names[i+1];
520 }
521 }
522
523 if (match) {
524 ac->num_characters--;
525 return 0;
526 }
527 /* Otherwise, did not find player name */
528 return 2;
529 }
530
531
532 /**
533 * Returns an array of strings for the characters on this account - the array
534 * is null terminated. In fact, it just returns the ac->character_names.
535 * @param account_name
536 * name of the account to get the players for.
537 * @return
538 * array of character names for this account. This will return NULL in the
539 * case where we can not find the account. It will return an array with the
540 * first entry being NULL in the case of a valid account with no characters added.
541 * This returned data should not be altered in any way.
542 */
543 char **account_get_players_for_account(const char *account_name)
544 {
545 account_struct *ac;
546
547 for (ac=accounts; ac; ac=ac->next) {
548 if (!strcasecmp(ac->name, account_name)) return ac->character_names;
549 }
550 return NULL;
551 }
552
553 /**
554 * This looks at all the accounts and sees if charname is associated
555 * with any of them.
556 *
557 * @param charname
558 * character name to check for.
559 * @return
560 * Account name the character is associated with, NULL if none.
561 */
562 const char *account_get_account_for_char(const char *charname)
563 {
564 account_struct *ac;
565 int i;
566
567 for (ac=accounts; ac; ac=ac->next) {
568 for (i=0; i<ac->num_characters; i++) {
569 if (!strcmp(ac->character_names[i], charname)) {
570 return ac->name;
571 }
572 }
573 }
574 return NULL;
575
576 }
577
578 /**
579 * This checks to see if the account is logged in with a player attached
580 * If so, it returns the player object.
581 *
582 * @param name
583 * account name to check against.
584 * @return
585 * player structure of matching account, or NULL if no match.
586 */
587 player *account_get_logged_in_player(const char *name)
588 {
589 player *pl;
590
591 for (pl = first_player; pl; pl=pl->next) {
592 if (pl->socket.account_name &&
593 !strcasecmp(pl->socket.account_name, name)) return pl;
594 }
595 return NULL;
596 }
597
598 /**
599 * This is like the above routine, but checks the init_sockets
600 * (account in process of logging in).
601 *
602 * @param name
603 * account name to check against
604 * @return
605 * index value into init_sockets[] of matching socket, or -1 if
606 * no match.
607 */
608 socket_struct *account_get_logged_in_init_socket(const char *name)
609 {
610 int i;
611
612 for (i=0; i < socket_info.allocated_sockets; i++) {
613 if (init_sockets[i].status == Ns_Add &&
614 init_sockets[i].account_name &&
615 !strcasecmp(init_sockets[i].account_name, name)) return(&init_sockets[i]);
616 }
617 return NULL;
618 }
619
620 /**
621 * This checkes if an account is logged in. It is mainly
622 * used because some operations should not be done on logged
623 * in accounts (keeping everything synchronized is harder.)
624 *
625 * @param name
626 * name to look for.
627 * @return
628 * 0 if not logged in, 1 if logged in.
629 */
630 int account_is_logged_in(const char *name)
631 {
632 if (account_get_logged_in_player(name)) return 1;
633
634 if (account_get_logged_in_init_socket(name)!=NULL) return 1;
635
636 return 0;
637 }
638
639 /**
640 * Change an account password. It does error checking, but the caller might
641 * want to do checking before getting here.
642 * @param account_name
643 * account name we are changing
644 * @param current_password
645 * current password for the account. This is the unencrypted password (password as entered
646 * by user)
647 * @param new_password
648 * new password to set, unencrypted.
649 * @retval 0
650 * password changed successfully.
651 * @retval 1
652 * account name, old or new password has invalid character.
653 * @retval 2
654 * account does not exist.
655 * @retval 3
656 * current password is invalid.
657 */
658 int account_change_password(const char *account_name,
659 const char *current_password, const char *new_password) {
660 account_struct *ac;
661
662 // Check password for invalid characters as in account_new().
663 if (account_check_string(account_name) || account_check_string(current_password) ||
664 account_check_string(new_password)) {
665 return 1;
666 }
667
668 // Iterate through accounts list until a matching name is found.
669 for (ac = accounts; ac; ac = ac->next) {
670 if (!strcasecmp(ac->name, account_name)) {
671 break;
672 }
673 }
674
675 // Check if the given account actually exists.
676 if (ac == NULL) {
677 return 2;
678 }
679
680 // Return an error if the current password does not match.
681 if (!check_password(current_password, ac->password)) {
682 return 3;
683 }
684
685 free(ac->password);
686 ac->password = strdup_local(newhash(new_password));
687
688 return 0;
689 }