source: unix.c @ d2d2b80

Last change on this file since d2d2b80 was 5535a47, checked in by dequis <dx@…>, at 2015-05-07T23:12:06Z

More coverity fixes!

CID 18634: 'Logically dead code' in jabber_get_info
CID 18638: 'Dereference after null check' in oauth2_access_token_done
CID 18691: 'Copy into fixed size buffer' in bee_irc_user_new
CID 20274: Leak in bee_irc_chat_invite
CID 20297, CID 20283: Leaks in crypt_main

Some the base64 leaks there weren't detected, needs modeling.

  • Property mode set to 100644
File size: 8.6 KB
RevLine 
[5ebff60]1/********************************************************************\
[b7d3cc34]2  * BitlBee -- An IRC to other IM-networks gateway                     *
3  *                                                                    *
[0e788f5]4  * Copyright 2002-2012 Wilmer van der Gaast and others                *
[b7d3cc34]5  \********************************************************************/
6
7/* Main file (Unix specific part)                                       */
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;
[6f10697]22  if not, write to the Free Software Foundation, Inc., 51 Franklin St.,
23  Fifth Floor, Boston, MA  02110-1301  USA
[b7d3cc34]24*/
25
26#include "bitlbee.h"
[7c9db24]27
28#include "arc.h"
29#include "base64.h"
[b7d3cc34]30#include "commands.h"
31#include "protocols/nogaim.h"
32#include "help.h"
[54879ab]33#include "ipc.h"
[7c9db24]34#include "md5.h"
35#include "misc.h"
[b7d3cc34]36#include <signal.h>
37#include <unistd.h>
38#include <sys/time.h>
[d25f6fc]39#include <sys/wait.h>
[aaf92a9]40#include <pwd.h>
[badd148]41#include <locale.h>
[ad46e4d]42#include <grp.h>
[b7d3cc34]43
[d150a9d]44#if defined(OTR_BI) || defined(OTR_PI)
45#include "otr.h"
46#endif
47
[5ebff60]48global_t global;        /* Against global namespace pollution */
[b7d3cc34]49
[7233f68]50static int signal_shutdown_pipe[2] = { -1, -1 };
[5ebff60]51static void sighandler_shutdown(int signal);
52static void sighandler_crash(int signal);
[b7d3cc34]53
[5ebff60]54static int crypt_main(int argc, char *argv[]);
[7c9db24]55
[5ebff60]56int main(int argc, char *argv[])
[b7d3cc34]57{
58        int i = 0;
[58bc4e6]59        char *old_cwd = NULL;
[b7d3cc34]60        struct sigaction sig, old;
[5ebff60]61
[badd148]62        /* Required to make iconv to ASCII//TRANSLIT work. This makes BitlBee
63           system-locale-sensitive. :-( */
[5ebff60]64        setlocale(LC_CTYPE, "");
65
66        if (argc > 1 && strcmp(argv[1], "-x") == 0) {
67                return crypt_main(argc, argv);
68        }
69
[d25f6fc]70        log_init();
[5ebff60]71
72        global.conf_file = g_strdup(CONF_FILE_DEF);
73        global.conf = conf_load(argc, argv);
74        if (global.conf == NULL) {
75                return(1);
76        }
77
[60c1a4e]78        b_main_init();
[5ebff60]79
[3fc6c32]80        /* libpurple doesn't like fork()s after initializing itself, so if
81           we use it, do this init a little later (in case we're running in
82           ForkDaemon mode). */
83#ifndef WITH_PURPLE
84        nogaim_init();
85#endif
[5ebff60]86
[858ea01]87#ifdef OTR_BI
[5ebff60]88        otr_init();
[858ea01]89#endif
[5ebff60]90
91        global.helpfile = g_strdup(HELP_FILE);
92        if (help_init(&global.help, global.helpfile) == NULL) {
93                log_message(LOGLVL_WARNING, "Error opening helpfile %s.", HELP_FILE);
[5674207]94        }
[b7d3cc34]95
[5ebff60]96        global.storage = storage_init(global.conf->primary_storage, global.conf->migrate_storage);
97        if (global.storage == NULL) {
98                log_message(LOGLVL_ERROR, "Unable to load storage backend '%s'", global.conf->primary_storage);
99                return(1);
[b7d3cc34]100        }
[5ebff60]101
102        if (global.conf->runmode == RUNMODE_INETD) {
103                log_link(LOGLVL_ERROR, LOGOUTPUT_IRC);
104                log_link(LOGLVL_WARNING, LOGOUTPUT_IRC);
105
106                i = bitlbee_inetd_init();
107                log_message(LOGLVL_INFO, "%s %s starting in inetd mode.", PACKAGE, BITLBEE_VERSION);
108
109        } else if (global.conf->runmode == RUNMODE_DAEMON) {
110                log_link(LOGLVL_ERROR, LOGOUTPUT_CONSOLE);
111                log_link(LOGLVL_WARNING, LOGOUTPUT_CONSOLE);
[99318ad]112
[b7d3cc34]113                i = bitlbee_daemon_init();
[5ebff60]114                log_message(LOGLVL_INFO, "%s %s starting in daemon mode.", PACKAGE, BITLBEE_VERSION);
115        } else if (global.conf->runmode == RUNMODE_FORKDAEMON) {
116                log_link(LOGLVL_ERROR, LOGOUTPUT_CONSOLE);
117                log_link(LOGLVL_WARNING, LOGOUTPUT_CONSOLE);
[156bbd7]118
[54879ab]119                /* In case the operator requests a restart, we need this. */
[5ebff60]120                old_cwd = g_malloc(256);
121                if (getcwd(old_cwd, 255) == NULL) {
122                        log_message(LOGLVL_WARNING, "Could not save current directory: %s", strerror(errno));
123                        g_free(old_cwd);
[54879ab]124                        old_cwd = NULL;
125                }
[5ebff60]126
[d25f6fc]127                i = bitlbee_daemon_init();
[5ebff60]128                log_message(LOGLVL_INFO, "%s %s starting in forking daemon mode.", PACKAGE, BITLBEE_VERSION);
129        }
130        if (i != 0) {
131                return(i);
[d25f6fc]132        }
[5ebff60]133
134        if ((global.conf->user && *global.conf->user) &&
135            (global.conf->runmode == RUNMODE_DAEMON ||
136             global.conf->runmode == RUNMODE_FORKDAEMON) &&
137            (!getuid() || !geteuid())) {
[aaf92a9]138                struct passwd *pw = NULL;
[5ebff60]139                pw = getpwnam(global.conf->user);
140                if (pw) {
141                        initgroups(global.conf->user, pw->pw_gid);
142                        setgid(pw->pw_gid);
143                        setuid(pw->pw_uid);
144                } else {
145                        log_message(LOGLVL_WARNING, "Failed to look up user %s.", global.conf->user);
[ad46e4d]146                }
[aaf92a9]147        }
[5ebff60]148
[b7d3cc34]149        /* Catch some signals to tell the user what's happening before quitting */
[5ebff60]150        memset(&sig, 0, sizeof(sig));
[7233f68]151        sig.sa_handler = SIG_IGN;
[5ebff60]152        sigaction(SIGCHLD, &sig, &old);
153        sigaction(SIGPIPE, &sig, &old);
[b7d3cc34]154        sig.sa_flags = SA_RESETHAND;
[7233f68]155        sig.sa_handler = sighandler_crash;
[5ebff60]156        sigaction(SIGSEGV, &sig, &old);
[7233f68]157
158        /* Use a pipe for SIGTERM/SIGINT so the actual signal handler doesn't do anything unsafe */
[5ebff60]159        if (pipe(signal_shutdown_pipe) == 0) {
160                b_input_add(signal_shutdown_pipe[0], B_EV_IO_READ, bitlbee_shutdown, NULL);
[7233f68]161                sig.sa_handler = sighandler_shutdown;
[5ebff60]162                sigaction(SIGINT, &sig, &old);
163                sigaction(SIGTERM, &sig, &old);
[7233f68]164        }
[5ebff60]165
166        if (!getuid() || !geteuid()) {
167                log_message(LOGLVL_WARNING, "BitlBee is running with root privileges. Why?");
168        }
169
[ba9edaa]170        b_main_run();
[5ebff60]171
[0fbda193]172        /* Mainly good for restarting, to make sure we close the help.txt fd. */
[5ebff60]173        help_free(&global.help);
174
175        if (global.restart) {
[54879ab]176                char *fn = ipc_master_save_state();
[daae10f]177                char *env;
[5ebff60]178
179                env = g_strdup_printf("_BITLBEE_RESTART_STATE=%s", fn);
180                putenv(env);
181                g_free(fn);
[daae10f]182                /* Looks like env should *not* be freed here as putenv
183                   doesn't make a copy. Odd. */
[5ebff60]184
185                i = chdir(old_cwd);
186                close(global.listen_socket);
187
188                if (execv(argv[0], argv) == -1) {
[cd63d58]189                        /* Apparently the execve() failed, so let's just
190                           jump back into our own/current main(). */
191                        /* Need more cleanup code to make this work. */
192                        return 1; /* main( argc, argv ); */
[5ebff60]193                }
[54879ab]194        }
[5ebff60]195
196        return(0);
[b7d3cc34]197}
198
[5ebff60]199static int crypt_main(int argc, char *argv[])
[7c9db24]200{
201        int pass_len;
202        unsigned char *pass_cr, *pass_cl;
[5ebff60]203
204        if (argc < 4 || (strcmp(argv[2], "hash") != 0 &&
205                         strcmp(argv[2], "unhash") != 0 && argc < 5)) {
206                printf("Supported:\n"
207                       "  %s -x enc <key> <cleartext password>\n"
208                       "  %s -x dec <key> <encrypted password>\n"
209                       "  %s -x hash <cleartext password>\n"
210                       "  %s -x unhash <hashed password>\n"
211                       "  %s -x chkhash <hashed password> <cleartext password>\n",
212                       argv[0], argv[0], argv[0], argv[0], argv[0]);
213        } else if (strcmp(argv[2], "enc") == 0) {
[5535a47]214                char *encoded;
215
216                pass_len = arc_encode(argv[4], strlen(argv[4]), &pass_cr, argv[3], 12);
217
218                encoded = base64_encode(pass_cr, pass_len);
219                printf("%s\n", encoded);
220                g_free(encoded);
221                g_free(pass_cr);
[5ebff60]222        } else if (strcmp(argv[2], "dec") == 0) {
[5535a47]223                pass_len = base64_decode(argv[4], &pass_cr);
[5ebff60]224                arc_decode(pass_cr, pass_len, (char **) &pass_cl, argv[3]);
225                printf("%s\n", pass_cl);
[5535a47]226
227                g_free(pass_cr);
228                g_free(pass_cl);
[5ebff60]229        } else if (strcmp(argv[2], "hash") == 0) {
[7c9db24]230                md5_byte_t pass_md5[21];
231                md5_state_t md5_state;
[5535a47]232                char *encoded;
[5ebff60]233
234                random_bytes(pass_md5 + 16, 5);
235                md5_init(&md5_state);
236                md5_append(&md5_state, (md5_byte_t *) argv[3], strlen(argv[3]));
237                md5_append(&md5_state, pass_md5 + 16, 5);   /* Add the salt. */
238                md5_finish(&md5_state, pass_md5);
239
[5535a47]240                encoded = base64_encode(pass_md5, 21);
241                printf("%s\n", encoded);
242                g_free(encoded);
[5ebff60]243        } else if (strcmp(argv[2], "unhash") == 0) {
244                printf("Hash %s submitted to a massive Beowulf cluster of\n"
245                       "overclocked 486s. Expect your answer next year somewhere around this time. :-)\n", argv[3]);
246        } else if (strcmp(argv[2], "chkhash") == 0) {
247                char *hash = strncmp(argv[3], "md5:", 4) == 0 ? argv[3] + 4 : argv[3];
248                int st = md5_verify_password(argv[4], hash);
249
250                printf("Hash %s given password.\n", st == 0 ? "matches" : "does not match");
251
[7c9db24]252                return st;
253        }
[5ebff60]254
[7c9db24]255        return 0;
256}
257
[7233f68]258/* Signal handler for SIGTERM and SIGINT */
[5ebff60]259static void sighandler_shutdown(int signal)
[b7d3cc34]260{
[7233f68]261        /* Write a single null byte to the pipe, just to send a message to the main loop.
262         * This gets handled by bitlbee_shutdown (the b_input_add callback for this pipe) */
[5ebff60]263        write(signal_shutdown_pipe[1], "", 1);
[7233f68]264}
265
266/* Signal handler for SIGSEGV
267 * A desperate attempt to tell the user that everything is wrong in the world.
268 * Avoids using irc_abort() because it has several unsafe calls to malloc */
[5ebff60]269static void sighandler_crash(int signal)
[7233f68]270{
271        GSList *l;
272        const char *message = "ERROR :BitlBee crashed! (SIGSEGV received)\r\n";
273        int len = strlen(message);
274
[5ebff60]275        for (l = irc_connection_list; l; l = l->next) {
[7233f68]276                irc_t *irc = l->data;
[5ebff60]277                write(irc->fd, message, len);
[b7d3cc34]278        }
[7233f68]279
[5ebff60]280        raise(signal);
[b7d3cc34]281}
282
283double gettime()
284{
285        struct timeval time[1];
286
[5ebff60]287        gettimeofday(time, 0);
288        return((double) time->tv_sec + (double) time->tv_usec / 1000000);
[b7d3cc34]289}
Note: See TracBrowser for help on using the repository browser.