[1ee6c18] | 1 | /********************************************************************\ |
---|
| 2 | * BitlBee -- An IRC to other IM-networks gateway * |
---|
| 3 | * * |
---|
| 4 | * Copyright 2002-2004 Wilmer van der Gaast and others * |
---|
| 5 | \********************************************************************/ |
---|
| 6 | |
---|
| 7 | /* Storage backend that uses the same file format as <=1.0 */ |
---|
| 8 | |
---|
| 9 | /* |
---|
| 10 | This program is free software; you can redistribute it and/or modify |
---|
| 11 | it under the terms of the GNU General Public License as published by |
---|
| 12 | the Free Software Foundation; either version 2 of the License, or |
---|
| 13 | (at your option) any later version. |
---|
| 14 | |
---|
| 15 | This program is distributed in the hope that it will be useful, |
---|
| 16 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 18 | GNU General Public License for more details. |
---|
| 19 | |
---|
| 20 | You should have received a copy of the GNU General Public License with |
---|
| 21 | the Debian GNU/Linux distribution in /usr/share/common-licenses/GPL; |
---|
| 22 | if not, write to the Free Software Foundation, Inc., 59 Temple Place, |
---|
| 23 | Suite 330, Boston, MA 02111-1307 USA |
---|
| 24 | */ |
---|
| 25 | |
---|
| 26 | #define BITLBEE_CORE |
---|
| 27 | #include "bitlbee.h" |
---|
| 28 | #include "crypting.h" |
---|
| 29 | |
---|
| 30 | static void text_init (void) |
---|
| 31 | { |
---|
| 32 | if( access( global.conf->configdir, F_OK ) != 0 ) |
---|
| 33 | log_message( LOGLVL_WARNING, "The configuration directory %s does not exist. Configuration won't be saved.", CONFIG ); |
---|
| 34 | else if( access( global.conf->configdir, R_OK ) != 0 || access( global.conf->configdir, W_OK ) != 0 ) |
---|
| 35 | log_message( LOGLVL_WARNING, "Permission problem: Can't read/write from/to %s.", global.conf->configdir ); |
---|
| 36 | } |
---|
| 37 | |
---|
[a1f17d4] | 38 | static storage_status_t text_load ( const char *my_nick, const char* password, irc_t *irc ) |
---|
[1ee6c18] | 39 | { |
---|
| 40 | char s[512]; |
---|
| 41 | char *line; |
---|
| 42 | int proto; |
---|
| 43 | char nick[MAX_NICK_LENGTH+1]; |
---|
| 44 | FILE *fp; |
---|
| 45 | user_t *ru = user_find( irc, ROOT_NICK ); |
---|
| 46 | |
---|
| 47 | if( irc->status == USTATUS_IDENTIFIED ) |
---|
| 48 | return( 1 ); |
---|
| 49 | |
---|
| 50 | g_snprintf( s, 511, "%s%s%s", global.conf->configdir, my_nick, ".accounts" ); |
---|
| 51 | fp = fopen( s, "r" ); |
---|
[a1f17d4] | 52 | if( !fp ) return STORAGE_NO_SUCH_USER; |
---|
[1ee6c18] | 53 | |
---|
| 54 | fscanf( fp, "%32[^\n]s", s ); |
---|
| 55 | if( setpass( irc, password, s ) < 0 ) |
---|
| 56 | { |
---|
| 57 | fclose( fp ); |
---|
[a1f17d4] | 58 | return STORAGE_INVALID_PASSWORD; |
---|
[1ee6c18] | 59 | } |
---|
| 60 | |
---|
| 61 | /* Do this now. If the user runs with AuthMode = Registered, the |
---|
| 62 | account command will not work otherwise. */ |
---|
| 63 | irc->status = USTATUS_IDENTIFIED; |
---|
| 64 | |
---|
| 65 | while( fscanf( fp, "%511[^\n]s", s ) > 0 ) |
---|
| 66 | { |
---|
| 67 | fgetc( fp ); |
---|
| 68 | line = deobfucrypt( irc, s ); |
---|
| 69 | root_command_string( irc, ru, line, 0 ); |
---|
| 70 | g_free( line ); |
---|
| 71 | } |
---|
| 72 | fclose( fp ); |
---|
| 73 | |
---|
| 74 | g_snprintf( s, 511, "%s%s%s", global.conf->configdir, my_nick, ".nicks" ); |
---|
| 75 | fp = fopen( s, "r" ); |
---|
[a1f17d4] | 76 | if( !fp ) return STORAGE_NO_SUCH_USER; |
---|
[1ee6c18] | 77 | while( fscanf( fp, "%s %d %s", s, &proto, nick ) > 0 ) |
---|
| 78 | { |
---|
| 79 | http_decode( s ); |
---|
| 80 | nick_set( irc, s, proto, nick ); |
---|
| 81 | } |
---|
| 82 | fclose( fp ); |
---|
| 83 | |
---|
| 84 | if( set_getint( irc, "auto_connect" ) ) |
---|
| 85 | { |
---|
| 86 | strcpy( s, "account on" ); /* Can't do this directly because r_c_s alters the string */ |
---|
| 87 | root_command_string( irc, ru, s, 0 ); |
---|
| 88 | } |
---|
| 89 | |
---|
[a1f17d4] | 90 | return STORAGE_OK; |
---|
[1ee6c18] | 91 | } |
---|
| 92 | |
---|
[a1f17d4] | 93 | static storage_status_t text_save( irc_t *irc, int overwrite ) |
---|
[1ee6c18] | 94 | { |
---|
| 95 | char s[512]; |
---|
| 96 | char path[512], new_path[512]; |
---|
| 97 | char *line; |
---|
| 98 | nick_t *n; |
---|
| 99 | set_t *set; |
---|
| 100 | mode_t ou = umask( 0077 ); |
---|
| 101 | account_t *a; |
---|
| 102 | FILE *fp; |
---|
| 103 | char *hash; |
---|
[a1f17d4] | 104 | |
---|
| 105 | if (!overwrite) { |
---|
| 106 | g_snprintf( path, 511, "%s%s%s", global.conf->configdir, irc->nick, ".accounts" ); |
---|
| 107 | if (access( path, F_OK ) != -1) |
---|
| 108 | return STORAGE_ALREADY_EXISTS; |
---|
| 109 | |
---|
| 110 | g_snprintf( path, 511, "%s%s%s", global.conf->configdir, irc->nick, ".nicks" ); |
---|
| 111 | if (access( path, F_OK ) != -1) |
---|
| 112 | return STORAGE_ALREADY_EXISTS; |
---|
| 113 | } |
---|
[1ee6c18] | 114 | |
---|
| 115 | /*\ |
---|
| 116 | * [SH] Nothing should be saved if no password is set, because the |
---|
| 117 | * password is not set if it was wrong, or if one is not identified |
---|
| 118 | * yet. This means that a malicious user could easily overwrite |
---|
| 119 | * files owned by someone else: |
---|
| 120 | * a Bad Thing, methinks |
---|
| 121 | \*/ |
---|
| 122 | |
---|
| 123 | /* [WVG] No? Really? */ |
---|
| 124 | |
---|
| 125 | /*\ |
---|
| 126 | * [SH] Okay, okay, it wasn't really Wilmer who said that, it was |
---|
| 127 | * me. I just thought it was funny. |
---|
| 128 | \*/ |
---|
| 129 | |
---|
| 130 | hash = hashpass( irc ); |
---|
| 131 | if( hash == NULL ) |
---|
| 132 | { |
---|
| 133 | irc_usermsg( irc, "Please register yourself if you want to save your settings." ); |
---|
| 134 | return( 0 ); |
---|
| 135 | } |
---|
| 136 | |
---|
| 137 | g_snprintf( path, 511, "%s%s%s", global.conf->configdir, irc->nick, ".nicks~" ); |
---|
| 138 | fp = fopen( path, "w" ); |
---|
[a1f17d4] | 139 | if( !fp ) return STORAGE_OTHER_ERROR; |
---|
[1ee6c18] | 140 | for( n = irc->nicks; n; n = n->next ) |
---|
| 141 | { |
---|
| 142 | strcpy( s, n->handle ); |
---|
| 143 | s[169] = 0; /* Prevent any overflow (169 ~ 512 / 3) */ |
---|
| 144 | http_encode( s ); |
---|
| 145 | g_snprintf( s + strlen( s ), 510 - strlen( s ), " %d %s", n->proto, n->nick ); |
---|
| 146 | if( fprintf( fp, "%s\n", s ) != strlen( s ) + 1 ) |
---|
| 147 | { |
---|
| 148 | irc_usermsg( irc, "fprintf() wrote too little. Disk full?" ); |
---|
| 149 | fclose( fp ); |
---|
[a1f17d4] | 150 | return STORAGE_OTHER_ERROR; |
---|
[1ee6c18] | 151 | } |
---|
| 152 | } |
---|
| 153 | if( fclose( fp ) != 0 ) |
---|
| 154 | { |
---|
| 155 | irc_usermsg( irc, "fclose() reported an error. Disk full?" ); |
---|
[a1f17d4] | 156 | return STORAGE_OTHER_ERROR; |
---|
[1ee6c18] | 157 | } |
---|
| 158 | |
---|
| 159 | g_snprintf( new_path, 512, "%s%s%s", global.conf->configdir, irc->nick, ".nicks" ); |
---|
| 160 | if( unlink( new_path ) != 0 ) |
---|
| 161 | { |
---|
| 162 | if( errno != ENOENT ) |
---|
| 163 | { |
---|
| 164 | irc_usermsg( irc, "Error while removing old .nicks file" ); |
---|
[a1f17d4] | 165 | return STORAGE_OTHER_ERROR; |
---|
[1ee6c18] | 166 | } |
---|
| 167 | } |
---|
| 168 | if( rename( path, new_path ) != 0 ) |
---|
| 169 | { |
---|
| 170 | irc_usermsg( irc, "Error while renaming new .nicks file" ); |
---|
[a1f17d4] | 171 | return STORAGE_OTHER_ERROR; |
---|
[1ee6c18] | 172 | } |
---|
| 173 | |
---|
| 174 | g_snprintf( path, 511, "%s%s%s", global.conf->configdir, irc->nick, ".accounts~" ); |
---|
| 175 | fp = fopen( path, "w" ); |
---|
[a1f17d4] | 176 | if( !fp ) return STORAGE_OTHER_ERROR; |
---|
[1ee6c18] | 177 | if( fprintf( fp, "%s", hash ) != strlen( hash ) ) |
---|
| 178 | { |
---|
| 179 | irc_usermsg( irc, "fprintf() wrote too little. Disk full?" ); |
---|
| 180 | fclose( fp ); |
---|
[a1f17d4] | 181 | return STORAGE_OTHER_ERROR; |
---|
[1ee6c18] | 182 | } |
---|
| 183 | g_free( hash ); |
---|
| 184 | |
---|
| 185 | for( a = irc->accounts; a; a = a->next ) |
---|
| 186 | { |
---|
| 187 | if( a->protocol == PROTO_OSCAR || a->protocol == PROTO_ICQ || a->protocol == PROTO_TOC ) |
---|
| 188 | g_snprintf( s, sizeof( s ), "account add oscar \"%s\" \"%s\" %s", a->user, a->pass, a->server ); |
---|
| 189 | else |
---|
| 190 | g_snprintf( s, sizeof( s ), "account add %s \"%s\" \"%s\" \"%s\"", |
---|
| 191 | proto_name[a->protocol], a->user, a->pass, a->server ? a->server : "" ); |
---|
| 192 | |
---|
| 193 | line = obfucrypt( irc, s ); |
---|
| 194 | if( *line ) |
---|
| 195 | { |
---|
| 196 | if( fprintf( fp, "%s\n", line ) != strlen( line ) + 1 ) |
---|
| 197 | { |
---|
| 198 | irc_usermsg( irc, "fprintf() wrote too little. Disk full?" ); |
---|
| 199 | fclose( fp ); |
---|
[a1f17d4] | 200 | return STORAGE_OTHER_ERROR; |
---|
[1ee6c18] | 201 | } |
---|
| 202 | } |
---|
| 203 | g_free( line ); |
---|
| 204 | } |
---|
| 205 | |
---|
| 206 | for( set = irc->set; set; set = set->next ) |
---|
| 207 | { |
---|
| 208 | if( set->value && set->def ) |
---|
| 209 | { |
---|
| 210 | g_snprintf( s, sizeof( s ), "set %s \"%s\"", set->key, set->value ); |
---|
| 211 | line = obfucrypt( irc, s ); |
---|
| 212 | if( *line ) |
---|
| 213 | { |
---|
| 214 | if( fprintf( fp, "%s\n", line ) != strlen( line ) + 1 ) |
---|
| 215 | { |
---|
| 216 | irc_usermsg( irc, "fprintf() wrote too little. Disk full?" ); |
---|
| 217 | fclose( fp ); |
---|
[a1f17d4] | 218 | return STORAGE_OTHER_ERROR; |
---|
[1ee6c18] | 219 | } |
---|
| 220 | } |
---|
| 221 | g_free( line ); |
---|
| 222 | } |
---|
| 223 | } |
---|
| 224 | |
---|
| 225 | if( strcmp( irc->mynick, ROOT_NICK ) != 0 ) |
---|
| 226 | { |
---|
| 227 | g_snprintf( s, sizeof( s ), "rename %s %s", ROOT_NICK, irc->mynick ); |
---|
| 228 | line = obfucrypt( irc, s ); |
---|
| 229 | if( *line ) |
---|
| 230 | { |
---|
| 231 | if( fprintf( fp, "%s\n", line ) != strlen( line ) + 1 ) |
---|
| 232 | { |
---|
| 233 | irc_usermsg( irc, "fprintf() wrote too little. Disk full?" ); |
---|
| 234 | fclose( fp ); |
---|
[a1f17d4] | 235 | return STORAGE_OTHER_ERROR; |
---|
[1ee6c18] | 236 | } |
---|
| 237 | } |
---|
| 238 | g_free( line ); |
---|
| 239 | } |
---|
| 240 | if( fclose( fp ) != 0 ) |
---|
| 241 | { |
---|
| 242 | irc_usermsg( irc, "fclose() reported an error. Disk full?" ); |
---|
[a1f17d4] | 243 | return STORAGE_OTHER_ERROR; |
---|
[1ee6c18] | 244 | } |
---|
| 245 | |
---|
| 246 | g_snprintf( new_path, 512, "%s%s%s", global.conf->configdir, irc->nick, ".accounts" ); |
---|
| 247 | if( unlink( new_path ) != 0 ) |
---|
| 248 | { |
---|
| 249 | if( errno != ENOENT ) |
---|
| 250 | { |
---|
| 251 | irc_usermsg( irc, "Error while removing old .accounts file" ); |
---|
[a1f17d4] | 252 | return STORAGE_OTHER_ERROR; |
---|
[1ee6c18] | 253 | } |
---|
| 254 | } |
---|
| 255 | if( rename( path, new_path ) != 0 ) |
---|
| 256 | { |
---|
| 257 | irc_usermsg( irc, "Error while renaming new .accounts file" ); |
---|
[a1f17d4] | 258 | return STORAGE_OTHER_ERROR; |
---|
[1ee6c18] | 259 | } |
---|
| 260 | |
---|
| 261 | umask( ou ); |
---|
| 262 | |
---|
[a1f17d4] | 263 | return STORAGE_OK; |
---|
[1ee6c18] | 264 | } |
---|
| 265 | |
---|
[a1f17d4] | 266 | static storage_status_t text_remove( const char *nick, const char *password ) |
---|
[1ee6c18] | 267 | { |
---|
| 268 | char s[512]; |
---|
| 269 | FILE *fp; |
---|
| 270 | |
---|
| 271 | g_snprintf( s, 511, "%s%s%s", global.conf->configdir, nick, ".nicks" ); |
---|
| 272 | fp = fopen( s, "r" ); |
---|
[a1f17d4] | 273 | if (!fp) |
---|
| 274 | return STORAGE_NO_SUCH_USER; |
---|
[1ee6c18] | 275 | |
---|
| 276 | fscanf( fp, "%32[^\n]s", s ); |
---|
| 277 | fclose( fp ); |
---|
| 278 | |
---|
[a1f17d4] | 279 | /*FIXME Test if password is correct */ |
---|
| 280 | |
---|
| 281 | g_snprintf( s, 511, "%s%s%s", global.conf->configdir, nick, ".accounts" ); |
---|
| 282 | if (unlink( s ) == -1) |
---|
| 283 | return STORAGE_OTHER_ERROR; |
---|
| 284 | |
---|
| 285 | g_snprintf( s, 511, "%s%s%s", global.conf->configdir, nick, ".nicks" ); |
---|
| 286 | if (unlink( s ) == -1) |
---|
| 287 | return STORAGE_OTHER_ERROR; |
---|
| 288 | |
---|
| 289 | return STORAGE_OK; |
---|
[1ee6c18] | 290 | } |
---|
| 291 | |
---|
| 292 | storage_t storage_text = { |
---|
| 293 | .name = "text", |
---|
| 294 | .init = text_init, |
---|
| 295 | .remove = text_remove, |
---|
| 296 | .load = text_load, |
---|
| 297 | .save = text_save |
---|
| 298 | }; |
---|