[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" |
---|
| 27 | #include "commands.h" |
---|
| 28 | #include "crypting.h" |
---|
| 29 | #include "protocols/nogaim.h" |
---|
| 30 | #include "help.h" |
---|
[54879ab] | 31 | #include "ipc.h" |
---|
[b7d3cc34] | 32 | #include <signal.h> |
---|
| 33 | #include <unistd.h> |
---|
| 34 | #include <sys/time.h> |
---|
[d25f6fc] | 35 | #include <sys/wait.h> |
---|
[aaf92a9] | 36 | #include <pwd.h> |
---|
[b7d3cc34] | 37 | |
---|
| 38 | global_t global; /* Against global namespace pollution */ |
---|
| 39 | |
---|
| 40 | static void sighandler( int signal ); |
---|
| 41 | |
---|
[54879ab] | 42 | int main( int argc, char *argv[], char **envp ) |
---|
[b7d3cc34] | 43 | { |
---|
| 44 | int i = 0; |
---|
[58bc4e6] | 45 | char *old_cwd = NULL; |
---|
[b7d3cc34] | 46 | struct sigaction sig, old; |
---|
| 47 | |
---|
[d25f6fc] | 48 | log_init(); |
---|
[eeb85a8] | 49 | global.conf_file = g_strdup( CONF_FILE_DEF ); |
---|
[b7d3cc34] | 50 | global.conf = conf_load( argc, argv ); |
---|
| 51 | if( global.conf == NULL ) |
---|
| 52 | return( 1 ); |
---|
[6e1fed7] | 53 | |
---|
[60c1a4e] | 54 | b_main_init(); |
---|
| 55 | nogaim_init(); |
---|
| 56 | |
---|
| 57 | srand( time( NULL ) ^ getpid() ); |
---|
| 58 | global.helpfile = g_strdup( HELP_FILE ); |
---|
| 59 | |
---|
[b7d3cc34] | 60 | if( global.conf->runmode == RUNMODE_INETD ) |
---|
| 61 | { |
---|
[99318ad] | 62 | log_link( LOGLVL_ERROR, LOGOUTPUT_IRC ); |
---|
| 63 | log_link( LOGLVL_WARNING, LOGOUTPUT_IRC ); |
---|
| 64 | |
---|
[b7d3cc34] | 65 | i = bitlbee_inetd_init(); |
---|
| 66 | log_message( LOGLVL_INFO, "Bitlbee %s starting in inetd mode.", BITLBEE_VERSION ); |
---|
| 67 | |
---|
| 68 | } |
---|
| 69 | else if( global.conf->runmode == RUNMODE_DAEMON ) |
---|
| 70 | { |
---|
[99318ad] | 71 | log_link( LOGLVL_ERROR, LOGOUTPUT_SYSLOG ); |
---|
| 72 | log_link( LOGLVL_WARNING, LOGOUTPUT_SYSLOG ); |
---|
| 73 | |
---|
[b7d3cc34] | 74 | i = bitlbee_daemon_init(); |
---|
| 75 | log_message( LOGLVL_INFO, "Bitlbee %s starting in daemon mode.", BITLBEE_VERSION ); |
---|
| 76 | } |
---|
[d25f6fc] | 77 | else if( global.conf->runmode == RUNMODE_FORKDAEMON ) |
---|
| 78 | { |
---|
[54879ab] | 79 | /* In case the operator requests a restart, we need this. */ |
---|
| 80 | old_cwd = g_malloc( 256 ); |
---|
| 81 | if( getcwd( old_cwd, 255 ) == NULL ) |
---|
| 82 | { |
---|
| 83 | log_message( LOGLVL_WARNING, "Could not save current directory: %s", strerror( errno ) ); |
---|
| 84 | g_free( old_cwd ); |
---|
| 85 | old_cwd = NULL; |
---|
| 86 | } |
---|
| 87 | |
---|
[d25f6fc] | 88 | i = bitlbee_daemon_init(); |
---|
| 89 | log_message( LOGLVL_INFO, "Bitlbee %s starting in forking daemon mode.", BITLBEE_VERSION ); |
---|
| 90 | } |
---|
[b7d3cc34] | 91 | if( i != 0 ) |
---|
| 92 | return( i ); |
---|
[6e1fed7] | 93 | |
---|
[aaf92a9] | 94 | if( ( global.conf->user && *global.conf->user ) && |
---|
| 95 | ( global.conf->runmode == RUNMODE_DAEMON || |
---|
| 96 | global.conf->runmode == RUNMODE_FORKDAEMON ) && |
---|
| 97 | ( !getuid() || !geteuid() ) ) |
---|
| 98 | { |
---|
| 99 | struct passwd *pw = NULL; |
---|
| 100 | pw = getpwnam( global.conf->user ); |
---|
| 101 | if( pw ) |
---|
| 102 | { |
---|
| 103 | setgid( pw->pw_gid ); |
---|
| 104 | setuid( pw->pw_uid ); |
---|
| 105 | } |
---|
| 106 | } |
---|
[1ee6c18] | 107 | |
---|
[d25f6fc] | 108 | global.storage = storage_init( global.conf->primary_storage, global.conf->migrate_storage ); |
---|
[aaf92a9] | 109 | if( global.storage == NULL ) |
---|
| 110 | { |
---|
[b73ac9c] | 111 | log_message( LOGLVL_ERROR, "Unable to load storage backend '%s'", global.conf->primary_storage ); |
---|
[1ee6c18] | 112 | return( 1 ); |
---|
| 113 | } |
---|
[b7d3cc34] | 114 | |
---|
| 115 | /* Catch some signals to tell the user what's happening before quitting */ |
---|
| 116 | memset( &sig, 0, sizeof( sig ) ); |
---|
| 117 | sig.sa_handler = sighandler; |
---|
[d25f6fc] | 118 | sigaction( SIGCHLD, &sig, &old ); |
---|
[b7d3cc34] | 119 | sigaction( SIGPIPE, &sig, &old ); |
---|
| 120 | sig.sa_flags = SA_RESETHAND; |
---|
| 121 | sigaction( SIGINT, &sig, &old ); |
---|
| 122 | sigaction( SIGILL, &sig, &old ); |
---|
| 123 | sigaction( SIGBUS, &sig, &old ); |
---|
| 124 | sigaction( SIGFPE, &sig, &old ); |
---|
| 125 | sigaction( SIGSEGV, &sig, &old ); |
---|
| 126 | sigaction( SIGTERM, &sig, &old ); |
---|
| 127 | sigaction( SIGQUIT, &sig, &old ); |
---|
| 128 | sigaction( SIGXCPU, &sig, &old ); |
---|
| 129 | |
---|
| 130 | if( !getuid() || !geteuid() ) |
---|
| 131 | log_message( LOGLVL_WARNING, "BitlBee is running with root privileges. Why?" ); |
---|
[0fbda193] | 132 | if( help_init( &global.help, global.helpfile ) == NULL ) |
---|
[b7d3cc34] | 133 | log_message( LOGLVL_WARNING, "Error opening helpfile %s.", HELP_FILE ); |
---|
| 134 | |
---|
[ba9edaa] | 135 | b_main_run(); |
---|
[b7d3cc34] | 136 | |
---|
[0fbda193] | 137 | /* Mainly good for restarting, to make sure we close the help.txt fd. */ |
---|
| 138 | help_free( &global.help ); |
---|
[b7d3cc34] | 139 | |
---|
[54879ab] | 140 | if( global.restart ) |
---|
| 141 | { |
---|
| 142 | char *fn = ipc_master_save_state(); |
---|
| 143 | char **args; |
---|
| 144 | int n, i; |
---|
| 145 | |
---|
| 146 | chdir( old_cwd ); |
---|
| 147 | |
---|
| 148 | n = 0; |
---|
| 149 | args = g_new0( char *, argc + 3 ); |
---|
| 150 | args[n++] = argv[0]; |
---|
| 151 | if( fn ) |
---|
| 152 | { |
---|
| 153 | args[n++] = "-R"; |
---|
| 154 | args[n++] = fn; |
---|
| 155 | } |
---|
| 156 | for( i = 1; argv[i] && i < argc; i ++ ) |
---|
| 157 | { |
---|
| 158 | if( strcmp( argv[i], "-R" ) == 0 ) |
---|
| 159 | i += 2; |
---|
| 160 | |
---|
| 161 | args[n++] = argv[i]; |
---|
| 162 | } |
---|
| 163 | |
---|
| 164 | close( global.listen_socket ); |
---|
| 165 | |
---|
| 166 | execve( args[0], args, envp ); |
---|
| 167 | } |
---|
| 168 | |
---|
[b7d3cc34] | 169 | return( 0 ); |
---|
| 170 | } |
---|
| 171 | |
---|
| 172 | static void sighandler( int signal ) |
---|
| 173 | { |
---|
[d25f6fc] | 174 | /* FIXME: Calling log_message() here is not a very good idea! */ |
---|
[b7d3cc34] | 175 | |
---|
| 176 | if( signal == SIGTERM ) |
---|
| 177 | { |
---|
| 178 | static int first = 1; |
---|
| 179 | |
---|
| 180 | if( first ) |
---|
| 181 | { |
---|
| 182 | /* We don't know what we were doing when this signal came in. It's not safe to touch |
---|
| 183 | the user data now (not to mention writing them to disk), so add a timer. */ |
---|
| 184 | |
---|
| 185 | log_message( LOGLVL_ERROR, "SIGTERM received, cleaning up process." ); |
---|
[ba9edaa] | 186 | b_timeout_add( 1, (b_event_handler) bitlbee_shutdown, NULL ); |
---|
[b7d3cc34] | 187 | |
---|
| 188 | first = 0; |
---|
| 189 | } |
---|
| 190 | else |
---|
| 191 | { |
---|
| 192 | /* Well, actually, for now we'll never need this part because this signal handler |
---|
| 193 | will never be called more than once in a session for a non-SIGPIPE signal... |
---|
| 194 | But just in case we decide to change that: */ |
---|
| 195 | |
---|
| 196 | log_message( LOGLVL_ERROR, "SIGTERM received twice, so long for a clean shutdown." ); |
---|
| 197 | raise( signal ); |
---|
| 198 | } |
---|
| 199 | } |
---|
[d25f6fc] | 200 | else if( signal == SIGCHLD ) |
---|
| 201 | { |
---|
| 202 | pid_t pid; |
---|
| 203 | int st; |
---|
| 204 | |
---|
| 205 | while( ( pid = waitpid( 0, &st, WNOHANG ) ) > 0 ) |
---|
| 206 | { |
---|
| 207 | if( WIFSIGNALED( st ) ) |
---|
[52744f8] | 208 | log_message( LOGLVL_INFO, "Client %d terminated normally. (status = %d)", (int) pid, WEXITSTATUS( st ) ); |
---|
[d25f6fc] | 209 | else if( WIFEXITED( st ) ) |
---|
[52744f8] | 210 | log_message( LOGLVL_INFO, "Client %d killed by signal %d.", (int) pid, WTERMSIG( st ) ); |
---|
[d25f6fc] | 211 | } |
---|
| 212 | } |
---|
[b7d3cc34] | 213 | else if( signal != SIGPIPE ) |
---|
| 214 | { |
---|
| 215 | log_message( LOGLVL_ERROR, "Fatal signal received: %d. That's probably a bug.", signal ); |
---|
| 216 | raise( signal ); |
---|
| 217 | } |
---|
| 218 | } |
---|
| 219 | |
---|
| 220 | double gettime() |
---|
| 221 | { |
---|
| 222 | struct timeval time[1]; |
---|
| 223 | |
---|
| 224 | gettimeofday( time, 0 ); |
---|
| 225 | return( (double) time->tv_sec + (double) time->tv_usec / 1000000 ); |
---|
| 226 | } |
---|
[5be87b2] | 227 | |
---|
| 228 | /* A pretty reliable random number generator. Tries to use the /dev/random |
---|
| 229 | devices first, and falls back to the random number generator from libc |
---|
| 230 | when it fails. Opens randomizer devices with O_NONBLOCK to make sure a |
---|
| 231 | lack of entropy won't halt BitlBee. */ |
---|
| 232 | void random_bytes( unsigned char *buf, int count ) |
---|
| 233 | { |
---|
| 234 | static int use_dev = -1; |
---|
| 235 | |
---|
| 236 | /* Actually this probing code isn't really necessary, is it? */ |
---|
| 237 | if( use_dev == -1 ) |
---|
| 238 | { |
---|
| 239 | if( access( "/dev/random", R_OK ) == 0 || access( "/dev/urandom", R_OK ) == 0 ) |
---|
| 240 | use_dev = 1; |
---|
| 241 | else |
---|
| 242 | { |
---|
| 243 | use_dev = 0; |
---|
| 244 | srand( ( getpid() << 16 ) ^ time( NULL ) ); |
---|
| 245 | } |
---|
| 246 | } |
---|
| 247 | |
---|
| 248 | if( use_dev ) |
---|
| 249 | { |
---|
| 250 | int fd; |
---|
| 251 | |
---|
| 252 | /* At least on Linux, /dev/random can block if there's not |
---|
| 253 | enough entropy. We really don't want that, so if it can't |
---|
| 254 | give anything, use /dev/urandom instead. */ |
---|
| 255 | if( ( fd = open( "/dev/random", O_RDONLY | O_NONBLOCK ) ) >= 0 ) |
---|
| 256 | if( read( fd, buf, count ) == count ) |
---|
| 257 | { |
---|
| 258 | close( fd ); |
---|
| 259 | return; |
---|
| 260 | } |
---|
| 261 | close( fd ); |
---|
| 262 | |
---|
| 263 | /* urandom isn't supposed to block at all, but just to be |
---|
| 264 | sure. If it blocks, we'll disable use_dev and use the libc |
---|
| 265 | randomizer instead. */ |
---|
| 266 | if( ( fd = open( "/dev/urandom", O_RDONLY | O_NONBLOCK ) ) >= 0 ) |
---|
| 267 | if( read( fd, buf, count ) == count ) |
---|
| 268 | { |
---|
| 269 | close( fd ); |
---|
| 270 | return; |
---|
| 271 | } |
---|
| 272 | close( fd ); |
---|
| 273 | |
---|
| 274 | /* If /dev/random blocks once, we'll still try to use it |
---|
| 275 | again next time. If /dev/urandom also fails for some |
---|
| 276 | reason, stick with libc during this session. */ |
---|
| 277 | |
---|
| 278 | use_dev = 0; |
---|
| 279 | srand( ( getpid() << 16 ) ^ time( NULL ) ); |
---|
| 280 | } |
---|
| 281 | |
---|
| 282 | if( !use_dev ) |
---|
| 283 | { |
---|
| 284 | int i; |
---|
| 285 | |
---|
| 286 | /* Possibly the LSB of rand() isn't very random on some |
---|
| 287 | platforms. Seems okay on at least Linux and OSX though. */ |
---|
| 288 | for( i = 0; i < count; i ++ ) |
---|
| 289 | buf[i] = rand() & 0xff; |
---|
| 290 | } |
---|
| 291 | } |
---|
| 292 | |
---|
| 293 | |
---|