summaryrefslogtreecommitdiffstats
path: root/httpserver/config.cc
blob: d4c235bf0848b6990b44ef1a8e7d68515e257cf6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
/*
* This file is part of rasdaman community.
*
* Rasdaman community is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Rasdaman community is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with rasdaman community.  If not, see <http://www.gnu.org/licenses/>.
*
* Copyright 2003, 2004, 2005, 2006, 2007, 2008, 2009 Peter Baumann /
rasdaman GmbH.
*
* For more information please see <http://www.rasdaman.org>
* or contact Peter Baumann via <baumann@rasdaman.com>.
*/
#include "mymalloc/mymalloc.h"
/*------------------------------------------------------------------------*/
/*  config.c - configuration and setup stuff.                             */
/*------------------------------------------------------------------------*/
/*  Comments:                                                             */
/*      - intended purpose:                                               */
/*          Contains all code related to setup the configuration.         */
/*      - Status:                                                         */
/*          - There were more features planned than are now available.    */
/*          - Especially some arguments are disabled or non-functional,   */
/*            so: onle use "-c <config-filepath>", and don't care about   */
/*            the rest!                                                   */
/*          - The Flag Handling of Verbose/Debug-Mode is somewhat broken  */
/*          - ReadConfig() uses static buffers. But they are protected    */
/*            against overwriting.                                        */
/*          - ReadConfig() contains some printf()'s.                      */
/*          - Handling of Sub-Server configuration still disabled.        */
/*          - The rest should be okay.                                    */
/*------------------------------------------------------------------------*/

#include <iostream>
using namespace std;

#include   "defs.h"
#include   "protos.h"
#include   "server.h"
#include   <unistd.h>    // for gethostname()

#ifdef SOLARIS
  // function prototype with C linkage
  extern "C" int gethostname(char *name, int namelen);
#endif

extern int globalHTTPPort;

/****** config/ReadArgs ******************************************************
*                                                                             
*   NAME                                                                      
*       ReadArgs -- 
*                                                                             
*   SYNOPSIS                                                                  
*       int ReadArgs( int argc, char *argv[] );                               
*                                                                             
*   FUNCTION                                                                  
*                                                                             
*   RESULT                                                                    
*                                                                             
*   NOTES                                                                     
*                                                                             
*   BUGS                                                                      
*                                                                             
*   SEE ALSO                                                                  
*                                                                             
******************************************************************************
*
*/

rc_t ReadArgs( struct ServerBase *Server, int argc, char *argv[] )
{
  int   i;
  int   LogMode   = LM_VERBOSE;
  char *Filename  = NULL;

  for( i = 1; i < argc; ++i)
    {
      if( argv[i][0] != '-' )
	{
	  break;
	}
      switch( argv[i][1] )
	{
	case 'D':             /* Debug-Flag setzen  */
	  LogMode |= LF_STDERR;
	  break;
	case 'V':             /* Verbose-Flag setzen  */
	  LogMode |= LF_VERB;
	  break;
	case 'c':             /* Config-File im naechsten Argument */
	  if( ++i >= argc )   /* Keine weiteren Argumente? */
	    {
	      printf( "Oops: Missing config filename after '-c' switch.\n" );
	      printf( "Please try again.\n" );
	      Exit( FAIL );
	    }
	  else    
	    {
	      Filename = argv[i];
	    }
	  break;
	case 'h':
	  printf( "Usage: %s [-c <ConfigFile>] [-V] [-D]\n", argv[0] );
	  Exit( WARN );
	  break;
	case '?':
	  printf( "Usage: %s [-c <ConfigFile>] [-V] [-D]\n", argv[0] );
	  Exit( WARN );
	  break;
	default:              /* Kein bekanntes Argument */
	  printf( "Unknown option '%s'.\n", argv[i] );
	  Exit( FAIL );
	  break;
	}
    }

  return( CheckAndSet( Server, Filename, LogMode ) );
}


/****** config/CheckAndSet ***************************************************
*                                                                             
*   NAME                                                                      
*       CheckAndSet -- 
*                                                                             
*   SYNOPSIS                                                                  
*       int CheckAndSet( char *Directory, char *Filename, int LogMode );      
*                                                                             
*   FUNCTION                                                                  
*                                                                             
*   RESULT                                                                    
*                                                                             
*   NOTES                                                                     
*                                                                             
*   BUGS                                                                      
*                                                                             
*   SEE ALSO                                                                  
*                                                                             
******************************************************************************
*
*/

