~ubuntu-branches/ubuntu/trusty/bip/trusty-proposed

« back to all changes in this revision

Viewing changes to .pc/armel.patch/src/bip.c

  • Committer: Bazaar Package Importer
  • Author(s): Pierre-Louis Bonicoli
  • Date: 2010-09-22 11:15:15 UTC
  • Revision ID: james.westby@ubuntu.com-20100922111515-e8z0kt0nki6eb96m
Tags: 0.8.6-2
* New maintainer (with Nohar's blessing).
* Add armel.patch: fix build errors on armel (Closes: #597262). 

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * $Id: bip.c,v 1.39 2005/04/21 06:58:50 nohar Exp $
 
3
 *
 
4
 * This file is part of the bip project
 
5
 * Copyright (C) 2004 2005 Arnaud Cornet and Loïc Gomez
 
6
 *
 
7
 * This program is free software; you can redistribute it and/or modify
 
8
 * it under the terms of the GNU General Public License as published by
 
9
 * the Free Software Foundation; either version 2 of the License, or
 
10
 * (at your option) any later version.
 
11
 * See the file "COPYING" for the exact licensing terms.
 
12
 */
 
13
 
 
14
#include "config.h"
 
15
#include <stdlib.h>
 
16
#include <stdio.h>
 
17
#include <unistd.h>
 
18
#include <signal.h>
 
19
#include <stdarg.h>
 
20
#include <string.h>
 
21
#include <sys/time.h>
 
22
#include <sys/resource.h>
 
23
#include <sys/types.h>
 
24
#include <sys/stat.h>
 
25
#include <fcntl.h>
 
26
#include "irc.h"
 
27
#include "conf.h"
 
28
#include "tuple.h"
 
29
#include "log.h"
 
30
#include "bip.h"
 
31
#include "line.h"
 
32
#include "version.h"
 
33
#include "defaults.h"
 
34
 
 
35
int sighup = 0;
 
36
 
 
37
char *conf_log_root;
 
38
char *conf_log_format;
 
39
int conf_log_level;
 
40
char *conf_ip;
 
41
unsigned short conf_port;
 
42
int conf_css;
 
43
#ifdef HAVE_LIBSSL
 
44
char *conf_ssl_certfile;
 
45
#endif
 
46
int conf_daemonize;
 
47
char *conf_pid_file;
 
48
char *conf_biphome;
 
49
 
 
50
/* log options, for sure the trickiest :) */
 
51
int conf_log = DEFAULT_LOG;
 
52
int conf_log_system = DEFAULT_LOG_SYSTEM;
 
53
int conf_log_sync_interval = DEFAULT_LOG_SYNC_INTERVAL;
 
54
 
 
55
list_t *parse_conf(FILE *file, int *err);
 
56
void conf_die(bip_t *bip, char *fmt, ...);
 
57
static char *get_tuple_pvalue(list_t *tuple_l, int lex);
 
58
void bip_notify(struct link_client *ic, char *fmt, ...);
 
59
void adm_list_connections(struct link_client *ic, struct user *bu);
 
60
void free_conf(list_t *l);
 
61
 
 
62
#ifdef HAVE_OIDENTD
 
63
#define OIDENTD_FILENAME ".oidentd.conf"
 
64
#endif
 
65
 
 
66
static void hash_binary(char *hex, unsigned char **password, unsigned int *seed)
 
67
{
 
68
        unsigned char *md5;
 
69
        unsigned int buf;
 
70
        int i;
 
71
 
 
72
        if (strlen(hex) != 40)
 
73
                fatal("Incorrect password format %s\n", hex);
 
74
 
 
75
        md5 = bip_malloc(20);
 
76
        for (i = 0; i < 20; i++) {
 
77
                sscanf(hex + 2 * i, "%02x", &buf);
 
78
                md5[i] = buf;
 
79
        }
 
80
 
 
81
        *seed = 0;
 
82
        sscanf(hex, "%02x", &buf);
 
83
        *seed |= buf << 24;
 
84
        sscanf(hex + 2, "%02x", &buf);
 
85
        *seed |= buf << 16;
 
86
        sscanf(hex + 2 * 2, "%02x", &buf);
 
87
        *seed |= buf << 8;
 
88
        sscanf(hex + 2 * 3, "%02x", &buf);
 
89
        *seed |= buf;
 
90
 
 
91
        *password = md5;
 
92
}
 
93
 
 
94
static int add_server(bip_t *bip, struct server *s, list_t *data)
 
95
{
 
96
        struct tuple *t;
 
97
 
 
98
        s->port = 6667; /* default port */
 
99
 
 
100
        while ((t = list_remove_first(data))) {
 
101
                switch (t->type) {
 
102
                case LEX_HOST:
 
103
                        MOVE_STRING(s->host, t->pdata);
 
104
                        break;
 
105
                case LEX_PORT:
 
106
                        s->port = t->ndata;
 
107
                        break;
 
108
                default:
 
109
                        fatal("Config error in server block (%d)", t->type);
 
110
                }
 
111
                if (t->tuple_type == TUPLE_STR && t->pdata)
 
112
                        free(t->pdata);
 
113
                free(t);
 
114
        }
 
115
        if (!s->host) {
 
116
                free(s);
 
117
                conf_die(bip, "Server conf: host not set");
 
118
                return 0;
 
119
        }
 
120
        return 1;
 
121
}
 
122
 
 
123
#define ERRBUFSZ 128
 
124
 
 
125
extern list_t *root_list;
 
126
int yyparse();
 
127
 
 
128
void conf_die(bip_t *bip, char *fmt, ...)
 
129
{
 
130
        va_list ap;
 
131
        int size = ERRBUFSZ;
 
132
        int n;
 
133
        char *error = bip_malloc(size);
 
134
 
 
135
        for (;;) {
 
136
                va_start(ap, fmt);
 
137
                n = vsnprintf(error, size, fmt, ap);
 
138
                va_end(ap);
 
139
                if (n > -1 && n < size) {
 
140
                        list_add_last(&bip->errors, error);
 
141
                        break;
 
142
                }
 
143
                if (n > -1)
 
144
                        size = n + 1;
 
145
                else
 
146
                        size *= 2;
 
147
                error = bip_realloc(error, size);
 
148
        }
 
149
        va_start(ap, fmt);
 
150
        _mylog(LOG_ERROR, fmt, ap);
 
151
        va_end(ap);
 
152
}
 
153
 
 
154
FILE *conf_global_log_file;
 
155
 
 
156
static pid_t daemonize(void)
 
157
{
 
158
        switch (fork()) {
 
159
        case -1:
 
160
                fatal("Fork failed");
 
161
                break;
 
162
        case 0:
 
163
                break;
 
164
        default:
 
165
                _exit(0);
 
166
        }
 
167
 
 
168
        if (setsid() < 0)
 
169
                fatal("setsid() failed");
 
170
 
 
171
        switch (fork()) {
 
172
        case -1:
 
173
                fatal("Fork failed");
 
174
                break;
 
175
        case 0:
 
176
                break;
 
177
        default:
 
178
                _exit(0);
 
179
        }
 
180
 
 
181
        close(0);
 
182
        close(1);
 
183
        close(2);
 
184
        /* This better be the very last action since fatal makes use of
 
185
         * conf_global_log_file */
 
186
        return getpid();
 
187
}
 
188
 
 
189
/* RACE CONDITION! */
 
190
int do_pid_stuff(void)
 
191
{
 
192
        char hname[1024];
 
193
        char longpath[1024];
 
194
        FILE *f;
 
195
        int fd;
 
196
 
 
197
try_again:
 
198
        fd = -1;
 
199
        f = fopen(conf_pid_file, "r");
 
200
        if (f)
 
201
                goto pid_is_there;
 
202
        if (gethostname(hname, 1023) == -1)
 
203
                fatal("%s %s", "gethostname", strerror(errno));
 
204
        snprintf(longpath, 1023, "%s.%s.%ld", conf_pid_file, hname,
 
205
                        (long unsigned int)getpid());
 
206
        if ((fd = open(longpath, O_CREAT|O_WRONLY, S_IWUSR|S_IRUSR)) == -1)
 
207
                fatal("Cannot write to PID file (%s) %s", longpath,
 
208
                                strerror(errno));
 
209
        if (link(longpath, conf_pid_file) ==  -1) {
 
210
                struct stat buf;
 
211
                if (stat(longpath, &buf) == -1) {
 
212
                        if (buf.st_nlink != 2) {
 
213
                                f = fopen(conf_pid_file, "r");
 
214
                                goto pid_is_there;
 
215
                        }
 
216
                }
 
217
        }
 
218
        unlink(longpath);
 
219
        return fd;
 
220
pid_is_there:
 
221
        {
 
222
                pid_t pid;
 
223
                long unsigned int p;
 
224
                if (fd != -1)
 
225
                        close(fd);
 
226
                if (f) {
 
227
                        int c = fscanf(f, "%ld", &p);
 
228
                        fclose(f);
 
229
                        pid = p;
 
230
                        if (c != 1 || p == 0) {
 
231
                                mylog(LOG_INFO, "pid file found but invalid "
 
232
                                                "data inside. Continuing...\n");
 
233
                                if (unlink(conf_pid_file)) {
 
234
                                        fatal("Cannot delete pid file '%s', "
 
235
                                                        "check permissions.\n",
 
236
                                                        conf_pid_file);
 
237
                                }
 
238
                                goto try_again;
 
239
                        }
 
240
                } else
 
241
                        pid = 0;
 
242
                int kr = kill(pid, 0);
 
243
                if (kr == -1 && (errno == ESRCH || errno == EPERM)) {
 
244
                        /* that's not bip! */
 
245
                        if (unlink(conf_pid_file)) {
 
246
                                fatal("Cannot delete pid file '%s', check "
 
247
                                                "permissions.\n",
 
248
                                                conf_pid_file);
 
249
                        }
 
250
                        goto try_again;
 
251
                }
 
252
                if (pid)
 
253
                        mylog(LOG_INFO, "pid file found (pid %ld).", pid);
 
254
                mylog(LOG_FATAL,
 
255
                                "Another instance of bip is certainly runing.");
 
256
                mylog(LOG_FATAL, "If you are sure this is not the case remove"
 
257
                                        " %s.", conf_pid_file);
 
258
                exit(2);
 
259
        }
 
260
        return 0;
 
261
}
 
262
 
 
263
#define S_CONF "bip.conf"
 
264
 
 
265
static void usage(char *name)
 
266
{
 
267
        printf(
 
268
"Usage: %s [-f config_file] [-h] [-n]\n"
 
269
"       -f config_file: Use config_file as the configuration file\n"
 
270
"               If no config file is given %s will try to open ~/.bip/" S_CONF "\n"
 
271
"       -n: Don't daemonize, log in stderr\n"
 
272
"       -v: Print version and exit\n"
 
273
"       -h: This help\n", name, name);
 
274
        exit(1);
 
275
}
 
276
 
 
277
static void version()
 
278
{
 
279
        printf(
 
280
"Bip IRC Proxy - %s\n"
 
281
"Copyright © Arnaud Cornet and Loïc Gomez (2004 - 2008)\n"
 
282
"Distributed under the GNU Public License Version 2\n", BIP_VERSION);
 
283
}
 
284
 
 
285
bip_t *_bip;
 
286
 
 
287
void reload_config(int i)
 
288
{
 
289
        (void)i;
 
290
        sighup = 1;
 
291
        _bip->reloading_client = NULL;
 
292
}
 
293
 
 
294
void rlimit_cpu_reached(int i)
 
295
{
 
296
        (void)i;
 
297
        mylog(LOG_WARN, "This process has reached the CPU time usage limit. "
 
298
                "It means bip will be killed by the Operating System soon.");
 
299
}
 
300
 
 
301
void rlimit_bigfile_reached(int i)
 
302
{
 
303
        (void)i;
 
304
        mylog(LOG_WARN, "A file has reached the max size this process is "
 
305
                "allowed to create. The file will not be written correctly, "
 
306
                "an error message should follow. This is not fatal.");
 
307
}
 
308
 
 
309
void bad_quit(int i)
 
310
{
 
311
        list_iterator_t it;
 
312
        for (list_it_init(&_bip->link_list, &it); list_it_item(&it);
 
313
                        list_it_next(&it)) {
 
314
                struct link *l = list_it_item(&it);
 
315
                struct link_server *ls = l->l_server;
 
316
                if (ls && l->s_state == IRCS_CONNECTED) {
 
317
                        write_line_fast(CONN(ls), "QUIT :Coyote finally "
 
318
                                        "caught me\r\n");
 
319
                }
 
320
        }
 
321
        unlink(conf_pid_file);
 
322
        exit(i);
 
323
}
 
324
 
 
325
static int add_network(bip_t *bip, list_t *data)
 
326
{
 
327
        struct tuple *t;
 
328
        struct network *n;
 
329
        int i;
 
330
        int r;
 
331
 
 
332
        char *name = get_tuple_pvalue(data, LEX_NAME);
 
333
 
 
334
        if (name == NULL) {
 
335
                conf_die(bip, "Network with no name");
 
336
                return 0;
 
337
        }
 
338
        n = hash_get(&bip->networks, name);
 
339
        if (n) {
 
340
                for (i = 0; i < n->serverc; i++)
 
341
                        free(n->serverv[i].host);
 
342
                free(n->serverv);
 
343
                n->serverv = NULL;
 
344
                n->serverc = 0;
 
345
        } else {
 
346
                n = bip_calloc(sizeof(struct network), 1);
 
347
                hash_insert(&bip->networks, name, n);
 
348
        }
 
349
 
 
350
        while ((t = list_remove_first(data))) {
 
351
                switch (t->type) {
 
352
                case LEX_NAME:
 
353
                        MOVE_STRING(n->name, t->pdata);
 
354
                        break;
 
355
#ifdef HAVE_LIBSSL
 
356
                case LEX_SSL:
 
357
                        n->ssl = t->ndata;
 
358
                        break;
 
359
#endif
 
360
                case LEX_SERVER:
 
361
                        n->serverv = bip_realloc(n->serverv, (n->serverc + 1)
 
362
                                                * sizeof(struct server));
 
363
                        n->serverc++;
 
364
                        memset(&n->serverv[n->serverc - 1], 0,
 
365
                                        sizeof(struct server));
 
366
                        r = add_server(bip, &n->serverv[n->serverc - 1],
 
367
                                        t->pdata);
 
368
                        if (!r) {
 
369
                                n->serverc--;
 
370
                                return 0;
 
371
                        }
 
372
                        free(t->pdata);
 
373
                        t->pdata = NULL;
 
374
                        break;
 
375
                default:
 
376
                        conf_die(bip, "unknown keyword in network statement");
 
377
                        return 0;
 
378
                        break;
 
379
                }
 
380
                if (t->tuple_type == TUPLE_STR && t->pdata)
 
381
                        free(t->pdata);
 
382
                free(t);
 
383
        }
 
384
        return 1;
 
385
}
 
386
 
 
387
void adm_bip_delconn(bip_t *bip, struct link_client *ic, const char *conn_name)
 
388
{
 
389
        struct user *user = LINK(ic)->user;
 
390
        struct link *l;
 
391
 
 
392
        if (!(l = hash_get(&user->connections, conn_name))) {
 
393
                bip_notify(ic, "cannot find this connection");
 
394
                return;
 
395
        }
 
396
 
 
397
        bip_notify(ic, "deleting");
 
398
        link_kill(bip, l);
 
399
}
 
400
 
 
401
void adm_bip_addconn(bip_t *bip, struct link_client *ic, const char *conn_name,
 
402
                const char *network_name)
 
403
{
 
404
        struct user *user = LINK(ic)->user;
 
405
        struct network *network;
 
406
 
 
407
        /* check name uniqueness */
 
408
        if (hash_get(&user->connections, conn_name)) {
 
409
                bip_notify(ic, "connection name already exists for this user.");
 
410
                return;
 
411
        }
 
412
 
 
413
        /* check we know about this network */
 
414
        network = hash_get(&bip->networks, network_name);
 
415
        if (!network) {
 
416
                bip_notify(ic, "no such network name");
 
417
                return;
 
418
        }
 
419
 
 
420
        struct link *l;
 
421
        l = irc_link_new();
 
422
        l->name = bip_strdup(conn_name);
 
423
        hash_insert(&user->connections, conn_name, l);
 
424
        list_add_last(&bip->link_list, l);
 
425
        l->user = user;
 
426
        l->network = network;
 
427
        l->log = log_new(user, conn_name);
 
428
#ifdef HAVE_LIBSSL
 
429
        l->ssl_check_mode = user->ssl_check_mode;
 
430
        l->untrusted_certs = sk_X509_new_null();
 
431
#endif
 
432
 
 
433
 
 
434
#define SCOPY(member) l->member = (LINK(ic)->member ? bip_strdup(LINK(ic)->member) : NULL)
 
435
#define ICOPY(member) l->member = LINK(ic)->member
 
436
 
 
437
        SCOPY(connect_nick);
 
438
        SCOPY(username);
 
439
        SCOPY(realname);
 
440
        /* we don't copy server password */
 
441
        SCOPY(vhost);
 
442
        ICOPY(follow_nick);
 
443
        ICOPY(ignore_first_nick);
 
444
        SCOPY(away_nick);
 
445
        SCOPY(no_client_away_msg);
 
446
        /* we don't copy on_connect_send */
 
447
#ifdef HAVE_LIBSSL
 
448
        ICOPY(ssl_check_mode);
 
449
#endif
 
450
#undef SCOPY
 
451
#undef ICOPY
 
452
        bip_notify(ic, "connection added, you should soon be able to connect");
 
453
}
 
454
 
 
455
static int add_connection(bip_t *bip, struct user *user, list_t *data)
 
456
{
 
457
        struct tuple *t, *t2;
 
458
        struct link *l;
 
459
        struct chan_info *ci;
 
460
        char *name = get_tuple_pvalue(data, LEX_NAME);
 
461
 
 
462
        if (name == NULL) {
 
463
                conf_die(bip, "Connection with no name");
 
464
                return 0;
 
465
        }
 
466
        l = hash_get(&user->connections, name);
 
467
        if (!l) {
 
468
                l = irc_link_new();
 
469
                hash_insert(&user->connections, name, l);
 
470
                list_add_last(&bip->link_list, l);
 
471
                l->user = user;
 
472
                l->log = log_new(user, name);
 
473
#ifdef HAVE_LIBSSL
 
474
                l->ssl_check_mode = user->ssl_check_mode;
 
475
                l->untrusted_certs = sk_X509_new_null();
 
476
#endif
 
477
        } else {
 
478
                l->network = NULL;
 
479
                log_reset_all(l->log);
 
480
        }
 
481
 
 
482
        while ((t = list_remove_first(data))) {
 
483
                switch (t->type) {
 
484
                case LEX_NAME:
 
485
                        MOVE_STRING(l->name, t->pdata);
 
486
                        break;
 
487
                case LEX_NETWORK:
 
488
                        l->network = hash_get(&bip->networks, t->pdata);
 
489
                        if (!l->network) {
 
490
                                conf_die(bip, "Undefined network %s.\n",
 
491
                                                t->pdata);
 
492
                                return 0;
 
493
                        }
 
494
                        break;
 
495
                case LEX_NICK:
 
496
                        if (!is_valid_nick(t->pdata)) {
 
497
                                conf_die(bip, "Invalid nickname %s.", t->pdata);
 
498
                                return 0;
 
499
                        }
 
500
                        MOVE_STRING(l->connect_nick, t->pdata);
 
501
                        break;
 
502
                case LEX_USER:
 
503
                        MOVE_STRING(l->username, t->pdata);
 
504
                        break;
 
505
                case LEX_REALNAME:
 
506
                        MOVE_STRING(l->realname, t->pdata);
 
507
                        break;
 
508
                case LEX_PASSWORD:
 
509
                        MOVE_STRING(l->s_password, t->pdata);
 
510
                        break;
 
511
                case LEX_VHOST:
 
512
                        MOVE_STRING(l->vhost, t->pdata);
 
513
                        break;
 
514
                case LEX_CHANNEL:
 
515
                        name = get_tuple_pvalue(t->pdata, LEX_NAME);
 
516
                        if (name == NULL) {
 
517
                                conf_die(bip, "Channel with no name");
 
518
                                return 0;
 
519
                        }
 
520
 
 
521
                        ci = hash_get(&l->chan_infos, name);
 
522
                        if (!ci) {
 
523
                                ci = chan_info_new();
 
524
                                hash_insert(&l->chan_infos, name, ci);
 
525
                                /* FIXME: this order is not reloaded */
 
526
                                list_add_last(&l->chan_infos_order, ci);
 
527
                                ci->backlog = 1;
 
528
                        }
 
529
 
 
530
                        while ((t2 = list_remove_first(t->pdata))) {
 
531
                                switch (t2->type) {
 
532
                                case LEX_NAME:
 
533
                                        MOVE_STRING(ci->name, t2->pdata);
 
534
                                        break;
 
535
                                case LEX_KEY:
 
536
                                        MOVE_STRING(ci->key, t2->pdata);
 
537
                                        break;
 
538
                                case LEX_BACKLOG:
 
539
                                        ci->backlog = t2->ndata;
 
540
                                        break;
 
541
                                }
 
542
                                if (t2->tuple_type == TUPLE_STR && t2->pdata)
 
543
                                        free(t2->pdata);
 
544
                                free(t2);
 
545
                        }
 
546
                        list_free(t->pdata);
 
547
                        break;
 
548
                case LEX_AUTOJOIN_ON_KICK:
 
549
                        l->autojoin_on_kick = t->ndata;
 
550
                        break;
 
551
                case LEX_FOLLOW_NICK:
 
552
                        l->follow_nick = t->ndata;
 
553
                        break;
 
554
                case LEX_IGN_FIRST_NICK:
 
555
                        l->ignore_first_nick = t->ndata;
 
556
                        break;
 
557
                case LEX_IGNORE_CAPAB:
 
558
                        l->ignore_server_capab = t->ndata;
 
559
                        break;
 
560
                case LEX_AWAY_NICK:
 
561
                        MOVE_STRING(l->away_nick, t->pdata);
 
562
                        break;
 
563
                case LEX_NO_CLIENT_AWAY_MSG:
 
564
                        MOVE_STRING(l->no_client_away_msg, t->pdata);
 
565
                        break;
 
566
                case LEX_ON_CONNECT_SEND:
 
567
                        list_add_last(&l->on_connect_send, t->pdata);
 
568
                        t->pdata = NULL;
 
569
                        break;
 
570
#ifdef HAVE_LIBSSL
 
571
                case LEX_SSL_CHECK_MODE:
 
572
                        if (strcmp(t->pdata, "basic") == 0)
 
573
                                l->ssl_check_mode = SSL_CHECK_BASIC;
 
574
                        if (strcmp(t->pdata, "ca") == 0)
 
575
                                l->ssl_check_mode = SSL_CHECK_CA;
 
576
                        if (strcmp(t->pdata, "none") == 0)
 
577
                                l->ssl_check_mode = SSL_CHECK_NONE;
 
578
                        break;
 
579
#else
 
580
                case LEX_SSL_CHECK_MODE:
 
581
                        mylog(LOG_WARN, "Found SSL option whereas bip is "
 
582
                                        "not built with SSL support.");
 
583
                        break;
 
584
#endif
 
585
                default:
 
586
                        conf_die(bip, "Unknown keyword in connection "
 
587
                                        "statement");
 
588
                        return 0;
 
589
                }
 
590
                if (t->tuple_type == TUPLE_STR && t->pdata)
 
591
                        free(t->pdata);
 
592
                free(t);
 
593
        }
 
594
        /* checks that can only be here, or must */
 
595
        if (!l->network) {
 
596
                conf_die(bip, "Missing network in connection block");
 
597
                return 0;
 
598
        }
 
599
        if (!l->connect_nick) {
 
600
                if (!user->default_nick) {
 
601
                        conf_die(bip, "No nick set and no default nick.");
 
602
                        return 0;
 
603
                }
 
604
                l->connect_nick = bip_strdup(user->default_nick);
 
605
        }
 
606
        if (!l->username) {
 
607
                if (!user->default_username) {
 
608
                        conf_die(bip, "No username set and no default "
 
609
                                        "username.");
 
610
                        return 0;
 
611
                }
 
612
                l->username = bip_strdup(user->default_username);
 
613
        }
 
614
        if (!l->realname) {
 
615
                if (!user->default_realname) {
 
616
                        conf_die(bip, "No realname set and no default "
 
617
                                        "realname.");
 
618
                        return 0;
 
619
                }
 
620
                l->realname = bip_strdup(user->default_realname);
 
621
        }
 
622
 
 
623
        l->in_use = 1;
 
624
        return 1;
 
625
}
 
626
 
 
627
static char *get_tuple_pvalue(list_t *tuple_l, int lex)
 
628
{
 
629
        struct tuple *t;
 
630
        list_iterator_t it;
 
631
 
 
632
        for (list_it_init(tuple_l, &it); (t = list_it_item(&it));
 
633
                        list_it_next(&it)) {
 
634
                if (t->type == lex)
 
635
                        return t->pdata;
 
636
        }
 
637
        return NULL;
 
638
}
 
639
 
 
640
static int get_tuple_nvalue(list_t *tuple_l, int lex)
 
641
{
 
642
        struct tuple *t;
 
643
        list_iterator_t it;
 
644
 
 
645
        for (list_it_init(tuple_l, &it); (t = list_it_item(&it));
 
646
                        list_it_next(&it)) {
 
647
                if (t->type == lex)
 
648
                        return t->ndata;
 
649
        }
 
650
        return -1;
 
651
}
 
652
 
 
653
struct historical_directives {
 
654
        int always_backlog;
 
655
        int backlog;
 
656
        int bl_msg_only;
 
657
        int backlog_lines;
 
658
        int backlog_no_timestamp;
 
659
        int blreset_on_talk;
 
660
};
 
661
 
 
662
static int add_user(bip_t *bip, list_t *data, struct historical_directives *hds)
 
663
{
 
664
        int r;
 
665
        struct tuple *t;
 
666
        struct user *u;
 
667
        char *name = get_tuple_pvalue(data, LEX_NAME);
 
668
        list_t connection_list, *cl;
 
669
 
 
670
        list_init(&connection_list, NULL);
 
671
 
 
672
        if (name == NULL) {
 
673
                conf_die(bip, "User with no name");
 
674
                return 0;
 
675
        }
 
676
        u = hash_get(&bip->users, name);
 
677
        if (!u) {
 
678
                u = bip_calloc(sizeof(struct user), 1);
 
679
                hash_insert(&bip->users, name, u);
 
680
                hash_init(&u->connections, HASH_NOCASE);
 
681
                u->admin = 0;
 
682
                u->backlog = DEFAULT_BACKLOG;
 
683
                u->always_backlog = DEFAULT_ALWAYS_BACKLOG;
 
684
                u->bl_msg_only = DEFAULT_BL_MSG_ONLY;
 
685
                u->backlog_lines = DEFAULT_BACKLOG_LINES;
 
686
                u->backlog_no_timestamp = DEFAULT_BACKLOG_NO_TIMESTAMP;
 
687
                u->blreset_on_talk = DEFAULT_BLRESET_ON_TALK;
 
688
                u->blreset_connection = DEFAULT_BLRESET_CONNECTION;
 
689
                u->bip_use_notice = DEFAULT_BIP_USE_NOTICE;
 
690
        } else {
 
691
                FREE(u->name);
 
692
                FREE(u->password);
 
693
                FREE(u->default_nick);
 
694
                FREE(u->default_username);
 
695
                FREE(u->default_realname);
 
696
#ifdef HAVE_LIBSSL
 
697
                FREE(u->ssl_check_store);
 
698
                FREE(u->ssl_client_certfile);
 
699
#endif
 
700
        }
 
701
 
 
702
        u->backlog = hds->backlog;
 
703
        u->always_backlog = hds->always_backlog;
 
704
        u->bl_msg_only = hds->bl_msg_only;
 
705
        u->backlog_lines = hds->backlog_lines;
 
706
        u->backlog_no_timestamp = hds->backlog_no_timestamp;
 
707
        u->blreset_on_talk = hds->blreset_on_talk;
 
708
 
 
709
        while ((t = list_remove_first(data))) {
 
710
                switch (t->type) {
 
711
                case LEX_NAME:
 
712
                        MOVE_STRING(u->name, t->pdata);
 
713
                        break;
 
714
                case LEX_ADMIN:
 
715
                        u->admin = t->ndata;
 
716
                        break;
 
717
                case LEX_PASSWORD:
 
718
                        hash_binary(t->pdata, &u->password, &u->seed);
 
719
                        free(t->pdata);
 
720
                        t->pdata = NULL;
 
721
                        break;
 
722
                case LEX_DEFAULT_NICK:
 
723
                        MOVE_STRING(u->default_nick, t->pdata);
 
724
                        break;
 
725
                case LEX_DEFAULT_USER:
 
726
                        MOVE_STRING(u->default_username, t->pdata);
 
727
                        break;
 
728
                case LEX_DEFAULT_REALNAME:
 
729
                        MOVE_STRING(u->default_realname, t->pdata);
 
730
                        break;
 
731
                case LEX_ALWAYS_BACKLOG:
 
732
                        u->always_backlog = t->ndata;
 
733
                        break;
 
734
                case LEX_BACKLOG:
 
735
                        u->backlog = t->ndata;
 
736
                        break;
 
737
                case LEX_BL_MSG_ONLY:
 
738
                        u->bl_msg_only = t->ndata;
 
739
                        break;
 
740
                case LEX_BACKLOG_LINES:
 
741
                        u->backlog_lines = t->ndata;
 
742
                        break;
 
743
                case LEX_BACKLOG_NO_TIMESTAMP:
 
744
                        u->backlog_no_timestamp = t->ndata;
 
745
                        break;
 
746
                case LEX_BLRESET_ON_TALK:
 
747
                        u->blreset_on_talk = t->ndata;
 
748
                        break;
 
749
                case LEX_BLRESET_CONNECTION:
 
750
                        u->blreset_connection = t->ndata;
 
751
                        break;
 
752
                case LEX_BIP_USE_NOTICE:
 
753
                        u->bip_use_notice = t->ndata;
 
754
                        break;
 
755
                case LEX_CONNECTION:
 
756
                        list_add_last(&connection_list, t->pdata);
 
757
                        t->pdata = NULL;
 
758
                        break;
 
759
#ifdef HAVE_LIBSSL
 
760
                case LEX_SSL_CHECK_MODE:
 
761
                        if (!strcmp(t->pdata, "basic"))
 
762
                                u->ssl_check_mode = SSL_CHECK_BASIC;
 
763
                        if (!strcmp(t->pdata, "ca"))
 
764
                                u->ssl_check_mode = SSL_CHECK_CA;
 
765
                        if (!strcmp(t->pdata, "none"))
 
766
                                u->ssl_check_mode = SSL_CHECK_NONE;
 
767
                        free(t->pdata);
 
768
                        t->pdata = NULL;
 
769
                        break;
 
770
                case LEX_SSL_CHECK_STORE:
 
771
                        MOVE_STRING(u->ssl_check_store, t->pdata);
 
772
                        break;
 
773
                case LEX_SSL_CLIENT_CERTFILE:
 
774
                        MOVE_STRING(u->ssl_client_certfile, t->pdata);
 
775
                        break;
 
776
#else
 
777
                case LEX_SSL_CLIENT_CERTFILE:
 
778
                case LEX_SSL_CHECK_MODE:
 
779
                case LEX_SSL_CHECK_STORE:
 
780
                        mylog(LOG_WARN, "Found SSL option whereas bip is "
 
781
                                        "not built with SSL support.");
 
782
                        break;
 
783
#endif
 
784
                default:
 
785
                        conf_die(bip, "Uknown keyword in user statement");
 
786
                        return 0;
 
787
                }
 
788
                if (t->tuple_type == TUPLE_STR && t->pdata)
 
789
                        free(t->pdata);
 
790
                free(t);
 
791
        }
 
792
        if (!u->password) {
 
793
                conf_die(bip, "Missing password in user block");
 
794
                return 0;
 
795
        }
 
796
 
 
797
        while ((cl = list_remove_first(&connection_list))) {
 
798
                r = add_connection(bip, u, cl);
 
799
                free(cl);
 
800
                if (!r)
 
801
                        return 0;
 
802
        }
 
803
 
 
804
        u->in_use = 1;
 
805
        return 1;
 
806
}
 
807
 
 
808
static int validate_config(bip_t *bip)
 
809
{
 
810
        /* nick username realname or default_{nick,username,realname} in user */
 
811
        hash_iterator_t it, sit, cit;
 
812
        struct user *user;
 
813
        struct link *link;
 
814
        struct chan_info *ci;
 
815
        int r = 1;
 
816
 
 
817
        for (hash_it_init(&bip->users, &it); (user = hash_it_item(&it));
 
818
                        hash_it_next(&it)) {
 
819
                for (hash_it_init(&user->connections, &sit);
 
820
                                (link = hash_it_item(&sit));
 
821
                                hash_it_next(&sit)) {
 
822
                        if (!user->default_nick || !user->default_username ||
 
823
                                        !user->default_realname) {
 
824
                                if ((!link->username &&
 
825
                                                !user->default_username) ||
 
826
                                                (!link->connect_nick &&
 
827
                                                 !user->default_nick) ||
 
828
                                                (!link->realname &&
 
829
                                                 !user->default_realname)) {
 
830
                                        conf_die(bip, "user %s, "
 
831
                                                "connection %s: you must defin"
 
832
                                                "e nick, user and realname.",
 
833
                                                user->name, link->name);
 
834
                                        link_kill(bip, link);
 
835
                                        r = 0;
 
836
                                        continue;
 
837
                                }
 
838
                        }
 
839
 
 
840
                        for (hash_it_init(&link->chan_infos, &cit);
 
841
                                        (ci = hash_it_item(&cit));
 
842
                                        hash_it_next(&cit)) {
 
843
                                if (!ci->name) {
 
844
                                        conf_die(bip, "user %s, "
 
845
                                                "connection "
 
846
                                                "%s: channel must have"
 
847
                                                "a name.", user->name,
 
848
                                                link->name);
 
849
                                        r = 0;
 
850
                                        continue;
 
851
                                }
 
852
                        }
 
853
                }
 
854
 
 
855
                if (user->backlog && !conf_log && user->backlog_lines == 0) {
 
856
                        conf_die(bip, "If conf_log = false, you must set "
 
857
                                "backlog_"
 
858
                                "lines to a non-nul value for each user with"
 
859
                                "backlog = true. Faulty user is %s",
 
860
                                user->name);
 
861
                        r = 0;
 
862
                        continue;
 
863
                }
 
864
        }
 
865
 
 
866
        hash_it_init(&bip->users, &it);
 
867
        hash_it_next(&it);
 
868
        if (hash_it_item(&it) && !strstr(conf_log_format, "%u"))
 
869
                mylog(LOG_WARN, "log_format does not contain %%u, all users'"
 
870
                        " logs will be mixed !");
 
871
        return r;
 
872
}
 
873
 
 
874
void clear_marks(bip_t *bip)
 
875
{
 
876
        list_iterator_t lit;
 
877
        hash_iterator_t hit;
 
878
 
 
879
        for (list_it_init(&bip->link_list, &lit); list_it_item(&lit);
 
880
                        list_it_next(&lit))
 
881
                ((struct link *)list_it_item(&lit))->in_use = 0;
 
882
        for (hash_it_init(&bip->users, &hit); hash_it_item(&hit);
 
883
                        hash_it_next(&hit))
 
884
                ((struct user *)hash_it_item(&hit))->in_use = 0;
 
885
}
 
886
 
 
887
void user_kill(bip_t *bip, struct user *user)
 
888
{
 
889
        (void)bip;
 
890
        if (!hash_is_empty(&user->connections))
 
891
                fatal("user_kill, user still has connections");
 
892
        free(user->name);
 
893
        free(user->password);
 
894
        MAYFREE(user->default_nick);
 
895
        MAYFREE(user->default_username);
 
896
        MAYFREE(user->default_realname);
 
897
 
 
898
#ifdef HAVE_LIBSSL
 
899
        MAYFREE(user->ssl_check_store);
 
900
        MAYFREE(user->ssl_client_certfile);
 
901
#endif
 
902
        free(user);
 
903
}
 
904
 
 
905
void sweep(bip_t *bip)
 
906
{
 
907
        list_iterator_t lit;
 
908
        hash_iterator_t hit;
 
909
 
 
910
        for (list_it_init(&bip->link_list, &lit); list_it_item(&lit);
 
911
                        list_it_next(&lit)) {
 
912
                struct link *l = ((struct link *)list_it_item(&lit));
 
913
                if (!l->in_use) {
 
914
                        mylog(LOG_INFO, "Administratively killing %s/%s",
 
915
                                        l->user->name, l->name);
 
916
                        list_remove_if_exists(&bip->conn_list, l);
 
917
                        link_kill(bip, l);
 
918
                        list_it_remove(&lit);
 
919
                }
 
920
        }
 
921
        for (hash_it_init(&bip->users, &hit); hash_it_item(&hit);
 
922
                        hash_it_next(&hit)) {
 
923
                struct user *u = (struct user *)hash_it_item(&hit);
 
924
                if (!u->in_use) {
 
925
                        hash_it_remove(&hit);
 
926
                        user_kill(bip, u);
 
927
                }
 
928
        }
 
929
}
 
930
 
 
931
int fireup(bip_t *bip, FILE *conf)
 
932
{
 
933
        int r;
 
934
        struct tuple *t;
 
935
        int err = 0;
 
936
        struct historical_directives hds;
 
937
        char *l;
 
938
 
 
939
        clear_marks(bip);
 
940
        while ((l = list_remove_first(&bip->errors)))
 
941
                free(l);
 
942
        parse_conf(conf, &err);
 
943
        if (err) {
 
944
                free_conf(root_list);
 
945
                root_list = NULL;
 
946
                return 0;
 
947
        }
 
948
 
 
949
#define SET_HV(d, n) do {\
 
950
        int __gtv = get_tuple_nvalue(root_list, LEX_##n);\
 
951
        if (__gtv != -1) \
 
952
                d = __gtv;\
 
953
        else\
 
954
                d = DEFAULT_##n;\
 
955
        } while(0);
 
956
        SET_HV(hds.always_backlog, ALWAYS_BACKLOG);
 
957
        SET_HV(hds.backlog, BACKLOG);
 
958
        SET_HV(hds.bl_msg_only, BL_MSG_ONLY);
 
959
        SET_HV(hds.backlog_lines, BACKLOG_LINES);
 
960
        SET_HV(hds.backlog_no_timestamp, BACKLOG_NO_TIMESTAMP);
 
961
        SET_HV(hds.blreset_on_talk, BLRESET_ON_TALK);
 
962
#undef SET_HV
 
963
 
 
964
        while ((t = list_remove_first(root_list))) {
 
965
                switch (t->type) {
 
966
                case LEX_LOG_SYNC_INTERVAL:
 
967
                        conf_log_sync_interval = t->ndata;
 
968
                        break;
 
969
                case LEX_LOG:
 
970
                        conf_log = t->ndata;
 
971
                        break;
 
972
                case LEX_LOG_SYSTEM:
 
973
                        conf_log_system = t->ndata;
 
974
                        break;
 
975
                case LEX_LOG_ROOT:
 
976
                        MOVE_STRING(conf_log_root, t->pdata);
 
977
                        break;
 
978
                case LEX_LOG_FORMAT:
 
979
                        MOVE_STRING(conf_log_format, t->pdata);
 
980
                        break;
 
981
                case LEX_LOG_LEVEL:
 
982
                        conf_log_level = t->ndata;
 
983
                        break;
 
984
                case LEX_IP:
 
985
                        MOVE_STRING(conf_ip, t->pdata);
 
986
                        break;
 
987
                case LEX_PORT:
 
988
                        conf_port = t->ndata;
 
989
                        break;
 
990
#ifdef HAVE_LIBSSL
 
991
                case LEX_CSS:
 
992
                        conf_css = t->ndata;
 
993
                        break;
 
994
                case LEX_CSS_PEM:
 
995
                        MOVE_STRING(conf_ssl_certfile, t->pdata);
 
996
                        break;
 
997
#else
 
998
                case LEX_CSS:
 
999
                case LEX_CSS_PEM:
 
1000
                        mylog(LOG_WARN, "Found SSL option whereas bip is "
 
1001
                                        "not built with SSL support.");
 
1002
                        break;
 
1003
#endif
 
1004
                case LEX_PID_FILE:
 
1005
                        MOVE_STRING(conf_pid_file, t->pdata);
 
1006
                        break;
 
1007
                case LEX_ALWAYS_BACKLOG:
 
1008
                        hds.always_backlog = t->ndata;
 
1009
                        break;
 
1010
                case LEX_BACKLOG:
 
1011
                        hds.backlog = t->ndata;
 
1012
                        break;
 
1013
                case LEX_BL_MSG_ONLY:
 
1014
                        hds.bl_msg_only = t->ndata;
 
1015
                        break;
 
1016
                case LEX_BACKLOG_LINES:
 
1017
                        hds.backlog_lines = t->ndata;
 
1018
                        break;
 
1019
                case LEX_BACKLOG_NO_TIMESTAMP:
 
1020
                        hds.backlog_no_timestamp = t->ndata;
 
1021
                        break;
 
1022
                case LEX_BLRESET_ON_TALK:
 
1023
                        hds.blreset_on_talk = t->ndata;
 
1024
                        break;
 
1025
                case LEX_NETWORK:
 
1026
                        r = add_network(bip, t->pdata);
 
1027
                        list_free(t->pdata);
 
1028
                        if (!r)
 
1029
                                goto out_conf_error;
 
1030
                        break;
 
1031
                case LEX_USER:
 
1032
                        r = add_user(bip, t->pdata, &hds);
 
1033
                        list_free(t->pdata);
 
1034
                        if (!r)
 
1035
                                goto out_conf_error;
 
1036
                        break;
 
1037
                default:
 
1038
                        conf_die(bip, "Config error in base config (%d)",
 
1039
                                        t->type);
 
1040
                        goto out_conf_error;
 
1041
                }
 
1042
                if (t->tuple_type == TUPLE_STR && t->pdata)
 
1043
                        free(t->pdata);
 
1044
                free(t);
 
1045
        }
 
1046
        free(root_list);
 
1047
        root_list = NULL;
 
1048
 
 
1049
        if (validate_config(bip)) {
 
1050
                sweep(bip);
 
1051
                return 1;
 
1052
        } else {
 
1053
                return 0;
 
1054
        }
 
1055
out_conf_error:
 
1056
        free_conf(root_list);
 
1057
        root_list = NULL;
 
1058
        return 0;
 
1059
}
 
1060
 
 
1061
static void log_file_setup(void)
 
1062
{
 
1063
        char buf[4096];
 
1064
 
 
1065
        if (conf_log_system && conf_daemonize) {
 
1066
                if (conf_global_log_file && conf_global_log_file != stderr)
 
1067
                        fclose(conf_global_log_file);
 
1068
                snprintf(buf, 4095, "%s/bip.log", conf_log_root);
 
1069
                FILE *f = fopen(buf, "a");
 
1070
                if (!f)
 
1071
                        fatal("Can't open %s: %s", buf, strerror(errno));
 
1072
                conf_global_log_file = f;
 
1073
        } else {
 
1074
                conf_global_log_file = stderr;
 
1075
        }
 
1076
}
 
1077
 
 
1078
void check_rlimits()
 
1079
{
 
1080
        int r, cklim;
 
1081
        struct rlimit lt;
 
1082
 
 
1083
        cklim = 0;
 
1084
 
 
1085
#ifdef RLIMIT_AS
 
1086
        r = getrlimit(RLIMIT_AS, &lt);
 
1087
        if (r) {
 
1088
                mylog(LOG_ERROR, "getrlimit(): failed with %s",
 
1089
                                strerror(errno));
 
1090
        } else {
 
1091
                if (lt.rlim_max != RLIM_INFINITY) {
 
1092
                        mylog(LOG_WARN, "virtual memory rlimit active, "
 
1093
                                "bip may be KILLED by the system");
 
1094
                        cklim = 1;
 
1095
                }
 
1096
        }
 
1097
#endif
 
1098
 
 
1099
        r = getrlimit(RLIMIT_CPU, &lt);
 
1100
        if (r) {
 
1101
                mylog(LOG_ERROR, "getrlimit(): failed with %s",
 
1102
                                strerror(errno));
 
1103
        } else {
 
1104
                if (lt.rlim_max != RLIM_INFINITY) {
 
1105
                        mylog(LOG_WARN, "CPU rlimit active, bip may "
 
1106
                                "be OFTEN KILLED by the system");
 
1107
                        cklim = 1;
 
1108
                }
 
1109
        }
 
1110
 
 
1111
        r = getrlimit(RLIMIT_FSIZE, &lt);
 
1112
        if (r) {
 
1113
                mylog(LOG_ERROR, "getrlimit(): failed with %s",
 
1114
                                strerror(errno));
 
1115
        } else {
 
1116
                if (lt.rlim_max != RLIM_INFINITY) {
 
1117
                        mylog(LOG_WARN, "FSIZE rlimit active, bip will "
 
1118
                                "fail to create files of size greater than "
 
1119
                                "%d bytes.", (int)lt.rlim_max);
 
1120
                        cklim = 1;
 
1121
                }
 
1122
        }
 
1123
 
 
1124
        r = getrlimit(RLIMIT_NOFILE, &lt);
 
1125
        if (r) {
 
1126
                mylog(LOG_ERROR, "getrlimit(): failed with %s",
 
1127
                                strerror(errno));
 
1128
        } else {
 
1129
                if (lt.rlim_max != RLIM_INFINITY && lt.rlim_max < 256) {
 
1130
                        mylog(LOG_WARN, "opened files count rlimit "
 
1131
                                "active, bip will not be allowed to open more "
 
1132
                                "than %d files at a time", (int)lt.rlim_max);
 
1133
                        cklim = 1;
 
1134
                }
 
1135
        }
 
1136
 
 
1137
        r = getrlimit(RLIMIT_STACK, &lt);
 
1138
        if (r) {
 
1139
                mylog(LOG_ERROR, "getrlimit(): failed with %s",
 
1140
                                strerror(errno));
 
1141
        } else {
 
1142
                if (lt.rlim_max != RLIM_INFINITY) {
 
1143
                        mylog(LOG_WARN, "stack rlimit active, "
 
1144
                                "bip may be KILLED by the system");
 
1145
                        cklim = 1;
 
1146
                }
 
1147
        }
 
1148
 
 
1149
        if (cklim)
 
1150
                mylog(LOG_WARN, "You can check your limits with `ulimit -a'");
 
1151
}
 
1152
 
 
1153
int main(int argc, char **argv)
 
1154
{
 
1155
        FILE *conf = NULL;
 
1156
        char *confpath = NULL;
 
1157
        int ch;
 
1158
        int r, fd;
 
1159
        char buf[30];
 
1160
        bip_t bip;
 
1161
 
 
1162
        bip_init(&bip);
 
1163
        _bip = &bip;
 
1164
 
 
1165
        conf_ip = bip_strdup("0.0.0.0");
 
1166
        conf_port = 7778;
 
1167
        conf_css = 0;
 
1168
 
 
1169
        signal(SIGPIPE, SIG_IGN);
 
1170
        signal(SIGHUP, reload_config);
 
1171
        signal(SIGINT, bad_quit);
 
1172
        signal(SIGQUIT, bad_quit);
 
1173
        signal(SIGTERM, bad_quit);
 
1174
        signal(SIGXFSZ, rlimit_bigfile_reached);
 
1175
        signal(SIGXCPU, rlimit_cpu_reached);
 
1176
 
 
1177
        conf_log_root = NULL;
 
1178
        conf_log_format = bip_strdup(DEFAULT_LOG_FORMAT);
 
1179
        conf_log_level = DEFAULT_LOG_LEVEL;
 
1180
        conf_daemonize = 1;
 
1181
        conf_global_log_file = stderr;
 
1182
        conf_pid_file = NULL;
 
1183
#ifdef HAVE_LIBSSL
 
1184
        conf_ssl_certfile = NULL;
 
1185
#endif
 
1186
 
 
1187
        while ((ch = getopt(argc, argv, "hvnf:s:")) != -1) {
 
1188
                switch (ch) {
 
1189
                case 'f':
 
1190
                        confpath = bip_strdup(optarg);
 
1191
                        break;
 
1192
                case 'n':
 
1193
                        conf_daemonize = 0;
 
1194
                        break;
 
1195
                case 's':
 
1196
                        conf_biphome = bip_strdup(optarg);
 
1197
                        break;
 
1198
                case 'v':
 
1199
                        version();
 
1200
                        exit(0);
 
1201
                        break;
 
1202
                default:
 
1203
                        version();
 
1204
                        usage(argv[0]);
 
1205
                }
 
1206
        }
 
1207
 
 
1208
        umask(0027);
 
1209
 
 
1210
        check_rlimits();
 
1211
 
 
1212
        char *home = NULL; /* oidentd path searching ignores conf_biphome */
 
1213
        home = getenv("HOME");
 
1214
        if (!home) {
 
1215
                conf_die(&bip, "no $HOME !, do you live in a trailer ?");
 
1216
                return 0;
 
1217
        }
 
1218
#ifdef HAVE_OIDENTD
 
1219
        bip.oidentdpath = bip_malloc(strlen(home) + 1 +
 
1220
                        strlen(OIDENTD_FILENAME) + 1);
 
1221
        strcpy(bip.oidentdpath, home);
 
1222
        strcat(bip.oidentdpath, "/");
 
1223
        strcat(bip.oidentdpath, OIDENTD_FILENAME);
 
1224
#endif
 
1225
 
 
1226
 
 
1227
        if (!conf_biphome) {
 
1228
                conf_biphome = bip_malloc(strlen(home) + strlen("/.bip") + 1);
 
1229
                strcpy(conf_biphome, home);
 
1230
                strcat(conf_biphome, "/.bip");
 
1231
        }
 
1232
 
 
1233
        if (!confpath) {
 
1234
                confpath = bip_malloc(strlen(conf_biphome) + 1 +
 
1235
                                strlen(S_CONF) + 1);
 
1236
                strcpy(confpath, conf_biphome);
 
1237
                strcat(confpath, "/");
 
1238
                strcat(confpath, S_CONF);
 
1239
        }
 
1240
        conf = fopen(confpath, "r");
 
1241
        if (!conf)
 
1242
                fatal("config file not found");
 
1243
 
 
1244
        r = fireup(&bip, conf);
 
1245
        fclose(conf);
 
1246
        if (!r)
 
1247
                fatal("Not starting: error in config file.");
 
1248
 
 
1249
        if (!conf_log_root) {
 
1250
                char *ap = "/logs";
 
1251
                conf_log_root = bip_malloc(strlen(conf_biphome) +
 
1252
                                strlen(ap) + 1);
 
1253
                strcpy(conf_log_root, conf_biphome);
 
1254
                strcat(conf_log_root, ap);
 
1255
                mylog(LOG_INFO, "Default log root: %s", conf_log_root);
 
1256
        }
 
1257
        if (!conf_pid_file) {
 
1258
                char *pid = "/bip.pid";
 
1259
                conf_pid_file = bip_malloc(strlen(conf_biphome) +
 
1260
                                strlen(pid) + 1);
 
1261
                strcpy(conf_pid_file, conf_biphome);
 
1262
                strcat(conf_pid_file, pid);
 
1263
                mylog(LOG_INFO, "Default pid file: %s", conf_pid_file);
 
1264
        }
 
1265
 
 
1266
#ifdef HAVE_LIBSSL
 
1267
        if (conf_css) {
 
1268
                int e, fd;
 
1269
                struct stat fs;
 
1270
 
 
1271
                if (!conf_ssl_certfile) {
 
1272
                        char *ap = "/bip.pem";
 
1273
                        conf_ssl_certfile = bip_malloc(strlen(conf_biphome) +
 
1274
                                        strlen(ap) + 1);
 
1275
                        strcpy(conf_ssl_certfile, conf_biphome);
 
1276
                        strcat(conf_ssl_certfile, ap);
 
1277
                        mylog(LOG_INFO, "Using default SSL certificate file: "
 
1278
                                        "%s", conf_ssl_certfile);
 
1279
                }
 
1280
 
 
1281
                if ((fd = open(conf_ssl_certfile, O_RDONLY)) == -1)
 
1282
                        fatal("Unable to open PEM file %s for reading",
 
1283
                                conf_ssl_certfile);
 
1284
                else
 
1285
                        close(fd);
 
1286
 
 
1287
                e = stat(conf_ssl_certfile, &fs);
 
1288
                if (e)
 
1289
                        mylog(LOG_WARN, "Unable to check PEM file, stat(%s): "
 
1290
                                "%s", conf_ssl_certfile, strerror(errno));
 
1291
                else if ((fs.st_mode & S_IROTH) | (fs.st_mode & S_IWOTH))
 
1292
                        mylog(LOG_ERROR, "PEM file %s should not be world "
 
1293
                                "readable / writable. Please fix the modes.",
 
1294
                                conf_ssl_certfile);
 
1295
        }
 
1296
#endif
 
1297
 
 
1298
        check_dir(conf_log_root, 1);
 
1299
        fd = do_pid_stuff();
 
1300
        pid_t pid = 0;
 
1301
 
 
1302
        log_file_setup();
 
1303
        if (conf_daemonize)
 
1304
                pid = daemonize();
 
1305
        else
 
1306
                pid = getpid();
 
1307
        snprintf(buf, 29, "%ld\n", (long unsigned int)pid);
 
1308
        write(fd, buf, strlen(buf));
 
1309
        close(fd);
 
1310
 
 
1311
        bip.listener = listen_new(conf_ip, conf_port, conf_css);
 
1312
        if (!bip.listener)
 
1313
                fatal("Could not create listening socket");
 
1314
 
 
1315
        for (;;) {
 
1316
                irc_main(&bip);
 
1317
 
 
1318
                sighup = 0;
 
1319
 
 
1320
                conf = fopen(confpath, "r");
 
1321
                if (!conf)
 
1322
                        fatal("%s config file not found", confpath);
 
1323
                fireup(&bip, conf);
 
1324
                fclose(conf);
 
1325
 
 
1326
                /* re-open to allow logfile rotate */
 
1327
                log_file_setup();
 
1328
        }
 
1329
        return 1;
 
1330
}
 
1331
 
 
1332
#define RET_STR_LEN 256
 
1333
#define LINE_SIZE_LIM 70
 
1334
void adm_print_connection(struct link_client *ic, struct link *lnk,
 
1335
                struct user *bu)
 
1336
{
 
1337
        hash_iterator_t lit;
 
1338
        char buf[RET_STR_LEN + 1];
 
1339
        int t_written = 0;
 
1340
 
 
1341
        if (!bu)
 
1342
                bu = lnk->user;
 
1343
 
 
1344
        bip_notify(ic, "* %s to %s as \"%s\" (%s!%s) :", lnk->name,
 
1345
                lnk->network->name,
 
1346
                (lnk->realname ? lnk->realname : bu->default_realname),
 
1347
                (lnk->connect_nick ? lnk->connect_nick : bu->default_nick),
 
1348
                (lnk->username ? lnk->username : bu->default_username));
 
1349
 
 
1350
        t_written = snprintf(buf, RET_STR_LEN, "  Options:");
 
1351
        if (t_written >= RET_STR_LEN)
 
1352
                goto noroom;
 
1353
        if (lnk->follow_nick) {
 
1354
                t_written += snprintf(buf + t_written,
 
1355
                        RET_STR_LEN - t_written, " follow_nick");
 
1356
                if (t_written >= RET_STR_LEN)
 
1357
                        goto noroom;
 
1358
        }
 
1359
        if (lnk->ignore_first_nick) {
 
1360
                t_written += snprintf(buf + t_written,
 
1361
                        RET_STR_LEN - t_written, " ignore_first_nick");
 
1362
                if (t_written >= RET_STR_LEN)
 
1363
                        goto noroom;
 
1364
        }
 
1365
        if (lnk->away_nick) {
 
1366
                t_written += snprintf(buf + t_written,
 
1367
                        RET_STR_LEN - t_written, " away_nick=%s",
 
1368
                        lnk->away_nick);
 
1369
                if (t_written >= RET_STR_LEN)
 
1370
                        goto noroom;
 
1371
        }
 
1372
        if (lnk->no_client_away_msg) {
 
1373
                t_written += snprintf(buf + t_written,
 
1374
                        RET_STR_LEN - t_written, " no_client_away_msg=%s",
 
1375
                        lnk->no_client_away_msg);
 
1376
                if (t_written >= RET_STR_LEN)
 
1377
                        goto noroom;
 
1378
        }
 
1379
        if (lnk->vhost) {
 
1380
                t_written += snprintf(buf + t_written,
 
1381
                        RET_STR_LEN - t_written, " vhost=%s",
 
1382
                        lnk->vhost);
 
1383
                if (t_written >= RET_STR_LEN)
 
1384
                        goto noroom;
 
1385
        }
 
1386
        if (lnk->bind_port) {
 
1387
                t_written += snprintf(buf + t_written,
 
1388
                        RET_STR_LEN - t_written, " bind_port=%u",
 
1389
                        lnk->bind_port);
 
1390
                if (t_written >= RET_STR_LEN)
 
1391
                        goto noroom;
 
1392
        }
 
1393
noroom: /* that means the line is larger that RET_STR_LEN. We're not likely to
 
1394
           even read such a long line */
 
1395
        buf[RET_STR_LEN] = 0;
 
1396
        bip_notify(ic, "%s", buf);
 
1397
 
 
1398
        // TODO: on_connect_send
 
1399
 
 
1400
        // TODO : check channels struct
 
1401
        t_written = snprintf(buf, RET_STR_LEN, "  Channels (* with key, ` no backlog)");
 
1402
        if (t_written >= RET_STR_LEN)
 
1403
                goto noroomchan;
 
1404
        for (hash_it_init(&lnk->chan_infos, &lit); hash_it_item(&lit);
 
1405
                        hash_it_next(&lit)) {
 
1406
                struct chan_info *ch = hash_it_item(&lit);
 
1407
 
 
1408
                t_written += snprintf(buf + t_written, RET_STR_LEN - t_written, " %s%s%s",
 
1409
                        ch->name, (ch->key ? "*" : ""),
 
1410
                        (ch->backlog ? "" : "`"));
 
1411
                if (t_written > LINE_SIZE_LIM) {
 
1412
                        buf[RET_STR_LEN] = 0;
 
1413
                        bip_notify(ic, "%s", buf);
 
1414
                        t_written = 0;
 
1415
                }
 
1416
        }
 
1417
noroomchan:
 
1418
        buf[RET_STR_LEN] = 0;
 
1419
        bip_notify(ic, "%s", buf);
 
1420
 
 
1421
        t_written = snprintf(buf, RET_STR_LEN, "  Status: ");
 
1422
        if (t_written >= RET_STR_LEN)
 
1423
                goto noroomstatus;
 
1424
        switch (lnk->s_state) {
 
1425
        case  IRCS_NONE:
 
1426
                t_written += snprintf(buf + t_written, RET_STR_LEN - t_written,
 
1427
                        "not started");
 
1428
                if (t_written >= RET_STR_LEN)
 
1429
                        goto noroomstatus;
 
1430
                break;
 
1431
        case  IRCS_CONNECTING:
 
1432
                t_written += snprintf(buf + t_written, RET_STR_LEN - t_written,
 
1433
                        "connecting... attempts: %d, last: %s",
 
1434
                        lnk->s_conn_attempt,
 
1435
                        hrtime(lnk->last_connection_attempt));
 
1436
                if (t_written >= RET_STR_LEN)
 
1437
                        goto noroomstatus;
 
1438
                break;
 
1439
        case  IRCS_CONNECTED:
 
1440
                t_written += snprintf(buf + t_written, RET_STR_LEN - t_written,
 
1441
                        "connected !");
 
1442
                if (t_written >= RET_STR_LEN)
 
1443
                        goto noroomstatus;
 
1444
                break;
 
1445
        case  IRCS_WAS_CONNECTED:
 
1446
                t_written += snprintf(buf + t_written, RET_STR_LEN - t_written,
 
1447
                        "disconnected, attempts: %d, last: %s",
 
1448
                        lnk->s_conn_attempt,
 
1449
                        hrtime(lnk->last_connection_attempt));
 
1450
                if (t_written >= RET_STR_LEN)
 
1451
                        goto noroomstatus;
 
1452
                break;
 
1453
        case  IRCS_RECONNECTING:
 
1454
                t_written += snprintf(buf + t_written, RET_STR_LEN - t_written,
 
1455
                        "reconnecting... attempts: %d, last: %s",
 
1456
                        lnk->s_conn_attempt,
 
1457
                        hrtime(lnk->last_connection_attempt));
 
1458
                if (t_written >= RET_STR_LEN)
 
1459
                        goto noroomstatus;
 
1460
                break;
 
1461
        case  IRCS_TIMER_WAIT:
 
1462
                t_written += snprintf(buf + t_written, RET_STR_LEN - t_written,
 
1463
                        "waiting to reconnect, attempts: %d, last: %s",
 
1464
                        lnk->s_conn_attempt,
 
1465
                        hrtime(lnk->last_connection_attempt));
 
1466
                if (t_written >= RET_STR_LEN)
 
1467
                        goto noroomstatus;
 
1468
                break;
 
1469
        default:
 
1470
                t_written += snprintf(buf + t_written, RET_STR_LEN - t_written,
 
1471
                        "unknown");
 
1472
                if (t_written >= RET_STR_LEN)
 
1473
                        goto noroomstatus;
 
1474
                break;
 
1475
                // s_conn_attempt recon_timer last_connection_attempt
 
1476
        }
 
1477
noroomstatus:
 
1478
        buf[RET_STR_LEN] = 0;
 
1479
        bip_notify(ic, "%s", buf);
 
1480
}
 
1481
 
 
1482
void adm_list_all_links(struct link_client *ic)
 
1483
{
 
1484
        list_iterator_t it;
 
1485
 
 
1486
        bip_notify(ic, "-- All links");
 
1487
        for (list_it_init(&_bip->link_list, &it); list_it_item(&it);
 
1488
                        list_it_next(&it)) {
 
1489
                struct link *l = list_it_item(&it);
 
1490
                if (l)
 
1491
                        adm_print_connection(ic, l, NULL);
 
1492
        }
 
1493
        bip_notify(ic, "-- End of All links");
 
1494
}
 
1495
 
 
1496
void adm_list_all_connections(struct link_client *ic)
 
1497
{
 
1498
        hash_iterator_t it;
 
1499
 
 
1500
        bip_notify(ic, "-- All connections");
 
1501
        for (hash_it_init(&_bip->users, &it); hash_it_item(&it);
 
1502
                        hash_it_next(&it)) {
 
1503
                struct user *u = hash_it_item(&it);
 
1504
                if (u)
 
1505
                        adm_list_connections(ic, u);
 
1506
        }
 
1507
        bip_notify(ic, "-- End of All connections");
 
1508
}
 
1509
 
 
1510
#define STRORNULL(s) ((s) == NULL ? "unset" : (s))
 
1511
 
 
1512
void adm_info_user(struct link_client *ic, const char *name)
 
1513
{
 
1514
        struct user *u;
 
1515
        char buf[RET_STR_LEN + 1];
 
1516
        int t_written = 0;
 
1517
 
 
1518
        bip_notify(ic, "-- User '%s' info", name);
 
1519
        u = hash_get(&_bip->users, name);
 
1520
        if (!u) {
 
1521
                bip_notify(ic, "Unknown user");
 
1522
                return;
 
1523
        }
 
1524
 
 
1525
        t_written += snprintf(buf + t_written, RET_STR_LEN - t_written,
 
1526
                              "user: %s", u->name);
 
1527
        if (t_written >= RET_STR_LEN)
 
1528
                goto noroom;
 
1529
        if (u->admin) {
 
1530
                t_written += snprintf(buf + t_written, RET_STR_LEN - t_written,
 
1531
                                ", is bip admin");
 
1532
                if (t_written >= RET_STR_LEN)
 
1533
                        goto noroom;
 
1534
        }
 
1535
 
 
1536
noroom:
 
1537
        buf[RET_STR_LEN] = 0;
 
1538
        bip_notify(ic, "%s", buf);
 
1539
 
 
1540
#ifdef HAVE_LIBSSL
 
1541
        bip_notify(ic, "SSL check mode '%s', stored into '%s'",
 
1542
                   checkmode2text(u->ssl_check_mode),
 
1543
                   STRORNULL(u->ssl_check_store));
 
1544
        if (u->ssl_client_certfile)
 
1545
                bip_notify(ic, "SSL client certificate stored into '%s'",
 
1546
                                u->ssl_client_certfile);
 
1547
#endif
 
1548
        bip_notify(ic, "Defaults nick: %s, user: %s, realname: %s",
 
1549
                   STRORNULL(u->default_nick), STRORNULL(u->default_username),
 
1550
                   STRORNULL(u->default_realname));
 
1551
        if (u->backlog) {
 
1552
                bip_notify(ic, "Backlog enabled, lines: %d, no timestamp: %s,"
 
1553
                        "  messages only: %s", u->backlog_lines,
 
1554
                        bool2text(u->backlog_no_timestamp),
 
1555
                        bool2text(u->bl_msg_only));
 
1556
                bip_notify(ic, "always backlog: %s, reset on talk: %s",
 
1557
                        bool2text(u->always_backlog),
 
1558
                        bool2text(u->blreset_on_talk));
 
1559
        } else {
 
1560
                bip_notify(ic, "Backlog disabled");
 
1561
        }
 
1562
        adm_list_connections(ic, u);
 
1563
        bip_notify(ic, "-- End of User '%s' info", name);
 
1564
}
 
1565
 
 
1566
void adm_list_users(struct link_client *ic)
 
1567
{
 
1568
        hash_iterator_t it;
 
1569
        hash_iterator_t lit;
 
1570
        char buf[RET_STR_LEN + 1];
 
1571
        connection_t *c;
 
1572
 
 
1573
        c = CONN(ic);
 
1574
 
 
1575
        bip_notify(ic, "-- User list");
 
1576
        for (hash_it_init(&_bip->users, &it); hash_it_item(&it);
 
1577
                        hash_it_next(&it)) {
 
1578
                struct user *u = hash_it_item(&it);
 
1579
                int first = 1;
 
1580
                int t_written = 0;
 
1581
 
 
1582
                buf[RET_STR_LEN] = 0;
 
1583
                t_written += snprintf(buf, RET_STR_LEN, "* %s%s:", u->name,
 
1584
                                (u->admin ? "(admin)": ""));
 
1585
                if (t_written >= RET_STR_LEN)
 
1586
                        goto noroom;
 
1587
                for (hash_it_init(&u->connections, &lit); hash_it_item(&lit);
 
1588
                                hash_it_next(&lit)) {
 
1589
                        struct link *lnk = hash_it_item(&lit);
 
1590
                        if (first) {
 
1591
                                first = 0;
 
1592
                        } else {
 
1593
                                t_written += snprintf(buf + t_written,
 
1594
                                                RET_STR_LEN - t_written, ",");
 
1595
                                if (t_written >= RET_STR_LEN)
 
1596
                                        goto noroom;
 
1597
                        }
 
1598
 
 
1599
                        t_written += snprintf(buf + t_written,
 
1600
                                        RET_STR_LEN - t_written,
 
1601
                                        " %s", lnk->name);
 
1602
                        if (t_written >= RET_STR_LEN)
 
1603
                                goto noroom;
 
1604
                        if (t_written > LINE_SIZE_LIM) {
 
1605
                                buf[RET_STR_LEN] = 0;
 
1606
                                bip_notify(ic, "%s", buf);
 
1607
                                t_written = 0;
 
1608
                        }
 
1609
                }
 
1610
noroom:
 
1611
                buf[RET_STR_LEN] = 0;
 
1612
                bip_notify(ic, "%s", buf);
 
1613
        }
 
1614
        bip_notify(ic, "-- End of User list");
 
1615
}
 
1616
 
 
1617
void adm_list_networks(struct link_client *ic)
 
1618
{
 
1619
        hash_iterator_t it;
 
1620
        char buf[RET_STR_LEN + 1];
 
1621
        connection_t *c;
 
1622
 
 
1623
        c = CONN(ic);
 
1624
 
 
1625
        bip_notify(ic, "-- Network list (* means SSL):");
 
1626
        for (hash_it_init(&_bip->networks, &it); hash_it_item(&it);
 
1627
                        hash_it_next(&it)) {
 
1628
                struct network *n = hash_it_item(&it);
 
1629
                int t_written = 0;
 
1630
                int i;
 
1631
 
 
1632
                buf[RET_STR_LEN] = 0;
 
1633
#ifdef HAVE_LIBSSL
 
1634
                if (n->ssl) {
 
1635
                        t_written += snprintf(buf, RET_STR_LEN, "- %s*:",
 
1636
                                        n->name);
 
1637
                        if (t_written >= RET_STR_LEN)
 
1638
                                goto noroom;
 
1639
                } else {
 
1640
#endif
 
1641
                        t_written += snprintf(buf, RET_STR_LEN, "- %s:", n->name);
 
1642
                        if (t_written >= RET_STR_LEN)
 
1643
                                goto noroom;
 
1644
#ifdef HAVE_LIBSSL
 
1645
                }
 
1646
#endif
 
1647
                for (i = 0; i < n->serverc; i++) {
 
1648
                        struct server *serv = i+n->serverv;
 
1649
                        t_written += snprintf(buf + t_written, RET_STR_LEN
 
1650
                                - t_written, " %s:%d", serv->host,
 
1651
                                serv->port);
 
1652
                        if (t_written >= RET_STR_LEN)
 
1653
                                goto noroom;
 
1654
                        if (t_written > LINE_SIZE_LIM) {
 
1655
                                buf[RET_STR_LEN] = 0;
 
1656
                                bip_notify(ic, "%s", buf);
 
1657
                                t_written = 0;
 
1658
                        }
 
1659
                }
 
1660
noroom:
 
1661
                buf[RET_STR_LEN] = 0;
 
1662
                bip_notify(ic, "%s", buf);
 
1663
        }
 
1664
        bip_notify(ic, "-- End of Network list");
 
1665
}
 
1666
 
 
1667
void adm_list_connections(struct link_client *ic, struct user *bu)
 
1668
{
 
1669
        hash_iterator_t it;
 
1670
        connection_t *c;
 
1671
 
 
1672
        c = CONN(ic);
 
1673
        if (!bu) {
 
1674
                bip_notify(ic, "-- Your connections:");
 
1675
                bu = LINK(ic)->user;
 
1676
        } else {
 
1677
                bip_notify(ic, "-- User %s's connections:", bu->name);
 
1678
        }
 
1679
 
 
1680
        for (hash_it_init(&bu->connections, &it); hash_it_item(&it);
 
1681
                        hash_it_next(&it)) {
 
1682
                struct link *lnk= hash_it_item(&it);
 
1683
                adm_print_connection(ic, lnk, bu);
 
1684
        }
 
1685
        bip_notify(ic, "-- End of Connection list");
 
1686
}
 
1687
 
 
1688
#ifdef HAVE_LIBSSL
 
1689
int link_add_untrusted(struct link_server *ls, X509 *cert)
 
1690
{
 
1691
        int i;
 
1692
 
 
1693
        /* Check whether the cert is already in the stack */
 
1694
        for (i = 0; i < sk_X509_num(LINK(ls)->untrusted_certs); i++) {
 
1695
                if (!X509_cmp(cert,
 
1696
                                sk_X509_value(LINK(ls)->untrusted_certs, i)))
 
1697
                        return 1;
 
1698
        }
 
1699
 
 
1700
        return sk_X509_push(LINK(ls)->untrusted_certs, cert);
 
1701
}
 
1702
 
 
1703
int ssl_check_trust(struct link_client *ic)
 
1704
{
 
1705
        X509 *trustcert = NULL;
 
1706
        char subject[270];
 
1707
        char issuer[270];
 
1708
        unsigned char fp[EVP_MAX_MD_SIZE];
 
1709
        char fpstr[EVP_MAX_MD_SIZE * 3 + 20];
 
1710
        unsigned int fplen;
 
1711
        int i;
 
1712
 
 
1713
        if(!LINK(ic)->untrusted_certs ||
 
1714
                        sk_X509_num(LINK(ic)->untrusted_certs) <= 0)
 
1715
                return 0;
 
1716
 
 
1717
        trustcert = sk_X509_value(LINK(ic)->untrusted_certs, 0);
 
1718
        strcpy(subject, "Subject: ");
 
1719
        strcpy(issuer, "Issuer:  ");
 
1720
        strcpy(fpstr, "MD5 fingerprint: ");
 
1721
        X509_NAME_oneline(X509_get_subject_name(trustcert), subject + 9, 256);
 
1722
        X509_NAME_oneline(X509_get_issuer_name(trustcert), issuer + 9, 256);
 
1723
 
 
1724
        X509_digest(trustcert, EVP_md5(), fp, &fplen);
 
1725
        for(i = 0; i < (int)fplen; i++)
 
1726
                sprintf(fpstr + 17 + (i * 3), "%02X%c",
 
1727
                                fp[i], (i == (int)fplen - 1) ? '\0' : ':');
 
1728
 
 
1729
        WRITE_LINE2(CONN(ic), P_SERV, "NOTICE", "TrustEm",
 
1730
                        "This server SSL certificate was not "
 
1731
                        "accepted because it is not in your store "
 
1732
                        "of trusted certificates:");
 
1733
 
 
1734
        WRITE_LINE2(CONN(ic), P_SERV, "NOTICE", "TrustEm", subject);
 
1735
        WRITE_LINE2(CONN(ic), P_SERV, "NOTICE", "TrustEm", issuer);
 
1736
        WRITE_LINE2(CONN(ic), P_SERV, "NOTICE", "TrustEm", fpstr);
 
1737
 
 
1738
        WRITE_LINE2(CONN(ic), P_SERV, "NOTICE", "TrustEm",
 
1739
                        "WARNING: if you've already trusted a "
 
1740
                        "certificate for this server before, that "
 
1741
                        "probably means it has changed.");
 
1742
 
 
1743
        WRITE_LINE2(CONN(ic), P_SERV, "NOTICE", "TrustEm",
 
1744
                        "If so, YOU MAY BE SUBJECT OF A "
 
1745
                        "MAN-IN-THE-MIDDLE ATTACK! PLEASE DON'T TRUST "
 
1746
                        "THIS CERTIFICATE IF YOU'RE NOT SURE THIS IS "
 
1747
                        "NOT THE CASE.");
 
1748
 
 
1749
        WRITE_LINE2(CONN(ic), P_SERV, "NOTICE", "TrustEm",
 
1750
                        "Type /QUOTE BIP TRUST OK to trust this "
 
1751
                        "certificate, /QUOTE BIP TRUST NO to discard it.");
 
1752
 
 
1753
        return 1;
 
1754
}
 
1755
 
 
1756
#if 0
 
1757
static int ssl_trust_next_cert(struct link_client *ic)
 
1758
{
 
1759
        (void)ic;
 
1760
}
 
1761
 
 
1762
static int ssl_discard_next_cert(struct link_client *ic)
 
1763
{
 
1764
        (void)ic;
 
1765
}
 
1766
#endif /* 0 */
 
1767
#endif
 
1768
 
 
1769
#ifdef HAVE_LIBSSL
 
1770
int adm_trust(struct link_client *ic, struct line *line)
 
1771
{
 
1772
        if (ic->allow_trust != 1) {
 
1773
                mylog(LOG_ERROR, "User attempted TRUST command without "
 
1774
                                "being allowed to!");
 
1775
                unbind_from_link(ic);
 
1776
                return OK_CLOSE;
 
1777
        }
 
1778
 
 
1779
        if(!LINK(ic)->untrusted_certs ||
 
1780
                        sk_X509_num(LINK(ic)->untrusted_certs) <= 0) {
 
1781
                /* shouldn't have been asked to /QUOTE BIP TRUST but well... */
 
1782
                WRITE_LINE2(CONN(ic), P_SERV, "NOTICE", "TrustEm",
 
1783
                                "No untrusted certificates.");
 
1784
                return ERR_PROTOCOL;
 
1785
        }
 
1786
 
 
1787
        if (irc_line_count(line) != 3)
 
1788
                return ERR_PROTOCOL;
 
1789
 
 
1790
        if (irc_line_elem_case_equals(line, 2, "OK")) {
 
1791
                /* OK, attempt to trust the cert! */
 
1792
                BIO *bio = BIO_new_file(LINK(ic)->user->ssl_check_store, "a+");
 
1793
                X509 *trustcert = sk_X509_shift(LINK(ic)->untrusted_certs);
 
1794
 
 
1795
                if(!bio || !trustcert ||
 
1796
                                PEM_write_bio_X509(bio, trustcert) <= 0)
 
1797
                        write_line_fast(CONN(ic), ":irc.bip.net NOTICE pouet "
 
1798
                                        ":==== Error while trusting test!\r\n");
 
1799
                else
 
1800
                        write_line_fast(CONN(ic), ":irc.bip.net NOTICE pouet "
 
1801
                                        ":==== Certificate now trusted.\r\n");
 
1802
 
 
1803
                BIO_free_all(bio);
 
1804
                X509_free(trustcert);
 
1805
        } else if (irc_line_elem_case_equals(line, 2, "NO")) {
 
1806
                /* NO, discard the cert! */
 
1807
                write_line_fast(CONN(ic), ":irc.bip.net NOTICE pouet "
 
1808
                                ":==== Certificate discarded.\r\n");
 
1809
 
 
1810
                X509_free(sk_X509_shift(LINK(ic)->untrusted_certs));
 
1811
        } else
 
1812
                return ERR_PROTOCOL;
 
1813
 
 
1814
        if (!ssl_check_trust(ic)) {
 
1815
                write_line_fast(CONN(ic), ":irc.bip.net NOTICE pouet "
 
1816
                                ":No more certificates waiting awaiting "
 
1817
                                "user trust, thanks!\r\n");
 
1818
                write_line_fast(CONN(ic), ":irc.bip.net NOTICE pouet "
 
1819
                                ":If the certificate is trusted, bip should "
 
1820
                                "be able to connect to the server on the "
 
1821
                                "next retry. Please wait a while and try "
 
1822
                                "connecting your client again.\r\n");
 
1823
 
 
1824
                LINK(ic)->recon_timer = 1; /* Speed up reconnection... */
 
1825
                unbind_from_link(ic);
 
1826
                return OK_CLOSE;
 
1827
        }
 
1828
        return OK_FORGET;
 
1829
}
 
1830
#endif
 
1831
 
 
1832
void _bip_notify(struct link_client *ic, char *fmt, va_list ap)
 
1833
{
 
1834
        char *nick;
 
1835
        char str[4096];
 
1836
 
 
1837
        if (LINK(ic)->l_server)
 
1838
                nick = LINK(ic)->l_server->nick;
 
1839
        else
 
1840
                nick = LINK(ic)->prev_nick;
 
1841
 
 
1842
        vsnprintf(str, 4095, fmt, ap);
 
1843
        str[4095] = 0;
 
1844
        WRITE_LINE2(CONN(ic), P_IRCMASK, (LINK(ic)->user->bip_use_notice ?
 
1845
                                "NOTICE" : "PRIVMSG"), nick, str);
 
1846
}
 
1847
 
 
1848
void bip_notify(struct link_client *ic, char *fmt, ...)
 
1849
{
 
1850
        va_list ap;
 
1851
 
 
1852
        va_start(ap, fmt);
 
1853
        _bip_notify(ic, fmt, ap);
 
1854
        va_end(ap);
 
1855
}
 
1856
 
 
1857
void adm_blreset(struct link_client *ic)
 
1858
{
 
1859
        log_reset_all(LINK(ic)->log);
 
1860
        bip_notify(ic, "backlog resetted for this network.");
 
1861
}
 
1862
 
 
1863
void adm_blreset_store(struct link_client *ic, const char *store)
 
1864
{
 
1865
        log_reset_store(LINK(ic)->log, store);
 
1866
        bip_notify(ic, "backlog resetted for %s.", store);
 
1867
}
 
1868
 
 
1869
void adm_follow_nick(struct link_client *ic, const char *val)
 
1870
{
 
1871
        struct link *link = LINK(ic);
 
1872
        if (strcasecmp(val, "TRUE") == 0) {
 
1873
                link->follow_nick = 1;
 
1874
                bip_notify(ic, "follow_nick is now true.");
 
1875
        } else {
 
1876
                link->follow_nick = 0;
 
1877
                bip_notify(ic, "follow_nick is now false.");
 
1878
        }
 
1879
}
 
1880
 
 
1881
void adm_ignore_first_nick(struct link_client *ic, const char *val)
 
1882
{
 
1883
        struct link *link = LINK(ic);
 
1884
        if (strcasecmp(val, "TRUE") == 0) {
 
1885
                link->ignore_first_nick = 1;
 
1886
                bip_notify(ic, "ignore_first_nick is now true.");
 
1887
        } else {
 
1888
                link->ignore_first_nick = 0;
 
1889
                bip_notify(ic, "ignore_first_nick is now false.");
 
1890
        }
 
1891
}
 
1892
 
 
1893
void set_on_connect_send(struct link_client *ic, char *val)
 
1894
{
 
1895
        struct link *link = LINK(ic);
 
1896
        char *s;
 
1897
 
 
1898
        if (val != NULL) {
 
1899
                list_add_last(&link->on_connect_send, bip_strdup(val));
 
1900
                bip_notify(ic, "added to on_connect_send.");
 
1901
        } else {
 
1902
                s = list_remove_last(&link->on_connect_send);
 
1903
                if (s)
 
1904
                        free(s);
 
1905
                bip_notify(ic, "last on_connect_send string deleted.");
 
1906
        }
 
1907
}
 
1908
 
 
1909
#define ON_CONNECT_MAX_STRSIZE 1024
 
1910
void adm_on_connect_send(struct link_client *ic, struct line *line,
 
1911
                unsigned int privmsg)
 
1912
{
 
1913
        char buf[ON_CONNECT_MAX_STRSIZE];
 
1914
        int t_written = 0;
 
1915
        int i;
 
1916
 
 
1917
        if (!line) {
 
1918
                set_on_connect_send(ic, NULL);
 
1919
                return;
 
1920
        }
 
1921
 
 
1922
        if (irc_line_includes(line, 2))
 
1923
                return;
 
1924
 
 
1925
        for (i = privmsg + 2; i < irc_line_count(line); i++) {
 
1926
                if (t_written) {
 
1927
                        t_written += snprintf(buf,
 
1928
                                        ON_CONNECT_MAX_STRSIZE - 1 - t_written,
 
1929
                                        " %s", irc_line_elem(line, i));
 
1930
                        if (t_written >= ON_CONNECT_MAX_STRSIZE)
 
1931
                                goto noroom;
 
1932
                } else {
 
1933
                        t_written = snprintf(buf, ON_CONNECT_MAX_STRSIZE - 1,
 
1934
                                        "%s", irc_line_elem(line, i));
 
1935
                        if (t_written >= ON_CONNECT_MAX_STRSIZE)
 
1936
                                goto noroom;
 
1937
                }
 
1938
        }
 
1939
ok:
 
1940
        buf[ON_CONNECT_MAX_STRSIZE - 1] = 0;
 
1941
        set_on_connect_send(ic, buf);
 
1942
        return;
 
1943
noroom:
 
1944
        bip_notify(ic, "on connect send string too big, truncated");
 
1945
        goto ok;
 
1946
}
 
1947
 
 
1948
void adm_away_nick(struct link_client *ic, const char *val)
 
1949
{
 
1950
        struct link *link = LINK(ic);
 
1951
        if (link->away_nick) {
 
1952
                free(link->away_nick);
 
1953
                link->away_nick = NULL;
 
1954
        }
 
1955
        if (val != NULL) {
 
1956
                link->away_nick = bip_strdup(val);
 
1957
                bip_notify(ic, "away_nick set.");
 
1958
        } else {
 
1959
                bip_notify(ic, "away_nick cleared.");
 
1960
        }
 
1961
}
 
1962
 
 
1963
void adm_bip_help(struct link_client *ic, int admin, const char *subhelp)
 
1964
{
 
1965
        if (subhelp == NULL) {
 
1966
                if (admin) {
 
1967
                        bip_notify(ic, "/BIP RELOAD # Re-read bip "
 
1968
                                        "configuration and apply changes.");
 
1969
                        bip_notify(ic, "/BIP INFO user <username> "
 
1970
                                        "# show a user's configuration");
 
1971
                        bip_notify(ic, "/BIP LIST networks|users|connections|"
 
1972
                                        "all_links|all_connections");
 
1973
                        bip_notify(ic, "/BIP ADD_CONN <connection name> "
 
1974
                                        "<network>");
 
1975
                        bip_notify(ic, "/BIP DEL_CONN <connection name>");
 
1976
                } else {
 
1977
                        bip_notify(ic, "/BIP LIST networks|connections");
 
1978
                }
 
1979
                bip_notify(ic, "/BIP JUMP # jump to next server (in same "
 
1980
                                "network)");
 
1981
                bip_notify(ic, "/BIP BLRESET [channel|query]# reset backlog "
 
1982
                                "(this connection only). Add -q flag and the "
 
1983
                                "operation is quiet. You can specify a channel "
 
1984
                                "or a nick to reset only this channel/query.");
 
1985
                bip_notify(ic, "/BIP HELP [subhelp] # show this help...");
 
1986
                bip_notify(ic, "## Temporary changes for this connection:");
 
1987
                bip_notify(ic, "/BIP FOLLOW_NICK|IGNORE_FIRST_NICK TRUE|FALSE");
 
1988
                bip_notify(ic, "/BIP ON_CONNECT_SEND <str> # Adds a string to "
 
1989
                        "send on connect");
 
1990
                bip_notify(ic, "/BIP ON_CONNECT_SEND # Clears on_connect_send");
 
1991
                bip_notify(ic, "/BIP AWAY_NICK <nick> # Set away nick");
 
1992
                bip_notify(ic, "/BIP AWAY_NICK # clear away nick");
 
1993
                bip_notify(ic, "/BIP BACKLOG [n] # backlog text of the n last "
 
1994
                                "hours");
 
1995
        } else if (admin && strcasecmp(subhelp, "RELOAD") == 0) {
 
1996
                bip_notify(ic, "/BIP RELOAD (admin only) :");
 
1997
                bip_notify(ic, "  Reloads bip configuration file and apply "
 
1998
                        "changes.");
 
1999
                bip_notify(ic, "  Please note that changes to 'user' or "
 
2000
                        "'realname' will not be applied without a JUMP.");
 
2001
        } else if (admin && strcasecmp(subhelp, "INFO") == 0) {
 
2002
                bip_notify(ic, "/BIP INFO USER <user> (admin only) :");
 
2003
                bip_notify(ic, "  Show <user>'s current configuration.");
 
2004
                bip_notify(ic, "  That means it may be different from the "
 
2005
                        "configuration stored in bip.conf");
 
2006
        } else if (admin && strcasecmp(subhelp, "ADD_CONN") == 0) {
 
2007
                bip_notify(ic, "/BIP ADD_CONN <connection name> <network> "
 
2008
                        "(admin only) :");
 
2009
                bip_notify(ic, "  Add a connection named <connection name> to "
 
2010
                        "the network <network> to your connection list");
 
2011
                bip_notify(ic, "  <network> should already exist in bip's "
 
2012
                        "configuration.");
 
2013
        } else if (admin && strcasecmp(subhelp, "DEL_CONN") == 0) {
 
2014
                bip_notify(ic, "/BIP DEL_CONN <connection name> (admin only) "
 
2015
                        ":");
 
2016
                bip_notify(ic, "  Remove the connection named <connection "
 
2017
                        "name> from your connection list.");
 
2018
                bip_notify(ic, "  Removing a connection will cause "
 
2019
                        "its disconnection.");
 
2020
        } else if (strcasecmp(subhelp, "JUMP") == 0) {
 
2021
                bip_notify(ic, "/BIP JUMP :");
 
2022
                bip_notify(ic, "  Jump to next server in current network.");
 
2023
        } else if (strcasecmp(subhelp, "BLRESET") == 0) {
 
2024
                bip_notify(ic, "/BIP BLRESET :");
 
2025
                bip_notify(ic, "  Reset backlog on this network.");
 
2026
        } else if (strcasecmp(subhelp, "FOLLOW_NICK") == 0) {
 
2027
                bip_notify(ic, "/BIP FOLLOW_NICK TRUE|FALSE :");
 
2028
                bip_notify(ic, "  Change the value of the follow_nick option "
 
2029
                        "for this connection.");
 
2030
                bip_notify(ic, "  If set to true, when you change nick, "
 
2031
                        "BIP stores the new nickname as the new default "
 
2032
                        "nickname value.");
 
2033
                bip_notify(ic, "  Thus, if you are disconnected from the "
 
2034
                        "server, BIP will restore the correct nickname.");
 
2035
        } else if (strcasecmp(subhelp, "IGNORE_FIRST_NICK") == 0) {
 
2036
                bip_notify(ic, "/BIP IGNORE_FIRST_NICK TRUE|FALSE :");
 
2037
                bip_notify(ic, "  Change the value of the ignore_first_nick "
 
2038
                        "option for this connection.");
 
2039
                bip_notify(ic, "  If set to TRUE, BIP will ignore the nickname"
 
2040
                        "sent by the client upon connect.");
 
2041
                bip_notify(ic, "  Further nickname changes will be processed "
 
2042
                        "as usual.");
 
2043
        } else if (strcasecmp(subhelp, "ON_CONNECT_SEND") == 0) {
 
2044
                bip_notify(ic, "/BIP ON_CONNECT_SEND [some text] :");
 
2045
                bip_notify(ic, "  BIP will send the text as is to the server "
 
2046
                        "upon connection.");
 
2047
                bip_notify(ic, "  You can call this command more than once.");
 
2048
                bip_notify(ic, "  If [some text] is empty, this command will "
 
2049
                        "remove any on_connect_send defined for this connection.");
 
2050
        } else if (strcasecmp(subhelp, "AWAY_NICK") == 0) {
 
2051
                bip_notify(ic, "/BIP AWAY_NICK [some_nick] :");
 
2052
                bip_notify(ic, "  If [some_nick] is set, BIP will change "
 
2053
                        "nickname to [some_nick] if there are no more client "
 
2054
                        "attached");
 
2055
                bip_notify(ic, "  If [some_nick] is empty, this command will "
 
2056
                        "unset current connection's away_nick.");
 
2057
        } else if (strcasecmp(subhelp, "LIST") == 0) {
 
2058
                bip_notify(ic, "/BIP LIST <section> :");
 
2059
                bip_notify(ic, "  List information from a these sections :");
 
2060
                bip_notify(ic, "  - networks: list all available networks");
 
2061
                bip_notify(ic, "  - connections: list all your configured "
 
2062
                        "connections and their state.");
 
2063
                if (admin) {
 
2064
                        bip_notify(ic, "  - users: list all users (admin)");
 
2065
                        bip_notify(ic, "  - all_links: list all connected "
 
2066
                                "sockets from and to BIP (admin)");
 
2067
                        bip_notify(ic, "  - all_connections: list all users' "
 
2068
                                "configured connections (admin)");
 
2069
                }
 
2070
        } else {
 
2071
                bip_notify(ic, "-- No sub-help for '%s'", subhelp);
 
2072
        }
 
2073
}
 
2074
 
 
2075
int adm_bip(bip_t *bip, struct link_client *ic, struct line *line, int privmsg)
 
2076
{
 
2077
        int admin = LINK(ic)->user->admin;
 
2078
 
 
2079
        if (privmsg) {
 
2080
                char *linestr, *elemstr;
 
2081
                char *ptr, *eptr;
 
2082
                int slen;
 
2083
 
 
2084
                if (irc_line_count(line) != 3)
 
2085
                        return OK_FORGET;
 
2086
 
 
2087
                linestr = irc_line_pop(line);
 
2088
                ptr = linestr;
 
2089
 
 
2090
                /* all elem size <= linestr size */
 
2091
                elemstr = bip_malloc(strlen(linestr) + 1);
 
2092
 
 
2093
                while((eptr = strstr(ptr, " "))) {
 
2094
                        slen = eptr - ptr;
 
2095
                        if (slen == 0) {
 
2096
                                ptr++;
 
2097
                                continue;
 
2098
                        }
 
2099
                        memcpy(elemstr, ptr, slen);
 
2100
                        elemstr[slen] = 0;
 
2101
                        irc_line_append(line, elemstr);
 
2102
                        ptr = eptr + 1;
 
2103
                }
 
2104
                eptr = ptr + strlen(ptr);
 
2105
                slen = eptr - ptr;
 
2106
                if (slen != 0) {
 
2107
                        memcpy(elemstr, ptr, slen);
 
2108
                        elemstr[slen] = 0;
 
2109
                        irc_line_append(line, elemstr);
 
2110
                }
 
2111
                free(elemstr);
 
2112
                free(linestr);
 
2113
        }
 
2114
 
 
2115
        if (!irc_line_includes(line, privmsg + 1))
 
2116
                return OK_FORGET;
 
2117
 
 
2118
        mylog(LOG_INFO, "/BIP %s from %s", irc_line_elem(line, privmsg + 1),
 
2119
                        LINK(ic)->user->name);
 
2120
        if (strcasecmp(irc_line_elem(line, privmsg + 1), "RELOAD") == 0) {
 
2121
                if (!admin) {
 
2122
                        bip_notify(ic, "-- You're not allowed to reload bip");
 
2123
                        return OK_FORGET;
 
2124
                }
 
2125
                bip_notify(ic, "-- Reloading bip...");
 
2126
                bip->reloading_client = ic;
 
2127
                sighup = 1;
 
2128
        } else if (strcasecmp(irc_line_elem(line, privmsg + 1), "LIST") == 0) {
 
2129
                if (irc_line_count(line) != privmsg + 3) {
 
2130
                        bip_notify(ic, "-- LIST command needs one argument");
 
2131
                        return OK_FORGET;
 
2132
                }
 
2133
 
 
2134
                if (admin && strcasecmp(irc_line_elem(line, privmsg + 2),
 
2135
                                        "users") == 0) {
 
2136
                        adm_list_users(ic);
 
2137
                } else if (strcasecmp(irc_line_elem(line, privmsg + 2),
 
2138
                                        "networks") == 0) {
 
2139
                        adm_list_networks(ic);
 
2140
                } else if (strcasecmp(irc_line_elem(line, privmsg + 2),
 
2141
                                        "connections") == 0) {
 
2142
                        adm_list_connections(ic, NULL);
 
2143
                } else if (admin && strcasecmp(irc_line_elem(line, privmsg + 2),
 
2144
                                        "all_connections") == 0) {
 
2145
                        adm_list_all_connections(ic);
 
2146
                } else if (admin && strcasecmp(irc_line_elem(line, privmsg + 2),
 
2147
                                        "all_links") == 0) {
 
2148
                        adm_list_all_links(ic);
 
2149
                } else {
 
2150
                        bip_notify(ic, "-- Invalid LIST request");
 
2151
                }
 
2152
        } else if (strcasecmp(irc_line_elem(line, privmsg + 1), "INFO") == 0) {
 
2153
                if (!irc_line_includes(line, privmsg + 2)) {
 
2154
                        bip_notify(ic, "-- INFO command needs at least one "
 
2155
                                        "argument");
 
2156
                        return OK_FORGET;
 
2157
                }
 
2158
 
 
2159
                if (admin && irc_line_elem_case_equals(line, privmsg + 2,
 
2160
                                        "user") == 0) {
 
2161
                        if (irc_line_count(line) == privmsg + 4) {
 
2162
                                adm_info_user(ic,
 
2163
                                        irc_line_elem(line, privmsg + 3));
 
2164
                        } else {
 
2165
                                bip_notify(ic, "-- INFO USER command needs one"
 
2166
                                        " argument");
 
2167
                        }
 
2168
#if 0
 
2169
                        TODO network info
 
2170
#endif
 
2171
                } else {
 
2172
                        bip_notify(ic, "-- Invalid INFO request");
 
2173
                }
 
2174
        } else if (irc_line_elem_case_equals(line, privmsg + 1, "JUMP")) {
 
2175
                if (LINK(ic)->l_server) {
 
2176
                        WRITE_LINE1(CONN(LINK(ic)->l_server), NULL, "QUIT",
 
2177
                                        "jumpin' jumpin'");
 
2178
                        connection_close(CONN(LINK(ic)->l_server));
 
2179
                }
 
2180
                bip_notify(ic, "-- Jumping to next server");
 
2181
        } else if (irc_line_elem_case_equals(line, privmsg + 1, "BLRESET")) {
 
2182
                if (irc_line_includes(line, privmsg + 2)) {
 
2183
                        if (irc_line_elem_equals(line, privmsg + 2, "-q")) {
 
2184
                                if (irc_line_includes(line, privmsg + 3)) {
 
2185
                                        log_reset_store(LINK(ic)->log,
 
2186
                                                irc_line_elem(line,
 
2187
                                                        privmsg + 3));
 
2188
                                } else {
 
2189
                                        log_reset_all(LINK(ic)->log);
 
2190
                                }
 
2191
                        } else {
 
2192
                                adm_blreset_store(ic, irc_line_elem(line,
 
2193
                                                        privmsg + 2));
 
2194
                        }
 
2195
                } else {
 
2196
                        adm_blreset(ic);
 
2197
                }
 
2198
        } else if (irc_line_elem_case_equals(line, privmsg + 1, "HELP")) {
 
2199
                if (irc_line_count(line) == privmsg + 2)
 
2200
                        adm_bip_help(ic, admin, NULL);
 
2201
                else if (irc_line_count(line) == privmsg + 3)
 
2202
                        adm_bip_help(ic, admin,
 
2203
                                        irc_line_elem(line, privmsg + 2));
 
2204
                else
 
2205
                        bip_notify(ic,
 
2206
                                "-- HELP command needs at most one argument");
 
2207
        } else if (irc_line_elem_case_equals(line, privmsg + 1, "FOLLOW_NICK")) {
 
2208
                if (irc_line_count(line) != privmsg + 3) {
 
2209
                        bip_notify(ic,
 
2210
                                "-- FOLLOW_NICK command needs one argument");
 
2211
                        return OK_FORGET;
 
2212
                }
 
2213
                adm_follow_nick(ic, irc_line_elem(line, privmsg + 2));
 
2214
        } else if (irc_line_elem_case_equals(line, privmsg + 1,
 
2215
                                "IGNORE_FIRST_NICK")) {
 
2216
                if (irc_line_count(line) != privmsg + 3) {
 
2217
                        bip_notify(ic, "-- IGNORE_FIRST_NICK "
 
2218
                                "command needs one argument");
 
2219
                        return OK_FORGET;
 
2220
                }
 
2221
                adm_ignore_first_nick(ic, irc_line_elem(line, privmsg + 2));
 
2222
        } else if (irc_line_elem_case_equals(line, privmsg + 1,
 
2223
                                "ON_CONNECT_SEND")) {
 
2224
                if (irc_line_count(line) == privmsg + 2) {
 
2225
                        adm_on_connect_send(ic, NULL, 0);
 
2226
                } else if (irc_line_includes(line, privmsg + 2)) {
 
2227
                        adm_on_connect_send(ic, line, privmsg);
 
2228
                } else {
 
2229
                        bip_notify(ic, "-- ON_CONNECT_SEND command needs at "
 
2230
                                "least one argument");
 
2231
                }
 
2232
        } else if (irc_line_elem_case_equals(line, privmsg + 1, "AWAY_NICK")) {
 
2233
                if (irc_line_count(line) == privmsg + 2) {
 
2234
                        adm_away_nick(ic, NULL);
 
2235
                } else if (irc_line_count(line) == privmsg + 3) {
 
2236
                        adm_away_nick(ic, irc_line_elem(line, privmsg + 2));
 
2237
                } else {
 
2238
                        bip_notify(ic, "-- AWAY_NICK command needs zero or one"
 
2239
                                " argument");
 
2240
                }
 
2241
        } else if (irc_line_elem_case_equals(line, privmsg + 1, "BACKLOG")) {
 
2242
                if (irc_line_count(line) == privmsg + 2) {
 
2243
                        irc_cli_backlog(ic, 0);
 
2244
                } else if (irc_line_count(line) == privmsg + 3) {
 
2245
                        int hours = atoi(irc_line_elem(line, privmsg + 2));
 
2246
                        irc_cli_backlog(ic, hours);
 
2247
                } else {
 
2248
                        bip_notify(ic, "-- BACKLOG takes 0 or one argument");
 
2249
                }
 
2250
        } else if (admin && irc_line_elem_case_equals(line, privmsg + 1,
 
2251
                                "ADD_CONN")) {
 
2252
                if (irc_line_count(line) != privmsg + 4) {
 
2253
                        bip_notify(ic, "/BIP ADD_CONN <connection name> "
 
2254
                                        "<network name>");
 
2255
                } else {
 
2256
                        adm_bip_addconn(bip, ic,
 
2257
                                        irc_line_elem(line, privmsg + 2),
 
2258
                                        irc_line_elem(line, privmsg + 3));
 
2259
                }
 
2260
        } else if (admin && irc_line_elem_case_equals(line, privmsg + 1,
 
2261
                                "DEL_CONN")) {
 
2262
                if (irc_line_count(line) != privmsg + 3) {
 
2263
                        bip_notify(ic, "/BIP DEL_CONN <connection name>");
 
2264
                } else {
 
2265
                        adm_bip_delconn(bip, ic,
 
2266
                                        irc_line_elem(line, privmsg + 2));
 
2267
                }
 
2268
        } else {
 
2269
                bip_notify(ic, "Unknown command.");
 
2270
        }
 
2271
        return OK_FORGET;
 
2272
}
 
2273
 
 
2274
void free_conf(list_t *l)
 
2275
{
 
2276
        struct tuple *t;
 
2277
        list_iterator_t li;
 
2278
 
 
2279
        for (list_it_init(l, &li); (t = list_it_item(&li)); list_it_next(&li)) {
 
2280
                switch (t->tuple_type) {
 
2281
                case TUPLE_STR:
 
2282
                        free(t->pdata);
 
2283
                        break;
 
2284
                case TUPLE_INT:
 
2285
                        break;
 
2286
                case TUPLE_LIST:
 
2287
                        free_conf(t->pdata);
 
2288
                        break;
 
2289
                default:
 
2290
                        fatal("internal error free_conf");
 
2291
                        break;
 
2292
                }
 
2293
                free(t);
 
2294
        }
 
2295
        free(l);
 
2296
}
 
2297