]>
Commit | Line | Data |
---|---|---|
7ac48069 | 1 | /* $Id$ |
d8e20246 | 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 | * | |
7ac48069 | 8 | * Copyright (C) 1992-1998 by the Massachusetts Institute of Technology. |
d8e20246 | 9 | * For copying and distribution information, please see the file |
10 | * <mit-copyright.h>. | |
11 | */ | |
12 | ||
13 | #include <mit-copyright.h> | |
7ac48069 | 14 | #include <moira.h> |
15 | #include "update_server.h" | |
16 | ||
17 | #include <sys/stat.h> | |
18 | ||
19 | #include <ctype.h> | |
85330553 | 20 | #include <errno.h> |
7ac48069 | 21 | #include <fcntl.h> |
d8e20246 | 22 | #include <stdio.h> |
5eaef520 | 23 | #include <stdlib.h> |
c6a18c8f | 24 | #include <string.h> |
7ac48069 | 25 | #include <unistd.h> |
d8e20246 | 26 | |
7ac48069 | 27 | RCSID("$Header$"); |
d8e20246 | 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 | ||
7ac48069 | 44 | static int init(void) |
d8e20246 | 45 | { |
5eaef520 | 46 | int fd, count = 0; |
47 | struct stat st; | |
85330553 | 48 | char *p, *start; |
5eaef520 | 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); | |
85330553 | 59 | if (!config_keys) |
60 | return ENOMEM; | |
5eaef520 | 61 | config_keys[0] = config_keys[1] = NULL; |
62 | return MR_SUCCESS; | |
d8e20246 | 63 | } |
85330553 | 64 | |
5eaef520 | 65 | if (fstat(fd, &st) < 0) |
66 | return MR_INTERNAL; | |
85330553 | 67 | |
5eaef520 | 68 | config_buf = malloc(st.st_size + 2); |
69 | if (!config_buf) | |
85330553 | 70 | return ENOMEM; |
71 | ||
5eaef520 | 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; | |
d8e20246 | 77 | } |
5eaef520 | 78 | config_buf[st.st_size] = '\0'; |
79 | ||
80 | for (p = config_buf; *p; p++) | |
81 | { | |
82 | if (*p == '\n') | |
83 | count++; | |
d8e20246 | 84 | } |
5eaef520 | 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); | |
85330553 | 91 | free(config_keys); |
92 | free(config_values); | |
5eaef520 | 93 | config_buf = NULL; |
85330553 | 94 | return ENOMEM; |
d8e20246 | 95 | } |
85330553 | 96 | |
5eaef520 | 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; | |
85330553 | 101 | |
5eaef520 | 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 | { | |
d8e20246 | 109 | *p++ = '\0'; |
5eaef520 | 110 | while (*p && isspace(*p)) |
111 | p++; | |
d8e20246 | 112 | config_values[count] = p; |
5eaef520 | 113 | } |
114 | } | |
d8e20246 | 115 | } |
5eaef520 | 116 | return MR_SUCCESS; |
d8e20246 | 117 | } |
118 | ||
119 | ||
5eaef520 | 120 | /* Given a key, lookup the associated value. |
d8e20246 | 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 | ||
5eaef520 | 126 | char *config_lookup(char *key) |
d8e20246 | 127 | { |
5eaef520 | 128 | static int i = 0; |
129 | int start; | |
d8e20246 | 130 | |
5eaef520 | 131 | if (init() != MR_SUCCESS) |
132 | return NULL; | |
d8e20246 | 133 | |
5eaef520 | 134 | start = i++; |
135 | if (!config_keys[i]) | |
136 | i = 0; | |
137 | if (!config_keys[i]) | |
138 | return NULL; | |
d8e20246 | 139 | |
5eaef520 | 140 | do |
141 | { | |
d8e20246 | 142 | if (!strcasecmp(key, config_keys[i])) |
5eaef520 | 143 | return config_values[i]; |
144 | if (!config_keys[++i]) | |
d8e20246 | 145 | i = 0; |
5eaef520 | 146 | } |
147 | while (i != start); | |
d8e20246 | 148 | |
5eaef520 | 149 | if (!strcasecmp(key, config_keys[i])) |
150 | return config_values[i]; | |
d8e20246 | 151 | |
5eaef520 | 152 | return NULL; |
d8e20246 | 153 | } |