[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 ); |
---|
[7cad7b4] | 55 | |
---|
| 56 | if (checkpass (password, s) != 0) |
---|
[1ee6c18] | 57 | { |
---|
| 58 | fclose( fp ); |
---|
[a1f17d4] | 59 | return STORAGE_INVALID_PASSWORD; |
---|
[1ee6c18] | 60 | } |
---|
| 61 | |
---|
| 62 | /* Do this now. If the user runs with AuthMode = Registered, the |
---|
| 63 | account command will not work otherwise. */ |
---|
| 64 | irc->status = USTATUS_IDENTIFIED; |
---|
| 65 | |
---|
| 66 | while( fscanf( fp, "%511[^\n]s", s ) > 0 ) |
---|
| 67 | { |
---|
| 68 | fgetc( fp ); |
---|
[b73ac9c] | 69 | line = deobfucrypt( s, password ); |
---|
| 70 | if (line == NULL) return STORAGE_OTHER_ERROR; |
---|
[1ee6c18] | 71 | root_command_string( irc, ru, line, 0 ); |
---|
| 72 | g_free( line ); |
---|
| 73 | } |
---|
| 74 | fclose( fp ); |
---|
| 75 | |
---|
| 76 | g_snprintf( s, 511, "%s%s%s", global.conf->configdir, my_nick, ".nicks" ); |
---|
| 77 | fp = fopen( s, "r" ); |
---|
[a1f17d4] | 78 | if( !fp ) return STORAGE_NO_SUCH_USER; |
---|
[1ee6c18] | 79 | while( fscanf( fp, "%s %d %s", s, &proto, nick ) > 0 ) |
---|
| 80 | { |
---|
| 81 | http_decode( s ); |
---|
| 82 | nick_set( irc, s, proto, nick ); |
---|
| 83 | } |
---|
| 84 | fclose( fp ); |
---|
| 85 | |
---|
| 86 | if( set_getint( irc, "auto_connect" ) ) |
---|
| 87 | { |
---|
| 88 | strcpy( s, "account on" ); /* Can't do this directly because r_c_s alters the string */ |
---|
| 89 | root_command_string( irc, ru, s, 0 ); |
---|
| 90 | } |
---|
| 91 | |
---|
[a1f17d4] | 92 | return STORAGE_OK; |
---|
[1ee6c18] | 93 | } |
---|
| 94 | |
---|
[a1f17d4] | 95 | static storage_status_t text_save( irc_t *irc, int overwrite ) |
---|
[1ee6c18] | 96 | { |
---|
| 97 | char s[512]; |
---|
| 98 | char path[512], new_path[512]; |
---|
| 99 | char *line; |
---|
| 100 | nick_t *n; |
---|
| 101 | set_t *set; |
---|
| 102 | mode_t ou = umask( 0077 ); |
---|
| 103 | account_t *a; |
---|
| 104 | FILE *fp; |
---|
| 105 | char *hash; |
---|
[a1f17d4] | 106 | |
---|
| 107 | if (!overwrite) { |
---|
| 108 | g_snprintf( path, 511, "%s%s%s", global.conf->configdir, irc->nick, ".accounts" ); |
---|
| 109 | if (access( path, F_OK ) != -1) |
---|
| 110 | return STORAGE_ALREADY_EXISTS; |
---|
| 111 | |
---|
| 112 | g_snprintf( path, 511, "%s%s%s", global.conf->configdir, irc->nick, ".nicks" ); |
---|
| 113 | if (access( path, F_OK ) != -1) |
---|
| 114 | return STORAGE_ALREADY_EXISTS; |
---|
| 115 | } |
---|
[1ee6c18] | 116 | |
---|
| 117 | /*\ |
---|
| 118 | * [SH] Nothing should be saved if no password is set, because the |
---|
| 119 | * password is not set if it was wrong, or if one is not identified |
---|
| 120 | * yet. This means that a malicious user could easily overwrite |
---|
| 121 | * files owned by someone else: |
---|
| 122 | * a Bad Thing, methinks |
---|
| 123 | \*/ |
---|
| 124 | |
---|
| 125 | /* [WVG] No? Really? */ |
---|
| 126 | |
---|
| 127 | /*\ |
---|
| 128 | * [SH] Okay, okay, it wasn't really Wilmer who said that, it was |
---|
| 129 | * me. I just thought it was funny. |
---|
| 130 | \*/ |
---|
| 131 | |
---|
[b73ac9c] | 132 | hash = hashpass( irc->password ); |
---|
[1ee6c18] | 133 | if( hash == NULL ) |
---|
| 134 | { |
---|
| 135 | irc_usermsg( irc, "Please register yourself if you want to save your settings." ); |
---|
[b73ac9c] | 136 | return STORAGE_OTHER_ERROR; |
---|
[1ee6c18] | 137 | } |
---|
| 138 | |
---|
| 139 | g_snprintf( path, 511, "%s%s%s", global.conf->configdir, irc->nick, ".nicks~" ); |
---|
| 140 | fp = fopen( path, "w" ); |
---|
[a1f17d4] | 141 | if( !fp ) return STORAGE_OTHER_ERROR; |
---|
[1ee6c18] | 142 | for( n = irc->nicks; n; n = n->next ) |
---|
| 143 | { |
---|
| 144 | strcpy( s, n->handle ); |
---|
| 145 | s[169] = 0; /* Prevent any overflow (169 ~ 512 / 3) */ |
---|
| 146 | http_encode( s ); |
---|
| 147 | g_snprintf( s + strlen( s ), 510 - strlen( s ), " %d %s", n->proto, n->nick ); |
---|
| 148 | if( fprintf( fp, "%s\n", s ) != strlen( s ) + 1 ) |
---|
| 149 | { |
---|
| 150 | irc_usermsg( irc, "fprintf() wrote too little. Disk full?" ); |
---|
| 151 | fclose( fp ); |
---|
[a1f17d4] | 152 | return STORAGE_OTHER_ERROR; |
---|
[1ee6c18] | 153 | } |
---|
| 154 | } |
---|
| 155 | if( fclose( fp ) != 0 ) |
---|
| 156 | { |
---|
| 157 | irc_usermsg( irc, "fclose() reported an error. Disk full?" ); |
---|
[a1f17d4] | 158 | return STORAGE_OTHER_ERROR; |
---|
[1ee6c18] | 159 | } |
---|
| 160 | |
---|
| 161 | g_snprintf( new_path, 512, "%s%s%s", global.conf->configdir, irc->nick, ".nicks" ); |
---|
| 162 | if( unlink( new_path ) != 0 ) |
---|
| 163 | { |
---|
| 164 | if( errno != ENOENT ) |
---|
| 165 | { |
---|
| 166 | irc_usermsg( irc, "Error while removing old .nicks file" ); |
---|
[a1f17d4] | 167 | return STORAGE_OTHER_ERROR; |
---|
[1ee6c18] | 168 | } |
---|
| 169 | } |
---|
| 170 | if( rename( path, new_path ) != 0 ) |
---|
| 171 | { |
---|
| 172 | irc_usermsg( irc, "Error while renaming new .nicks file" ); |
---|
[a1f17d4] | 173 | return STORAGE_OTHER_ERROR; |
---|
[1ee6c18] | 174 | } |
---|
| 175 | |
---|
| 176 | g_snprintf( path, 511, "%s%s%s", global.conf->configdir, irc->nick, ".accounts~" ); |
---|
| 177 | fp = fopen( path, "w" ); |
---|
[a1f17d4] | 178 | if( !fp ) return STORAGE_OTHER_ERROR; |
---|
[1ee6c18] | 179 | if( fprintf( fp, "%s", hash ) != strlen( hash ) ) |
---|
| 180 | { |
---|
| 181 | irc_usermsg( irc, "fprintf() wrote too little. Disk full?" ); |
---|
| 182 | fclose( fp ); |
---|
[a1f17d4] | 183 | return STORAGE_OTHER_ERROR; |
---|
[1ee6c18] | 184 | } |
---|
| 185 | g_free( hash ); |
---|
| 186 | |
---|
| 187 | for( a = irc->accounts; a; a = a->next ) |
---|
| 188 | { |
---|
| 189 | if( a->protocol == PROTO_OSCAR || a->protocol == PROTO_ICQ || a->protocol == PROTO_TOC ) |
---|
| 190 | g_snprintf( s, sizeof( s ), "account add oscar \"%s\" \"%s\" %s", a->user, a->pass, a->server ); |
---|
| 191 | else |
---|
| 192 | g_snprintf( s, sizeof( s ), "account add %s \"%s\" \"%s\" \"%s\"", |
---|
| 193 | proto_name[a->protocol], a->user, a->pass, a->server ? a->server : "" ); |
---|
| 194 | |
---|
[b73ac9c] | 195 | line = obfucrypt( s, irc->password ); |
---|
[1ee6c18] | 196 | if( *line ) |
---|
| 197 | { |
---|
| 198 | if( fprintf( fp, "%s\n", line ) != strlen( line ) + 1 ) |
---|
| 199 | { |
---|
| 200 | irc_usermsg( irc, "fprintf() wrote too little. Disk full?" ); |
---|
| 201 | fclose( fp ); |
---|
[a1f17d4] | 202 | return STORAGE_OTHER_ERROR; |
---|
[1ee6c18] | 203 | } |
---|
| 204 | } |
---|
| 205 | g_free( line ); |
---|
| 206 | } |
---|
| 207 | |
---|
| 208 | for( set = irc->set; set; set = set->next ) |
---|
| 209 | { |
---|
| 210 | if( set->value && set->def ) |
---|
| 211 | { |
---|
| 212 | g_snprintf( s, sizeof( s ), "set %s \"%s\"", set->key, set->value ); |
---|
[b73ac9c] | 213 | line = obfucrypt( s, irc->password ); |
---|
[1ee6c18] | 214 | if( *line ) |
---|
| 215 | { |
---|
| 216 | if( fprintf( fp, "%s\n", line ) != strlen( line ) + 1 ) |
---|
| 217 | { |
---|
| 218 | irc_usermsg( irc, "fprintf() wrote too little. Disk full?" ); |
---|
| 219 | fclose( fp ); |
---|
[a1f17d4] | 220 | return STORAGE_OTHER_ERROR; |
---|
[1ee6c18] | 221 | } |
---|
| 222 | } |
---|
| 223 | g_free( line ); |
---|
| 224 | } |
---|
| 225 | } |
---|
| 226 | |
---|
| 227 | if( strcmp( irc->mynick, ROOT_NICK ) != 0 ) |
---|
| 228 | { |
---|
| 229 | g_snprintf( s, sizeof( s ), "rename %s %s", ROOT_NICK, irc->mynick ); |
---|
[b73ac9c] | 230 | line = obfucrypt( s, irc->password ); |
---|
[1ee6c18] | 231 | if( *line ) |
---|
| 232 | { |
---|
| 233 | if( fprintf( fp, "%s\n", line ) != strlen( line ) + 1 ) |
---|
| 234 | { |
---|
| 235 | irc_usermsg( irc, "fprintf() wrote too little. Disk full?" ); |
---|
| 236 | fclose( fp ); |
---|
[a1f17d4] | 237 | return STORAGE_OTHER_ERROR; |
---|
[1ee6c18] | 238 | } |
---|
| 239 | } |
---|
| 240 | g_free( line ); |
---|
| 241 | } |
---|
| 242 | if( fclose( fp ) != 0 ) |
---|
| 243 | { |
---|
| 244 | irc_usermsg( irc, "fclose() reported an error. Disk full?" ); |
---|
[a1f17d4] | 245 | return STORAGE_OTHER_ERROR; |
---|
[1ee6c18] | 246 | } |
---|
| 247 | |
---|
| 248 | g_snprintf( new_path, 512, "%s%s%s", global.conf->configdir, irc->nick, ".accounts" ); |
---|
| 249 | if( unlink( new_path ) != 0 ) |
---|
| 250 | { |
---|
| 251 | if( errno != ENOENT ) |
---|
| 252 | { |
---|
| 253 | irc_usermsg( irc, "Error while removing old .accounts file" ); |
---|
[a1f17d4] | 254 | return STORAGE_OTHER_ERROR; |
---|
[1ee6c18] | 255 | } |
---|
| 256 | } |
---|
| 257 | if( rename( path, new_path ) != 0 ) |
---|
| 258 | { |
---|
| 259 | irc_usermsg( irc, "Error while renaming new .accounts file" ); |
---|
[a1f17d4] | 260 | return STORAGE_OTHER_ERROR; |
---|
[1ee6c18] | 261 | } |
---|
| 262 | |
---|
| 263 | umask( ou ); |
---|
| 264 | |
---|
[a1f17d4] | 265 | return STORAGE_OK; |
---|
[1ee6c18] | 266 | } |
---|
| 267 | |
---|
[7989fcf3] | 268 | static storage_status_t text_check_pass( const char *nick, const char *password ) |
---|
[1ee6c18] | 269 | { |
---|
| 270 | char s[512]; |
---|
| 271 | FILE *fp; |
---|
| 272 | |
---|
[b73ac9c] | 273 | g_snprintf( s, 511, "%s%s%s", global.conf->configdir, nick, ".accounts" ); |
---|
[1ee6c18] | 274 | fp = fopen( s, "r" ); |
---|
[a1f17d4] | 275 | if (!fp) |
---|
| 276 | return STORAGE_NO_SUCH_USER; |
---|
[1ee6c18] | 277 | |
---|
| 278 | fscanf( fp, "%32[^\n]s", s ); |
---|
| 279 | fclose( fp ); |
---|
| 280 | |
---|
[7cad7b4] | 281 | if (checkpass( password, s) == -1) |
---|
| 282 | return STORAGE_INVALID_PASSWORD; |
---|
[a1f17d4] | 283 | |
---|
[7989fcf3] | 284 | return STORAGE_OK; |
---|
| 285 | } |
---|
| 286 | |
---|
| 287 | static storage_status_t text_remove( const char *nick, const char *password ) |
---|
| 288 | { |
---|
| 289 | char s[512]; |
---|
| 290 | storage_status_t status; |
---|
| 291 | |
---|
| 292 | status = text_check_pass( nick, password ); |
---|
| 293 | if (status != STORAGE_OK) |
---|
| 294 | return status; |
---|
| 295 | |
---|
[a1f17d4] | 296 | g_snprintf( s, 511, "%s%s%s", global.conf->configdir, nick, ".accounts" ); |
---|
| 297 | if (unlink( s ) == -1) |
---|
| 298 | return STORAGE_OTHER_ERROR; |
---|
| 299 | |
---|
| 300 | g_snprintf( s, 511, "%s%s%s", global.conf->configdir, nick, ".nicks" ); |
---|
| 301 | if (unlink( s ) == -1) |
---|
| 302 | return STORAGE_OTHER_ERROR; |
---|
| 303 | |
---|
| 304 | return STORAGE_OK; |
---|
[1ee6c18] | 305 | } |
---|
| 306 | |
---|
| 307 | storage_t storage_text = { |
---|
| 308 | .name = "text", |
---|
| 309 | .init = text_init, |
---|
[7989fcf3] | 310 | .check_pass = text_check_pass, |
---|
[1ee6c18] | 311 | .remove = text_remove, |
---|
| 312 | .load = text_load, |
---|
| 313 | .save = text_save |
---|
| 314 | }; |
---|