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