File: cfgfile.c

package info (click to toggle)
cvsd 1.0.13
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 1,372 kB
  • ctags: 157
  • sloc: sh: 4,552; ansic: 1,868; perl: 198; makefile: 147
file content (506 lines) | stat: -rw-r--r-- 14,021 bytes parent folder | download | duplicates (5)
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
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
/*
   cfgfile.c - handle configuration files

   Copyright (C) 2002, 2003, 2004 Arthur de Jong.

   This program 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 2, or (at your option)
   any later version.

   This program 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 this program; if not, write to the Free Software
   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/


#include "config.h"

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <pwd.h>
#include <grp.h>
#include <sys/types.h>

#include "cfgfile.h"
#include "xmalloc.h"
#ifdef HAVE_SETRLIMIT
#include "reslimit.h"
#endif /* HAVE_SETRLIMIT */
#include "cfg.h"
#include "log.h"


/* maximum linelength in the configuration file */
#define MAX_LINE_LENGTH 1024


/* the maximum number of options on a line */
#define MAX_LINE_OPTIONS 10


/* find out the uid this string describes
   returns 1 on success, 0 on failure */
static int get_uid(const char *str,uid_t *val)
{
  struct passwd *pwent;
  char *tmp;
  /* check if it is a valid numerical uid */
  *val=(uid_t)strtol(str,&tmp,0);
  if ((*str!='\0')&&(*tmp=='\0'))
    return 1;
  /* find by name */
  pwent=getpwnam(str);
  if (pwent!=NULL)
  {
    *val=pwent->pw_uid;
    return 1;
  }
  return 0;
}


/* find the group id this string represents
   returns 1 on success, 0 on failure */
static int get_gid(const char *str,gid_t *val)
{
  struct group *grent;
  char *tmp;
  /* check if it is a valid numerical uid */
  *val=(gid_t)strtol(str,&tmp,0);
  if ((*str!='\0')&&(*tmp=='\0'))
    return 1;
  /* find by name */
  grent=getgrnam(str);
  if (grent!=NULL)
  {
    *val=grent->gr_gid;
    return 1;
  }
  return 0;
}


/* split a line from the configuration file
   note that this code is not thread safe!
   the line value will be rewritten! */
static char **parse_configline(char *line,int *nopt)
{
  static char *retv[MAX_LINE_OPTIONS];
  int opt;
  for (opt=0;opt<MAX_LINE_OPTIONS;opt++)
  {
    /* skip beginning spaces */
    while ((*line==' ')||(*line=='\t'))
      line++;
    /* check for end of line or comment */
    if ((*line=='\0')||(*line=='#'))
      break; /* we're done */
    /* we have a new keyword */
    retv[opt]=line;
    /* find the end of the token */
    while ((*line!=' ')&&(*line!='\t')&&(*line!='\0'))
      line++;
    /* mark the end of the token */
    if (*line!='\0')
      *line++='\0';
  }
  *nopt=opt;
  return retv;
}


/* parse a log statement from the configfile */
static void parse_option_log(const char *filename,int lnr,
                             char * const opts[],int nopts)
{
  int level;
  if ((nopts<=1)||(nopts>3))
  {
    log_log(LOG_ERR,"%s:%d: Log: wrong number of arguments",filename,lnr);
    exit(1);
  }
  /* get the loglevel */
  level=LOG_INFO;
  if (nopts==3)
  {
    if ((level=log_getloglevel(opts[2]))<0)
    {
      log_log(LOG_ERR,"%s:%d: Log: wrong loglevel '%s'",filename,lnr,opts[2]);
      exit(1);
    }
  }
  /* find the method */
  if ( strcmp(opts[1],"none")==0 )
    log_addlogging_none();
  else if ( strcmp(opts[1],"syslog")==0 )
    log_addlogging_syslog(level);
  else if ( opts[1][0]=='/' )
    log_addlogging_file(opts[1],level);
  else
  {
    log_log(LOG_ERR,"%s:%d: Log: wrong argument '%s'",filename,lnr,opts[1]);
    exit(1);
  }
}


