| 1 |
/* |
| 2 |
* Copyright (c) 2005-2006 Atheme Development Group |
| 3 |
* Rights to this code are as documented in doc/LICENSE. |
| 4 |
* |
| 5 |
* This file contains the OpenSEX (Open Services Exchange) database backend for |
| 6 |
* Atheme. The purpose of OpenSEX is to destroy the old DB format, subjugate its |
| 7 |
* peoples, burn its cities to the ground, and salt the earth so that nothing |
| 8 |
* ever grows there again. |
| 9 |
*/ |
| 10 |
|
| 11 |
#include "atheme.h" |
| 12 |
|
| 13 |
DECLARE_MODULE_V1 |
| 14 |
( |
| 15 |
"backend/opensex", true, _modinit, NULL, |
| 16 |
PACKAGE_STRING, |
| 17 |
VENDOR_STRING |
| 18 |
); |
| 19 |
|
| 20 |
typedef struct opensex_ { |
| 21 |
/* Lexing state */ |
| 22 |
char *buf; |
| 23 |
unsigned int bufsize; |
| 24 |
char *token; |
| 25 |
FILE *f; |
| 26 |
|
| 27 |
/* Interpreting state */ |
| 28 |
unsigned int grver; |
| 29 |
} opensex_t; |
| 30 |
|
| 31 |
static void opensex_db_parse(database_handle_t *db) |
| 32 |
{ |
| 33 |
const char *cmd; |
| 34 |
while (db_read_next_row(db)) |
| 35 |
{ |
| 36 |
cmd = db_read_word(db); |
| 37 |
if (!cmd || !*cmd || strchr("#\n\t \r", *cmd)) continue; |
| 38 |
db_process(db, cmd); |
| 39 |
} |
| 40 |
} |
| 41 |
|
| 42 |
static void opensex_h_grver(database_handle_t *db, const char *type) |
| 43 |
{ |
| 44 |
opensex_t *rs = (opensex_t *)db->priv; |
| 45 |
rs->grver = db_sread_int(db); |
| 46 |
slog(LG_INFO, "opensex: grammar version is %d.", rs->grver); |
| 47 |
|
| 48 |
if (rs->grver != 1) |
| 49 |
slog(LG_ERROR, "opensex: grammar version %d is unsupported. dazed and confused, but trying to continue.", rs->grver); |
| 50 |
} |
| 51 |
|
| 52 |
/***************************************************************************************************/ |
| 53 |
|
| 54 |
static bool opensex_read_next_row(database_handle_t *hdl) |
| 55 |
{ |
| 56 |
int c = 0; |
| 57 |
unsigned int n = 0; |
| 58 |
opensex_t *rs = (opensex_t *)hdl->priv; |
| 59 |
|
| 60 |
while ((c = getc(rs->f)) != EOF && c != '\n') |
| 61 |
{ |
| 62 |
rs->buf[n++] = c; |
| 63 |
if (n == rs->bufsize) |
| 64 |
{ |
| 65 |
rs->bufsize *= 2; |
| 66 |
rs->buf = srealloc(rs->buf, rs->bufsize); |
| 67 |
} |
| 68 |
} |
| 69 |
rs->buf[n] = '\0'; |
| 70 |
rs->token = rs->buf; |
| 71 |
|
| 72 |
if (c == EOF && ferror(rs->f)) |
| 73 |
{ |
| 74 |
slog(LG_ERROR, "opensex-read-next-row: error at %s line %d: %s", hdl->file, hdl->line, strerror(errno)); |
| 75 |
slog(LG_ERROR, "opensex-read-next-row: exiting to avoid data loss"); |
| 76 |
exit(EXIT_FAILURE); |
| 77 |
} |
| 78 |
|
| 79 |
if (c == EOF && n == 0) |
| 80 |
return false; |
| 81 |
|
| 82 |
hdl->line++; |
| 83 |
hdl->token = 0; |
| 84 |
return true; |
| 85 |
} |
| 86 |
|
| 87 |
static const char *opensex_read_word(database_handle_t *db) |
| 88 |
{ |
| 89 |
opensex_t *rs = (opensex_t *)db->priv; |
| 90 |
char *ptr = rs->token; |
| 91 |
char *res; |
| 92 |
static char buf[BUFSIZE]; |
| 93 |
|
| 94 |
res = rs->token; |
| 95 |
if (res == NULL) |
| 96 |
return NULL; |
| 97 |
|
| 98 |
ptr = strchr(res, ' '); |
| 99 |
if (ptr != NULL) |
| 100 |
{ |
| 101 |
*ptr++ = '\0'; |
| 102 |
rs->token = ptr; |
| 103 |
} |
| 104 |
else |
| 105 |
rs->token = NULL; |
| 106 |
|
| 107 |
db->token++; |
| 108 |
|
| 109 |
return res; |
| 110 |
} |
| 111 |
|
| 112 |
static const char *opensex_read_str(database_handle_t *db) |
| 113 |
{ |
| 114 |
opensex_t *rs = (opensex_t *)db->priv; |
| 115 |
char *res; |
| 116 |
|
| 117 |
res = rs->token; |
| 118 |
|
| 119 |
db->token++; |
| 120 |
return res; |
| 121 |
} |
| 122 |
|
| 123 |
static bool opensex_read_int(database_handle_t *db, int *res) |
| 124 |
{ |
| 125 |
const char *s = db_read_word(db); |
| 126 |
char *rp; |
| 127 |
|
| 128 |
if (!s) return false; |
| 129 |
|
| 130 |
*res = strtol(s, &rp, 0); |
| 131 |
return *s && !*rp; |
| 132 |
} |
| 133 |
|
| 134 |
static bool opensex_read_uint(database_handle_t *db, unsigned int *res) |
| 135 |
{ |
| 136 |
const char *s = db_read_word(db); |
| 137 |
char *rp; |
| 138 |
|
| 139 |
if (!s) return false; |
| 140 |
|
| 141 |
*res = strtoul(s, &rp, 0); |
| 142 |
return *s && !*rp; |
| 143 |
} |
| 144 |
|
| 145 |
static bool opensex_read_time(database_handle_t *db, time_t *res) |
| 146 |
{ |
| 147 |
const char *s = db_read_word(db); |
| 148 |
char *rp; |
| 149 |
|
| 150 |
if (!s) return false; |
| 151 |
|
| 152 |
*res = strtoul(s, &rp, 0); |
| 153 |
return *s && !*rp; |
| 154 |
} |
| 155 |
|
| 156 |
static bool opensex_start_row(database_handle_t *db, const char *type) |
| 157 |
{ |
| 158 |
opensex_t *rs; |
| 159 |
|
| 160 |
return_val_if_fail(db != NULL, false); |
| 161 |
return_val_if_fail(type != NULL, false); |
| 162 |
rs = (opensex_t *)db->priv; |
| 163 |
|
| 164 |
fprintf(rs->f, "%s ", type); |
| 165 |
|
| 166 |
return true; |
| 167 |
} |
| 168 |
|
| 169 |
static bool opensex_write_cell(database_handle_t *db, const char *data, bool multiword) |
| 170 |
{ |
| 171 |
opensex_t *rs; |
| 172 |
char buf[BUFSIZE], *bi; |
| 173 |
const char *i; |
| 174 |
|
| 175 |
return_val_if_fail(db != NULL, false); |
| 176 |
rs = (opensex_t *)db->priv; |
| 177 |
|
| 178 |
fprintf(rs->f, "%s%s", data != NULL ? data : "*", !multiword ? " " : ""); |
| 179 |
|
| 180 |
return true; |
| 181 |
} |
| 182 |
|
| 183 |
static bool opensex_write_word(database_handle_t *db, const char *word) |
| 184 |
{ |
| 185 |
return opensex_write_cell(db, word, false); |
| 186 |
} |
| 187 |
|
| 188 |
static bool opensex_write_str(database_handle_t *db, const char *word) |
| 189 |
{ |
| 190 |
return opensex_write_cell(db, word, true); |
| 191 |
} |
| 192 |
|
| 193 |
static bool opensex_write_int(database_handle_t *db, int num) |
| 194 |
{ |
| 195 |
char buf[32]; |
| 196 |
snprintf(buf, sizeof buf, "%d", num); |
| 197 |
return opensex_write_cell(db, buf, false); |
| 198 |
} |
| 199 |
|
| 200 |
static bool opensex_write_uint(database_handle_t *db, unsigned int num) |
| 201 |
{ |
| 202 |
char buf[32]; |
| 203 |
snprintf(buf, sizeof buf, "%u", num); |
| 204 |
return opensex_write_cell(db, buf, false); |
| 205 |
} |
| 206 |
|
| 207 |
static bool opensex_write_time(database_handle_t *db, time_t tm) |
| 208 |
{ |
| 209 |
char buf[32]; |
| 210 |
snprintf(buf, sizeof buf, "%lu", tm); |
| 211 |
return opensex_write_cell(db, buf, false); |
| 212 |
} |
| 213 |
|
| 214 |
static bool opensex_commit_row(database_handle_t *db) |
| 215 |
{ |
| 216 |
opensex_t *rs; |
| 217 |
|
| 218 |
return_val_if_fail(db != NULL, false); |
| 219 |
rs = (opensex_t *)db->priv; |
| 220 |
|
| 221 |
fprintf(rs->f, "\n"); |
| 222 |
|
| 223 |
return true; |
| 224 |
} |
| 225 |
|
| 226 |
static database_vtable_t opensex_vt = { |
| 227 |
.name = "opensex", |
| 228 |
|
| 229 |
.read_next_row = opensex_read_next_row, |
| 230 |
|
| 231 |
.read_word = opensex_read_word, |
| 232 |
.read_str = opensex_read_str, |
| 233 |
.read_int = opensex_read_int, |
| 234 |
.read_uint = opensex_read_uint, |
| 235 |
.read_time = opensex_read_time, |
| 236 |
|
| 237 |
.start_row = opensex_start_row, |
| 238 |
.write_word = opensex_write_word, |
| 239 |
.write_str = opensex_write_str, |
| 240 |
.write_int = opensex_write_int, |
| 241 |
.write_uint = opensex_write_uint, |
| 242 |
.write_time = opensex_write_time, |
| 243 |
.commit_row = opensex_commit_row |
| 244 |
}; |
| 245 |
|
| 246 |
static database_handle_t *opensex_db_open_read(const char *filename) |
| 247 |
{ |
| 248 |
database_handle_t *db; |
| 249 |
opensex_t *rs; |
| 250 |
FILE *f; |
| 251 |
int errno1; |
| 252 |
char path[BUFSIZE]; |
| 253 |
|
| 254 |
snprintf(path, BUFSIZE, "%s/%s", datadir, filename != NULL ? filename : "services.db"); |
| 255 |
f = fopen(path, "r"); |
| 256 |
if (!f) |
| 257 |
{ |
| 258 |
errno1 = errno; |
| 259 |
|
| 260 |
/* ENOENT can happen if the database does not exist yet. */ |
| 261 |
if (errno == ENOENT) |
| 262 |
{ |
| 263 |
slog(LG_ERROR, "db-open-read: database '%s' does not yet exist; a new one will be created.", path); |
| 264 |
return NULL; |
| 265 |
} |
| 266 |
|
| 267 |
slog(LG_ERROR, "db-open-read: cannot open '%s' for reading: %s", path, strerror(errno1)); |
| 268 |
wallops(_("\2DATABASE ERROR\2: db-open-read: cannot open '%s' for reading: %s"), path, strerror(errno1)); |
| 269 |
return NULL; |
| 270 |
} |
| 271 |
|
| 272 |
rs = scalloc(sizeof(opensex_t), 1); |
| 273 |
rs->grver = 1; |
| 274 |
rs->buf = scalloc(512, 1); |
| 275 |
rs->bufsize = 512; |
| 276 |
rs->token = NULL; |
| 277 |
rs->f = f; |
| 278 |
|
| 279 |
db = scalloc(sizeof(database_handle_t), 1); |
| 280 |
db->priv = rs; |
| 281 |
db->vt = &opensex_vt; |
| 282 |
db->txn = DB_READ; |
| 283 |
db->file = sstrdup(path); |
| 284 |
db->line = 0; |
| 285 |
db->token = 0; |
| 286 |
|
| 287 |
return db; |
| 288 |
} |
| 289 |
|
| 290 |
static database_handle_t *opensex_db_open_write(const char *filename) |
| 291 |
{ |
| 292 |
database_handle_t *db; |
| 293 |
opensex_t *rs; |
| 294 |
int fd; |
| 295 |
FILE *f; |
| 296 |
int errno1; |
| 297 |
char bpath[BUFSIZE], path[BUFSIZE]; |
| 298 |
|
| 299 |
snprintf(bpath, BUFSIZE, "%s/%s", datadir, filename != NULL ? filename : "services.db"); |
| 300 |
|
| 301 |
mowgli_strlcpy(path, bpath, sizeof path); |
| 302 |
mowgli_strlcat(path, ".new", sizeof path); |
| 303 |
|
| 304 |
fd = open(path, O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP); |
| 305 |
if (fd < 0 || ! (f = fdopen(fd, "w"))) |
| 306 |
{ |
| 307 |
errno1 = errno; |
| 308 |
slog(LG_ERROR, "db-open-write: cannot open '%s' for writing: %s", path, strerror(errno1)); |
| 309 |
wallops(_("\2DATABASE ERROR\2: db-open-write: cannot open '%s' for writing: %s"), path, strerror(errno1)); |
| 310 |
return NULL; |
| 311 |
} |
| 312 |
|
| 313 |
rs = scalloc(sizeof(opensex_t), 1); |
| 314 |
rs->f = f; |
| 315 |
rs->grver = 1; |
| 316 |
|
| 317 |
db = scalloc(sizeof(database_handle_t), 1); |
| 318 |
db->priv = rs; |
| 319 |
db->vt = &opensex_vt; |
| 320 |
db->txn = DB_WRITE; |
| 321 |
db->file = sstrdup(bpath); |
| 322 |
db->line = 0; |
| 323 |
db->token = 0; |
| 324 |
|
| 325 |
db_start_row(db, "GRVER"); |
| 326 |
db_write_int(db, rs->grver); |
| 327 |
db_commit_row(db); |
| 328 |
|
| 329 |
return db; |
| 330 |
} |
| 331 |
|
| 332 |
static database_handle_t *opensex_db_open(const char *filename, database_transaction_t txn) |
| 333 |
{ |
| 334 |
if (txn == DB_WRITE) |
| 335 |
return opensex_db_open_write(filename); |
| 336 |
return opensex_db_open_read(filename); |
| 337 |
} |
| 338 |
|
| 339 |
static void opensex_db_close(database_handle_t *db) |
| 340 |
{ |
| 341 |
opensex_t *rs; |
| 342 |
int errno1; |
| 343 |
char oldpath[BUFSIZE], newpath[BUFSIZE]; |
| 344 |
|
| 345 |
return_if_fail(db != NULL); |
| 346 |
rs = db->priv; |
| 347 |
|
| 348 |
mowgli_strlcpy(oldpath, db->file, sizeof oldpath); |
| 349 |
mowgli_strlcat(oldpath, ".new", sizeof oldpath); |
| 350 |
|
| 351 |
mowgli_strlcpy(newpath, db->file, sizeof newpath); |
| 352 |
|
| 353 |
fclose(rs->f); |
| 354 |
|
| 355 |
if (db->txn == DB_WRITE) |
| 356 |
{ |
| 357 |
/* now, replace the old database with the new one, using an atomic rename */ |
| 358 |
if (srename(oldpath, newpath) < 0) |
| 359 |
{ |
| 360 |
errno1 = errno; |
| 361 |
slog(LG_ERROR, "db_save(): cannot rename services.db.new to services.db: %s", strerror(errno1)); |
| 362 |
wallops(_("\2DATABASE ERROR\2: db_save(): cannot rename services.db.new to services.db: %s"), strerror(errno1)); |
| 363 |
} |
| 364 |
|
| 365 |
hook_call_db_saved(); |
| 366 |
} |
| 367 |
|
| 368 |
free(rs->buf); |
| 369 |
free(rs); |
| 370 |
free(db->file); |
| 371 |
free(db); |
| 372 |
} |
| 373 |
|
| 374 |
static database_module_t opensex_mod = { |
| 375 |
.db_open = opensex_db_open, |
| 376 |
.db_close = opensex_db_close, |
| 377 |
.db_parse = opensex_db_parse, |
| 378 |
}; |
| 379 |
|
| 380 |
void _modinit(module_t *m) |
| 381 |
{ |
| 382 |
MODULE_TRY_REQUEST_DEPENDENCY(m, "backend/corestorage"); |
| 383 |
|
| 384 |
m->mflags = MODTYPE_CORE; |
| 385 |
|
| 386 |
db_mod = &opensex_mod; |
| 387 |
|
| 388 |
db_register_type_handler("GRVER", opensex_h_grver); |
| 389 |
|
| 390 |
backend_loaded = true; |
| 391 |
} |
| 392 |
|
| 393 |
/* vim:cinoptions=>s,e0,n0,f0,{0,}0,^0,=s,ps,t0,c3,+s,(2s,us,)20,*30,gs,hs |
| 394 |
* vim:ts=8 |
| 395 |
* vim:sw=8 |
| 396 |
* vim:noexpandtab |
| 397 |
*/ |