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 | /* DO NOT USE THIS FUNCTION IN NEW CODE. This |
---|
31 | * function is here merely because the save/load code still uses |
---|
32 | * ids rather than names */ |
---|
33 | static struct prpl *find_protocol_by_id(int id) |
---|
34 | { |
---|
35 | switch (id) { |
---|
36 | case 0: case 1: case 3: return find_protocol("oscar"); |
---|
37 | case 4: return find_protocol("msn"); |
---|
38 | case 2: return find_protocol("yahoo"); |
---|
39 | case 8: return find_protocol("jabber"); |
---|
40 | default: break; |
---|
41 | } |
---|
42 | return NULL; |
---|
43 | } |
---|
44 | |
---|
45 | static int find_protocol_id(const char *name) |
---|
46 | { |
---|
47 | if (!strcmp(name, "oscar")) return 1; |
---|
48 | if (!strcmp(name, "msn")) return 4; |
---|
49 | if (!strcmp(name, "yahoo")) return 2; |
---|
50 | if (!strcmp(name, "jabber")) return 8; |
---|
51 | |
---|
52 | return -1; |
---|
53 | } |
---|
54 | |
---|
55 | |
---|
56 | static void text_init (void) |
---|
57 | { |
---|
58 | if( access( global.conf->configdir, F_OK ) != 0 ) |
---|
59 | log_message( LOGLVL_WARNING, "The configuration directory %s does not exist. Configuration won't be saved.", CONFIG ); |
---|
60 | else if( access( global.conf->configdir, R_OK ) != 0 || access( global.conf->configdir, W_OK ) != 0 ) |
---|
61 | log_message( LOGLVL_WARNING, "Permission problem: Can't read/write from/to %s.", global.conf->configdir ); |
---|
62 | } |
---|
63 | |
---|
64 | static storage_status_t text_load ( const char *my_nick, const char* password, irc_t *irc ) |
---|
65 | { |
---|
66 | char s[512]; |
---|
67 | char *line; |
---|
68 | int proto; |
---|
69 | char nick[MAX_NICK_LENGTH+1]; |
---|
70 | FILE *fp; |
---|
71 | user_t *ru = user_find( irc, ROOT_NICK ); |
---|
72 | |
---|
73 | if( irc->status >= USTATUS_IDENTIFIED ) |
---|
74 | return( 1 ); |
---|
75 | |
---|
76 | g_snprintf( s, 511, "%s%s%s", global.conf->configdir, my_nick, ".accounts" ); |
---|
77 | fp = fopen( s, "r" ); |
---|
78 | if( !fp ) return STORAGE_NO_SUCH_USER; |
---|
79 | |
---|
80 | fscanf( fp, "%32[^\n]s", s ); |
---|
81 | |
---|
82 | if (checkpass (password, s) != 0) |
---|
83 | { |
---|
84 | fclose( fp ); |
---|
85 | return STORAGE_INVALID_PASSWORD; |
---|
86 | } |
---|
87 | |
---|
88 | /* Do this now. If the user runs with AuthMode = Registered, the |
---|
89 | account command will not work otherwise. */ |
---|
90 | irc->status = USTATUS_IDENTIFIED; |
---|
91 | |
---|
92 | while( fscanf( fp, "%511[^\n]s", s ) > 0 ) |
---|
93 | { |
---|
94 | fgetc( fp ); |
---|
95 | line = deobfucrypt( s, password ); |
---|
96 | if (line == NULL) return STORAGE_OTHER_ERROR; |
---|
97 | root_command_string( irc, ru, line, 0 ); |
---|
98 | g_free( line ); |
---|
99 | } |
---|
100 | fclose( fp ); |
---|
101 | |
---|
102 | g_snprintf( s, 511, "%s%s%s", global.conf->configdir, my_nick, ".nicks" ); |
---|
103 | fp = fopen( s, "r" ); |
---|
104 | if( !fp ) return STORAGE_NO_SUCH_USER; |
---|
105 | while( fscanf( fp, "%s %d %s", s, &proto, nick ) > 0 ) |
---|
106 | { |
---|
107 | struct prpl *prpl; |
---|
108 | |
---|
109 | prpl = find_protocol_by_id(proto); |
---|
110 | |
---|
111 | if (!prpl) |
---|
112 | continue; |
---|
113 | |
---|
114 | http_decode( s ); |
---|
115 | nick_set( irc, s, prpl, nick ); |
---|
116 | } |
---|
117 | fclose( fp ); |
---|
118 | |
---|
119 | if( set_getint( irc, "auto_connect" ) ) |
---|
120 | { |
---|
121 | strcpy( s, "account on" ); /* Can't do this directly because r_c_s alters the string */ |
---|
122 | root_command_string( irc, ru, s, 0 ); |
---|
123 | } |
---|
124 | |
---|
125 | return STORAGE_OK; |
---|
126 | } |
---|
127 | |
---|
128 | static storage_status_t text_save( irc_t *irc, int overwrite ) |
---|
129 | { |
---|
130 | char s[512]; |
---|
131 | char path[512], new_path[512]; |
---|
132 | char *line; |
---|
133 | nick_t *n; |
---|
134 | set_t *set; |
---|
135 | mode_t ou = umask( 0077 ); |
---|
136 | account_t *a; |
---|
137 | FILE *fp; |
---|
138 | char *hash; |
---|
139 | |
---|
140 | if (!overwrite) { |
---|
141 | g_snprintf( path, 511, "%s%s%s", global.conf->configdir, irc->nick, ".accounts" ); |
---|
142 | if (access( path, F_OK ) != -1) |
---|
143 | return STORAGE_ALREADY_EXISTS; |
---|
144 | |
---|
145 | g_snprintf( path, 511, "%s%s%s", global.conf->configdir, irc->nick, ".nicks" ); |
---|
146 | if (access( path, F_OK ) != -1) |
---|
147 | return STORAGE_ALREADY_EXISTS; |
---|
148 | } |
---|
149 | |
---|
150 | /*\ |
---|
151 | * [SH] Nothing should be saved if no password is set, because the |
---|
152 | * password is not set if it was wrong, or if one is not identified |
---|
153 | * yet. This means that a malicious user could easily overwrite |
---|
154 | * files owned by someone else: |
---|
155 | * a Bad Thing, methinks |
---|
156 | \*/ |
---|
157 | |
---|
158 | /* [WVG] No? Really? */ |
---|
159 | |
---|
160 | /*\ |
---|
161 | * [SH] Okay, okay, it wasn't really Wilmer who said that, it was |
---|
162 | * me. I just thought it was funny. |
---|
163 | \*/ |
---|
164 | |
---|
165 | hash = hashpass( irc->password ); |
---|
166 | if( hash == NULL ) |
---|
167 | { |
---|
168 | irc_usermsg( irc, "Please register yourself if you want to save your settings." ); |
---|
169 | return STORAGE_OTHER_ERROR; |
---|
170 | } |
---|
171 | |
---|
172 | g_snprintf( path, 511, "%s%s%s", global.conf->configdir, irc->nick, ".nicks~" ); |
---|
173 | fp = fopen( path, "w" ); |
---|
174 | if( !fp ) return STORAGE_OTHER_ERROR; |
---|
175 | for( n = irc->nicks; n; n = n->next ) |
---|
176 | { |
---|
177 | strcpy( s, n->handle ); |
---|
178 | s[169] = 0; /* Prevent any overflow (169 ~ 512 / 3) */ |
---|
179 | http_encode( s ); |
---|
180 | g_snprintf( s + strlen( s ), 510 - strlen( s ), " %d %s", find_protocol_id(n->proto->name), n->nick ); |
---|
181 | if( fprintf( fp, "%s\n", s ) != strlen( s ) + 1 ) |
---|
182 | { |
---|
183 | irc_usermsg( irc, "fprintf() wrote too little. Disk full?" ); |
---|
184 | fclose( fp ); |
---|
185 | return STORAGE_OTHER_ERROR; |
---|
186 | } |
---|
187 | } |
---|
188 | if( fclose( fp ) != 0 ) |
---|
189 | { |
---|
190 | irc_usermsg( irc, "fclose() reported an error. Disk full?" ); |
---|
191 | return STORAGE_OTHER_ERROR; |
---|
192 | } |
---|
193 | |
---|
194 | g_snprintf( new_path, 512, "%s%s%s", global.conf->configdir, irc->nick, ".nicks" ); |
---|
195 | if( unlink( new_path ) != 0 ) |
---|
196 | { |
---|
197 | if( errno != ENOENT ) |
---|
198 | { |
---|
199 | irc_usermsg( irc, "Error while removing old .nicks file" ); |
---|
200 | return STORAGE_OTHER_ERROR; |
---|
201 | } |
---|
202 | } |
---|
203 | if( rename( path, new_path ) != 0 ) |
---|
204 | { |
---|
205 | irc_usermsg( irc, "Error while renaming new .nicks file" ); |
---|
206 | return STORAGE_OTHER_ERROR; |
---|
207 | } |
---|
208 | |
---|
209 | g_snprintf( path, 511, "%s%s%s", global.conf->configdir, irc->nick, ".accounts~" ); |
---|
210 | fp = fopen( path, "w" ); |
---|
211 | if( !fp ) return STORAGE_OTHER_ERROR; |
---|
212 | if( fprintf( fp, "%s", hash ) != strlen( hash ) ) |
---|
213 | { |
---|
214 | irc_usermsg( irc, "fprintf() wrote too little. Disk full?" ); |
---|
215 | fclose( fp ); |
---|
216 | return STORAGE_OTHER_ERROR; |
---|
217 | } |
---|
218 | g_free( hash ); |
---|
219 | |
---|
220 | for( a = irc->accounts; a; a = a->next ) |
---|
221 | { |
---|
222 | if( !strcmp(a->prpl->name, "oscar") ) |
---|
223 | g_snprintf( s, sizeof( s ), "account add oscar \"%s\" \"%s\" %s", a->user, a->pass, a->server ); |
---|
224 | else |
---|
225 | g_snprintf( s, sizeof( s ), "account add %s \"%s\" \"%s\" \"%s\"", |
---|
226 | a->prpl->name, a->user, a->pass, a->server ? a->server : "" ); |
---|
227 | |
---|
228 | line = obfucrypt( s, irc->password ); |
---|
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 ); |
---|
235 | return STORAGE_OTHER_ERROR; |
---|
236 | } |
---|
237 | } |
---|
238 | g_free( line ); |
---|
239 | } |
---|
240 | |
---|
241 | for( set = irc->set; set; set = set->next ) |
---|
242 | { |
---|
243 | if( set->value && set->def ) |
---|
244 | { |
---|
245 | g_snprintf( s, sizeof( s ), "set %s \"%s\"", set->key, set->value ); |
---|
246 | line = obfucrypt( s, irc->password ); |
---|
247 | if( *line ) |
---|
248 | { |
---|
249 | if( fprintf( fp, "%s\n", line ) != strlen( line ) + 1 ) |
---|
250 | { |
---|
251 | irc_usermsg( irc, "fprintf() wrote too little. Disk full?" ); |
---|
252 | fclose( fp ); |
---|
253 | return STORAGE_OTHER_ERROR; |
---|
254 | } |
---|
255 | } |
---|
256 | g_free( line ); |
---|
257 | } |
---|
258 | } |
---|
259 | |
---|
260 | if( strcmp( irc->mynick, ROOT_NICK ) != 0 ) |
---|
261 | { |
---|
262 | g_snprintf( s, sizeof( s ), "rename %s %s", ROOT_NICK, irc->mynick ); |
---|
263 | line = obfucrypt( s, irc->password ); |
---|
264 | if( *line ) |
---|
265 | { |
---|
266 | if( fprintf( fp, "%s\n", line ) != strlen( line ) + 1 ) |
---|
267 | { |
---|
268 | irc_usermsg( irc, "fprintf() wrote too little. Disk full?" ); |
---|
269 | fclose( fp ); |
---|
270 | return STORAGE_OTHER_ERROR; |
---|
271 | } |
---|
272 | } |
---|
273 | g_free( line ); |
---|
274 | } |
---|
275 | if( fclose( fp ) != 0 ) |
---|
276 | { |
---|
277 | irc_usermsg( irc, "fclose() reported an error. Disk full?" ); |
---|
278 | return STORAGE_OTHER_ERROR; |
---|
279 | } |
---|
280 | |
---|
281 | g_snprintf( new_path, 512, "%s%s%s", global.conf->configdir, irc->nick, ".accounts" ); |
---|
282 | if( unlink( new_path ) != 0 ) |
---|
283 | { |
---|
284 | if( errno != ENOENT ) |
---|
285 | { |
---|
286 | irc_usermsg( irc, "Error while removing old .accounts file" ); |
---|
287 | return STORAGE_OTHER_ERROR; |
---|
288 | } |
---|
289 | } |
---|
290 | if( rename( path, new_path ) != 0 ) |
---|
291 | { |
---|
292 | irc_usermsg( irc, "Error while renaming new .accounts file" ); |
---|
293 | return STORAGE_OTHER_ERROR; |
---|
294 | } |
---|
295 | |
---|
296 | umask( ou ); |
---|
297 | |
---|
298 | return STORAGE_OK; |
---|
299 | } |
---|
300 | |
---|
301 | static storage_status_t text_check_pass( const char *nick, const char *password ) |
---|
302 | { |
---|
303 | char s[512]; |
---|
304 | FILE *fp; |
---|
305 | |
---|
306 | g_snprintf( s, 511, "%s%s%s", global.conf->configdir, nick, ".accounts" ); |
---|
307 | fp = fopen( s, "r" ); |
---|
308 | if (!fp) |
---|
309 | return STORAGE_NO_SUCH_USER; |
---|
310 | |
---|
311 | fscanf( fp, "%32[^\n]s", s ); |
---|
312 | fclose( fp ); |
---|
313 | |
---|
314 | if (checkpass( password, s) == -1) |
---|
315 | return STORAGE_INVALID_PASSWORD; |
---|
316 | |
---|
317 | return STORAGE_OK; |
---|
318 | } |
---|
319 | |
---|
320 | static storage_status_t text_remove( const char *nick, const char *password ) |
---|
321 | { |
---|
322 | char s[512]; |
---|
323 | storage_status_t status; |
---|
324 | |
---|
325 | status = text_check_pass( nick, password ); |
---|
326 | if (status != STORAGE_OK) |
---|
327 | return status; |
---|
328 | |
---|
329 | g_snprintf( s, 511, "%s%s%s", global.conf->configdir, nick, ".accounts" ); |
---|
330 | if (unlink( s ) == -1) |
---|
331 | return STORAGE_OTHER_ERROR; |
---|
332 | |
---|
333 | g_snprintf( s, 511, "%s%s%s", global.conf->configdir, nick, ".nicks" ); |
---|
334 | if (unlink( s ) == -1) |
---|
335 | return STORAGE_OTHER_ERROR; |
---|
336 | |
---|
337 | return STORAGE_OK; |
---|
338 | } |
---|
339 | |
---|
340 | storage_t storage_text = { |
---|
341 | .name = "text", |
---|
342 | .init = text_init, |
---|
343 | .check_pass = text_check_pass, |
---|
344 | .remove = text_remove, |
---|
345 | .load = text_load, |
---|
346 | .save = text_save |
---|
347 | }; |
---|