Changeset 1ee6c18


Ignore:
Timestamp:
2005-12-08T13:41:53Z (18 years ago)
Author:
Jelmer Vernooij <jelmer@…>
Branches:
master
Children:
a1f17d4
Parents:
d636233
Message:

Add abstraction layer for storage

Files:
3 added
10 edited

Legend:

Unmodified
Added
Removed
  • Makefile

    rd636233 r1ee6c18  
    1010
    1111# Program variables
    12 objects = account.o bitlbee.o commands.o conf.o crypting.o help.o ini.o irc.o log.o nick.o query.o set.o unix.o url.o user.o
     12objects = account.o bitlbee.o commands.o conf.o crypting.o help.o ini.o irc.o log.o nick.o query.o set.o unix.o url.o user.o storage_text.o storage.o
    1313subdirs = protocols
    1414
  • bitlbee.c

    rd636233 r1ee6c18  
    2727#include "bitlbee.h"
    2828#include "commands.h"
    29 #include "crypting.h"
    3029#include "protocols/nogaim.h"
    3130#include "help.h"
     
    244243}
    245244
    246 int bitlbee_load( irc_t *irc, char* password )
    247 {
    248         char s[512];
    249         char *line;
    250         int proto;
    251         char nick[MAX_NICK_LENGTH+1];
    252         FILE *fp;
    253         user_t *ru = user_find( irc, ROOT_NICK );
    254        
    255         if( irc->status == USTATUS_IDENTIFIED )
    256                 return( 1 );
    257        
    258         g_snprintf( s, 511, "%s%s%s", global.conf->configdir, irc->nick, ".accounts" );
    259         fp = fopen( s, "r" );
    260         if( !fp ) return( 0 );
    261        
    262         fscanf( fp, "%32[^\n]s", s );
    263         if( setpass( irc, password, s ) < 0 )
    264         {
    265                 fclose( fp );
    266                 return( -1 );
    267         }
    268        
    269         /* Do this now. If the user runs with AuthMode = Registered, the
    270            account command will not work otherwise. */
    271         irc->status = USTATUS_IDENTIFIED;
    272        
    273         while( fscanf( fp, "%511[^\n]s", s ) > 0 )
    274         {
    275                 fgetc( fp );
    276                 line = deobfucrypt( irc, s );
    277                 root_command_string( irc, ru, line, 0 );
    278                 g_free( line );
    279         }
    280         fclose( fp );
    281        
    282         g_snprintf( s, 511, "%s%s%s", global.conf->configdir, irc->nick, ".nicks" );
    283         fp = fopen( s, "r" );
    284         if( !fp ) return( 0 );
    285         while( fscanf( fp, "%s %d %s", s, &proto, nick ) > 0 )
    286         {
    287                 http_decode( s );
    288                 nick_set( irc, s, proto, nick );
    289         }
    290         fclose( fp );
    291        
    292         if( set_getint( irc, "auto_connect" ) )
    293         {
    294                 strcpy( s, "account on" );      /* Can't do this directly because r_c_s alters the string */
    295                 root_command_string( irc, ru, s, 0 );
    296         }
    297        
    298         return( 1 );
    299 }
    300 
    301 int bitlbee_save( irc_t *irc )
    302 {
    303         char s[512];
    304         char path[512], new_path[512];
    305         char *line;
    306         nick_t *n;
    307         set_t *set;
    308         mode_t ou = umask( 0077 );
    309         account_t *a;
    310         FILE *fp;
    311         char *hash;
    312        
    313         /*\
    314          *  [SH] Nothing should be saved if no password is set, because the
    315          *  password is not set if it was wrong, or if one is not identified
    316          *  yet. This means that a malicious user could easily overwrite
    317          *  files owned by someone else:
    318          *  a Bad Thing, methinks
    319         \*/
    320 
    321         /* [WVG] No? Really? */
    322 
    323         /*\
    324          *  [SH] Okay, okay, it wasn't really Wilmer who said that, it was
    325          *  me. I just thought it was funny.
    326         \*/
    327        
    328         hash = hashpass( irc );
    329         if( hash == NULL )
    330         {
    331                 irc_usermsg( irc, "Please register yourself if you want to save your settings." );
    332                 return( 0 );
    333         }
    334        
    335         g_snprintf( path, 511, "%s%s%s", global.conf->configdir, irc->nick, ".nicks~" );
    336         fp = fopen( path, "w" );
    337         if( !fp ) return( 0 );
    338         for( n = irc->nicks; n; n = n->next )
    339         {
    340                 strcpy( s, n->handle );
    341                 s[169] = 0; /* Prevent any overflow (169 ~ 512 / 3) */
    342                 http_encode( s );
    343                 g_snprintf( s + strlen( s ), 510 - strlen( s ), " %d %s", n->proto, n->nick );
    344                 if( fprintf( fp, "%s\n", s ) != strlen( s ) + 1 )
    345                 {
    346                         irc_usermsg( irc, "fprintf() wrote too little. Disk full?" );
    347                         fclose( fp );
    348                         return( 0 );
    349                 }
    350         }
    351         if( fclose( fp ) != 0 )
    352         {
    353                 irc_usermsg( irc, "fclose() reported an error. Disk full?" );
    354                 return( 0 );
    355         }
    356  
    357         g_snprintf( new_path, 512, "%s%s%s", global.conf->configdir, irc->nick, ".nicks" );
    358         if( unlink( new_path ) != 0 )
    359         {
    360                 if( errno != ENOENT )
    361                 {
    362                         irc_usermsg( irc, "Error while removing old .nicks file" );
    363                         return( 0 );
    364                 }
    365         }
    366         if( rename( path, new_path ) != 0 )
    367         {
    368                 irc_usermsg( irc, "Error while renaming new .nicks file" );
    369                 return( 0 );
    370         }
    371        
    372         g_snprintf( path, 511, "%s%s%s", global.conf->configdir, irc->nick, ".accounts~" );
    373         fp = fopen( path, "w" );
    374         if( !fp ) return( 0 );
    375         if( fprintf( fp, "%s", hash ) != strlen( hash ) )
    376         {
    377                 irc_usermsg( irc, "fprintf() wrote too little. Disk full?" );
    378                 fclose( fp );
    379                 return( 0 );
    380         }
    381         g_free( hash );
    382 
    383         for( a = irc->accounts; a; a = a->next )
    384         {
    385                 if( a->protocol == PROTO_OSCAR || a->protocol == PROTO_ICQ || a->protocol == PROTO_TOC )
    386                         g_snprintf( s, sizeof( s ), "account add oscar \"%s\" \"%s\" %s", a->user, a->pass, a->server );
    387                 else
    388                         g_snprintf( s, sizeof( s ), "account add %s \"%s\" \"%s\" \"%s\"",
    389                                     proto_name[a->protocol], a->user, a->pass, a->server ? a->server : "" );
    390                
    391                 line = obfucrypt( irc, s );
    392                 if( *line )
    393                 {
    394                         if( fprintf( fp, "%s\n", line ) != strlen( line ) + 1 )
    395                         {
    396                                 irc_usermsg( irc, "fprintf() wrote too little. Disk full?" );
    397                                 fclose( fp );
    398                                 return( 0 );
    399                         }
    400                 }
    401                 g_free( line );
    402         }
    403        
    404         for( set = irc->set; set; set = set->next )
    405         {
    406                 if( set->value && set->def )
    407                 {
    408                         g_snprintf( s, sizeof( s ), "set %s \"%s\"", set->key, set->value );
    409                         line = obfucrypt( irc, s );
    410                         if( *line )
    411                         {
    412                                 if( fprintf( fp, "%s\n", line ) != strlen( line ) + 1 )
    413                                 {
    414                                         irc_usermsg( irc, "fprintf() wrote too little. Disk full?" );
    415                                         fclose( fp );
    416                                         return( 0 );
    417                                 }
    418                         }
    419                         g_free( line );
    420                 }
    421         }
    422        
    423         if( strcmp( irc->mynick, ROOT_NICK ) != 0 )
    424         {
    425                 g_snprintf( s, sizeof( s ), "rename %s %s", ROOT_NICK, irc->mynick );
    426                 line = obfucrypt( irc, s );
    427                 if( *line )
    428                 {
    429                         if( fprintf( fp, "%s\n", line ) != strlen( line ) + 1 )
    430                         {
    431                                 irc_usermsg( irc, "fprintf() wrote too little. Disk full?" );
    432                                 fclose( fp );
    433                                 return( 0 );
    434                         }
    435                 }
    436                 g_free( line );
    437         }
    438         if( fclose( fp ) != 0 )
    439         {
    440                 irc_usermsg( irc, "fclose() reported an error. Disk full?" );
    441                 return( 0 );
    442         }
    443        
    444         g_snprintf( new_path, 512, "%s%s%s", global.conf->configdir, irc->nick, ".accounts" );
    445         if( unlink( new_path ) != 0 )
    446         {
    447                 if( errno != ENOENT )
    448                 {
    449                         irc_usermsg( irc, "Error while removing old .accounts file" );
    450                         return( 0 );
    451                 }
    452         }
    453         if( rename( path, new_path ) != 0 )
    454         {
    455                 irc_usermsg( irc, "Error while renaming new .accounts file" );
    456                 return( 0 );
    457         }
    458        
    459         umask( ou );
    460        
    461         return( 1 );
    462 }
    463 
    464245void bitlbee_shutdown( gpointer data )
    465246{
  • bitlbee.h

    rd636233 r1ee6c18  
    100100
    101101#include "irc.h"
     102#include "storage.h"
    102103#include "set.h"
    103104#include "protocols/nogaim.h"
     
    115116        help_t *help;
    116117        conf_t *conf;
     118        storage_t *storage;
    117119        char *helpfile;
    118120        GMainLoop *loop;
     
    127129int root_command_string( irc_t *irc, user_t *u, char *command, int flags );
    128130int root_command( irc_t *irc, char *command[] );
    129 int bitlbee_load( irc_t *irc, char *password );
    130 int bitlbee_save( irc_t *irc );
    131131void bitlbee_shutdown( gpointer data );
    132132double gettime( void );
  • commands.c

    rd636233 r1ee6c18  
    8686int cmd_identify( irc_t *irc, char **cmd )
    8787{
    88         int checkie = bitlbee_load( irc, cmd[1] );
     88        int checkie = global.storage->load( irc->nick, cmd[1], irc );
    8989       
    9090        if( checkie == -1 )
     
    110110int cmd_register( irc_t *irc, char **cmd )
    111111{
    112         int checkie;
    113         char path[512];
    114        
    115112        if( global.conf->authmode == AUTHMODE_REGISTERED )
    116113        {
     
    118115                return( 0 );
    119116        }
    120        
    121         g_snprintf( path, 511, "%s%s%s", global.conf->configdir, irc->nick, ".accounts" );
    122         checkie = access( path, F_OK );
    123        
    124         g_snprintf( path, 511, "%s%s%s", global.conf->configdir, irc->nick, ".nicks" );
    125         checkie += access( path, F_OK );
    126        
    127         if( checkie == -2 )
     117
     118        if( !global.storage->exists( irc->nick ))
    128119        {
    129120                setpassnc( irc, cmd[1] );
     
    141132int cmd_drop( irc_t *irc, char **cmd )
    142133{
    143         char s[512];
    144         FILE *fp;
    145        
    146         g_snprintf( s, 511, "%s%s%s", global.conf->configdir, irc->nick, ".accounts" );
    147         fp = fopen( s, "r" );
    148         if( !fp )
     134        if( ! global.storage->exists (irc->nick) )
    149135        {
    150136                irc_usermsg( irc, "That account does not exist" );
    151137                return( 0 );
    152138        }
    153        
    154         fscanf( fp, "%32[^\n]s", s );
    155         fclose( fp );
    156         if( setpass( irc, cmd[1], s ) < 0 )
    157         {
    158                 irc_usermsg( irc, "Incorrect password" );
    159                 return( 0 );
    160         }
    161        
    162         g_snprintf( s, 511, "%s%s%s", global.conf->configdir, irc->nick, ".accounts" );
    163         unlink( s );
    164        
    165         g_snprintf( s, 511, "%s%s%s", global.conf->configdir, irc->nick, ".nicks" );
    166         unlink( s );
     139
     140        if ( global.storage->check_pass (irc->nick, cmd[1]) )
     141        {
     142                irc_usermsg( irc, "Password invalid" );
     143                return( 0 );
     144        }
     145       
     146        global.storage->remove (irc->nick);
    167147       
    168148        setpassnc( irc, NULL );
     
    634614int cmd_save( irc_t *irc, char **cmd )
    635615{
    636         if( bitlbee_save( irc ) )
     616        if( global.storage->save( irc ) )
    637617                irc_usermsg( irc, "Configuration saved" );
    638618        else
  • conf.c

    rd636233 r1ee6c18  
    5050        conf->nofork = 0;
    5151        conf->verbose = 0;
     52        conf->storage = "text";
    5253        conf->runmode = RUNMODE_INETD;
    5354        conf->authmode = AUTHMODE_OPEN;
     
    198199                                conf->motdfile = g_strdup( ini->value );
    199200                        }
     201                        else if( g_strcasecmp( ini->key, "storage" ) == 0 )
     202                        {
     203                                g_free( conf->storage );
     204                                conf->storage = g_strdup( ini->value );
     205                        }
    200206                        else if( g_strcasecmp( ini->key, "pinginterval" ) == 0 )
    201207                        {
  • conf.h

    rd636233 r1ee6c18  
    4242        char *configdir;
    4343        char *motdfile;
     44        char *storage;
    4445        int ping_interval;
    4546        int ping_timeout;
  • crypting.c

    rd636233 r1ee6c18  
    6767/* USE WITH CAUTION!
    6868   Sets pass without checking */
    69 void setpassnc (irc_t *irc, char *pass) {
     69void setpassnc (irc_t *irc, const char *pass)
     70{
    7071        if (!set_find (irc, "password"))
    7172                set_add (irc, "password", NULL, passchange);
     
    8687}
    8788
    88 int setpass (irc_t *irc, char *pass, char* md5sum) {
     89int setpass (irc_t *irc, const char *pass, char* md5sum) {
    8990        md5_state_t md5state;
    9091        md5_byte_t digest[16];
  • crypting.h

    rd636233 r1ee6c18  
    2424*/
    2525
    26 void setpassnc (irc_t *irc, char *pass); /* USE WITH CAUTION! */
     26void setpassnc (irc_t *irc, const char *pass); /* USE WITH CAUTION! */
    2727char *passchange (irc_t *irc, void *set, char *value);
    28 int setpass (irc_t *irc, char *pass, char* md5sum);
     28int setpass (irc_t *irc, const char *pass, char* md5sum);
    2929char *hashpass (irc_t *irc);
    3030char *obfucrypt (irc_t *irc, char *line);
  • irc.c

    rd636233 r1ee6c18  
    154154       
    155155        if( irc->status >= USTATUS_IDENTIFIED && set_getint( irc, "save_on_quit" ) )
    156                 if( !bitlbee_save( irc ) )
     156                if( !global.storage->save( irc ) )
    157157                        irc_usermsg( irc, "Error while saving settings!" );
    158158       
  • unix.c

    rd636233 r1ee6c18  
    5252       
    5353        global.helpfile = g_strdup( HELP_FILE );
    54        
     54
    5555        global.conf = conf_load( argc, argv );
    5656        if( global.conf == NULL )
    5757                return( 1 );
    58        
     58
     59
    5960        if( global.conf->runmode == RUNMODE_INETD )
    6061        {
     
    7071        if( i != 0 )
    7172                return( i );
     73
     74        global.storage = storage_init( global.conf->storage );
     75        if ( global.storage == NULL) {
     76                log_message( LOGLVL_ERROR, "No such storage backend '%s'", global.conf->storage );
     77                return( 1 );
     78        }
     79       
    7280       
    7381        /* Catch some signals to tell the user what's happening before quitting */
     
    8795        if( !getuid() || !geteuid() )
    8896                log_message( LOGLVL_WARNING, "BitlBee is running with root privileges. Why?" );
    89         if( access( global.conf->configdir, F_OK ) != 0 )
    90                 log_message( LOGLVL_WARNING, "The configuration directory %s does not exist. Configuration won't be saved.", CONFIG );
    91         else if( access( global.conf->configdir, R_OK ) != 0 || access( global.conf->configdir, W_OK ) != 0 )
    92                 log_message( LOGLVL_WARNING, "Permission problem: Can't read/write from/to %s.", global.conf->configdir );
    9397        if( help_init( &(global.help) ) == NULL )
    9498                log_message( LOGLVL_WARNING, "Error opening helpfile %s.", HELP_FILE );
Note: See TracChangeset for help on using the changeset viewer.