source: conf.c @ e62762b

Last change on this file since e62762b was d25f6fc, checked in by Wilmer van der Gaast <wilmer@…>, at 2005-12-26T14:02:47Z

Added OperPassword and RunMode = ForkDaemon settings. Oper stuff is
*INSECURE* because users can just do /mode +o to become operator.

  • Property mode set to 100644
File size: 8.3 KB
Line 
1  /********************************************************************\
2  * BitlBee -- An IRC to other IM-networks gateway                     *
3  *                                                                    *
4  * Copyright 2002-2005 Wilmer van der Gaast and others                *
5  \********************************************************************/
6
7/* Configuration reading code                                           */
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 <stdio.h>
29#include <string.h>
30#include <stdlib.h>
31#include "conf.h"
32#include "ini.h"
33#include "url.h"
34
35#include "protocols/proxy.h"
36
37char *CONF_FILE;
38
39static int conf_loadini( conf_t *conf, char *file );
40
41conf_t *conf_load( int argc, char *argv[] )
42{
43        conf_t *conf;
44        int opt, i;
45       
46        conf = g_new0( conf_t, 1 );
47       
48        conf->iface = "0.0.0.0";
49        conf->port = 6667;
50        conf->nofork = 0;
51        conf->verbose = 0;
52        conf->primary_storage = "text";
53        conf->runmode = RUNMODE_INETD;
54        conf->authmode = AUTHMODE_OPEN;
55        conf->auth_pass = NULL;
56        conf->oper_pass = NULL;
57        conf->configdir = g_strdup( CONFIG );
58        conf->plugindir = g_strdup( PLUGINDIR );
59        conf->motdfile = g_strdup( ETCDIR "/motd.txt" );
60        conf->ping_interval = 180;
61        conf->ping_timeout = 300;
62       
63        i = conf_loadini( conf, CONF_FILE );
64        if( i == 0 )
65        {
66                fprintf( stderr, "Error: Syntax error in configuration file `%s'.\n", CONF_FILE );
67                return( NULL );
68        }
69        else if( i == -1 )
70        {
71                fprintf( stderr, "Warning: Unable to read configuration file `%s'.\n", CONF_FILE );
72        }
73       
74        while( ( opt = getopt( argc, argv, "i:p:nvIDFc:d:h" ) ) >= 0 )
75        {
76                if( opt == 'i' )
77                {
78                        conf->iface = g_strdup( optarg );
79                }
80                else if( opt == 'p' )
81                {
82                        if( ( sscanf( optarg, "%d", &i ) != 1 ) || ( i <= 0 ) || ( i > 65535 ) )
83                        {
84                                fprintf( stderr, "Invalid port number: %s\n", optarg );
85                                return( NULL );
86                        }
87                        conf->port = i;
88                }
89                else if( opt == 'n' )
90                        conf->nofork=1;
91                else if( opt == 'v' )
92                        conf->verbose=1;
93                else if( opt == 'I' )
94                        conf->runmode=RUNMODE_INETD;
95                else if( opt == 'D' )
96                        conf->runmode=RUNMODE_DAEMON;
97                else if( opt == 'F' )
98                        conf->runmode=RUNMODE_FORKDAEMON;
99                else if( opt == 'c' )
100                {
101                        if( strcmp( CONF_FILE, optarg ) != 0 )
102                        {
103                                g_free( CONF_FILE );
104                                CONF_FILE = g_strdup( optarg );
105                                g_free( conf );
106                                return( conf_load( argc, argv ) );
107                        }
108                }
109                else if( opt == 'd' )
110                {
111                        g_free( conf->configdir );
112                        conf->configdir = g_strdup( optarg );
113                }
114                else if( opt == 'h' )
115                {
116                        printf( "Usage: bitlbee [-D [-i <interface>] [-p <port>] [-n] [-v]] [-I]\n"
117                                "               [-c <file>] [-d <dir>] [-h]\n"
118                                "\n"
119                                "An IRC-to-other-chat-networks gateway\n"
120                                "\n"
121                                "  -I  Classic/InetD mode. (Default)\n"
122                                "  -D  Daemon mode. (Still EXPERIMENTAL!)\n"
123                                "  -F  Forking daemon. (one process per client)\n"
124                                "  -i  Specify the interface (by IP address) to listen on.\n"
125                                "      (Default: 0.0.0.0 (any interface))\n"
126                                "  -p  Port number to listen on. (Default: 6667)\n"
127                                "  -n  Don't fork.\n"
128                                "  -v  Be verbose (only works in combination with -n)\n"
129                                "  -c  Load alternative configuration file\n"
130                                "  -d  Specify alternative user configuration directory\n"
131                                "  -h  Show this help page.\n" );
132                        return( NULL );
133                }
134        }
135       
136        if( conf->configdir[strlen(conf->configdir)-1] != '/' )
137        {
138                char *s = g_new( char, strlen( conf->configdir ) + 2 );
139               
140                sprintf( s, "%s/", conf->configdir );
141                g_free( conf->configdir );
142                conf->configdir = s;
143        }
144       
145        return( conf );
146}
147
148static int conf_loadini( conf_t *conf, char *file )
149{
150        ini_t *ini;
151        int i;
152       
153        ini = ini_open( file );
154        if( ini == NULL ) return( -1 );
155        while( ini_read( ini ) )
156        {
157                if( g_strcasecmp( ini->section, "settings" ) == 0 )
158                {
159                        if( g_strcasecmp( ini->key, "runmode" ) == 0 )
160                        {
161                                if( g_strcasecmp( ini->value, "daemon" ) == 0 )
162                                        conf->runmode = RUNMODE_DAEMON;
163                                else if( g_strcasecmp( ini->value, "forkdaemon" ) == 0 )
164                                        conf->runmode = RUNMODE_FORKDAEMON;
165                                else
166                                        conf->runmode = RUNMODE_INETD;
167                        }
168                        else if( g_strcasecmp( ini->key, "daemoninterface" ) == 0 )
169                        {
170                                conf->iface = g_strdup( ini->value );
171                        }
172                        else if( g_strcasecmp( ini->key, "daemonport" ) == 0 )
173                        {
174                                if( ( sscanf( ini->value, "%d", &i ) != 1 ) || ( i <= 0 ) || ( i > 65535 ) )
175                                {
176                                        fprintf( stderr, "Invalid port number: %s\n", ini->value );
177                                        return( 0 );
178                                }
179                                conf->port = i;
180                        }
181                        else if( g_strcasecmp( ini->key, "authmode" ) == 0 )
182                        {
183                                if( g_strcasecmp( ini->value, "registered" ) == 0 )
184                                        conf->authmode = AUTHMODE_REGISTERED;
185                                else if( g_strcasecmp( ini->value, "closed" ) == 0 )
186                                        conf->authmode = AUTHMODE_CLOSED;
187                                else
188                                        conf->authmode = AUTHMODE_OPEN;
189                        }
190                        else if( g_strcasecmp( ini->key, "authpassword" ) == 0 )
191                        {
192                                conf->auth_pass = g_strdup( ini->value );
193                        }
194                        else if( g_strcasecmp( ini->key, "operpassword" ) == 0 )
195                        {
196                                conf->oper_pass = g_strdup( ini->value );
197                        }
198                        else if( g_strcasecmp( ini->key, "hostname" ) == 0 )
199                        {
200                                conf->hostname = g_strdup( ini->value );
201                        }
202                        else if( g_strcasecmp( ini->key, "configdir" ) == 0 )
203                        {
204                                g_free( conf->configdir );
205                                conf->configdir = g_strdup( ini->value );
206                        }
207                        else if( g_strcasecmp( ini->key, "motdfile" ) == 0 )
208                        {
209                                g_free( conf->motdfile );
210                                conf->motdfile = g_strdup( ini->value );
211                        }
212                        else if( g_strcasecmp( ini->key, "account_storage" ) == 0 )
213                        {
214                                g_free( conf->primary_storage );
215                                conf->primary_storage = g_strdup( ini->value );
216                        }
217                        else if( g_strcasecmp( ini->key, "account_storage_migrate" ) == 0 )
218                        {
219                                g_strfreev( conf->migrate_storage );
220                                conf->migrate_storage = g_strsplit( ini->value, " \t,;", -1 );
221                        }
222                        else if( g_strcasecmp( ini->key, "pinginterval" ) == 0 )
223                        {
224                                if( sscanf( ini->value, "%d", &i ) != 1 )
225                                {
226                                        fprintf( stderr, "Invalid %s value: %s\n", ini->key, ini->value );
227                                        return( 0 );
228                                }
229                                conf->ping_interval = i;
230                        }
231                        else if( g_strcasecmp( ini->key, "pingtimeout" ) == 0 )
232                        {
233                                if( sscanf( ini->value, "%d", &i ) != 1 )
234                                {
235                                        fprintf( stderr, "Invalid %s value: %s\n", ini->key, ini->value );
236                                        return( 0 );
237                                }
238                                conf->ping_timeout = i;
239                        }
240                        else if( g_strcasecmp( ini->key, "proxy" ) == 0 )
241                        {
242                                url_t *url = g_new0( url_t, 1 );
243                               
244                                if( !url_set( url, ini->value ) )
245                                {
246                                        fprintf( stderr, "Invalid %s value: %s\n", ini->key, ini->value );
247                                        g_free( url );
248                                        return( 0 );
249                                }
250                               
251                                strncpy( proxyhost, url->host, sizeof( proxyhost ) );
252                                strncpy( proxyuser, url->user, sizeof( proxyuser ) );
253                                strncpy( proxypass, url->pass, sizeof( proxypass ) );
254                                proxyport = url->port;
255                                if( url->proto == PROTO_HTTP )
256                                        proxytype = PROXY_HTTP;
257                                else if( url->proto == PROTO_SOCKS4 )
258                                        proxytype = PROXY_SOCKS4;
259                                else if( url->proto == PROTO_SOCKS5 )
260                                        proxytype = PROXY_SOCKS5;
261                               
262                                g_free( url );
263                        }
264                        else
265                        {
266                                fprintf( stderr, "Error: Unknown setting `%s` in configuration file.\n", ini->key );
267                                return( 0 );
268                                /* For now just ignore unknown keys... */
269                        }
270                }
271                else if( g_strcasecmp( ini->section, "defaults" ) != 0 )
272                {
273                        fprintf( stderr, "Error: Unknown section [%s] in configuration file. "
274                                         "BitlBee configuration must be put in a [settings] section!\n", ini->section );
275                        return( 0 );
276                }
277        }
278        ini_close( ini );
279       
280        return( 1 );
281}
282
283void conf_loaddefaults( irc_t *irc )
284{
285        ini_t *ini;
286       
287        ini = ini_open( CONF_FILE );
288        if( ini == NULL ) return;
289        while( ini_read( ini ) )
290        {
291                if( g_strcasecmp( ini->section, "defaults" ) == 0 )
292                {
293                        set_t *s = set_find( irc, ini->key );
294                       
295                        if( s )
296                        {
297                                if( s->def ) g_free( s->def );
298                                s->def = g_strdup( ini->value );
299                        }
300                }
301        }
302        ini_close( ini );
303}
Note: See TracBrowser for help on using the repository browser.