/* read the configuration file and save settings in struct */
void read_configfile(const char *filename,struct cvsd_cfg *cfg)
{
  FILE *fp;
  int lnr=0;
  char line[MAX_LINE_LENGTH];
  char *tmp;
  char **opts;
  int nopts;
  int i;
  int numrepos=0;
#ifdef HAVE_SETRLIMIT
  rlim_t resval;
#endif /* HAVE_SETRLIMIT */
  /* open configuration file */
  if ((fp=fopen(filename,"r"))==NULL)
  {
    log_log(LOG_ERR,"cannot open config file (%s): %s",filename,strerror(errno));
    exit(1);
  }
  log_log(LOG_DEBUG,"debug: reading config file (%s)",filename);
  /* read all lines in the file */
  while (fgets(line,MAX_LINE_LENGTH,fp)!=NULL)
  {
    lnr++;
    /* strip newline */
    i=(int)strlen(line);
    if ((i<=0)||(line[i-1]!='\n'))
    {
      log_log(LOG_ERR,"%s:%d: line too long or last line missing newline",filename,lnr);
      exit(1);
    }
    line[i-1]='\0';
    /* split the line in tokens */
    opts=parse_configline(line,&nopts);
    /* find identifier */
    if (nopts==0)
    {} /* ignore empty lines */
    else if (strcmp(opts[0],"RootJail")==0)
    {
      if (nopts!=2)
      {
        log_log(LOG_ERR,"%s:%d: RootJail: wrong number of arguments",filename,lnr);
        exit(1);
      }
      /* only accept absolute paths. abort otherwise.*/
      if ((*opts[1]!='/')&&(strcmp(opts[1],"none")!=0))
      {
        log_log(LOG_ERR,"%s:%d: RootJail: '%s' must be an absolute path or 'none'",filename,lnr,opts[1]);
        exit(1);
      }
      /* save value */
      if (cfg->rootjail!=NULL)
      {
        log_log(LOG_ERR,"%s:%d: RootJail: can only be specified once",filename,lnr);
      }
      else
      {
        cfg->rootjail=xstrdup(opts[1]);
      }
    }
    else if (strcmp(opts[0],"Nice")==0)
    {
      if (nopts!=2)
      {
        log_log(LOG_ERR,"%s:%d: Nice: wrong number of arguments",filename,lnr);
        exit(1);
      }
      /* check if it is a valid numerical uid */
      cfg->nice=(int)strtol(opts[1],&tmp,0);
      if (*tmp!='\0')
      {
        log_log(LOG_ERR,"%s:%d: Nice: wrong argument '%s'",filename,lnr,opts[1]);
        exit(1);
      }
    }
    else if (strcmp(opts[0],"Uid")==0)
    {
      if (nopts!=2)
      {
        log_log(LOG_ERR,"%s:%d: Uid: wrong number of arguments",filename,lnr);
        exit(1);
      }
      if (!get_uid(opts[1],&cfg->uid))
      {
        log_log(LOG_ERR,"%s:%d: Uid: wrong argument '%s'",filename,lnr,opts[1]);
        exit(1);
      }
    }
    else if (strcmp(opts[0],"Gid")==0)
    {
      if (nopts!=2)
      {
        log_log(LOG_ERR,"%s:%d: Gid: wrong number of arguments",filename,lnr);
        exit(1);
      }
      if (!get_gid(opts[1],&cfg->gid))
      {
        log_log(LOG_ERR,"%s:%d: Gid: wrong argument '%s'",filename,lnr,opts[1]);
        exit(1);
      }
    }
    else if (strcmp(opts[0],"Listen")==0)
    {
      if (nopts==2)
      {
        /* accept [node]:service and [node].service */
        if (tmp=strrchr(opts[1],']'),(tmp!=NULL)&&(opts[1][0]=='[')&&((tmp[1]=='.')||(tmp[1]==':')))
        {
          *tmp='\0';
          cfg_addaddress(cfg,filename,lnr,opts[1]+1,tmp+2);
        }
        /* accept nodewithoutcolons:service */
        else if ((tmp=strchr(opts[1],':'),(tmp!=NULL)&&(tmp==strrchr(opts[1],':'))))
        {
          *tmp='\0';
          cfg_addaddress(cfg,filename,lnr,opts[1],tmp+1);
        }
        /* accept nodewithoutdots.service */
        else if ((tmp=strchr(opts[1],'.'),(tmp!=NULL)&&(tmp==strrchr(opts[1],'.'))))
        {
          *tmp='\0';
          cfg_addaddress(cfg,filename,lnr,opts[1],tmp+1);
        }
        else
        {
          log_log(LOG_ERR,"%s:%d: Listen: wrong argument '%s'",filename,lnr,opts[1]);
          exit(1);
        }
      }
      else if (nopts==3)
      {
        cfg_addaddress(cfg,filename,lnr,opts[1],opts[2]);
      }
      else
      {
        log_log(LOG_ERR,"%s:%d: Listen: wrong number of arguments",filename,lnr);
        exit(1);
      }
    }
    else if (strcmp(opts[0],"MaxConnections")==0)
    {
      if (nopts!=2)
      {
        log_log(LOG_ERR,"%s:%d: MaxConnections: wrong number of arguments",filename,lnr);
        exit(1);
      }
      cfg->maxconnections=(int)strtol(opts[1],&tmp,0);
      if (*tmp!='\0')
      {
        log_log(LOG_ERR,"%s:%d: MaxConnections: wrong argument '%s'",filename,lnr,opts[1]);
        exit(1);
      }
    }
    else if (strcmp(opts[0],"Repos")==0)
    {
      if (nopts!=2)
      {
        log_log(LOG_ERR,"%s:%d: Repos: wrong number of arguments",filename,lnr);
        exit(1);
      }
      /* only accept absolute paths. abort otherwise.*/
      if (*opts[1]!='/')
      {
        log_log(LOG_ERR,"%s:%d: Repos: '%s' must be an absolute path",filename,lnr,opts[1]);
        exit(1);
      }
      /* add --allow-root=path to cvs parameters */
      tmp=(char *)xmalloc((13+strlen(opts[1])+1)*sizeof(char));
      strcpy(tmp,"--allow-root=");
      strcat(tmp,opts[1]);
      cfg_addcvsarg(cfg,tmp);
      numrepos++;
    }
    else if (strcmp(opts[0],"PidFile")==0)
    {
      if (nopts!=2)
      {
        log_log(LOG_ERR,"%s:%d: PidFile: wrong number of arguments",filename,lnr);
        exit(1);
      }
      /* only accept absolute paths. abort otherwise.*/
      if (*opts[1]!='/')
      {
        log_log(LOG_ERR,"%s:%d: PidFile: '%s' must be an absolute path",filename,lnr,opts[1]);
        exit(1);
      }
      if (cfg->pidfile!=NULL)
      {
        log_log(LOG_ERR,"%s:%d: PidFile: can only be specified once",filename,lnr);
        exit(1);
      }
      else
      {
        cfg->pidfile=xstrdup(opts[1]);
      }
    }
    else if (strcmp(opts[0],"CvsCommand")==0)
    {
      if (nopts!=2)
      {
        log_log(LOG_ERR,"%s:%d: CvsCommand: wrong number of arguments",filename,lnr);
        exit(1);
      }
      /* only accept absolute paths, abort otherwise */
      if (*opts[1]!='/')
      {
        log_log(LOG_ERR,"%s:%d: CvsCommand: '%s' must be an absolute path",filename,lnr,opts[1]);
        exit(1);
      }
      /* save value */
      if (cfg->cvscmd!=NULL)
      {
        log_log(LOG_ERR,"%s:%d: CvsCommand: can only be specified once",filename,lnr);
        exit(1);
      }
      else
      {
        cfg->cvscmd=xstrdup(opts[1]);
      }
    }
    else if (strcmp(opts[0],"CvsArgs")==0)
    {
      if (nopts<2)
      {
        log_log(LOG_ERR,"%s:%d: CvsArgs: wrong number of arguments",filename,lnr);
        exit(1);
      }
      for (i=1;i<nopts;i++)
      {
        /* TODO: check if arguments look like valid cvs parameters and issue warning otherwise */
        tmp=(char *)xmalloc((strlen(opts[i])+1)*sizeof(char));
        strcpy(tmp,opts[i]);
        cfg_addcvsarg(cfg,tmp);
      }
    }
    else if (strcmp(opts[0],"Limit")==0)
    {
#ifdef HAVE_SETRLIMIT
      if (nopts!=3)
      {
        log_log(LOG_ERR,"%s:%d: Limit: wrong number of arguments",filename,lnr);
        exit(1);
      }
      /* read value */
      if ( (strcmp(opts[2],"none")==0) ||
           (strcmp(opts[2],"inf")==0) ||
           (strcmp(opts[2],"unlimited")==0) )
        resval=RLIM_INFINITY;
      else
        resval=0;
      switch (getresourcetype(opts[1]))
      {
      case RESTYPE_NUM:  /* numeric value */
      case RESTYPE_SIZE: /* size value */
        if (resval==RLIM_INFINITY) break;
        resval=(rlim_t)strtol(opts[2],&tmp,0);
        if ((tmp[0]=='k')&&(tmp[1]=='\0'))
          resval*=(rlim_t)1024;
        else if ((tmp[0]=='m')&&(tmp[1]=='\0'))
          resval*=(rlim_t)(1024*1024);
        else if ((tmp[0]!='\0')&&(tmp[0]!='b'))
        {
          log_log(LOG_ERR,"%s:%d: Limit: wrong value '%s'",filename,lnr,opts[2]);
          exit(1);
        }
        break;
      case RESTYPE_TIME: /* time value */
        if (resval==RLIM_INFINITY) break;
        resval=(rlim_t)strtol(opts[2],&tmp,0);
        if ((tmp[0]=='m')&&(tmp[1]=='\0'))
          resval*=(rlim_t)60;
        else if ((tmp[0]==':')&&(tmp[1]!='\0'))
        {
          tmp++;
          resval*=(rlim_t)60;
          resval+=(rlim_t)strtol(tmp,&tmp,0);
          if (tmp[0]!='\0')
          {
            log_log(LOG_ERR,"%s:%d: Limit: wrong value '%s'",filename,lnr,opts[2]);
            exit(1);
          }
        }
        else if ((tmp[0]!='\0')&&(tmp[0]!='s'))
        {
          log_log(LOG_ERR,"%s:%d: Limit: wrong value '%s'",filename,lnr,opts[2]);
          exit(1);
        }
        break;
      default:
        log_log(LOG_ERR,"%s:%d: Limit: unknown or unsupported resource '%s'",filename,lnr,opts[1]);
        exit(1);
      }
      /* save the limit, execute with setreslimits() */
      savereslimit(opts[1],resval,resval);
#else /* HAVE_SETRLIMIT */
      log_log(LOG_ERR,"%s:%d: Limit: not supported on this system",filename,lnr);
      exit(1);
#endif /* not HAVE_SETRLIMIT */
    }
#ifdef USE_CAPABILITIES
    else if (strcmp(opts[0],"Capabilities")==0)
    {
      /* TODO: fix this because capabilities may be space separated */
      if (nopts!=2)
      {
        log_log(LOG_ERR,"%s:%d: Capabilities: wrong number of arguments",filename,lnr);
        exit(1);
      }
      /* save value */
      if (cfg->capabilities!=NULL)
      {
        log_log(LOG_ERR,"%s:%d: Capabilities: can only be specified once",filename,lnr);
      }
      else if ((cfg->capabilities=cap_from_text(opts[1]))==NULL)
      {
        log_log(LOG_ERR,"%s:%d: Capabilities: wrong value '%s': %s",filename,lnr,opts[1],strerror(errno));
        exit(1);
      }
    }
#endif /* USE_CAPABILITIES */
    else if (strcmp(opts[0],"Umask")==0)
    {
      if (nopts!=2)
      {
        log_log(LOG_ERR,"%s:%d: Umask: wrong number of arguments",filename,lnr);
        exit(1);
      }
      /* check if it is a valid numerical uid */
      cfg->umask=(mode_t)strtol(opts[1],&tmp,8);
      if ((*tmp!='\0')||(cfg->umask<0000)||(cfg->umask>0777))
      {
        log_log(LOG_ERR,"%s:%d: Umask: wrong argument '%s'",filename,lnr,opts[1]);
        exit(1);
      }
    }
    else if (strcmp(opts[0],"Log")==0)
      parse_option_log(filename,lnr,opts,nopts);
    else
    {
      log_log(LOG_ERR,"%s:%d: unrecognized line",filename,lnr);
      exit(1);
    }
  }
  fclose(fp);
  if (numrepos==0)
  {
    log_log(LOG_ERR,"%s: contains no 'Repos' statements",filename);
    exit(1);
  }
  log_log(LOG_DEBUG,"debug: done reading config file");
}