rc_t CheckAndSet( struct ServerBase *Server, char *Filename, int LogMode )
{
  rc_t  RC = 0;

  /* we skipped the config file
  char *Def_ConfFile = "httpserver.conf";

  if( Filename == NULL )
    {
      LogMsg( LG_SERVER, WARN, "WARN:  No config file given - using default %s.",
             Def_ConfFile );
      Filename = Def_ConfFile;
    }
 
  SetFilename( Server, FT_CONFFILE, Filename );
  */
  Server->Log.Mode = LogMode;

  return( RC );
}


/****** config/SetServDir ****************************************************
*                                                                             
*   NAME                                                                      
*       SetServDir -- 
*                                                                             
*   SYNOPSIS                                                                  
*       int SetServDir( char *Dirname );                                      
*                                                                             
*   FUNCTION                                                                  
*                                                                             
*   RESULT                                                                    
*                                                                             
*   NOTES                                                                     
*                                                                             
*   BUGS                                                                      
*                                                                             
*   SEE ALSO                                                                  
*                                                                             
******************************************************************************
*
*/

rc_t SetServDir( struct ServerBase *Server, char *Dirname )
{
  char   *Buffer;
  size_t  BuffSize;
  size_t  MaxPath;

  if ( ( Buffer = PathAlloc( &BuffSize ) ) == NULL )
    ErrorMsg( E_SYS, FAIL, "FAIL:  Buffer allocation for pathname of server directory." );
  Buffer[0] = '\0';

  /* Platz lassen, um auch noch Filenamen + '\0' im Buffer  */
  /* unterbringen zu können.  */

  MaxPath = BuffSize - strlen( "httpserver.conf" );
  if( strlen( Dirname ) >= MaxPath )
    ErrorMsg( E_SYS, FAIL, "FAIL:  Pathname of server directory too long." );
  Buffer = strncpy( Buffer, Dirname, MaxPath );

  if( Buffer[ strlen( Buffer ) - 1 ] != '/' )
      Buffer = strcat( Buffer, "/" );     

  if(Server->Directory) free(Server->Directory);
  Server->Directory = Buffer;
  return( OK );
}


/****** config/SetFilename ***************************************************
*                                                                             
*   NAME                                                                      
*       SetConfFile -- 
*                                                                             
*   SYNOPSIS                                                                  
*       int SetConfFile( char *Filename );                                    
*                                                                             
*   FUNCTION                                                                  
*                                                                             
*   RESULT                                                                    
*                                                                             
*   NOTES                                                                     
*                                                                             
*   BUGS                                                                      
*                                                                             
*   SEE ALSO                                                                  
*                                                                             
******************************************************************************
*
*/


rc_t SetFilename( struct ServerBase *Server, int Type, char *Filename )
{
  char   *Buffer = NULL;
  size_t  BuffSize = 0;
  size_t  PathLength, FileLength;

  if( Filename != NULL )
    {
      if ( ( Buffer = PathAlloc( &BuffSize ) ) == NULL )
	ErrorMsg( E_SYS, FAIL, "FAIL:  Buffer allocation for filename \"%s\".", Filename );
      Buffer[0] = '\0';
      
      if( Server->Directory != NULL )
	PathLength = strlen( Server->Directory );
      FileLength = strlen( Filename );

      if( FileLength >= BuffSize )
	ErrorMsg( E_SYS, FAIL, "FAIL:  Pathname of file \"%s\" too long.", Filename );
      
      if( Filename[0] != '/' )
	{
	  if( ( PathLength + FileLength ) >= BuffSize )
	    ErrorMsg( E_SYS, FAIL, "FAIL:  Path/filename of file \"%s\" too long.", Filename );
	  
	  Buffer = strncpy( Buffer, Server->Directory, BuffSize );
	}
      Buffer = strncat( Buffer, Filename, FileLength );
    }

  switch( Type )
    {
    case FT_CONFFILE:
      Server->ConfigFile = Buffer;
      break;
    case FT_ACCESSLOG:
      Server->Log.Access.Filename = Buffer;
      break;
    case FT_SERVERLOG:
      Server->Log.Server.Filename = Buffer;
      break;
    case FT_COMMLOG:
      Server->Log.Comm.Filename = Buffer;
      break;
    case FT_PIDFILE:
      Server->PidFile = Buffer;
      break;
    default:
      ErrorMsg( E_SYS, ERROR, "ERROR: Unknown filetype: %d.", Type );
      return( ERROR );
      break;
    }
  return( OK );
}


