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