| 1 | /* |
| 2 | * Copyright (C) 1998,1999,2000,2001 Ross Combs ([email protected]) |
| 3 | * Copyright (C) 2000,2001 Marco Ziech ([email protected]) |
| 4 | * Copyright (C) 2002,2003,2004 Dizzy |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU General Public License |
| 8 | * as published by the Free Software Foundation; either version 2 |
| 9 | * of the License, or (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with this program; if not, write to the Free Software |
| 18 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 19 | */ |
| 20 | #include "common/setup_before.h"cxx . . $ `<file>/src/common/setup_before.h`/ |
| 21 | #include "file_plain.h"cxx . . $ `<file>/src/bnetd/file_plain.h`/ |
| 22 | |
| 23 | #include <cstring> |
| 24 | #include <cerrno> |
| 25 | |
| 26 | #include "common/eventlog.h"cxx . . $ `<file>/src/common/eventlog.h`/ |
| 27 | #include "common/util.h"cxx . . $ `<file>/src/common/util.h`/ |
| 28 | |
| 29 | #include "common/setup_after.h"cxx . . $ `<file>/src/common/setup_after.h`/ |
| 30 | |
| 31 | namespace pvpgncxx . . $ pvpgn/ |
| 32 | { |
| 33 | |
| 34 | namespace bnetdcxx . . $ pvpgn/bnetd/ |
| 35 | { |
| 36 | |
| 37 | /* plain file storage API functions */ |
| 38 | |
| 39 | static t_attrcxx . . $ pvpgn/bnetd/t_attr# * plain_read_attrcxx . . $ pvpgn/bnetd/plain_read_attr(14a607e4a65cb9d5).(const char *filename, const char *key); |
| 40 | static int plain_read_attrscxx . . $ pvpgn/bnetd/plain_read_attrs(2c2d86622949cc23).(const char *filename, t_read_attr_funccxx . . $ pvpgn/bnetd/t_read_attr_func# cb, void *data); |
| 41 | static int plain_write_attrscxx . . $ pvpgn/bnetd/plain_write_attrs(f59d063f832e41f0).(const char *filename, const t_hlistcxx . . $ pvpgn/t_hlist# *attributes); |
| 42 | |
| 43 | /* file_engine struct populated with the functions above */ |
| 44 | |
| 45 | t_file_enginecxx . . $ pvpgn/bnetd/t_file_engine# file_plaincxx . . $ pvpgn/bnetd/file_plain. = { |
| 46 | plain_read_attrcxx . . $ pvpgn/bnetd/plain_read_attr(14a607e4a65cb9d5)., |
| 47 | plain_read_attrscxx . . $ pvpgn/bnetd/plain_read_attrs(2c2d86622949cc23)., |
| 48 | plain_write_attrscxx . . $ pvpgn/bnetd/plain_write_attrs(f59d063f832e41f0). |
| 49 | }; |
| 50 | |
| 51 | |
| 52 | static int plain_write_attrscxx . . $ pvpgn/bnetd/plain_write_attrs(f59d063f832e41f0).(const char *filename, const t_hlistcxx . . $ pvpgn/t_hlist# *attributes) |
| 53 | { |
| 54 | stdcxx . . $ std/::FILEcxx . . $ std/FILE# * accountfile; |
| 55 | t_hlistcxx . . $ pvpgn/t_hlist# * curr; |
| 56 | t_attrcxx . . $ pvpgn/bnetd/t_attr# * attr; |
| 57 | char const * key; |
| 58 | char const * val; |
| 59 | |
| 60 | if (!(accountfile = stdcxx . . $ std/::fopencxx . . $ std/fopen(82ec0787cd37e0a3).(filename, "w"))) { |
| 61 | eventlogcxx . . $ pvpgn/eventlog(f1d8dd72586f1e25).(eventlog_level_errorcxx . . $ pvpgn/eventlog_level_error., __FUNCTION__, "unable to open file \"{}\" for writing (std::fopen: {})", filename, stdcxx . . $ std/::strerror(errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!)); |
| 62 | return -1; |
| 63 | } |
| 64 | |
| 65 | hlist_for_eachcxx . . $ `src/common/elist.h:287:9`!(curr, attributes) { |
| 66 | attr = hlist_entrycxx . . $ `src/common/elist.h:284:9`!(curr, t_attr, link); |
| 67 | |
| 68 | if (attr_get_keycxx . . $ pvpgn/bnetd/attr_get_key(32e503b365e650a7).(attr)) |
| 69 | key = escape_charscxx . . $ pvpgn/escape_chars(cf893fee19605dd5).(attr_get_keycxx . . $ pvpgn/bnetd/attr_get_key(32e503b365e650a7).(attr), stdcxx . . $ std/::strlen(attr_get_keycxx . . $ pvpgn/bnetd/attr_get_key(32e503b365e650a7).(attr))); |
| 70 | else { |
| 71 | eventlogcxx . . $ pvpgn/eventlog(f1d8dd72586f1e25).(eventlog_level_errorcxx . . $ pvpgn/eventlog_level_error., __FUNCTION__, "attribute with NULL key in list"); |
| 72 | key = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!; |
| 73 | } |
| 74 | |
| 75 | if (attr_get_valcxx . . $ pvpgn/bnetd/attr_get_val(32e503b365e650a7).(attr)) |
| 76 | val = escape_charscxx . . $ pvpgn/escape_chars(cf893fee19605dd5).(attr_get_valcxx . . $ pvpgn/bnetd/attr_get_val(32e503b365e650a7).(attr), stdcxx . . $ std/::strlen(attr_get_valcxx . . $ pvpgn/bnetd/attr_get_val(32e503b365e650a7).(attr))); |
| 77 | else { |
| 78 | eventlogcxx . . $ pvpgn/eventlog(f1d8dd72586f1e25).(eventlog_level_debugcxx . . $ pvpgn/eventlog_level_debug., __FUNCTION__, "attribute with NULL val in list key=\"{}\"", key ? key : "NULL"); |
| 79 | val = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!; |
| 80 | } |
| 81 | |
| 82 | if (key && val) { |
| 83 | if (stdcxx . . $ std/::strncmp("BNET\\CharacterDefault\\", key, 20) == 0) { |
| 84 | eventlogcxx . . $ pvpgn/eventlog(f1d8dd72586f1e25).(eventlog_level_debugcxx . . $ pvpgn/eventlog_level_debug., __FUNCTION__, "skipping attribute key=\"{}\"", key); |
| 85 | } |
| 86 | else { |
| 87 | eventlogcxx . . $ pvpgn/eventlog(f1d8dd72586f1e25).(eventlog_level_debugcxx . . $ pvpgn/eventlog_level_debug., __FUNCTION__, "saving attribute key=\"{}\" val=\"{}\"", key, val); |
| 88 | stdcxx . . $ std/::fprintfcxx . . $ std/fprintf(bc216f8e5b1ebeac).(accountfile, "\"%s\"=\"%s\"\n", key, val); |
| 89 | } |
| 90 | } |
| 91 | else eventlogcxx . . $ pvpgn/eventlog(f1d8dd72586f1e25).(eventlog_level_debugcxx . . $ pvpgn/eventlog_level_debug., __FUNCTION__, "could not save attribute key=\"{}\"", key ? key : "NULL"); |
| 92 | |
| 93 | if (key) xfreecxx . . $ `src/common/xalloc.h:51:9`!((void *)key); /* avoid warning */ |
| 94 | if (val) xfreecxx . . $ `src/common/xalloc.h:51:9`!((void *)val); /* avoid warning */ |
| 95 | |
| 96 | attr_clear_dirtycxx . . $ pvpgn/bnetd/attr_clear_dirty(d2d8112715f5226d).(attr); |
| 97 | } |
| 98 | |
| 99 | if (stdcxx . . $ std/::fclosecxx . . $ std/fclose(89bc1efcdac0ca22).(accountfile) < 0) { |
| 100 | eventlogcxx . . $ pvpgn/eventlog(f1d8dd72586f1e25).(eventlog_level_errorcxx . . $ pvpgn/eventlog_level_error., __FUNCTION__, "could not close account file \"{}\" after writing (std::fclose: {})", filename, stdcxx . . $ std/::strerror(errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!)); |
| 101 | return -1; |
| 102 | } |
| 103 | |
| 104 | return 0; |
| 105 | } |
| 106 | |
| 107 | static int plain_read_attrscxx . . $ pvpgn/bnetd/plain_read_attrs(2c2d86622949cc23).(const char *filename, t_read_attr_funccxx . . $ pvpgn/bnetd/t_read_attr_func# cb, void *data) |
| 108 | { |
| 109 | stdcxx . . $ std/::FILEcxx . . $ std/FILE# * accountfile; |
| 110 | unsigned int line; |
| 111 | char const * buff; |
| 112 | unsigned int len; |
| 113 | char * esckey; |
| 114 | char * escval; |
| 115 | char * key; |
| 116 | char * val; |
| 117 | |
| 118 | if (!(accountfile = stdcxx . . $ std/::fopencxx . . $ std/fopen(82ec0787cd37e0a3).(filename, "r"))) { |
| 119 | eventlogcxx . . $ pvpgn/eventlog(f1d8dd72586f1e25).(eventlog_level_errorcxx . . $ pvpgn/eventlog_level_error., __FUNCTION__, "could not open account file \"{}\" for reading (std::fopen: {})", filename, stdcxx . . $ std/::strerror(errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!)); |
| 120 | return -1; |
| 121 | } |
| 122 | |
| 123 | for (line = 1; (buff = file_get_linecxx . . $ pvpgn/file_get_line(e31226b218f63160).(accountfile)); line++) { |
| 124 | if (buff[0] == '#' || buff[0] == '\0') { |
| 125 | continue; |
| 126 | } |
| 127 | |
| 128 | if (stdcxx . . $ std/::strlen(buff) < 6) /* "?"="" */ { |
| 129 | eventlogcxx . . $ pvpgn/eventlog(f1d8dd72586f1e25).(eventlog_level_errorcxx . . $ pvpgn/eventlog_level_error., __FUNCTION__, "malformed line {} of account file \"{}\"", line, filename); |
| 130 | continue; |
| 131 | } |
| 132 | |
| 133 | len = stdcxx . . $ std/::strlen(buff) - 5 + 1; /* - ""="" + NUL */ |
| 134 | esckey = (char*)xmalloccxx . . $ `src/common/xalloc.h:43:9`!(len); |
| 135 | escval = (char*)xmalloccxx . . $ `src/common/xalloc.h:43:9`!(len); |
| 136 | |
| 137 | if (stdcxx . . $ std/::sscanfcxx . . $ std/sscanf(6246fabbf6b5312d).(buff, "\"%[^\"]\" = \"%[^\"]\"", esckey, escval) != 2) { |
| 138 | if (stdcxx . . $ std/::sscanfcxx . . $ std/sscanf(6246fabbf6b5312d).(buff, "\"%[^\"]\" = \"\"", esckey) != 1) /* hack for an empty value field */ { |
| 139 | eventlogcxx . . $ pvpgn/eventlog(f1d8dd72586f1e25).(eventlog_level_errorcxx . . $ pvpgn/eventlog_level_error., __FUNCTION__, "malformed entry on line {} of account file \"{}\"", line, filename); |
| 140 | xfreecxx . . $ `src/common/xalloc.h:51:9`!(escval); |
| 141 | xfreecxx . . $ `src/common/xalloc.h:51:9`!(esckey); |
| 142 | continue; |
| 143 | } |
| 144 | escval[0] = '\0'; |
| 145 | } |
| 146 | |
| 147 | key = unescape_charscxx . . $ pvpgn/unescape_chars(a8b8e32347182212).(esckey); |
| 148 | val = unescape_charscxx . . $ pvpgn/unescape_chars(a8b8e32347182212).(escval); |
| 149 | |
| 150 | /* eventlog(eventlog_level_debug,__FUNCTION__,"std::strlen(esckey)=%u (%c), len=%u",std::strlen(esckey),esckey[0],len);*/ |
| 151 | xfreecxx . . $ `src/common/xalloc.h:51:9`!(esckey); |
| 152 | xfreecxx . . $ `src/common/xalloc.h:51:9`!(escval); |
| 153 | |
| 154 | if (cb(key, val, data)) |
| 155 | eventlogcxx . . $ pvpgn/eventlog(f1d8dd72586f1e25).(eventlog_level_errorcxx . . $ pvpgn/eventlog_level_error., __FUNCTION__, "got error from callback (key: '{}' val:'{}')", key, val); |
| 156 | |
| 157 | if (key) xfreecxx . . $ `src/common/xalloc.h:51:9`!((void *)key); /* avoid warning */ |
| 158 | if (val) xfreecxx . . $ `src/common/xalloc.h:51:9`!((void *)val); /* avoid warning */ |
| 159 | } |
| 160 | |
| 161 | file_get_linecxx . . $ pvpgn/file_get_line(e31226b218f63160).(NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!); // clear file_get_line buffer |
| 162 | |
| 163 | if (stdcxx . . $ std/::fclosecxx . . $ std/fclose(89bc1efcdac0ca22).(accountfile) < 0) |
| 164 | eventlogcxx . . $ pvpgn/eventlog(f1d8dd72586f1e25).(eventlog_level_errorcxx . . $ pvpgn/eventlog_level_error., __FUNCTION__, "could not close account file \"{}\" after reading (std::fclose: {})", filename, stdcxx . . $ std/::strerror(errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!)); |
| 165 | |
| 166 | return 0; |
| 167 | } |
| 168 | |
| 169 | static t_attrcxx . . $ pvpgn/bnetd/t_attr# * plain_read_attrcxx . . $ pvpgn/bnetd/plain_read_attr(14a607e4a65cb9d5).(const char *filename, const char *key) |
| 170 | { |
| 171 | /* flat file storage doesnt know to read selective attributes */ |
| 172 | return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!; |
| 173 | } |
| 174 | |
| 175 | } |
| 176 | |
| 177 | } |