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