rc_t SetString( struct ServerBase *Server, int Type, char *String )
{
  char   *Buffer = NULL;
  size_t  BuffSize = 0;

  if( String != NULL )
    {
      BuffSize = strlen( String );
      if ( ( Buffer = (char*)mymalloc( BuffSize+1 ) ) == NULL )
	ErrorMsg( E_SYS, FAIL, "FAIL:  Buffer allocation for string \"%s\".", String );
      strcpy( Buffer, String );
      
      switch( Type )
	{
	case ST_HOSTNAME:
	  Server->Host.Name = Buffer;
	  break;
	case ST_MAILADDRESS:
	  Server->AdminMailAddress = Buffer;
	  break;
	default:
	  ErrorMsg( E_SYS, ERROR, "ERROR: Unknown stringtype: %d.", Type );
	  return( ERROR );
	  break;
	}
      return( OK );
    }
  else
    return( ERROR );
}


/****** config/ConfigureServer ***********************************************
*                                                                             
*   NAME                                                                      
*       ConfigureServer -- 
*                                                                             
*   SYNOPSIS                                                                  
*       int ConfigureServer( char *Keyword, char *Value1, char *Value2,       
*                            char *Value3 );                                  
*                                                                             
*   FUNCTION                                                                  
*                                                                             
*   RESULT                                                                    
*                                                                             
*   NOTES                                                                     
*                                                                             
*   BUGS                                                                      
*                                                                             
*   SEE ALSO                                                                  
*                                                                             
******************************************************************************
*
*/

rc_t ConfigureServer( struct ServerBase *Server )
{
  char* myName;
  char* rmanhome = CONFDIR;
  int MaxURLLength = 120;
  int BuffSize;
  char* Buffer;

  BuffSize = strlen( rmanhome );

  if ( ( Buffer = (char*)mymalloc( BuffSize+200 ) ) == NULL )
	ErrorMsg( E_SYS, FAIL, "FAIL:  Buffer allocation for Server Configuration" );
  if( gethostname( Buffer, 200 ) )
	ErrorMsg( E_SYS, FAIL, "FAIL:  Could not determine my Hostname" );
 
  myName=strdup(Buffer);

  strcpy(Buffer,rmanhome);
  SetFilename( Server, FT_COMMLOG, strcat(Buffer,"/httpcomm.log") );
	
  Server->Port = globalHTTPPort;

  SetString( Server, ST_MAILADDRESS, "admin@localhost" );

  SetString( Server, ST_HOSTNAME, myName );

  SetServDir( Server, rmanhome );

  strcpy(Buffer,rmanhome);
  SetFilename( Server, FT_ACCESSLOG, strcat(Buffer,"/httpaccess.log") );

  Server->MaxURLLength = MaxURLLength;

  strcpy(Buffer,rmanhome);
  SetFilename( Server, FT_PIDFILE, strcat(Buffer,"/httpserver.pid") );

  strcpy(Buffer,rmanhome);
  SetFilename( Server, FT_SERVERLOG, strcat(Buffer,"/httpserver.log") );

  free(Buffer);
  free(myName);
  
  return( OK );
}


/****** config/GetConfigKey **************************************************
*                                                                             
*   NAME                                                                      
*       
*                                                                             
*   SYNOPSIS                                                                  
*       
*                                                                             
*   FUNCTION                                                                  
*       
*                                                                             
*   INPUTS                                                                    
*       
*                                                                             
*   RESULT                                                                    
*       
*                                                                             
*   NOTES                                                                     
*        With Help from K&R, Chapter 6, binsearch().                          
*                                                                             
*   BUGS                                                                      
*       
*                                                                             
*   SEE ALSO                                                                  
*       
*                                                                             
******************************************************************************
*
*/

int GetConfigKey( char *Keyword )
{
  struct KeywordKey KeyTable[] =
    {
      { "AccessLog",    KEY_ACCESSLOG },
      { "CommLog",      KEY_COMMLOG },
      { "IndexFile",    KEY_INDEXFILE },
      { "MaxURLLength", KEY_MAXURLLENGTH },
      { "PidFile",      KEY_PIDFILE },
      { "Port",         KEY_PORT },
      { "ServerAdmin",  KEY_SERVERADMIN },
      { "ServerLog",    KEY_SERVERLOG },
      { "ServerName",   KEY_SERVERNAME },
      { "ServerRoot",   KEY_SERVERROOT },
    };

    int cond;
    int low; 
    int high;

    low  = 0;
    high = NUM_KEYS - 1;
    
    while( low < high )
      {
	if( ( cond = strcmp( Keyword, KeyTable[ low ].Keyword ) ) == 0 )
	  return( KeyTable[ low ].Key );
	low++;
      }
      
   return -1;
}