source: unix.c @ 46d215d

Last change on this file since 46d215d was ae3dc99, checked in by Wilmer van der Gaast <wilmer@…>, at 2010-04-24T17:02:07Z

Merging stuff from mainline (1.2.6).

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