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