source: trunk/third/moira/update/config.c @ 23740

Revision 23740, 3.4 KB checked in by broder, 15 years ago (diff)
In moira: * New CVS snapshot (Trac: #195) * Drop patches that have been incorporated upstream. * Update to build without krb4 on systems that no longer have it. This doesn't build yet on squeeze, which lacks a krb4 library, but I'm committing now before I start hacking away at a patch to fix that.
Line 
1/* $Id: config.c,v 1.7 1998-02-15 17:49:27 danw Exp $
2 *
3 * Routines to handle configuration file for Moira's update_server.
4 * These routines must load the file into memory rather than parse
5 * it each time as one of the things the server may do is chroot()
6 * itself.
7 *
8 * Copyright (C) 1992-1998 by the Massachusetts Institute of Technology.
9 * For copying and distribution information, please see the file
10 * <mit-copyright.h>.
11 */
12
13#include <mit-copyright.h>
14#include <moira.h>
15#include "update_server.h"
16
17#include <sys/stat.h>
18
19#include <ctype.h>
20#include <errno.h>
21#include <fcntl.h>
22#include <stdio.h>
23#include <stdlib.h>
24#include <string.h>
25#include <unistd.h>
26
27RCSID("$Header: /afs/athena.mit.edu/astaff/project/moiradev/repository/moira/update/config.c,v 1.7 1998-02-15 17:49:27 danw Exp $");
28
29#define CONFIG_FILE     "/etc/athena/moira.conf"
30
31/* Variables currently supported:
32 * chroot directory     daemon will run chrooted to this directory
33 * user username        daemon will run with this user's uid
34 * port portname        daemon will listen on this port number
35 * nofork               server stays in foreground & logs to stdout
36 * auth krbname         this user is authorized to connect
37 * noclobber            will not overwrite existing files
38 * noexec               will not execute instructions received
39 */
40
41static char *config_buf = NULL;
42static char **config_keys, **config_values;
43
44static int init(void)
45{
46  int fd, count = 0;
47  struct stat st;
48  char *p, *start;
49
50  /* Only execute once */
51  if (config_buf)
52    return MR_SUCCESS;
53
54  fd = open(CONFIG_FILE, O_RDONLY, 0);
55  if (fd < 0)
56    {
57      config_buf = "";
58      config_keys = malloc(sizeof(char *) * 2);
59      if (!config_keys)
60        return ENOMEM;
61      config_keys[0] = config_keys[1] = NULL;
62      return MR_SUCCESS;
63    }
64
65  if (fstat(fd, &st) < 0)
66    return MR_INTERNAL;
67
68  config_buf = malloc(st.st_size + 2);
69  if (!config_buf)
70    return ENOMEM;
71
72  if (read(fd, config_buf, st.st_size) < st.st_size)
73    {
74      free(config_buf);
75      config_buf = NULL;
76      return MR_INTERNAL;
77    }
78  config_buf[st.st_size] = '\0';
79
80  for (p = config_buf; *p; p++)
81    {
82      if (*p == '\n')
83        count++;
84    }
85  count++;
86  config_keys = malloc(count * sizeof(char *));
87  config_values = malloc(count * sizeof(char *));
88  if (!config_keys || !config_values)
89    {
90      free(config_buf);
91      free(config_keys);
92      free(config_values);
93      config_buf = NULL;
94      return ENOMEM;
95    }
96
97  count = 0;
98  for (p = strtok(config_buf, "\n"); p; p = strtok(NULL, "\n"))
99    config_keys[count++] = p;
100  config_keys[count] = NULL;
101
102  for (count = 0; config_keys[count]; count++)
103    {
104      config_values[count] = "";
105      for (p = config_keys[count]; *p; p++)
106        {
107          if (isspace(*p))
108            {
109              *p++ = '\0';
110              while (*p && isspace(*p))
111                p++;
112              config_values[count] = p;
113            }
114        }
115    }
116  return MR_SUCCESS;
117}
118
119
120/* Given a key, lookup the associated value.
121 * Returns "" on a key without a value, NULL on a non-existant key.
122 * If a key appears multiple times, successive calls will cycle through
123 * the possible values.
124 */
125
126char *config_lookup(char *key)
127{
128  static int i = 0;
129  int start;
130
131  if (init() != MR_SUCCESS)
132    return NULL;
133
134  start = i++;
135  if (!config_keys[i])
136    i = 0;
137  if (!config_keys[i])
138    return NULL;
139
140  do
141    {
142      if (!strcasecmp(key, config_keys[i]))
143        return config_values[i];
144      if (!config_keys[++i])
145        i = 0;
146    }
147  while (i != start);
148
149  if (!strcasecmp(key, config_keys[i]))
150    return config_values[i];
151
152  return NULL;
153}
Note: See TracBrowser for help on using the repository browser.