source: protocols/account.c @ 9698fc0

Last change on this file since 9698fc0 was 3ac6d9f, checked in by Dennis Kaarsemaker <dennis@…>, at 2016-03-23T06:44:13Z

Support for locked-down accounts

In certain situations, e.g. when working with pregenerated
configurations, it is useful to be able lock down accounts so they
cannot be deleted and authentication information (user, password,
server) cannot be changed.

We mark such sensitive settings with ACC_SET_LOCKABLE and will refuse to
change them if the account is locked by setting the ACC_FLAG_LOCKED
flag.

This flag is stored in the xml files as account attribute locked="true".

  • Property mode set to 100644
File size: 10.9 KB
Line 
1/********************************************************************\
2  * BitlBee -- An IRC to other IM-networks gateway                     *
3  *                                                                    *
4  * Copyright 2002-2013 Wilmer van der Gaast and others                *
5  \********************************************************************/
6
7/* Account management functions                                         */
8
9/*
10  This program is free software; you can redistribute it and/or modify
11  it under the terms of the GNU General Public License as published by
12  the Free Software Foundation; either version 2 of the License, or
13  (at your option) any later version.
14
15  This program is distributed in the hope that it will be useful,
16  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  GNU General Public License for more details.
19
20  You should have received a copy of the GNU General Public License with
21  the Debian GNU/Linux distribution in /usr/share/common-licenses/GPL;
22  if not, write to the Free Software Foundation, Inc., 51 Franklin St.,
23  Fifth Floor, Boston, MA  02110-1301  USA
24*/
25
26#define BITLBEE_CORE
27#include "bitlbee.h"
28#include "account.h"
29
30static const char* account_protocols_local[] = {
31        "gg", "whatsapp", NULL
32};
33
34static char *set_eval_nick_source(set_t *set, char *value);
35
36account_t *account_add(bee_t *bee, struct prpl *prpl, char *user, char *pass)
37{
38        account_t *a;
39        set_t *s;
40        char tag[strlen(prpl->name) + 10];
41
42        if (bee->accounts) {
43                for (a = bee->accounts; a->next; a = a->next) {
44                        ;
45                }
46                a = a->next = g_new0(account_t, 1);
47        } else {
48                bee->accounts = a = g_new0(account_t, 1);
49        }
50
51        a->prpl = prpl;
52        a->user = g_strdup(user);
53        a->pass = g_strdup(pass);
54        a->auto_connect = 1;
55        a->bee = bee;
56
57        s = set_add(&a->set, "auto_connect", "true", set_eval_account, a);
58        s->flags |= SET_NOSAVE;
59
60        s = set_add(&a->set, "auto_reconnect", "true", set_eval_bool, a);
61
62        s = set_add(&a->set, "nick_format", NULL, NULL, a);
63        s->flags |= SET_NULL_OK;
64
65        s = set_add(&a->set, "nick_source", "handle", set_eval_nick_source, a);
66        s->flags |= SET_NOSAVE; /* Just for bw compatibility! */
67
68        s = set_add(&a->set, "password", NULL, set_eval_account, a);
69        s->flags |= SET_NOSAVE | SET_NULL_OK | SET_PASSWORD | ACC_SET_LOCKABLE;
70
71        s = set_add(&a->set, "tag", NULL, set_eval_account, a);
72        s->flags |= SET_NOSAVE;
73
74        s = set_add(&a->set, "username", NULL, set_eval_account, a);
75        s->flags |= SET_NOSAVE | ACC_SET_OFFLINE_ONLY | ACC_SET_LOCKABLE;
76        set_setstr(&a->set, "username", user);
77
78        /* Hardcode some more clever tag guesses. */
79        strcpy(tag, prpl->name);
80        if (strcmp(prpl->name, "oscar") == 0) {
81                if (g_ascii_isdigit(a->user[0])) {
82                        strcpy(tag, "icq");
83                } else {
84                        strcpy(tag, "aim");
85                }
86        } else if (strcmp(prpl->name, "jabber") == 0) {
87                if (strstr(a->user, "@gmail.com") ||
88                    strstr(a->user, "@googlemail.com")) {
89                        strcpy(tag, "gtalk");
90                }
91        }
92
93        if (account_by_tag(bee, tag)) {
94                char *numpos = tag + strlen(tag);
95                int i;
96
97                for (i = 2; i < 10000; i++) {
98                        sprintf(numpos, "%d", i);
99                        if (!account_by_tag(bee, tag)) {
100                                break;
101                        }
102                }
103        }
104        set_setstr(&a->set, "tag", tag);
105
106        a->nicks = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, g_free);
107
108        /* This function adds some more settings (and might want to do more
109           things that have to be done now, although I can't think of anything. */
110        if (prpl->init) {
111                prpl->init(a);
112        }
113
114        s = set_add(&a->set, "away", NULL, set_eval_account, a);
115        s->flags |= SET_NULL_OK;
116
117        if (a->flags & ACC_FLAG_STATUS_MESSAGE) {
118                s = set_add(&a->set, "status", NULL, set_eval_account, a);
119                s->flags |= SET_NULL_OK;
120        }
121
122        return a;
123}
124
125char *set_eval_account(set_t *set, char *value)
126{
127        account_t *acc = set->data;
128
129        /* Double-check: We refuse to edit on-line accounts. */
130        if (set->flags & ACC_SET_OFFLINE_ONLY && acc->ic) {
131                return SET_INVALID;
132        }
133
134        if (strcmp(set->key, "server") == 0) {
135                g_free(acc->server);
136                if (value && *value) {
137                        acc->server = g_strdup(value);
138                        return value;
139                } else {
140                        acc->server = g_strdup(set->def);
141                        return g_strdup(set->def);
142                }
143        } else if (strcmp(set->key, "username") == 0) {
144                g_free(acc->user);
145                acc->user = g_strdup(value);
146                return value;
147        } else if (strcmp(set->key, "password") == 0) {
148                /* set -del allows /oper to be used to change the password or,
149                   iff oauth is enabled, reset the oauth credential magic.
150                */
151                if (!value) {
152                        if (set_getbool(&(acc->set), "oauth")) {
153                                value = "";
154                        } else {
155                                value = PASSWORD_PENDING;
156                                ((irc_t *) acc->bee->ui_data)->status |= OPER_HACK_ACCOUNT_PASSWORD;
157                                irc_rootmsg((irc_t *) acc->bee->ui_data, "You may now use /OPER to set the password");
158                        }
159                }
160
161                g_free(acc->pass);
162                acc->pass = g_strdup(value);
163                return NULL;    /* password shouldn't be visible in plaintext! */
164        } else if (strcmp(set->key, "tag") == 0) {
165                account_t *oa;
166
167                /* Enforce uniqueness. */
168                if ((oa = account_by_tag(acc->bee, value)) && oa != acc) {
169                        return SET_INVALID;
170                }
171
172                g_free(acc->tag);
173                acc->tag = g_strdup(value);
174                return value;
175        } else if (strcmp(set->key, "auto_connect") == 0) {
176                if (!is_bool(value)) {
177                        return SET_INVALID;
178                }
179
180                acc->auto_connect = bool2int(value);
181                return value;
182        } else if (strcmp(set->key, "away") == 0 ||
183                   strcmp(set->key, "status") == 0) {
184                if (acc->ic && acc->ic->flags & OPT_LOGGED_IN) {
185                        /* If we're currently on-line, set the var now already
186                           (bit of a hack) and send an update. */
187                        g_free(set->value);
188                        set->value = g_strdup(value);
189
190                        imc_away_send_update(acc->ic);
191                }
192
193                return value;
194        }
195
196        return SET_INVALID;
197}
198
199/* For bw compatibility, have this write-only setting. */
200static char *set_eval_nick_source(set_t *set, char *value)
201{
202        account_t *a = set->data;
203
204        if (strcmp(value, "full_name") == 0) {
205                set_setstr(&a->set, "nick_format", "%full_name");
206        } else if (strcmp(value, "first_name") == 0) {
207                set_setstr(&a->set, "nick_format", "%first_name");
208        } else {
209                set_setstr(&a->set, "nick_format", "%-@nick");
210        }
211
212        return value;
213}
214
215account_t *account_get(bee_t *bee, const char *id)
216{
217        account_t *a, *ret = NULL;
218        char *handle, *s;
219        int nr;
220
221        /* Tags get priority above anything else. */
222        if ((a = account_by_tag(bee, id))) {
223                return a;
224        }
225
226        /* This checks if the id string ends with (...) */
227        if ((handle = strchr(id, '(')) && (s = strchr(handle, ')')) && s[1] == 0) {
228                struct prpl *proto;
229
230                *s = *handle = 0;
231                handle++;
232
233                if ((proto = find_protocol(id))) {
234                        for (a = bee->accounts; a; a = a->next) {
235                                if (a->prpl == proto &&
236                                    a->prpl->handle_cmp(handle, a->user) == 0) {
237                                        ret = a;
238                                }
239                        }
240                }
241
242                /* Restore the string. */
243                handle--;
244                *handle = '(';
245                *s = ')';
246
247                if (ret) {
248                        return ret;
249                }
250        }
251
252        if (sscanf(id, "%d", &nr) == 1 && nr < 1000) {
253                for (a = bee->accounts; a; a = a->next) {
254                        if ((nr--) == 0) {
255                                return(a);
256                        }
257                }
258
259                return(NULL);
260        }
261
262        for (a = bee->accounts; a; a = a->next) {
263                if (g_strcasecmp(id, a->prpl->name) == 0) {
264                        if (!ret) {
265                                ret = a;
266                        } else {
267                                return(NULL);   /* We don't want to match more than one... */
268                        }
269                } else if (strstr(a->user, id)) {
270                        if (!ret) {
271                                ret = a;
272                        } else {
273                                return(NULL);
274                        }
275                }
276        }
277
278        return(ret);
279}
280
281account_t *account_by_tag(bee_t *bee, const char *tag)
282{
283        account_t *a;
284
285        for (a = bee->accounts; a; a = a->next) {
286                if (a->tag && g_strcasecmp(tag, a->tag) == 0) {
287                        return a;
288                }
289        }
290
291        return NULL;
292}
293
294void account_del(bee_t *bee, account_t *acc)
295{
296        account_t *a, *l = NULL;
297
298        if (acc->ic) {
299                /* Caller should have checked, accounts still in use can't be deleted. */
300                return;
301        }
302
303        for (a = bee->accounts; a; a = (l = a)->next) {
304                if (a == acc) {
305                        if (l) {
306                                l->next = a->next;
307                        } else {
308                                bee->accounts = a->next;
309                        }
310
311                        /** FIXME
312                        for( c = bee->chatrooms; c; c = nc )
313                        {
314                                nc = c->next;
315                                if( acc == c->acc )
316                                        chat_del( bee, c );
317                        }
318                        */
319
320                        while (a->set) {
321                                set_del(&a->set, a->set->key);
322                        }
323
324                        g_hash_table_destroy(a->nicks);
325
326                        g_free(a->tag);
327                        g_free(a->user);
328                        g_free(a->pass);
329                        g_free(a->server);
330                        if (a->reconnect) {     /* This prevents any reconnect still queued to happen */
331                                cancel_auto_reconnect(a);
332                        }
333                        g_free(a);
334
335                        break;
336                }
337        }
338}
339
340static gboolean account_on_timeout(gpointer d, gint fd, b_input_condition cond);
341
342void account_on(bee_t *bee, account_t *a)
343{
344        if (a->ic) {
345                /* Trying to enable an already-enabled account */
346                return;
347        }
348
349        cancel_auto_reconnect(a);
350
351        a->reconnect = 0;
352        a->prpl->login(a);
353
354        if (a->ic && !(a->ic->flags & (OPT_SLOW_LOGIN | OPT_LOGGED_IN))) {
355                a->ic->keepalive = b_timeout_add(120000, account_on_timeout, a->ic);
356        }
357}
358
359void account_off(bee_t *bee, account_t *a)
360{
361        imc_logout(a->ic, FALSE);
362        a->ic = NULL;
363        if (a->reconnect) {
364                /* Shouldn't happen */
365                cancel_auto_reconnect(a);
366        }
367}
368
369static gboolean account_on_timeout(gpointer d, gint fd, b_input_condition cond)
370{
371        struct im_connection *ic = d;
372
373        if (!(ic->flags & (OPT_SLOW_LOGIN | OPT_LOGGED_IN))) {
374                imcb_error(ic, "Connection timeout");
375                imc_logout(ic, TRUE);
376        }
377
378        return FALSE;
379}
380
381struct account_reconnect_delay {
382        int start;
383        char op;
384        int step;
385        int max;
386};
387
388int account_reconnect_delay_parse(char *value, struct account_reconnect_delay *p)
389{
390        memset(p, 0, sizeof(*p));
391        /* A whole day seems like a sane "maximum maximum". */
392        p->max = 86400;
393
394        /* Format: /[0-9]+([*+][0-9]+(<[0-9+])?)?/ */
395        while (*value && g_ascii_isdigit(*value)) {
396                p->start = p->start * 10 + *value++ - '0';
397        }
398
399        /* Sure, call me evil for implementing my own fscanf here, but it's
400           dead simple and I immediately know where to continue parsing. */
401
402        if (*value == 0) {
403                /* If the string ends now, the delay is constant. */
404                return 1;
405        } else if (*value != '+' && *value != '*') {
406                /* Otherwise allow either a + or a * */
407                return 0;
408        }
409
410        p->op = *value++;
411
412        /* + or * the delay by this number every time. */
413        while (*value && g_ascii_isdigit(*value)) {
414                p->step = p->step * 10 + *value++ - '0';
415        }
416
417        if (*value == 0) {
418                /* Use the default maximum (one day). */
419                return 1;
420        } else if (*value != '<') {
421                return 0;
422        }
423
424        p->max = 0;
425        value++;
426        while (*value && g_ascii_isdigit(*value)) {
427                p->max = p->max * 10 + *value++ - '0';
428        }
429
430        return p->max > 0;
431}
432
433char *set_eval_account_reconnect_delay(set_t *set, char *value)
434{
435        struct account_reconnect_delay p;
436
437        return account_reconnect_delay_parse(value, &p) ? value : SET_INVALID;
438}
439
440int account_reconnect_delay(account_t *a)
441{
442        char *setting = set_getstr(&a->bee->set, "auto_reconnect_delay");
443        struct account_reconnect_delay p;
444
445        if (account_reconnect_delay_parse(setting, &p)) {
446                if (a->auto_reconnect_delay == 0) {
447                        a->auto_reconnect_delay = p.start;
448                } else if (p.op == '+') {
449                        a->auto_reconnect_delay += p.step;
450                } else if (p.op == '*') {
451                        a->auto_reconnect_delay *= p.step;
452                }
453
454                if (a->auto_reconnect_delay > p.max) {
455                        a->auto_reconnect_delay = p.max;
456                }
457        } else {
458                a->auto_reconnect_delay = 0;
459        }
460
461        return a->auto_reconnect_delay;
462}
463
464int protocol_account_islocal(const char* protocol)
465{
466        const char** p = account_protocols_local;
467
468        do {
469                if (strcmp(*p, protocol) == 0) {
470                        return 1;
471                }
472        } while (*(++p));
473        return 0;
474}
Note: See TracBrowser for help on using the repository browser.