~ubuntu-branches/debian/lenny/ggz-server/lenny

« back to all changes in this revision

Viewing changes to ggzd/database/ggzdb.c

  • Committer: Bazaar Package Importer
  • Author(s): Neil Williams
  • Date: 2008-08-30 22:28:31 UTC
  • mfrom: (3.1.5 intrepid)
  • Revision ID: james.westby@ubuntu.com-20080830222831-lt2l462usl5opxfd
Tags: 0.0.14.1-1.2
* Non-maintainer upload.
* Improve the database.m4 fix to migrate to libdb4.6
  instead of 4.4 {request from Steve Langasek.}

Show diffs side-by-side

added added

removed removed

Lines of Context:
4
4
 * Project: GGZ Server
5
5
 * Date: 06/11/2000
6
6
 * Desc: Front-end functions to handle database manipulation
7
 
 * $Id: ggzdb.c 7424 2005-08-15 09:00:27Z josef $
 
7
 * $Id: ggzdb.c 8504 2006-08-08 09:07:31Z josef $
8
8
 *
9
9
 * Copyright (C) 2000 Brent Hendricks.
10
10
 *
92
92
        }
93
93
 
94
94
        /* Call backend's initialization */
 
95
        connection.type = opt.dbtype;
95
96
        connection.datadir = opt.data_dir;
96
97
        connection.host = opt.dbhost;
97
98
        connection.database = opt.dbname;
119
120
        GGZDBResult rc = GGZDB_NO_ERROR;
120
121
        char orig[MAX_USER_NAME_LEN + 1];
121
122
        hash_t hash;
122
 
        char *password64;
 
123
        char *password_enc;
123
124
        char *origpassword = NULL;
124
125
 
125
126
        if(!opt.dbhashing) return GGZDB_ERR_DB;
138
139
                || (!strcmp(opt.dbhashing, "ripemd160")))
139
140
                {
140
141
                        hash = ggz_hash_create(opt.dbhashing, pe->password);
141
 
                        password64 = ggz_base64_encode(hash.hash, hash.hashlen);
142
 
                        if(password64)
 
142
                        if(!strcmp(opt.dbhashencoding, "base16")){
 
143
                                password_enc = ggz_base16_encode(hash.hash, hash.hashlen);
 
144
                        }else{
 
145
                                password_enc = ggz_base64_encode(hash.hash, hash.hashlen);
 
146
                        }
 
147
                        if(hash.hash)
 
148
                                ggz_free(hash.hash);
 
149
 
 
150
                        if(password_enc)
143
151
                        {
144
 
                                free(hash.hash);
145
 
                                origpassword = strdup(pe->password);
146
 
                                snprintf(pe->password, sizeof(pe->password), "%s", password64);
 
152
                                origpassword = ggz_strdup(pe->password);
 
153
                                snprintf(pe->password, sizeof(pe->password), "%s", password_enc);
 
154
                                ggz_free(password_enc);
147
155
                        }
 
156
                        // FIXME - shouldn't failing to encrypt the password be an error?
148
157
                }
149
158
                rc = _ggzdb_player_add(pe);
150
159
        }
154
163
        if(origpassword)
155
164
        {
156
165
                snprintf(pe->password, sizeof(pe->password), "%s", origpassword);
157
 
                free(origpassword);
 
166
                ggz_free(origpassword);
158
167
        }
159
168
        
160
169
        _ggzdb_exit();
373
382
int ggzdb_compare_password(const char *input, const char *password)
374
383
{
375
384
        hash_t hash;
376
 
        char *password64;
 
385
        char *password_enc;
377
386
        int ret = 0;
378
387
 
379
388
        if(!opt.dbhashing) return -1;
389
398
        || (!strcmp(opt.dbhashing, "ripemd160")))
390
399
        {
391
400
                hash = ggz_hash_create(opt.dbhashing, input);
392
 
                password64 = ggz_base64_encode(hash.hash, hash.hashlen);
393
 
                if(!password64) return -1;
394
 
                if(!strcmp(password64, password)) ret = 1;
395
 
                free(hash.hash);
396
 
                free(password64);
 
401
                if(!strcmp(opt.dbhashencoding, "base16")){
 
402
                        password_enc = ggz_base16_encode(hash.hash, hash.hashlen);
 
403
                }else{
 
404
                        password_enc = ggz_base64_encode(hash.hash, hash.hashlen);
 
405
                }
 
406
                if(hash.hash)
 
407
                        ggz_free(hash.hash);
 
408
 
 
409
                if(!password_enc) return -1;
 
410
                if(!strcmp(password_enc, password)) ret = 1;
 
411
                ggz_free(password_enc);
397
412
                return ret;
398
413
        }
399
414
 
402
417
        return -1;
403
418
}
404
419
 
 
420
/* Helper function, might go into libggz*/
 
421
char *_ggz_sql_escape(const char *str)
 
422
{
 
423
        char *newstr, *q;
 
424
        const char *p;
 
425
        size_t len = 0;
 
426
 
 
427
        if(str == NULL)
 
428
                return NULL;
 
429
 
 
430
        len = strlen(str);
 
431
 
 
432
        for(p = str; *p != '\0'; p++) {
 
433
                if(*p == '\'') {
 
434
                        len += 1;
 
435
                }
 
436
        }
 
437
 
 
438
        if(len == strlen(str))
 
439
                return ggz_strdup(str);
 
440
 
 
441
        newstr = ggz_malloc(len + 1);
 
442
        q = newstr;
 
443
 
 
444
        for(p = str; *p != '\0'; p++) {
 
445
                if(*p == '\'') {
 
446
                        *q++ = '\\';
 
447
                        *q = *p;
 
448
                } else {
 
449
                        *q = *p;
 
450
                }
 
451
                q++;
 
452
        }
 
453
        *q = '\0';
 
454
 
 
455
        return newstr;
 
456
}
 
457
 
 
458
/* Helper function, might go into libggz*/
 
459
/*char *_ggz_sql_unescape(const char *str)
 
460
{
 
461
        char *new, *q;
 
462
        const char *p;
 
463
        size_t len = 0;
 
464
 
 
465
        if(str == NULL)
 
466
                return NULL;
 
467
 
 
468
        len = strlen(str);
 
469
 
 
470
        for(p = str; *p != '\0'; p++) {
 
471
                if(!strncmp(p, "\\'", 2)) {
 
472
                        p += 1;
 
473
                }
 
474
                len++;
 
475
        }
 
476
 
 
477
        if(len == strlen(str))
 
478
                return ggz_strdup(str);
 
479
 
 
480
        q = new = ggz_malloc(len + 1);
 
481
        for(p = str; *p != '\0'; p++) {
 
482
                if(!strncmp(p, "\\'", 2)) {
 
483
                        *q = '\'';
 
484
                        q++;
 
485
                        p += 1;
 
486
                } else {
 
487
                        *q = *p;
 
488
                        q++;
 
489
                }
 
490
        }
 
491
        *q = '\0';
 
492
 
 
493
        return new;
 
494
}*/
 
495