1 | /********************************************************************\ |
---|
2 | * BitlBee -- An IRC to other IM-networks gateway * |
---|
3 | * * |
---|
4 | * Copyright 2002-2010 Wilmer van der Gaast and others * |
---|
5 | \********************************************************************/ |
---|
6 | |
---|
7 | /* IPC - communication between BitlBee processes */ |
---|
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 "ipc.h" |
---|
29 | #include "commands.h" |
---|
30 | #ifndef _WIN32 |
---|
31 | #include <sys/uio.h> |
---|
32 | #include <sys/un.h> |
---|
33 | #endif |
---|
34 | |
---|
35 | GSList *child_list = NULL; |
---|
36 | static int ipc_child_recv_fd = -1; |
---|
37 | |
---|
38 | static void ipc_master_takeover_fail( struct bitlbee_child *child, gboolean both ); |
---|
39 | static gboolean ipc_send_fd( int fd, int send_fd ); |
---|
40 | |
---|
41 | static void ipc_master_cmd_client( irc_t *data, char **cmd ) |
---|
42 | { |
---|
43 | /* Normally data points at an irc_t block, but for the IPC master |
---|
44 | this is different. We think this scary cast is better than |
---|
45 | creating a new command_t structure, just to make the compiler |
---|
46 | happy. */ |
---|
47 | struct bitlbee_child *child = (void*) data; |
---|
48 | |
---|
49 | if( child && cmd[1] ) |
---|
50 | { |
---|
51 | child->host = g_strdup( cmd[1] ); |
---|
52 | child->nick = g_strdup( cmd[2] ); |
---|
53 | child->realname = g_strdup( cmd[3] ); |
---|
54 | } |
---|
55 | |
---|
56 | /* CLIENT == On initial connects, HELLO is after /RESTARTs. */ |
---|
57 | if( g_strcasecmp( cmd[0], "CLIENT" ) == 0 ) |
---|
58 | ipc_to_children_str( "OPERMSG :Client connecting (PID=%d): %s@%s (%s)\r\n", |
---|
59 | (int) ( child ? child->pid : -1 ), cmd[2], cmd[1], cmd[3] ); |
---|
60 | } |
---|
61 | |
---|
62 | static void ipc_master_cmd_nick( irc_t *data, char **cmd ) |
---|
63 | { |
---|
64 | struct bitlbee_child *child = (void*) data; |
---|
65 | |
---|
66 | if( child && cmd[1] ) |
---|
67 | { |
---|
68 | g_free( child->nick ); |
---|
69 | child->nick = g_strdup( cmd[1] ); |
---|
70 | } |
---|
71 | } |
---|
72 | |
---|
73 | static void ipc_master_cmd_die( irc_t *data, char **cmd ) |
---|
74 | { |
---|
75 | if( global.conf->runmode == RUNMODE_FORKDAEMON ) |
---|
76 | ipc_to_children_str( "DIE\r\n" ); |
---|
77 | |
---|
78 | bitlbee_shutdown( NULL, -1, 0 ); |
---|
79 | } |
---|
80 | |
---|
81 | static void ipc_master_cmd_deaf( irc_t *data, char **cmd ) |
---|
82 | { |
---|
83 | if( global.conf->runmode == RUNMODE_DAEMON ) |
---|
84 | { |
---|
85 | b_event_remove( global.listen_watch_source_id ); |
---|
86 | close( global.listen_socket ); |
---|
87 | |
---|
88 | global.listen_socket = global.listen_watch_source_id = -1; |
---|
89 | |
---|
90 | ipc_to_children_str( "OPERMSG :Closed listening socket, waiting " |
---|
91 | "for all users to disconnect." ); |
---|
92 | } |
---|
93 | else |
---|
94 | { |
---|
95 | ipc_to_children_str( "OPERMSG :The DEAF command only works in " |
---|
96 | "normal daemon mode. Try DIE instead." ); |
---|
97 | } |
---|
98 | } |
---|
99 | |
---|
100 | void ipc_master_cmd_rehash( irc_t *data, char **cmd ) |
---|
101 | { |
---|
102 | runmode_t oldmode; |
---|
103 | |
---|
104 | oldmode = global.conf->runmode; |
---|
105 | |
---|
106 | g_free( global.conf ); |
---|
107 | global.conf = conf_load( 0, NULL ); |
---|
108 | |
---|
109 | if( global.conf->runmode != oldmode ) |
---|
110 | { |
---|
111 | log_message( LOGLVL_WARNING, "Can't change RunMode setting at runtime, restoring original setting" ); |
---|
112 | global.conf->runmode = oldmode; |
---|
113 | } |
---|
114 | |
---|
115 | if( global.conf->runmode == RUNMODE_FORKDAEMON ) |
---|
116 | ipc_to_children( cmd ); |
---|
117 | } |
---|
118 | |
---|
119 | void ipc_master_cmd_restart( irc_t *data, char **cmd ) |
---|
120 | { |
---|
121 | if( global.conf->runmode != RUNMODE_FORKDAEMON ) |
---|
122 | { |
---|
123 | /* Tell child that this is unsupported. */ |
---|
124 | return; |
---|
125 | } |
---|
126 | |
---|
127 | global.restart = -1; |
---|
128 | bitlbee_shutdown( NULL, -1, 0 ); |
---|
129 | } |
---|
130 | |
---|
131 | void ipc_master_cmd_identify( irc_t *data, char **cmd ) |
---|
132 | { |
---|
133 | struct bitlbee_child *child = (void*) data, *old = NULL; |
---|
134 | char *resp; |
---|
135 | GSList *l; |
---|
136 | |
---|
137 | if( !child || !child->nick || strcmp( child->nick, cmd[1] ) != 0 ) |
---|
138 | return; |
---|
139 | |
---|
140 | g_free( child->password ); |
---|
141 | child->password = g_strdup( cmd[2] ); |
---|
142 | |
---|
143 | for( l = child_list; l; l = l->next ) |
---|
144 | { |
---|
145 | old = l->data; |
---|
146 | if( child != old && |
---|
147 | old->nick && nick_cmp( old->nick, child->nick ) == 0 && |
---|
148 | old->password && strcmp( old->password, child->password ) == 0 ) |
---|
149 | break; |
---|
150 | } |
---|
151 | |
---|
152 | if( l && !child->to_child && !old->to_child ) |
---|
153 | { |
---|
154 | resp = "TAKEOVER INIT\r\n"; |
---|
155 | child->to_child = old; |
---|
156 | old->to_child = child; |
---|
157 | } |
---|
158 | else |
---|
159 | { |
---|
160 | /* Won't need the fd since we can't send it anywhere. */ |
---|
161 | closesocket( child->to_fd ); |
---|
162 | child->to_fd = -1; |
---|
163 | resp = "TAKEOVER NO\r\n"; |
---|
164 | } |
---|
165 | |
---|
166 | if( write( child->ipc_fd, resp, strlen( resp ) ) != strlen( resp ) ) |
---|
167 | ipc_master_free_one( child ); |
---|
168 | } |
---|
169 | |
---|
170 | |
---|
171 | void ipc_master_cmd_takeover( irc_t *data, char **cmd ) |
---|
172 | { |
---|
173 | struct bitlbee_child *child = (void*) data; |
---|
174 | char *fwd = NULL; |
---|
175 | |
---|
176 | /* Normal daemon mode doesn't keep these and has simplified code for |
---|
177 | takeovers. */ |
---|
178 | if( child == NULL ) |
---|
179 | return; |
---|
180 | |
---|
181 | if( child->to_child == NULL || |
---|
182 | g_slist_find( child_list, child->to_child ) == NULL ) |
---|
183 | return ipc_master_takeover_fail( child, FALSE ); |
---|
184 | |
---|
185 | if( strcmp( cmd[1], "AUTH" ) == 0 ) |
---|
186 | { |
---|
187 | /* New connection -> Master */ |
---|
188 | if( child->to_child && |
---|
189 | child->nick && child->to_child->nick && cmd[2] && |
---|
190 | child->password && child->to_child->password && cmd[3] && |
---|
191 | strcmp( child->nick, child->to_child->nick ) == 0 && |
---|
192 | strcmp( child->nick, cmd[2] ) == 0 && |
---|
193 | strcmp( child->password, child->to_child->password ) == 0 && |
---|
194 | strcmp( child->password, cmd[3] ) == 0 ) |
---|
195 | { |
---|
196 | ipc_send_fd( child->to_child->ipc_fd, child->to_fd ); |
---|
197 | |
---|
198 | fwd = irc_build_line( cmd ); |
---|
199 | if( write( child->to_child->ipc_fd, fwd, strlen( fwd ) ) != strlen( fwd ) ) |
---|
200 | ipc_master_free_one( child ); |
---|
201 | g_free( fwd ); |
---|
202 | } |
---|
203 | else |
---|
204 | return ipc_master_takeover_fail( child, TRUE ); |
---|
205 | } |
---|
206 | else if( strcmp( cmd[1], "DONE" ) == 0 || strcmp( cmd[1], "FAIL" ) == 0 ) |
---|
207 | { |
---|
208 | /* Old connection -> Master */ |
---|
209 | int fd; |
---|
210 | |
---|
211 | /* The copy was successful (or not), we don't need it anymore. */ |
---|
212 | closesocket( child->to_fd ); |
---|
213 | child->to_fd = -1; |
---|
214 | |
---|
215 | /* Pass it through to the other party, and flush all state. */ |
---|
216 | fwd = irc_build_line( cmd ); |
---|
217 | fd = child->to_child->ipc_fd; |
---|
218 | child->to_child->to_child = NULL; |
---|
219 | child->to_child = NULL; |
---|
220 | if( write( fd, fwd, strlen( fwd ) ) != strlen( fwd ) ) |
---|
221 | ipc_master_free_one( child ); |
---|
222 | g_free( fwd ); |
---|
223 | } |
---|
224 | } |
---|
225 | |
---|
226 | static const command_t ipc_master_commands[] = { |
---|
227 | { "client", 3, ipc_master_cmd_client, 0 }, |
---|
228 | { "hello", 0, ipc_master_cmd_client, 0 }, |
---|
229 | { "nick", 1, ipc_master_cmd_nick, 0 }, |
---|
230 | { "die", 0, ipc_master_cmd_die, 0 }, |
---|
231 | { "deaf", 0, ipc_master_cmd_deaf, 0 }, |
---|
232 | { "wallops", 1, NULL, IPC_CMD_TO_CHILDREN }, |
---|
233 | { "wall", 1, NULL, IPC_CMD_TO_CHILDREN }, |
---|
234 | { "opermsg", 1, NULL, IPC_CMD_TO_CHILDREN }, |
---|
235 | { "rehash", 0, ipc_master_cmd_rehash, 0 }, |
---|
236 | { "kill", 2, NULL, IPC_CMD_TO_CHILDREN }, |
---|
237 | { "restart", 0, ipc_master_cmd_restart, 0 }, |
---|
238 | { "identify", 2, ipc_master_cmd_identify, 0 }, |
---|
239 | { "takeover", 1, ipc_master_cmd_takeover, 0 }, |
---|
240 | { NULL } |
---|
241 | }; |
---|
242 | |
---|
243 | |
---|
244 | static void ipc_child_cmd_die( irc_t *irc, char **cmd ) |
---|
245 | { |
---|
246 | irc_abort( irc, 0, "Shutdown requested by operator" ); |
---|
247 | } |
---|
248 | |
---|
249 | static void ipc_child_cmd_wallops( irc_t *irc, char **cmd ) |
---|
250 | { |
---|
251 | if( !( irc->status & USTATUS_LOGGED_IN ) ) |
---|
252 | return; |
---|
253 | |
---|
254 | if( strchr( irc->umode, 'w' ) ) |
---|
255 | irc_write( irc, ":%s WALLOPS :%s", irc->root->host, cmd[1] ); |
---|
256 | } |
---|
257 | |
---|
258 | static void ipc_child_cmd_wall( irc_t *irc, char **cmd ) |
---|
259 | { |
---|
260 | if( !( irc->status & USTATUS_LOGGED_IN ) ) |
---|
261 | return; |
---|
262 | |
---|
263 | if( strchr( irc->umode, 's' ) ) |
---|
264 | irc_write( irc, ":%s NOTICE %s :%s", irc->root->host, irc->user->nick, cmd[1] ); |
---|
265 | } |
---|
266 | |
---|
267 | static void ipc_child_cmd_opermsg( irc_t *irc, char **cmd ) |
---|
268 | { |
---|
269 | if( !( irc->status & USTATUS_LOGGED_IN ) ) |
---|
270 | return; |
---|
271 | |
---|
272 | if( strchr( irc->umode, 'o' ) ) |
---|
273 | irc_write( irc, ":%s NOTICE %s :*** OperMsg *** %s", irc->root->host, irc->user->nick, cmd[1] ); |
---|
274 | } |
---|
275 | |
---|
276 | static void ipc_child_cmd_rehash( irc_t *irc, char **cmd ) |
---|
277 | { |
---|
278 | runmode_t oldmode; |
---|
279 | |
---|
280 | oldmode = global.conf->runmode; |
---|
281 | |
---|
282 | g_free( global.conf ); |
---|
283 | global.conf = conf_load( 0, NULL ); |
---|
284 | |
---|
285 | global.conf->runmode = oldmode; |
---|
286 | } |
---|
287 | |
---|
288 | static void ipc_child_cmd_kill( irc_t *irc, char **cmd ) |
---|
289 | { |
---|
290 | if( !( irc->status & USTATUS_LOGGED_IN ) ) |
---|
291 | return; |
---|
292 | |
---|
293 | if( nick_cmp( cmd[1], irc->user->nick ) != 0 ) |
---|
294 | return; /* It's not for us. */ |
---|
295 | |
---|
296 | irc_write( irc, ":%s!%s@%s KILL %s :%s", irc->root->nick, irc->root->nick, irc->root->host, irc->user->nick, cmd[2] ); |
---|
297 | irc_abort( irc, 0, "Killed by operator: %s", cmd[2] ); |
---|
298 | } |
---|
299 | |
---|
300 | static void ipc_child_cmd_hello( irc_t *irc, char **cmd ) |
---|
301 | { |
---|
302 | if( !( irc->status & USTATUS_LOGGED_IN ) ) |
---|
303 | ipc_to_master_str( "HELLO\r\n" ); |
---|
304 | else |
---|
305 | ipc_to_master_str( "HELLO %s %s :%s\r\n", irc->user->host, irc->user->nick, irc->user->fullname ); |
---|
306 | } |
---|
307 | |
---|
308 | static void ipc_child_cmd_takeover_yes( void *data ); |
---|
309 | static void ipc_child_cmd_takeover_no( void *data ); |
---|
310 | |
---|
311 | static void ipc_child_cmd_takeover( irc_t *irc, char **cmd ) |
---|
312 | { |
---|
313 | if( strcmp( cmd[1], "NO" ) == 0 ) |
---|
314 | { |
---|
315 | /* Master->New connection */ |
---|
316 | /* No takeover, finish the login. */ |
---|
317 | } |
---|
318 | else if( strcmp( cmd[1], "INIT" ) == 0 ) |
---|
319 | { |
---|
320 | /* Master->New connection */ |
---|
321 | if( !set_getbool( &irc->b->set, "allow_takeover" ) ) |
---|
322 | { |
---|
323 | ipc_child_cmd_takeover_no( irc ); |
---|
324 | return; |
---|
325 | } |
---|
326 | |
---|
327 | /* Offer to take over the old session, unless for some reason |
---|
328 | we're already logging into IM connections. */ |
---|
329 | if( irc->login_source_id != -1 ) |
---|
330 | query_add( irc, NULL, |
---|
331 | "You're already connected to this server. " |
---|
332 | "Would you like to take over this session?", |
---|
333 | ipc_child_cmd_takeover_yes, |
---|
334 | ipc_child_cmd_takeover_no, NULL, irc ); |
---|
335 | |
---|
336 | /* This one's going to connect to accounts, avoid that. */ |
---|
337 | b_event_remove( irc->login_source_id ); |
---|
338 | irc->login_source_id = -1; |
---|
339 | } |
---|
340 | else if( strcmp( cmd[1], "AUTH" ) == 0 ) |
---|
341 | { |
---|
342 | /* Master->Old connection */ |
---|
343 | if( irc->password && cmd[2] && cmd[3] && |
---|
344 | ipc_child_recv_fd != -1 && |
---|
345 | strcmp( irc->user->nick, cmd[2] ) == 0 && |
---|
346 | strcmp( irc->password, cmd[3] ) == 0 && |
---|
347 | set_getbool( &irc->b->set, "allow_takeover" ) ) |
---|
348 | { |
---|
349 | irc_switch_fd( irc, ipc_child_recv_fd ); |
---|
350 | irc_sync( irc ); |
---|
351 | irc_usermsg( irc, "You've successfully taken over your old session" ); |
---|
352 | ipc_child_recv_fd = -1; |
---|
353 | |
---|
354 | ipc_to_master_str( "TAKEOVER DONE\r\n" ); |
---|
355 | } |
---|
356 | else |
---|
357 | { |
---|
358 | ipc_to_master_str( "TAKEOVER FAIL\r\n" ); |
---|
359 | } |
---|
360 | } |
---|
361 | else if( strcmp( cmd[1], "DONE" ) == 0 ) |
---|
362 | { |
---|
363 | /* Master->New connection (now taken over by old process) */ |
---|
364 | irc_free( irc ); |
---|
365 | } |
---|
366 | else if( strcmp( cmd[1], "FAIL" ) == 0 ) |
---|
367 | { |
---|
368 | /* Master->New connection */ |
---|
369 | irc_usermsg( irc, "Could not take over old session" ); |
---|
370 | } |
---|
371 | } |
---|
372 | |
---|
373 | static void ipc_child_cmd_takeover_yes( void *data ) |
---|
374 | { |
---|
375 | irc_t *irc = data, *old = NULL; |
---|
376 | char *to_auth[] = { "TAKEOVER", "AUTH", irc->user->nick, irc->password, NULL }; |
---|
377 | |
---|
378 | /* Master->New connection */ |
---|
379 | ipc_to_master_str( "TAKEOVER AUTH %s :%s\r\n", |
---|
380 | irc->user->nick, irc->password ); |
---|
381 | |
---|
382 | if( global.conf->runmode == RUNMODE_DAEMON ) |
---|
383 | { |
---|
384 | GSList *l; |
---|
385 | |
---|
386 | for( l = irc_connection_list; l; l = l->next ) |
---|
387 | { |
---|
388 | old = l->data; |
---|
389 | |
---|
390 | if( irc != old && |
---|
391 | irc->user->nick && old->user->nick && |
---|
392 | irc->password && old->password && |
---|
393 | strcmp( irc->user->nick, old->user->nick ) == 0 && |
---|
394 | strcmp( irc->password, old->password ) == 0 ) |
---|
395 | break; |
---|
396 | } |
---|
397 | if( l == NULL ) |
---|
398 | { |
---|
399 | to_auth[1] = "FAIL"; |
---|
400 | ipc_child_cmd_takeover( irc, to_auth ); |
---|
401 | return; |
---|
402 | } |
---|
403 | } |
---|
404 | |
---|
405 | /* Drop credentials, we'll shut down soon and shouldn't overwrite |
---|
406 | any settings. */ |
---|
407 | irc_usermsg( irc, "Trying to take over existing session" ); |
---|
408 | |
---|
409 | irc_desync( irc ); |
---|
410 | |
---|
411 | if( old ) |
---|
412 | { |
---|
413 | ipc_child_recv_fd = dup( irc->fd ); |
---|
414 | ipc_child_cmd_takeover( old, to_auth ); |
---|
415 | } |
---|
416 | |
---|
417 | /* TODO: irc_setpass() should do all of this. */ |
---|
418 | irc_setpass( irc, NULL ); |
---|
419 | irc->status &= ~USTATUS_IDENTIFIED; |
---|
420 | irc_umode_set( irc, "-R", 1 ); |
---|
421 | |
---|
422 | if( old ) |
---|
423 | irc_abort( irc, FALSE, NULL ); |
---|
424 | } |
---|
425 | |
---|
426 | static void ipc_child_cmd_takeover_no( void *data ) |
---|
427 | { |
---|
428 | ipc_to_master_str( "TAKEOVER NO\r\n" ); |
---|
429 | cmd_identify_finish( data, 0, 0 ); |
---|
430 | } |
---|
431 | |
---|
432 | static const command_t ipc_child_commands[] = { |
---|
433 | { "die", 0, ipc_child_cmd_die, 0 }, |
---|
434 | { "wallops", 1, ipc_child_cmd_wallops, 0 }, |
---|
435 | { "wall", 1, ipc_child_cmd_wall, 0 }, |
---|
436 | { "opermsg", 1, ipc_child_cmd_opermsg, 0 }, |
---|
437 | { "rehash", 0, ipc_child_cmd_rehash, 0 }, |
---|
438 | { "kill", 2, ipc_child_cmd_kill, 0 }, |
---|
439 | { "hello", 0, ipc_child_cmd_hello, 0 }, |
---|
440 | { "takeover", 1, ipc_child_cmd_takeover, 0 }, |
---|
441 | { NULL } |
---|
442 | }; |
---|
443 | |
---|
444 | gboolean ipc_child_identify( irc_t *irc ) |
---|
445 | { |
---|
446 | if( global.conf->runmode == RUNMODE_FORKDAEMON ) |
---|
447 | { |
---|
448 | if( !ipc_send_fd( global.listen_socket, irc->fd ) ) |
---|
449 | ipc_child_disable(); |
---|
450 | |
---|
451 | ipc_to_master_str( "IDENTIFY %s :%s\r\n", irc->user->nick, irc->password ); |
---|
452 | |
---|
453 | return TRUE; |
---|
454 | } |
---|
455 | else if( global.conf->runmode == RUNMODE_DAEMON ) |
---|
456 | { |
---|
457 | GSList *l; |
---|
458 | irc_t *old; |
---|
459 | char *to_init[] = { "TAKEOVER", "INIT", NULL }; |
---|
460 | |
---|
461 | for( l = irc_connection_list; l; l = l->next ) |
---|
462 | { |
---|
463 | old = l->data; |
---|
464 | |
---|
465 | if( irc != old && |
---|
466 | irc->user->nick && old->user->nick && |
---|
467 | irc->password && old->password && |
---|
468 | strcmp( irc->user->nick, old->user->nick ) == 0 && |
---|
469 | strcmp( irc->password, old->password ) == 0 ) |
---|
470 | break; |
---|
471 | } |
---|
472 | if( l == NULL || |
---|
473 | !set_getbool( &irc->b->set, "allow_takeover" ) || |
---|
474 | !set_getbool( &old->b->set, "allow_takeover" ) ) |
---|
475 | return FALSE; |
---|
476 | |
---|
477 | ipc_child_cmd_takeover( irc, to_init ); |
---|
478 | |
---|
479 | return TRUE; |
---|
480 | } |
---|
481 | else |
---|
482 | return FALSE; |
---|
483 | } |
---|
484 | |
---|
485 | static void ipc_master_takeover_fail( struct bitlbee_child *child, gboolean both ) |
---|
486 | { |
---|
487 | if( child == NULL || g_slist_find( child_list, child ) == NULL ) |
---|
488 | return; |
---|
489 | |
---|
490 | if( both && child->to_child != NULL ) |
---|
491 | ipc_master_takeover_fail( child->to_child, FALSE ); |
---|
492 | |
---|
493 | if( child->to_fd > -1 ) |
---|
494 | { |
---|
495 | /* Send this error only to the new connection, which can be |
---|
496 | recognised by to_fd being set. */ |
---|
497 | if( write( child->ipc_fd, "TAKEOVER FAIL\r\n", 15 ) != 15 ) |
---|
498 | { |
---|
499 | ipc_master_free_one( child ); |
---|
500 | return; |
---|
501 | } |
---|
502 | close( child->to_fd ); |
---|
503 | child->to_fd = -1; |
---|
504 | } |
---|
505 | child->to_child = NULL; |
---|
506 | } |
---|
507 | |
---|
508 | static void ipc_command_exec( void *data, char **cmd, const command_t *commands ) |
---|
509 | { |
---|
510 | int i, j; |
---|
511 | |
---|
512 | if( !cmd[0] ) |
---|
513 | return; |
---|
514 | |
---|
515 | for( i = 0; commands[i].command; i ++ ) |
---|
516 | if( g_strcasecmp( commands[i].command, cmd[0] ) == 0 ) |
---|
517 | { |
---|
518 | /* There is no typo in this line: */ |
---|
519 | for( j = 1; cmd[j]; j ++ ); j --; |
---|
520 | |
---|
521 | if( j < commands[i].required_parameters ) |
---|
522 | break; |
---|
523 | |
---|
524 | if( commands[i].flags & IPC_CMD_TO_CHILDREN ) |
---|
525 | ipc_to_children( cmd ); |
---|
526 | else |
---|
527 | commands[i].execute( data, cmd ); |
---|
528 | |
---|
529 | break; |
---|
530 | } |
---|
531 | } |
---|
532 | |
---|
533 | /* Return just one line. Returns NULL if something broke, an empty string |
---|
534 | on temporary "errors" (EAGAIN and friends). */ |
---|
535 | static char *ipc_readline( int fd, int *recv_fd ) |
---|
536 | { |
---|
537 | struct msghdr msg; |
---|
538 | struct iovec iov; |
---|
539 | char ccmsg[CMSG_SPACE(sizeof(recv_fd))]; |
---|
540 | struct cmsghdr *cmsg; |
---|
541 | char buf[513], *eol; |
---|
542 | int size; |
---|
543 | |
---|
544 | /* Because this is internal communication, it should be pretty safe |
---|
545 | to just peek at the message, find its length (by searching for the |
---|
546 | end-of-line) and then just read that message. With internal |
---|
547 | sockets and limites message length, messages should always be |
---|
548 | complete. Saves us quite a lot of code and buffering. */ |
---|
549 | size = recv( fd, buf, sizeof( buf ) - 1, MSG_PEEK ); |
---|
550 | if( size == 0 || ( size < 0 && !sockerr_again() ) ) |
---|
551 | return NULL; |
---|
552 | else if( size < 0 ) /* && sockerr_again() */ |
---|
553 | return( g_strdup( "" ) ); |
---|
554 | else |
---|
555 | buf[size] = 0; |
---|
556 | |
---|
557 | if( ( eol = strstr( buf, "\r\n" ) ) == NULL ) |
---|
558 | return NULL; |
---|
559 | else |
---|
560 | size = eol - buf + 2; |
---|
561 | |
---|
562 | iov.iov_base = buf; |
---|
563 | iov.iov_len = size; |
---|
564 | |
---|
565 | memset( &msg, 0, sizeof( msg ) ); |
---|
566 | msg.msg_iov = &iov; |
---|
567 | msg.msg_iovlen = 1; |
---|
568 | msg.msg_control = ccmsg; |
---|
569 | msg.msg_controllen = sizeof( ccmsg ); |
---|
570 | |
---|
571 | if( recvmsg( fd, &msg, 0 ) != size ) |
---|
572 | return NULL; |
---|
573 | |
---|
574 | if( recv_fd ) |
---|
575 | for( cmsg = CMSG_FIRSTHDR( &msg ); cmsg; cmsg = CMSG_NXTHDR( &msg, cmsg ) ) |
---|
576 | if( cmsg->cmsg_level == SOL_SOCKET && cmsg->cmsg_type == SCM_RIGHTS ) |
---|
577 | { |
---|
578 | /* Getting more than one shouldn't happen but if it does, |
---|
579 | make sure we don't leave them around. */ |
---|
580 | if( *recv_fd != -1 ) |
---|
581 | close( *recv_fd ); |
---|
582 | |
---|
583 | *recv_fd = *(int*) CMSG_DATA( cmsg ); |
---|
584 | /* |
---|
585 | fprintf( stderr, "pid %d received fd %d\n", (int) getpid(), *recv_fd ); |
---|
586 | */ |
---|
587 | } |
---|
588 | |
---|
589 | /* |
---|
590 | fprintf( stderr, "pid %d received: %s", (int) getpid(), buf ); |
---|
591 | */ |
---|
592 | return g_strndup( buf, size - 2 ); |
---|
593 | } |
---|
594 | |
---|
595 | gboolean ipc_master_read( gpointer data, gint source, b_input_condition cond ) |
---|
596 | { |
---|
597 | struct bitlbee_child *child = data; |
---|
598 | char *buf, **cmd; |
---|
599 | |
---|
600 | if( ( buf = ipc_readline( source, &child->to_fd ) ) ) |
---|
601 | { |
---|
602 | cmd = irc_parse_line( buf ); |
---|
603 | if( cmd ) |
---|
604 | { |
---|
605 | ipc_command_exec( child, cmd, ipc_master_commands ); |
---|
606 | g_free( cmd ); |
---|
607 | } |
---|
608 | g_free( buf ); |
---|
609 | } |
---|
610 | else |
---|
611 | { |
---|
612 | ipc_master_free_fd( source ); |
---|
613 | } |
---|
614 | |
---|
615 | return TRUE; |
---|
616 | } |
---|
617 | |
---|
618 | gboolean ipc_child_read( gpointer data, gint source, b_input_condition cond ) |
---|
619 | { |
---|
620 | char *buf, **cmd; |
---|
621 | |
---|
622 | if( ( buf = ipc_readline( source, &ipc_child_recv_fd ) ) ) |
---|
623 | { |
---|
624 | cmd = irc_parse_line( buf ); |
---|
625 | if( cmd ) |
---|
626 | { |
---|
627 | ipc_command_exec( data, cmd, ipc_child_commands ); |
---|
628 | g_free( cmd ); |
---|
629 | } |
---|
630 | g_free( buf ); |
---|
631 | } |
---|
632 | else |
---|
633 | { |
---|
634 | ipc_child_disable(); |
---|
635 | } |
---|
636 | |
---|
637 | return TRUE; |
---|
638 | } |
---|
639 | |
---|
640 | void ipc_to_master( char **cmd ) |
---|
641 | { |
---|
642 | if( global.conf->runmode == RUNMODE_FORKDAEMON ) |
---|
643 | { |
---|
644 | char *s = irc_build_line( cmd ); |
---|
645 | ipc_to_master_str( "%s", s ); |
---|
646 | g_free( s ); |
---|
647 | } |
---|
648 | else if( global.conf->runmode == RUNMODE_DAEMON ) |
---|
649 | { |
---|
650 | ipc_command_exec( NULL, cmd, ipc_master_commands ); |
---|
651 | } |
---|
652 | } |
---|
653 | |
---|
654 | void ipc_to_master_str( char *format, ... ) |
---|
655 | { |
---|
656 | char *msg_buf; |
---|
657 | va_list params; |
---|
658 | |
---|
659 | va_start( params, format ); |
---|
660 | msg_buf = g_strdup_vprintf( format, params ); |
---|
661 | va_end( params ); |
---|
662 | |
---|
663 | if( strlen( msg_buf ) > 512 ) |
---|
664 | { |
---|
665 | /* Don't send it, it's too long... */ |
---|
666 | } |
---|
667 | else if( global.conf->runmode == RUNMODE_FORKDAEMON ) |
---|
668 | { |
---|
669 | if( global.listen_socket >= 0 ) |
---|
670 | if( write( global.listen_socket, msg_buf, strlen( msg_buf ) ) <= 0 ) |
---|
671 | ipc_child_disable(); |
---|
672 | } |
---|
673 | else if( global.conf->runmode == RUNMODE_DAEMON ) |
---|
674 | { |
---|
675 | char **cmd, *s; |
---|
676 | |
---|
677 | if( ( s = strchr( msg_buf, '\r' ) ) ) |
---|
678 | *s = 0; |
---|
679 | |
---|
680 | cmd = irc_parse_line( msg_buf ); |
---|
681 | ipc_command_exec( NULL, cmd, ipc_master_commands ); |
---|
682 | g_free( cmd ); |
---|
683 | } |
---|
684 | |
---|
685 | g_free( msg_buf ); |
---|
686 | } |
---|
687 | |
---|
688 | void ipc_to_children( char **cmd ) |
---|
689 | { |
---|
690 | if( global.conf->runmode == RUNMODE_FORKDAEMON ) |
---|
691 | { |
---|
692 | char *msg_buf = irc_build_line( cmd ); |
---|
693 | ipc_to_children_str( "%s", msg_buf ); |
---|
694 | g_free( msg_buf ); |
---|
695 | } |
---|
696 | else if( global.conf->runmode == RUNMODE_DAEMON ) |
---|
697 | { |
---|
698 | GSList *l; |
---|
699 | |
---|
700 | for( l = irc_connection_list; l; l = l->next ) |
---|
701 | ipc_command_exec( l->data, cmd, ipc_child_commands ); |
---|
702 | } |
---|
703 | } |
---|
704 | |
---|
705 | void ipc_to_children_str( char *format, ... ) |
---|
706 | { |
---|
707 | char *msg_buf; |
---|
708 | va_list params; |
---|
709 | |
---|
710 | va_start( params, format ); |
---|
711 | msg_buf = g_strdup_vprintf( format, params ); |
---|
712 | va_end( params ); |
---|
713 | |
---|
714 | if( strlen( msg_buf ) > 512 ) |
---|
715 | { |
---|
716 | /* Don't send it, it's too long... */ |
---|
717 | } |
---|
718 | else if( global.conf->runmode == RUNMODE_FORKDAEMON ) |
---|
719 | { |
---|
720 | int msg_len = strlen( msg_buf ); |
---|
721 | GSList *l, *next; |
---|
722 | |
---|
723 | for( l = child_list; l; l = next ) |
---|
724 | { |
---|
725 | struct bitlbee_child *c = l->data; |
---|
726 | |
---|
727 | next = l->next; |
---|
728 | if( write( c->ipc_fd, msg_buf, msg_len ) <= 0 ) |
---|
729 | ipc_master_free_one( c ); |
---|
730 | } |
---|
731 | } |
---|
732 | else if( global.conf->runmode == RUNMODE_DAEMON ) |
---|
733 | { |
---|
734 | char **cmd, *s; |
---|
735 | |
---|
736 | if( ( s = strchr( msg_buf, '\r' ) ) ) |
---|
737 | *s = 0; |
---|
738 | |
---|
739 | cmd = irc_parse_line( msg_buf ); |
---|
740 | ipc_to_children( cmd ); |
---|
741 | g_free( cmd ); |
---|
742 | } |
---|
743 | |
---|
744 | g_free( msg_buf ); |
---|
745 | } |
---|
746 | |
---|
747 | static gboolean ipc_send_fd( int fd, int send_fd ) |
---|
748 | { |
---|
749 | struct msghdr msg; |
---|
750 | struct iovec iov; |
---|
751 | char ccmsg[CMSG_SPACE(sizeof(fd))]; |
---|
752 | struct cmsghdr *cmsg; |
---|
753 | |
---|
754 | memset( &msg, 0, sizeof( msg ) ); |
---|
755 | iov.iov_base = "0x90\r\n"; /* Ja, noppes */ |
---|
756 | iov.iov_len = 6; |
---|
757 | msg.msg_iov = &iov; |
---|
758 | msg.msg_iovlen = 1; |
---|
759 | |
---|
760 | msg.msg_control = ccmsg; |
---|
761 | msg.msg_controllen = sizeof( ccmsg ); |
---|
762 | cmsg = CMSG_FIRSTHDR( &msg ); |
---|
763 | cmsg->cmsg_level = SOL_SOCKET; |
---|
764 | cmsg->cmsg_type = SCM_RIGHTS; |
---|
765 | cmsg->cmsg_len = CMSG_LEN( sizeof( send_fd ) ); |
---|
766 | *(int*)CMSG_DATA( cmsg ) = send_fd; |
---|
767 | msg.msg_controllen = cmsg->cmsg_len; |
---|
768 | |
---|
769 | return sendmsg( fd, &msg, 0 ) == 6; |
---|
770 | } |
---|
771 | |
---|
772 | void ipc_master_free_one( struct bitlbee_child *c ) |
---|
773 | { |
---|
774 | GSList *l; |
---|
775 | |
---|
776 | b_event_remove( c->ipc_inpa ); |
---|
777 | closesocket( c->ipc_fd ); |
---|
778 | |
---|
779 | if( c->to_fd != -1 ) |
---|
780 | close( c->to_fd ); |
---|
781 | |
---|
782 | g_free( c->host ); |
---|
783 | g_free( c->nick ); |
---|
784 | g_free( c->realname ); |
---|
785 | g_free( c->password ); |
---|
786 | g_free( c ); |
---|
787 | |
---|
788 | child_list = g_slist_remove( child_list, c ); |
---|
789 | |
---|
790 | /* Also, if any child has a reference to this one, remove it. */ |
---|
791 | for( l = child_list; l; l = l->next ) |
---|
792 | { |
---|
793 | struct bitlbee_child *oc = l->data; |
---|
794 | |
---|
795 | if( oc->to_child == c ) |
---|
796 | ipc_master_takeover_fail( oc, FALSE ); |
---|
797 | } |
---|
798 | } |
---|
799 | |
---|
800 | void ipc_master_free_fd( int fd ) |
---|
801 | { |
---|
802 | GSList *l; |
---|
803 | struct bitlbee_child *c; |
---|
804 | |
---|
805 | for( l = child_list; l; l = l->next ) |
---|
806 | { |
---|
807 | c = l->data; |
---|
808 | if( c->ipc_fd == fd ) |
---|
809 | { |
---|
810 | ipc_master_free_one( c ); |
---|
811 | break; |
---|
812 | } |
---|
813 | } |
---|
814 | } |
---|
815 | |
---|
816 | void ipc_master_free_all() |
---|
817 | { |
---|
818 | while( child_list ) |
---|
819 | ipc_master_free_one( child_list->data ); |
---|
820 | } |
---|
821 | |
---|
822 | void ipc_child_disable() |
---|
823 | { |
---|
824 | b_event_remove( global.listen_watch_source_id ); |
---|
825 | close( global.listen_socket ); |
---|
826 | |
---|
827 | global.listen_socket = -1; |
---|
828 | } |
---|
829 | |
---|
830 | #ifndef _WIN32 |
---|
831 | char *ipc_master_save_state() |
---|
832 | { |
---|
833 | char *fn = g_strdup( "/tmp/bee-restart.XXXXXX" ); |
---|
834 | int fd = mkstemp( fn ); |
---|
835 | GSList *l; |
---|
836 | FILE *fp; |
---|
837 | int i; |
---|
838 | |
---|
839 | if( fd == -1 ) |
---|
840 | { |
---|
841 | log_message( LOGLVL_ERROR, "Could not create temporary file: %s", strerror( errno ) ); |
---|
842 | g_free( fn ); |
---|
843 | return NULL; |
---|
844 | } |
---|
845 | |
---|
846 | /* This is more convenient now. */ |
---|
847 | fp = fdopen( fd, "w" ); |
---|
848 | |
---|
849 | for( l = child_list, i = 0; l; l = l->next ) |
---|
850 | i ++; |
---|
851 | |
---|
852 | /* Number of client processes. */ |
---|
853 | fprintf( fp, "%d\n", i ); |
---|
854 | |
---|
855 | for( l = child_list; l; l = l->next ) |
---|
856 | fprintf( fp, "%d %d\n", (int) ((struct bitlbee_child*)l->data)->pid, |
---|
857 | ((struct bitlbee_child*)l->data)->ipc_fd ); |
---|
858 | |
---|
859 | if( fclose( fp ) == 0 ) |
---|
860 | { |
---|
861 | return fn; |
---|
862 | } |
---|
863 | else |
---|
864 | { |
---|
865 | unlink( fn ); |
---|
866 | g_free( fn ); |
---|
867 | return NULL; |
---|
868 | } |
---|
869 | } |
---|
870 | |
---|
871 | |
---|
872 | static gboolean new_ipc_client( gpointer data, gint serversock, b_input_condition cond ) |
---|
873 | { |
---|
874 | struct bitlbee_child *child = g_new0( struct bitlbee_child, 1 ); |
---|
875 | |
---|
876 | child->to_fd = -1; |
---|
877 | child->ipc_fd = accept( serversock, NULL, 0 ); |
---|
878 | if( child->ipc_fd == -1 ) |
---|
879 | { |
---|
880 | log_message( LOGLVL_WARNING, "Unable to accept connection on UNIX domain socket: %s", strerror(errno) ); |
---|
881 | return TRUE; |
---|
882 | } |
---|
883 | |
---|
884 | child->ipc_inpa = b_input_add( child->ipc_fd, B_EV_IO_READ, ipc_master_read, child ); |
---|
885 | |
---|
886 | child_list = g_slist_prepend( child_list, child ); |
---|
887 | |
---|
888 | return TRUE; |
---|
889 | } |
---|
890 | |
---|
891 | int ipc_master_listen_socket() |
---|
892 | { |
---|
893 | struct sockaddr_un un_addr; |
---|
894 | int serversock; |
---|
895 | |
---|
896 | /* Clean up old socket files that were hanging around.. */ |
---|
897 | if (unlink(IPCSOCKET) == -1 && errno != ENOENT) { |
---|
898 | log_message( LOGLVL_ERROR, "Could not remove old IPC socket at %s: %s", IPCSOCKET, strerror(errno) ); |
---|
899 | return 0; |
---|
900 | } |
---|
901 | |
---|
902 | un_addr.sun_family = AF_UNIX; |
---|
903 | strcpy(un_addr.sun_path, IPCSOCKET); |
---|
904 | |
---|
905 | serversock = socket(AF_UNIX, SOCK_STREAM, PF_UNIX); |
---|
906 | |
---|
907 | if (serversock == -1) { |
---|
908 | log_message( LOGLVL_WARNING, "Unable to create UNIX socket: %s", strerror(errno) ); |
---|
909 | return 0; |
---|
910 | } |
---|
911 | |
---|
912 | if (bind(serversock, (struct sockaddr *)&un_addr, sizeof(un_addr)) == -1) { |
---|
913 | log_message( LOGLVL_WARNING, "Unable to bind UNIX socket to %s: %s", IPCSOCKET, strerror(errno) ); |
---|
914 | return 0; |
---|
915 | } |
---|
916 | |
---|
917 | if (listen(serversock, 5) == -1) { |
---|
918 | log_message( LOGLVL_WARNING, "Unable to listen on UNIX socket: %s", strerror(errno) ); |
---|
919 | return 0; |
---|
920 | } |
---|
921 | |
---|
922 | b_input_add( serversock, B_EV_IO_READ, new_ipc_client, NULL ); |
---|
923 | |
---|
924 | return 1; |
---|
925 | } |
---|
926 | #else |
---|
927 | int ipc_master_listen_socket() |
---|
928 | { |
---|
929 | /* FIXME: Open named pipe \\.\BITLBEE */ |
---|
930 | return 0; |
---|
931 | } |
---|
932 | #endif |
---|
933 | |
---|
934 | int ipc_master_load_state( char *statefile ) |
---|
935 | { |
---|
936 | struct bitlbee_child *child; |
---|
937 | FILE *fp; |
---|
938 | int i, n; |
---|
939 | |
---|
940 | if( statefile == NULL ) |
---|
941 | return 0; |
---|
942 | |
---|
943 | fp = fopen( statefile, "r" ); |
---|
944 | unlink( statefile ); /* Why do it later? :-) */ |
---|
945 | if( fp == NULL ) |
---|
946 | return 0; |
---|
947 | |
---|
948 | if( fscanf( fp, "%d", &n ) != 1 ) |
---|
949 | { |
---|
950 | log_message( LOGLVL_WARNING, "Could not import state information for child processes." ); |
---|
951 | fclose( fp ); |
---|
952 | return 0; |
---|
953 | } |
---|
954 | |
---|
955 | log_message( LOGLVL_INFO, "Importing information for %d child processes.", n ); |
---|
956 | for( i = 0; i < n; i ++ ) |
---|
957 | { |
---|
958 | child = g_new0( struct bitlbee_child, 1 ); |
---|
959 | |
---|
960 | if( fscanf( fp, "%d %d", (int *) &child->pid, &child->ipc_fd ) != 2 ) |
---|
961 | { |
---|
962 | log_message( LOGLVL_WARNING, "Unexpected end of file: Only processed %d clients.", i ); |
---|
963 | g_free( child ); |
---|
964 | fclose( fp ); |
---|
965 | return 0; |
---|
966 | } |
---|
967 | child->ipc_inpa = b_input_add( child->ipc_fd, B_EV_IO_READ, ipc_master_read, child ); |
---|
968 | child->to_fd = -1; |
---|
969 | |
---|
970 | child_list = g_slist_prepend( child_list, child ); |
---|
971 | } |
---|
972 | |
---|
973 | ipc_to_children_str( "HELLO\r\n" ); |
---|
974 | ipc_to_children_str( "OPERMSG :New BitlBee master process started (version " BITLBEE_VERSION ")\r\n" ); |
---|
975 | |
---|
976 | fclose( fp ); |
---|
977 | return 1; |
---|
978 | } |
---|