]> andersk Git - moira.git/blame - update/config.c
Code style cleanup. (No functional changes)
[moira.git] / update / config.c
CommitLineData
d8e20246 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>
5eaef520 15#include <stdlib.h>
c6a18c8f 16#include <string.h>
d8e20246 17#include <ctype.h>
18#include <sys/types.h>
19#include <sys/stat.h>
20#include <sys/file.h>
5f5f277b 21#include <fcntl.h>
d8e20246 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
37static char *config_buf = NULL;
38static char **config_keys, **config_values;
39
40
41static init()
42{
5eaef520 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;
d8e20246 58 }
5eaef520 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;
d8e20246 69 }
5eaef520 70 config_buf[st.st_size] = '\0';
71
72 for (p = config_buf; *p; p++)
73 {
74 if (*p == '\n')
75 count++;
d8e20246 76 }
5eaef520 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;
d8e20246 85 }
5eaef520 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 {
d8e20246 97 *p++ = '\0';
5eaef520 98 while (*p && isspace(*p))
99 p++;
d8e20246 100 config_values[count] = p;
5eaef520 101 }
102 }
d8e20246 103 }
5eaef520 104 return MR_SUCCESS;
d8e20246 105}
106
107
5eaef520 108/* Given a key, lookup the associated value.
d8e20246 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
5eaef520 114char *config_lookup(char *key)
d8e20246 115{
5eaef520 116 static int i = 0;
117 int start;
d8e20246 118
5eaef520 119 if (init() != MR_SUCCESS)
120 return NULL;
d8e20246 121
5eaef520 122 start = i++;
123 if (!config_keys[i])
124 i = 0;
125 if (!config_keys[i])
126 return NULL;
d8e20246 127
5eaef520 128 do
129 {
d8e20246 130 if (!strcasecmp(key, config_keys[i]))
5eaef520 131 return config_values[i];
132 if (!config_keys[++i])
d8e20246 133 i = 0;
5eaef520 134 }
135 while (i != start);
d8e20246 136
5eaef520 137 if (!strcasecmp(key, config_keys[i]))
138 return config_values[i];
d8e20246 139
5eaef520 140 return NULL;
d8e20246 141}
This page took 0.096597 seconds and 5 git commands to generate.