[b7d3cc34] | 1 | /********************************************************************\ |
---|
| 2 | * BitlBee -- An IRC to other IM-networks gateway * |
---|
| 3 | * * |
---|
| 4 | * Copyright 2002-2004 Wilmer van der Gaast and others * |
---|
| 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; |
---|
| 22 | if not, write to the Free Software Foundation, Inc., 59 Temple Place, |
---|
| 23 | Suite 330, Boston, MA 02111-1307 USA |
---|
| 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> |
---|
[b7d3cc34] | 42 | |
---|
| 43 | global_t global; /* Against global namespace pollution */ |
---|
| 44 | |
---|
| 45 | static void sighandler( int signal ); |
---|
| 46 | |
---|
[7c9db24] | 47 | static int crypt_main( int argc, char *argv[] ); |
---|
| 48 | |
---|
[cd63d58] | 49 | int main( int argc, char *argv[] ) |
---|
[b7d3cc34] | 50 | { |
---|
| 51 | int i = 0; |
---|
[58bc4e6] | 52 | char *old_cwd = NULL; |
---|
[b7d3cc34] | 53 | struct sigaction sig, old; |
---|
| 54 | |
---|
[badd148] | 55 | /* Required to make iconv to ASCII//TRANSLIT work. This makes BitlBee |
---|
| 56 | system-locale-sensitive. :-( */ |
---|
| 57 | setlocale( LC_CTYPE, "" ); |
---|
| 58 | |
---|
[7c9db24] | 59 | if( argc > 1 && strcmp( argv[1], "-x" ) == 0 ) |
---|
| 60 | return crypt_main( argc, argv ); |
---|
| 61 | |
---|
[d25f6fc] | 62 | log_init(); |
---|
[5674207] | 63 | |
---|
[eeb85a8] | 64 | global.conf_file = g_strdup( CONF_FILE_DEF ); |
---|
[b7d3cc34] | 65 | global.conf = conf_load( argc, argv ); |
---|
| 66 | if( global.conf == NULL ) |
---|
| 67 | return( 1 ); |
---|
[6e1fed7] | 68 | |
---|
[60c1a4e] | 69 | b_main_init(); |
---|
| 70 | |
---|
| 71 | srand( time( NULL ) ^ getpid() ); |
---|
[5674207] | 72 | |
---|
[60c1a4e] | 73 | global.helpfile = g_strdup( HELP_FILE ); |
---|
[5674207] | 74 | if( help_init( &global.help, global.helpfile ) == NULL ) |
---|
| 75 | log_message( LOGLVL_WARNING, "Error opening helpfile %s.", HELP_FILE ); |
---|
| 76 | |
---|
| 77 | global.storage = storage_init( global.conf->primary_storage, global.conf->migrate_storage ); |
---|
| 78 | if( global.storage == NULL ) |
---|
| 79 | { |
---|
| 80 | log_message( LOGLVL_ERROR, "Unable to load storage backend '%s'", global.conf->primary_storage ); |
---|
| 81 | return( 1 ); |
---|
| 82 | } |
---|
[60c1a4e] | 83 | |
---|
[b7d3cc34] | 84 | if( global.conf->runmode == RUNMODE_INETD ) |
---|
| 85 | { |
---|
[99318ad] | 86 | log_link( LOGLVL_ERROR, LOGOUTPUT_IRC ); |
---|
| 87 | log_link( LOGLVL_WARNING, LOGOUTPUT_IRC ); |
---|
| 88 | |
---|
[b7d3cc34] | 89 | i = bitlbee_inetd_init(); |
---|
[156bbd7] | 90 | log_message( LOGLVL_INFO, "BitlBee %s starting in inetd mode.", BITLBEE_VERSION ); |
---|
[b7d3cc34] | 91 | |
---|
| 92 | } |
---|
| 93 | else if( global.conf->runmode == RUNMODE_DAEMON ) |
---|
| 94 | { |
---|
[156bbd7] | 95 | log_link( LOGLVL_ERROR, LOGOUTPUT_CONSOLE ); |
---|
| 96 | log_link( LOGLVL_WARNING, LOGOUTPUT_CONSOLE ); |
---|
[99318ad] | 97 | |
---|
[b7d3cc34] | 98 | i = bitlbee_daemon_init(); |
---|
[156bbd7] | 99 | log_message( LOGLVL_INFO, "BitlBee %s starting in daemon mode.", BITLBEE_VERSION ); |
---|
[b7d3cc34] | 100 | } |
---|
[d25f6fc] | 101 | else if( global.conf->runmode == RUNMODE_FORKDAEMON ) |
---|
| 102 | { |
---|
[156bbd7] | 103 | log_link( LOGLVL_ERROR, LOGOUTPUT_CONSOLE ); |
---|
| 104 | log_link( LOGLVL_WARNING, LOGOUTPUT_CONSOLE ); |
---|
| 105 | |
---|
[54879ab] | 106 | /* In case the operator requests a restart, we need this. */ |
---|
| 107 | old_cwd = g_malloc( 256 ); |
---|
| 108 | if( getcwd( old_cwd, 255 ) == NULL ) |
---|
| 109 | { |
---|
| 110 | log_message( LOGLVL_WARNING, "Could not save current directory: %s", strerror( errno ) ); |
---|
| 111 | g_free( old_cwd ); |
---|
| 112 | old_cwd = NULL; |
---|
| 113 | } |
---|
| 114 | |
---|
[d25f6fc] | 115 | i = bitlbee_daemon_init(); |
---|
[156bbd7] | 116 | log_message( LOGLVL_INFO, "BitlBee %s starting in forking daemon mode.", BITLBEE_VERSION ); |
---|
[d25f6fc] | 117 | } |
---|
[b7d3cc34] | 118 | if( i != 0 ) |
---|
| 119 | return( i ); |
---|
[6e1fed7] | 120 | |
---|
[aaf92a9] | 121 | if( ( global.conf->user && *global.conf->user ) && |
---|
| 122 | ( global.conf->runmode == RUNMODE_DAEMON || |
---|
| 123 | global.conf->runmode == RUNMODE_FORKDAEMON ) && |
---|
| 124 | ( !getuid() || !geteuid() ) ) |
---|
| 125 | { |
---|
| 126 | struct passwd *pw = NULL; |
---|
| 127 | pw = getpwnam( global.conf->user ); |
---|
| 128 | if( pw ) |
---|
| 129 | { |
---|
| 130 | setgid( pw->pw_gid ); |
---|
| 131 | setuid( pw->pw_uid ); |
---|
| 132 | } |
---|
| 133 | } |
---|
[b7d3cc34] | 134 | |
---|
| 135 | /* Catch some signals to tell the user what's happening before quitting */ |
---|
| 136 | memset( &sig, 0, sizeof( sig ) ); |
---|
| 137 | sig.sa_handler = sighandler; |
---|
[d25f6fc] | 138 | sigaction( SIGCHLD, &sig, &old ); |
---|
[b7d3cc34] | 139 | sigaction( SIGPIPE, &sig, &old ); |
---|
| 140 | sig.sa_flags = SA_RESETHAND; |
---|
| 141 | sigaction( SIGINT, &sig, &old ); |
---|
| 142 | sigaction( SIGILL, &sig, &old ); |
---|
| 143 | sigaction( SIGBUS, &sig, &old ); |
---|
| 144 | sigaction( SIGFPE, &sig, &old ); |
---|
| 145 | sigaction( SIGSEGV, &sig, &old ); |
---|
| 146 | sigaction( SIGTERM, &sig, &old ); |
---|
| 147 | sigaction( SIGQUIT, &sig, &old ); |
---|
| 148 | sigaction( SIGXCPU, &sig, &old ); |
---|
| 149 | |
---|
| 150 | if( !getuid() || !geteuid() ) |
---|
| 151 | log_message( LOGLVL_WARNING, "BitlBee is running with root privileges. Why?" ); |
---|
| 152 | |
---|
[ba9edaa] | 153 | b_main_run(); |
---|
[b7d3cc34] | 154 | |
---|
[0fbda193] | 155 | /* Mainly good for restarting, to make sure we close the help.txt fd. */ |
---|
| 156 | help_free( &global.help ); |
---|
| 157 | |
---|
[54879ab] | 158 | if( global.restart ) |
---|
| 159 | { |
---|
| 160 | char *fn = ipc_master_save_state(); |
---|
[daae10f] | 161 | char *env; |
---|
[54879ab] | 162 | |
---|
[daae10f] | 163 | env = g_strdup_printf( "_BITLBEE_RESTART_STATE=%s", fn ); |
---|
| 164 | putenv( env ); |
---|
[cd63d58] | 165 | g_free( fn ); |
---|
[daae10f] | 166 | /* Looks like env should *not* be freed here as putenv |
---|
| 167 | doesn't make a copy. Odd. */ |
---|
[54879ab] | 168 | |
---|
[fef7813] | 169 | i = chdir( old_cwd ); |
---|
[54879ab] | 170 | close( global.listen_socket ); |
---|
| 171 | |
---|
[cd63d58] | 172 | if( execv( argv[0], argv ) == -1 ) |
---|
| 173 | /* Apparently the execve() failed, so let's just |
---|
| 174 | jump back into our own/current main(). */ |
---|
| 175 | /* Need more cleanup code to make this work. */ |
---|
| 176 | return 1; /* main( argc, argv ); */ |
---|
[54879ab] | 177 | } |
---|
| 178 | |
---|
[b7d3cc34] | 179 | return( 0 ); |
---|
| 180 | } |
---|
| 181 | |
---|
[7c9db24] | 182 | static int crypt_main( int argc, char *argv[] ) |
---|
| 183 | { |
---|
| 184 | int pass_len; |
---|
| 185 | unsigned char *pass_cr, *pass_cl; |
---|
| 186 | |
---|
[8b6b740] | 187 | if( argc < 4 || ( strcmp( argv[2], "hash" ) != 0 && |
---|
| 188 | strcmp( argv[2], "unhash" ) != 0 && argc < 5 ) ) |
---|
[7c9db24] | 189 | { |
---|
| 190 | printf( "Supported:\n" |
---|
| 191 | " %s -x enc <key> <cleartext password>\n" |
---|
| 192 | " %s -x dec <key> <encrypted password>\n" |
---|
| 193 | " %s -x hash <cleartext password>\n" |
---|
| 194 | " %s -x unhash <hashed password>\n" |
---|
| 195 | " %s -x chkhash <hashed password> <cleartext password>\n", |
---|
| 196 | argv[0], argv[0], argv[0], argv[0], argv[0] ); |
---|
| 197 | } |
---|
| 198 | else if( strcmp( argv[2], "enc" ) == 0 ) |
---|
| 199 | { |
---|
| 200 | pass_len = arc_encode( argv[4], strlen( argv[4] ), (unsigned char**) &pass_cr, argv[3], 12 ); |
---|
| 201 | printf( "%s\n", base64_encode( pass_cr, pass_len ) ); |
---|
| 202 | } |
---|
| 203 | else if( strcmp( argv[2], "dec" ) == 0 ) |
---|
| 204 | { |
---|
| 205 | pass_len = base64_decode( argv[4], (unsigned char**) &pass_cr ); |
---|
| 206 | arc_decode( pass_cr, pass_len, (char**) &pass_cl, argv[3] ); |
---|
| 207 | printf( "%s\n", pass_cl ); |
---|
| 208 | } |
---|
| 209 | else if( strcmp( argv[2], "hash" ) == 0 ) |
---|
| 210 | { |
---|
| 211 | md5_byte_t pass_md5[21]; |
---|
| 212 | md5_state_t md5_state; |
---|
| 213 | |
---|
| 214 | random_bytes( pass_md5 + 16, 5 ); |
---|
| 215 | md5_init( &md5_state ); |
---|
| 216 | md5_append( &md5_state, (md5_byte_t*) argv[3], strlen( argv[3] ) ); |
---|
| 217 | md5_append( &md5_state, pass_md5 + 16, 5 ); /* Add the salt. */ |
---|
| 218 | md5_finish( &md5_state, pass_md5 ); |
---|
| 219 | |
---|
| 220 | printf( "%s\n", base64_encode( pass_md5, 21 ) ); |
---|
| 221 | } |
---|
| 222 | else if( strcmp( argv[2], "unhash" ) == 0 ) |
---|
| 223 | { |
---|
| 224 | printf( "Hash %s submitted to a massive Beowulf cluster of\n" |
---|
| 225 | "overclocked 486s. Expect your answer next year somewhere around this time. :-)\n", argv[3] ); |
---|
| 226 | } |
---|
| 227 | else if( strcmp( argv[2], "chkhash" ) == 0 ) |
---|
| 228 | { |
---|
[8b6b740] | 229 | char *hash = strncmp( argv[3], "md5:", 4 ) == 0 ? argv[3] + 4 : argv[3]; |
---|
| 230 | int st = md5_verify_password( argv[4], hash ); |
---|
[7c9db24] | 231 | |
---|
| 232 | printf( "Hash %s given password.\n", st == 0 ? "matches" : "does not match" ); |
---|
| 233 | |
---|
| 234 | return st; |
---|
| 235 | } |
---|
| 236 | |
---|
| 237 | return 0; |
---|
| 238 | } |
---|
| 239 | |
---|
[b7d3cc34] | 240 | static void sighandler( int signal ) |
---|
| 241 | { |
---|
[d25f6fc] | 242 | /* FIXME: Calling log_message() here is not a very good idea! */ |
---|
[b7d3cc34] | 243 | |
---|
[82ca986] | 244 | if( signal == SIGTERM || signal == SIGQUIT || signal == SIGINT ) |
---|
[b7d3cc34] | 245 | { |
---|
| 246 | static int first = 1; |
---|
| 247 | |
---|
| 248 | if( first ) |
---|
| 249 | { |
---|
| 250 | /* We don't know what we were doing when this signal came in. It's not safe to touch |
---|
| 251 | the user data now (not to mention writing them to disk), so add a timer. */ |
---|
| 252 | |
---|
| 253 | log_message( LOGLVL_ERROR, "SIGTERM received, cleaning up process." ); |
---|
[ba9edaa] | 254 | b_timeout_add( 1, (b_event_handler) bitlbee_shutdown, NULL ); |
---|
[b7d3cc34] | 255 | |
---|
| 256 | first = 0; |
---|
| 257 | } |
---|
| 258 | else |
---|
| 259 | { |
---|
| 260 | /* Well, actually, for now we'll never need this part because this signal handler |
---|
| 261 | will never be called more than once in a session for a non-SIGPIPE signal... |
---|
| 262 | But just in case we decide to change that: */ |
---|
| 263 | |
---|
| 264 | log_message( LOGLVL_ERROR, "SIGTERM received twice, so long for a clean shutdown." ); |
---|
| 265 | raise( signal ); |
---|
| 266 | } |
---|
| 267 | } |
---|
[d25f6fc] | 268 | else if( signal == SIGCHLD ) |
---|
| 269 | { |
---|
| 270 | pid_t pid; |
---|
| 271 | int st; |
---|
| 272 | |
---|
| 273 | while( ( pid = waitpid( 0, &st, WNOHANG ) ) > 0 ) |
---|
| 274 | { |
---|
| 275 | if( WIFSIGNALED( st ) ) |
---|
[52744f8] | 276 | log_message( LOGLVL_INFO, "Client %d terminated normally. (status = %d)", (int) pid, WEXITSTATUS( st ) ); |
---|
[d25f6fc] | 277 | else if( WIFEXITED( st ) ) |
---|
[52744f8] | 278 | log_message( LOGLVL_INFO, "Client %d killed by signal %d.", (int) pid, WTERMSIG( st ) ); |
---|
[d25f6fc] | 279 | } |
---|
| 280 | } |
---|
[b7d3cc34] | 281 | else if( signal != SIGPIPE ) |
---|
| 282 | { |
---|
| 283 | log_message( LOGLVL_ERROR, "Fatal signal received: %d. That's probably a bug.", signal ); |
---|
| 284 | raise( signal ); |
---|
| 285 | } |
---|
| 286 | } |
---|
| 287 | |
---|
| 288 | double gettime() |
---|
| 289 | { |
---|
| 290 | struct timeval time[1]; |
---|
| 291 | |
---|
| 292 | gettimeofday( time, 0 ); |
---|
| 293 | return( (double) time->tv_sec + (double) time->tv_usec / 1000000 ); |
---|
| 294 | } |
---|