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