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