| 1 |
/*
|
| 2 |
* Copyright (C) 1998,1999,2000,2001 Ross Combs (rocombs@cs.nmsu.edu)
|
| 3 |
* Copyright (C) 2000,2001 Marco Ziech (mmz@gmx.net)
|
| 4 |
* Copyright (C) 2002,2003,2004 Mihai RUSU (dizzy@rdsnet.ro)
|
| 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"
|
| 21 |
#include <stdio.h>
|
| 22 |
#ifdef HAVE_STDDEF_H
|
| 23 |
# include <stddef.h>
|
| 24 |
#else
|
| 25 |
# ifndef NULL
|
| 26 |
# define NULL ((void *)0)
|
| 27 |
# endif
|
| 28 |
#endif
|
| 29 |
#ifdef STDC_HEADERS
|
| 30 |
# include <stdlib.h>
|
| 31 |
#else
|
| 32 |
# ifdef HAVE_MALLOC_H
|
| 33 |
# include <malloc.h>
|
| 34 |
# endif
|
| 35 |
#endif
|
| 36 |
#ifdef HAVE_STRING_H
|
| 37 |
# include <string.h>
|
| 38 |
#else
|
| 39 |
# ifdef HAVE_STRINGS_H
|
| 40 |
# include <strings.h>
|
| 41 |
# endif
|
| 42 |
#endif
|
| 43 |
#include "compat/strchr.h"
|
| 44 |
#include "compat/strdup.h"
|
| 45 |
#include "compat/strcasecmp.h"
|
| 46 |
#include "compat/strncasecmp.h"
|
| 47 |
#include <ctype.h>
|
| 48 |
#ifdef HAVE_LIMITS_H
|
| 49 |
# include <limits.h>
|
| 50 |
#endif
|
| 51 |
#include "compat/char_bit.h"
|
| 52 |
#ifdef TIME_WITH_SYS_TIME
|
| 53 |
# include <sys/time.h>
|
| 54 |
# include <time.h>
|
| 55 |
#else
|
| 56 |
# ifdef HAVE_SYS_TIME_H
|
| 57 |
# include <sys/time.h>
|
| 58 |
# else
|
| 59 |
# include <time.h>
|
| 60 |
# endif
|
| 61 |
#endif
|
| 62 |
#include <errno.h>
|
| 63 |
#include "compat/strerror.h"
|
| 64 |
#ifdef HAVE_SYS_TYPES_H
|
| 65 |
# include <sys/types.h>
|
| 66 |
#endif
|
| 67 |
#ifdef HAVE_UNISTD_H
|
| 68 |
# include <unistd.h>
|
| 69 |
#else
|
| 70 |
# ifdef WIN32
|
| 71 |
# include <io.h>
|
| 72 |
# endif
|
| 73 |
#endif
|
| 74 |
#include "compat/pdir.h"
|
| 75 |
#include "common/eventlog.h"
|
| 76 |
#include "prefs.h"
|
| 77 |
#include "common/util.h"
|
| 78 |
#include "common/field_sizes.h"
|
| 79 |
#include "common/bnethash.h"
|
| 80 |
#define CLAN_INTERNAL_ACCESS
|
| 81 |
#define ACCOUNT_INTERNAL_ACCESS
|
| 82 |
#include "common/introtate.h"
|
| 83 |
#include "account.h"
|
| 84 |
#include "common/hashtable.h"
|
| 85 |
#include "storage.h"
|
| 86 |
#include "storage_file.h"
|
| 87 |
#include "common/list.h"
|
| 88 |
#include "common/xalloc.h"
|
| 89 |
#include "connection.h"
|
| 90 |
#include "watch.h"
|
| 91 |
#include "clan.h"
|
| 92 |
#undef ACCOUNT_INTERNAL_ACCESS
|
| 93 |
#undef CLAN_INTERNAL_ACCESS
|
| 94 |
#include "common/tag.h"
|
| 95 |
#include "common/setup_after.h"
|
| 96 |
|
| 97 |
/* plain file storage API functions */
|
| 98 |
|
| 99 |
static void * plain_read_attr(const char *filename, const char *key);
|
| 100 |
static int plain_read_attrs(const char *filename, t_read_attr_func cb, void *data);
|
| 101 |
static int plain_write_attrs(const char *filename, void *attributes);
|
| 102 |
|
| 103 |
/* file_engine struct populated with the functions above */
|
| 104 |
|
| 105 |
t_file_engine file_plain = {
|
| 106 |
plain_read_attr,
|
| 107 |
plain_read_attrs,
|
| 108 |
plain_write_attrs
|
| 109 |
};
|
| 110 |
|
| 111 |
|
| 112 |
static int plain_write_attrs(const char *filename, void *attributes)
|
| 113 |
{
|
| 114 |
FILE * accountfile;
|
| 115 |
t_attribute * attr;
|
| 116 |
char const * key;
|
| 117 |
char const * val;
|
| 118 |
|
| 119 |
if (!(accountfile = fopen(filename,"w"))) {
|
| 120 |
eventlog(eventlog_level_error, __FUNCTION__, "unable to open file \"%s\" for writing (fopen: %s)",filename,pstrerror(errno));
|
| 121 |
return -1;
|
| 122 |
}
|
| 123 |
|
| 124 |
for (attr=(t_attribute *)attributes; attr; attr=attr->next) {
|
| 125 |
if (attr->key)
|
| 126 |
key = escape_chars(attr->key,strlen(attr->key));
|
| 127 |
else {
|
| 128 |
eventlog(eventlog_level_error, __FUNCTION__, "attribute with NULL key in list");
|
| 129 |
key = NULL;
|
| 130 |
}
|
| 131 |
|
| 132 |
if (attr->val)
|
| 133 |
val = escape_chars(attr->val,strlen(attr->val));
|
| 134 |
else {
|
| 135 |
eventlog(eventlog_level_error, __FUNCTION__, "attribute with NULL val in list");
|
| 136 |
val = NULL;
|
| 137 |
}
|
| 138 |
|
| 139 |
if (key && val) {
|
| 140 |
if (strncmp("BNET\\CharacterDefault\\", key, 20) == 0) {
|
| 141 |
eventlog(eventlog_level_debug, __FUNCTION__, "skipping attribute key=\"%s\"",attr->key);
|
| 142 |
} else {
|
| 143 |
eventlog(eventlog_level_debug, __FUNCTION__, "saving attribute key=\"%s\" val=\"%s\"",attr->key,attr->val);
|
| 144 |
fprintf(accountfile,"\"%s\"=\"%s\"\n",key,val);
|
| 145 |
}
|
| 146 |
} else eventlog(eventlog_level_error, __FUNCTION__,"could not save attribute key=\"%s\"",attr->key);
|
| 147 |
|
| 148 |
if (key) xfree((void *)key); /* avoid warning */
|
| 149 |
if (val) xfree((void *)val); /* avoid warning */
|
| 150 |
}
|
| 151 |
|
| 152 |
if (fclose(accountfile)<0) {
|
| 153 |
eventlog(eventlog_level_error, __FUNCTION__, "could not close account file \"%s\" after writing (fclose: %s)",filename,pstrerror(errno));
|
| 154 |
return -1;
|
| 155 |
}
|
| 156 |
|
| 157 |
return 0;
|
| 158 |
}
|
| 159 |
|
| 160 |
static int plain_read_attrs(const char *filename, t_read_attr_func cb, void *data)
|
| 161 |
{
|
| 162 |
FILE * accountfile;
|
| 163 |
unsigned int line;
|
| 164 |
char const * buff;
|
| 165 |
unsigned int len;
|
| 166 |
char * esckey;
|
| 167 |
char * escval;
|
| 168 |
char * key;
|
| 169 |
char * val;
|
| 170 |
|
| 171 |
if (!(accountfile = fopen(filename,"r"))) {
|
| 172 |
eventlog(eventlog_level_error, __FUNCTION__,"could not open account file \"%s\" for reading (fopen: %s)", filename, pstrerror(errno));
|
| 173 |
return -1;
|
| 174 |
}
|
| 175 |
|
| 176 |
for (line=1; (buff=file_get_line(accountfile)); line++) {
|
| 177 |
if (buff[0]=='#' || buff[0]=='\0') {
|
| 178 |
continue;
|
| 179 |
}
|
| 180 |
|
| 181 |
if (strlen(buff)<6) /* "?"="" */ {
|
| 182 |
eventlog(eventlog_level_error, __FUNCTION__, "malformed line %d of account file \"%s\"", line, filename);
|
| 183 |
continue;
|
| 184 |
}
|
| 185 |
|
| 186 |
len = strlen(buff)-5+1; /* - ""="" + NUL */
|
| 187 |
esckey = xmalloc(len);
|
| 188 |
escval = xmalloc(len);
|
| 189 |
|
| 190 |
if (sscanf(buff,"\"%[^\"]\" = \"%[^\"]\"",esckey,escval)!=2) {
|
| 191 |
if (sscanf(buff,"\"%[^\"]\" = \"\"",esckey)!=1) /* hack for an empty value field */ {
|
| 192 |
eventlog(eventlog_level_error, __FUNCTION__,"malformed entry on line %d of account file \"%s\"", line, filename);
|
| 193 |
xfree(escval);
|
| 194 |
xfree(esckey);
|
| 195 |
continue;
|
| 196 |
}
|
| 197 |
escval[0] = '\0';
|
| 198 |
}
|
| 199 |
|
| 200 |
key = unescape_chars(esckey);
|
| 201 |
val = unescape_chars(escval);
|
| 202 |
|
| 203 |
/* eventlog(eventlog_level_debug,__FUNCTION__,"strlen(esckey)=%u (%c), len=%u",strlen(esckey),esckey[0],len);*/
|
| 204 |
xfree(esckey);
|
| 205 |
xfree(escval);
|
| 206 |
|
| 207 |
if (cb(key,val,data))
|
| 208 |
eventlog(eventlog_level_error, __FUNCTION__, "got error from callback (key: '%s' val:'%s')", key, val);
|
| 209 |
|
| 210 |
if (key) xfree((void *)key); /* avoid warning */
|
| 211 |
if (val) xfree((void *)val); /* avoid warning */
|
| 212 |
}
|
| 213 |
|
| 214 |
file_get_line(NULL); // clear file_get_line buffer
|
| 215 |
|
| 216 |
if (fclose(accountfile)<0)
|
| 217 |
eventlog(eventlog_level_error, __FUNCTION__, "could not close account file \"%s\" after reading (fclose: %s)", filename, pstrerror(errno));
|
| 218 |
|
| 219 |
return 0;
|
| 220 |
}
|
| 221 |
|
| 222 |
static void * plain_read_attr(const char *filename, const char *key)
|
| 223 |
{
|
| 224 |
/* flat file storage doesnt know to read selective attributes */
|
| 225 |
return NULL;
|
| 226 |
}
|