]> andersk Git - moira.git/blob - update/config.c
eliminate use of the `register' keyword: let the compiler decide
[moira.git] / update / config.c
1 /* $Header$
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  * (c) Copyright 1992 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 <stdio.h>
15 #include <stdlib.h>
16 #include <string.h>
17 #include <ctype.h>
18 #include <sys/types.h>
19 #include <sys/stat.h>
20 #include <sys/file.h>
21 #include <fcntl.h>
22 #include <moira.h>
23
24
25 #define CONFIG_FILE     "/etc/athena/moira.conf"
26
27 /* Variables currently supported:
28  * chroot directory     daemon will run chrooted to this directory
29  * user username        daemon will run with this user's uid
30  * port portname        daemon will listen on this port number
31  * nofork               server stays in foreground & logs to stdout
32  * auth krbname         this user is authorized to connect
33  * noclobber            will not overwrite existing files
34  * noexec               will not execute instructions received
35  */
36
37 static char *config_buf = NULL;
38 static char **config_keys, **config_values;
39
40
41 static init()
42 {
43   int fd, count = 0;
44   struct stat st;
45   char *p, *start;
46
47   /* Only execute once */
48   if (config_buf)
49     return MR_SUCCESS;
50
51   fd = open(CONFIG_FILE, O_RDONLY, 0);
52   if (fd < 0)
53     {
54       config_buf = "";
55       config_keys = malloc(sizeof(char *) * 2);
56       config_keys[0] = config_keys[1] = NULL;
57       return MR_SUCCESS;
58     }
59   if (fstat(fd, &st) < 0)
60     return MR_INTERNAL;
61   config_buf = malloc(st.st_size + 2);
62   if (!config_buf)
63     return MR_NO_MEM;
64   if (read(fd, config_buf, st.st_size) < st.st_size)
65     {
66       free(config_buf);
67       config_buf = NULL;
68       return MR_INTERNAL;
69     }
70   config_buf[st.st_size] = '\0';
71
72   for (p = config_buf; *p; p++)
73     {
74       if (*p == '\n')
75         count++;
76     }
77   count++;
78   config_keys = malloc(count * sizeof(char *));
79   config_values = malloc(count * sizeof(char *));
80   if (!config_keys || !config_values)
81     {
82       free(config_buf);
83       config_buf = NULL;
84       return MR_NO_MEM;
85     }
86   count = 0;
87   for (p = strtok(config_buf, "\n"); p; p = strtok(NULL, "\n"))
88     config_keys[count++] = p;
89   config_keys[count] = NULL;
90   for (count = 0; config_keys[count]; count++)
91     {
92       config_values[count] = "";
93       for (p = config_keys[count]; *p; p++)
94         {
95           if (isspace(*p))
96             {
97               *p++ = '\0';
98               while (*p && isspace(*p))
99                 p++;
100               config_values[count] = p;
101             }
102         }
103     }
104   return MR_SUCCESS;
105 }
106
107
108 /* Given a key, lookup the associated value.
109  * Returns "" on a key without a value, NULL on a non-existant key.
110  * If a key appears multiple times, successive calls will cycle through
111  * the possible values.
112  */
113
114 char *config_lookup(char *key)
115 {
116   static int i = 0;
117   int start;
118
119   if (init() != MR_SUCCESS)
120     return NULL;
121
122   start = i++;
123   if (!config_keys[i])
124     i = 0;
125   if (!config_keys[i])
126     return NULL;
127
128   do
129     {
130       if (!strcasecmp(key, config_keys[i]))
131         return config_values[i];
132       if (!config_keys[++i])
133         i = 0;
134     }
135   while (i != start);
136
137   if (!strcasecmp(key, config_keys[i]))
138     return config_values[i];
139
140   return NULL;
141 }
This page took 0.049535 seconds and 5 git commands to generate.