[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] | 48 | global_t global; /* Against global namespace pollution */ |
---|
[b7d3cc34] | 49 | |
---|
[2e99039] | 50 | static struct { |
---|
| 51 | int fd[2]; |
---|
| 52 | int tag; |
---|
| 53 | } shutdown_pipe = {{-1 , -1}, 0}; |
---|
| 54 | |
---|
[5ebff60] | 55 | static void sighandler_shutdown(int signal); |
---|
| 56 | static void sighandler_crash(int signal); |
---|
[b7d3cc34] | 57 | |
---|
[5ebff60] | 58 | static int crypt_main(int argc, char *argv[]); |
---|
[7c9db24] | 59 | |
---|
[5ebff60] | 60 | int main(int argc, char *argv[]) |
---|
[b7d3cc34] | 61 | { |
---|
| 62 | int i = 0; |
---|
[58bc4e6] | 63 | char *old_cwd = NULL; |
---|
[b7d3cc34] | 64 | struct sigaction sig, old; |
---|
[5ebff60] | 65 | |
---|
[badd148] | 66 | /* Required to make iconv to ASCII//TRANSLIT work. This makes BitlBee |
---|
| 67 | system-locale-sensitive. :-( */ |
---|
[5ebff60] | 68 | setlocale(LC_CTYPE, ""); |
---|
| 69 | |
---|
| 70 | if (argc > 1 && strcmp(argv[1], "-x") == 0) { |
---|
| 71 | return crypt_main(argc, argv); |
---|
| 72 | } |
---|
| 73 | |
---|
[d25f6fc] | 74 | log_init(); |
---|
[5ebff60] | 75 | |
---|
| 76 | global.conf_file = g_strdup(CONF_FILE_DEF); |
---|
| 77 | global.conf = conf_load(argc, argv); |
---|
| 78 | if (global.conf == NULL) { |
---|
| 79 | return(1); |
---|
| 80 | } |
---|
| 81 | |
---|
[72d48b6] | 82 | if (global.conf->runmode == RUNMODE_INETD) { |
---|
| 83 | log_link(LOGLVL_ERROR, LOGOUTPUT_IRC); |
---|
| 84 | log_link(LOGLVL_WARNING, LOGOUTPUT_IRC); |
---|
| 85 | } else { |
---|
| 86 | log_link(LOGLVL_ERROR, LOGOUTPUT_CONSOLE); |
---|
| 87 | log_link(LOGLVL_WARNING, LOGOUTPUT_CONSOLE); |
---|
| 88 | } |
---|
| 89 | |
---|
[60c1a4e] | 90 | b_main_init(); |
---|
[5ebff60] | 91 | |
---|
[3fc6c32] | 92 | /* libpurple doesn't like fork()s after initializing itself, so if |
---|
| 93 | we use it, do this init a little later (in case we're running in |
---|
| 94 | ForkDaemon mode). */ |
---|
| 95 | #ifndef WITH_PURPLE |
---|
| 96 | nogaim_init(); |
---|
| 97 | #endif |
---|
[5ebff60] | 98 | |
---|
[858ea01] | 99 | #ifdef OTR_BI |
---|
[5ebff60] | 100 | otr_init(); |
---|
[858ea01] | 101 | #endif |
---|
[5ebff60] | 102 | |
---|
| 103 | global.helpfile = g_strdup(HELP_FILE); |
---|
| 104 | if (help_init(&global.help, global.helpfile) == NULL) { |
---|
| 105 | log_message(LOGLVL_WARNING, "Error opening helpfile %s.", HELP_FILE); |
---|
[5674207] | 106 | } |
---|
[b7d3cc34] | 107 | |
---|
[5ebff60] | 108 | global.storage = storage_init(global.conf->primary_storage, global.conf->migrate_storage); |
---|
| 109 | if (global.storage == NULL) { |
---|
| 110 | log_message(LOGLVL_ERROR, "Unable to load storage backend '%s'", global.conf->primary_storage); |
---|
| 111 | return(1); |
---|
[8e6ecfe] | 112 | } |
---|
| 113 | |
---|
| 114 | global.auth = auth_init(global.conf->auth_backend); |
---|
| 115 | if (global.conf->auth_backend && global.auth == NULL) { |
---|
| 116 | log_message(LOGLVL_ERROR, "Unable to load authentication backend '%s'", global.conf->auth_backend); |
---|
| 117 | return(1); |
---|
[b7d3cc34] | 118 | } |
---|
[5ebff60] | 119 | |
---|
| 120 | if (global.conf->runmode == RUNMODE_INETD) { |
---|
| 121 | i = bitlbee_inetd_init(); |
---|
| 122 | log_message(LOGLVL_INFO, "%s %s starting in inetd mode.", PACKAGE, BITLBEE_VERSION); |
---|
| 123 | |
---|
| 124 | } else if (global.conf->runmode == RUNMODE_DAEMON) { |
---|
[b7d3cc34] | 125 | i = bitlbee_daemon_init(); |
---|
[5ebff60] | 126 | log_message(LOGLVL_INFO, "%s %s starting in daemon mode.", PACKAGE, BITLBEE_VERSION); |
---|
| 127 | } else if (global.conf->runmode == RUNMODE_FORKDAEMON) { |
---|
[54879ab] | 128 | /* In case the operator requests a restart, we need this. */ |
---|
[5ebff60] | 129 | old_cwd = g_malloc(256); |
---|
| 130 | if (getcwd(old_cwd, 255) == NULL) { |
---|
| 131 | log_message(LOGLVL_WARNING, "Could not save current directory: %s", strerror(errno)); |
---|
| 132 | g_free(old_cwd); |
---|
[54879ab] | 133 | old_cwd = NULL; |
---|
| 134 | } |
---|
[5ebff60] | 135 | |
---|
[d25f6fc] | 136 | i = bitlbee_daemon_init(); |
---|
[5ebff60] | 137 | log_message(LOGLVL_INFO, "%s %s starting in forking daemon mode.", PACKAGE, BITLBEE_VERSION); |
---|
| 138 | } |
---|
| 139 | if (i != 0) { |
---|
| 140 | return(i); |
---|
[d25f6fc] | 141 | } |
---|
[5ebff60] | 142 | |
---|
| 143 | if ((global.conf->user && *global.conf->user) && |
---|
| 144 | (global.conf->runmode == RUNMODE_DAEMON || |
---|
| 145 | global.conf->runmode == RUNMODE_FORKDAEMON) && |
---|
| 146 | (!getuid() || !geteuid())) { |
---|
[aaf92a9] | 147 | struct passwd *pw = NULL; |
---|
[5ebff60] | 148 | pw = getpwnam(global.conf->user); |
---|
[0483e1e] | 149 | if (!pw) { |
---|
| 150 | log_message(LOGLVL_ERROR, "Failed to look up user %s.", global.conf->user); |
---|
| 151 | |
---|
| 152 | } else if (initgroups(global.conf->user, pw->pw_gid) != 0) { |
---|
| 153 | log_message(LOGLVL_ERROR, "initgroups: %s.", strerror(errno)); |
---|
| 154 | |
---|
| 155 | } else if (setgid(pw->pw_gid) != 0) { |
---|
| 156 | log_message(LOGLVL_ERROR, "setgid(%d): %s.", pw->pw_gid, strerror(errno)); |
---|
| 157 | |
---|
| 158 | } else if (setuid(pw->pw_uid) != 0) { |
---|
| 159 | log_message(LOGLVL_ERROR, "setuid(%d): %s.", pw->pw_uid, strerror(errno)); |
---|
[ad46e4d] | 160 | } |
---|
[aaf92a9] | 161 | } |
---|
[5ebff60] | 162 | |
---|
[b7d3cc34] | 163 | /* Catch some signals to tell the user what's happening before quitting */ |
---|
[5ebff60] | 164 | memset(&sig, 0, sizeof(sig)); |
---|
[7233f68] | 165 | sig.sa_handler = SIG_IGN; |
---|
[5ebff60] | 166 | sigaction(SIGCHLD, &sig, &old); |
---|
| 167 | sigaction(SIGPIPE, &sig, &old); |
---|
[b7d3cc34] | 168 | sig.sa_flags = SA_RESETHAND; |
---|
[7233f68] | 169 | sig.sa_handler = sighandler_crash; |
---|
[5ebff60] | 170 | sigaction(SIGSEGV, &sig, &old); |
---|
[7233f68] | 171 | |
---|
[2e99039] | 172 | sighandler_shutdown_setup(); |
---|
| 173 | |
---|
| 174 | sig.sa_handler = sighandler_shutdown; |
---|
| 175 | sigaction(SIGINT, &sig, &old); |
---|
| 176 | sigaction(SIGTERM, &sig, &old); |
---|
[5ebff60] | 177 | |
---|
| 178 | if (!getuid() || !geteuid()) { |
---|
| 179 | log_message(LOGLVL_WARNING, "BitlBee is running with root privileges. Why?"); |
---|
| 180 | } |
---|
| 181 | |
---|
[ba9edaa] | 182 | b_main_run(); |
---|
[5ebff60] | 183 | |
---|
[0fbda193] | 184 | /* Mainly good for restarting, to make sure we close the help.txt fd. */ |
---|
[5ebff60] | 185 | help_free(&global.help); |
---|
| 186 | |
---|
| 187 | if (global.restart) { |
---|
[54879ab] | 188 | char *fn = ipc_master_save_state(); |
---|
[daae10f] | 189 | char *env; |
---|
[5ebff60] | 190 | |
---|
| 191 | env = g_strdup_printf("_BITLBEE_RESTART_STATE=%s", fn); |
---|
| 192 | putenv(env); |
---|
| 193 | g_free(fn); |
---|
[daae10f] | 194 | /* Looks like env should *not* be freed here as putenv |
---|
| 195 | doesn't make a copy. Odd. */ |
---|
[5ebff60] | 196 | |
---|
| 197 | i = chdir(old_cwd); |
---|
| 198 | close(global.listen_socket); |
---|
| 199 | |
---|
| 200 | if (execv(argv[0], argv) == -1) { |
---|
[cd63d58] | 201 | /* Apparently the execve() failed, so let's just |
---|
| 202 | jump back into our own/current main(). */ |
---|
| 203 | /* Need more cleanup code to make this work. */ |
---|
| 204 | return 1; /* main( argc, argv ); */ |
---|
[5ebff60] | 205 | } |
---|
[54879ab] | 206 | } |
---|
[5ebff60] | 207 | |
---|
| 208 | return(0); |
---|
[b7d3cc34] | 209 | } |
---|
| 210 | |
---|
[5ebff60] | 211 | static int crypt_main(int argc, char *argv[]) |
---|
[7c9db24] | 212 | { |
---|
| 213 | int pass_len; |
---|
| 214 | unsigned char *pass_cr, *pass_cl; |
---|
[5ebff60] | 215 | |
---|
| 216 | if (argc < 4 || (strcmp(argv[2], "hash") != 0 && |
---|
| 217 | strcmp(argv[2], "unhash") != 0 && argc < 5)) { |
---|
| 218 | printf("Supported:\n" |
---|
| 219 | " %s -x enc <key> <cleartext password>\n" |
---|
| 220 | " %s -x dec <key> <encrypted password>\n" |
---|
| 221 | " %s -x hash <cleartext password>\n" |
---|
| 222 | " %s -x unhash <hashed password>\n" |
---|
| 223 | " %s -x chkhash <hashed password> <cleartext password>\n", |
---|
| 224 | argv[0], argv[0], argv[0], argv[0], argv[0]); |
---|
| 225 | } else if (strcmp(argv[2], "enc") == 0) { |
---|
[5535a47] | 226 | char *encoded; |
---|
| 227 | |
---|
| 228 | pass_len = arc_encode(argv[4], strlen(argv[4]), &pass_cr, argv[3], 12); |
---|
| 229 | |
---|
| 230 | encoded = base64_encode(pass_cr, pass_len); |
---|
| 231 | printf("%s\n", encoded); |
---|
| 232 | g_free(encoded); |
---|
| 233 | g_free(pass_cr); |
---|
[5ebff60] | 234 | } else if (strcmp(argv[2], "dec") == 0) { |
---|
[5535a47] | 235 | pass_len = base64_decode(argv[4], &pass_cr); |
---|
[5ebff60] | 236 | arc_decode(pass_cr, pass_len, (char **) &pass_cl, argv[3]); |
---|
| 237 | printf("%s\n", pass_cl); |
---|
[5535a47] | 238 | |
---|
| 239 | g_free(pass_cr); |
---|
| 240 | g_free(pass_cl); |
---|
[5ebff60] | 241 | } else if (strcmp(argv[2], "hash") == 0) { |
---|
[7c9db24] | 242 | md5_byte_t pass_md5[21]; |
---|
| 243 | md5_state_t md5_state; |
---|
[5535a47] | 244 | char *encoded; |
---|
[5ebff60] | 245 | |
---|
| 246 | random_bytes(pass_md5 + 16, 5); |
---|
| 247 | md5_init(&md5_state); |
---|
| 248 | md5_append(&md5_state, (md5_byte_t *) argv[3], strlen(argv[3])); |
---|
| 249 | md5_append(&md5_state, pass_md5 + 16, 5); /* Add the salt. */ |
---|
| 250 | md5_finish(&md5_state, pass_md5); |
---|
| 251 | |
---|
[5535a47] | 252 | encoded = base64_encode(pass_md5, 21); |
---|
| 253 | printf("%s\n", encoded); |
---|
| 254 | g_free(encoded); |
---|
[5ebff60] | 255 | } else if (strcmp(argv[2], "unhash") == 0) { |
---|
| 256 | printf("Hash %s submitted to a massive Beowulf cluster of\n" |
---|
| 257 | "overclocked 486s. Expect your answer next year somewhere around this time. :-)\n", argv[3]); |
---|
| 258 | } else if (strcmp(argv[2], "chkhash") == 0) { |
---|
| 259 | char *hash = strncmp(argv[3], "md5:", 4) == 0 ? argv[3] + 4 : argv[3]; |
---|
| 260 | int st = md5_verify_password(argv[4], hash); |
---|
| 261 | |
---|
| 262 | printf("Hash %s given password.\n", st == 0 ? "matches" : "does not match"); |
---|
| 263 | |
---|
[7c9db24] | 264 | return st; |
---|
| 265 | } |
---|
[5ebff60] | 266 | |
---|
[7c9db24] | 267 | return 0; |
---|
| 268 | } |
---|
| 269 | |
---|
[2e99039] | 270 | /* Set up a pipe for SIGTERM/SIGINT so the actual signal handler doesn't do anything unsafe */ |
---|
| 271 | void sighandler_shutdown_setup() |
---|
| 272 | { |
---|
| 273 | if (shutdown_pipe.fd[0] != -1) { |
---|
| 274 | /* called again from a forked process, clean up to avoid propagating the signal */ |
---|
| 275 | b_event_remove(shutdown_pipe.tag); |
---|
| 276 | close(shutdown_pipe.fd[0]); |
---|
| 277 | close(shutdown_pipe.fd[1]); |
---|
| 278 | } |
---|
| 279 | |
---|
| 280 | if (pipe(shutdown_pipe.fd) == 0) { |
---|
| 281 | shutdown_pipe.tag = b_input_add(shutdown_pipe.fd[0], B_EV_IO_READ, bitlbee_shutdown, NULL); |
---|
| 282 | } |
---|
| 283 | } |
---|
| 284 | |
---|
[7233f68] | 285 | /* Signal handler for SIGTERM and SIGINT */ |
---|
[5ebff60] | 286 | static void sighandler_shutdown(int signal) |
---|
[b7d3cc34] | 287 | { |
---|
[0483e1e] | 288 | int unused G_GNUC_UNUSED; |
---|
[7233f68] | 289 | /* Write a single null byte to the pipe, just to send a message to the main loop. |
---|
| 290 | * This gets handled by bitlbee_shutdown (the b_input_add callback for this pipe) */ |
---|
[0483e1e] | 291 | unused = write(shutdown_pipe.fd[1], "", 1); |
---|
[7233f68] | 292 | } |
---|
| 293 | |
---|
| 294 | /* Signal handler for SIGSEGV |
---|
| 295 | * A desperate attempt to tell the user that everything is wrong in the world. |
---|
| 296 | * Avoids using irc_abort() because it has several unsafe calls to malloc */ |
---|
[5ebff60] | 297 | static void sighandler_crash(int signal) |
---|
[7233f68] | 298 | { |
---|
| 299 | GSList *l; |
---|
[0483e1e] | 300 | int unused G_GNUC_UNUSED; |
---|
[7233f68] | 301 | const char *message = "ERROR :BitlBee crashed! (SIGSEGV received)\r\n"; |
---|
| 302 | int len = strlen(message); |
---|
| 303 | |
---|
[5ebff60] | 304 | for (l = irc_connection_list; l; l = l->next) { |
---|
[7233f68] | 305 | irc_t *irc = l->data; |
---|
[c467adc] | 306 | sock_make_blocking(irc->fd); |
---|
[0483e1e] | 307 | unused = write(irc->fd, message, len); |
---|
[b7d3cc34] | 308 | } |
---|
[7233f68] | 309 | |
---|
[5ebff60] | 310 | raise(signal); |
---|
[b7d3cc34] | 311 | } |
---|
| 312 | |
---|
| 313 | double gettime() |
---|
| 314 | { |
---|
| 315 | struct timeval time[1]; |
---|
| 316 | |
---|
[5ebff60] | 317 | gettimeofday(time, 0); |
---|
| 318 | return((double) time->tv_sec + (double) time->tv_usec / 1000000); |
---|
[b7d3cc34] | 319 | } |
---|