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