]> git.ipfire.org Git - people/ms/dma.git/blame - conf.c
make dma compile again on DragonFly
[people/ms/dma.git] / conf.c
CommitLineData
86e4d161
MS
1/*
2 * Copyright (c) 2008 The DragonFly Project. All rights reserved.
3 *
4 * This code is derived from software contributed to The DragonFly Project
5 * by Matthias Schmidt <matthias@dragonflybsd.org>, University of Marburg,
6 * Germany.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 *
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in
16 * the documentation and/or other materials provided with the
17 * distribution.
18 * 3. Neither the name of The DragonFly Project nor the names of its
19 * contributors may be used to endorse or promote products derived
20 * from this software without specific, prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
28 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
30 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
31 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
32 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
86e4d161
MS
34 */
35
36#include <err.h>
53885e5a 37#include <errno.h>
86e4d161
MS
38#include <stdio.h>
39#include <stdlib.h>
40#include <string.h>
41#include <syslog.h>
42#include <stdarg.h>
43
44#include "dma.h"
45
a0c4afa6
SS
46#define DP ": \t"
47#define EQS " \t"
86e4d161 48
86e4d161
MS
49
50/*
51 * Remove trailing \n's
52 */
53void
54trim_line(char *line)
55{
56 size_t linelen;
57 char *p;
58
59 p = line;
60
61 if ((p = strchr(line, '\n')))
62 *p = (char)0;
63
64 /* Escape leading dot in every case */
65 linelen = strlen(line);
66 if (line[0] == '.') {
67 if ((linelen + 2) > 1000) {
68 syslog(LOG_CRIT, "Cannot escape leading dot. Buffer overflow");
69 exit(1);
70 }
71 memmove((line + 1), line, (linelen + 1));
72 line[0] = '.';
73 }
74}
75
86e4d161 76static void
a0c4afa6 77chomp(char *str)
86e4d161 78{
a0c4afa6 79 size_t len = strlen(str);
86e4d161 80
a0c4afa6
SS
81 if (len == 0)
82 return;
83 if (str[len - 1] == '\n')
84 str[len - 1] = 0;
86e4d161
MS
85}
86
86e4d161
MS
87/*
88 * Read the SMTP authentication config file
a0c4afa6
SS
89 *
90 * file format is:
91 * user|host:password
92 *
93 * A line starting with # is treated as comment and ignored.
86e4d161 94 */
a0c4afa6 95void
86e4d161
MS
96parse_authfile(const char *path)
97{
a0c4afa6
SS
98 char line[2048];
99 struct authuser *au;
86e4d161 100 FILE *a;
86e4d161 101 char *data;
a0c4afa6 102 int lineno = 0;
86e4d161
MS
103
104 a = fopen(path, "r");
a0c4afa6
SS
105 if (a == NULL) {
106 errlog(1, "can not open auth file `%s'", path);
107 /* NOTREACHED */
108 }
86e4d161
MS
109
110 while (!feof(a)) {
5b84c25b
SS
111 if (fgets(line, sizeof(line), a) == NULL)
112 break;
a0c4afa6
SS
113 lineno++;
114
115 chomp(line);
116
86e4d161 117 /* We hit a comment */
a0c4afa6
SS
118 if (*line == '#')
119 continue;
120 /* Ignore empty lines */
121 if (*line == 0)
122 continue;
123
124 au = calloc(1, sizeof(*au));
125 if (au == NULL)
126 errlog(1, NULL);
127
128 data = strdup(line);
129 au->login = strsep(&data, "|");
130 au->host = strsep(&data, DP);
131 au->password = data;
132
133 if (au->login == NULL ||
134 au->host == NULL ||
135 au->password == NULL) {
136 errlogx(1, "syntax error in authfile %s:%d",
137 path, lineno);
138 /* NOTREACHED */
86e4d161 139 }
a0c4afa6
SS
140
141 SLIST_INSERT_HEAD(&authusers, au, next);
86e4d161
MS
142 }
143
144 fclose(a);
86e4d161
MS
145}
146
147/*
148 * XXX TODO
86e4d161
MS
149 * Check for bad things[TM]
150 */
a0c4afa6 151void
fd1de51a 152parse_conf(const char *config_path)
86e4d161
MS
153{
154 char *word;
155 char *data;
156 FILE *conf;
157 char line[2048];
a0c4afa6 158 int lineno = 0;
86e4d161
MS
159
160 conf = fopen(config_path, "r");
a0c4afa6
SS
161 if (conf == NULL) {
162 /* Don't treat a non-existing config file as error */
163 if (errno == ENOENT)
164 return;
165 errlog(1, "can not open config `%s'", config_path);
166 /* NOTREACHED */
167 }
86e4d161
MS
168
169 while (!feof(conf)) {
5b84c25b
SS
170 if (fgets(line, sizeof(line), conf) == NULL)
171 break;
a0c4afa6
SS
172 lineno++;
173
174 chomp(line);
175
86e4d161
MS
176 /* We hit a comment */
177 if (strchr(line, '#'))
178 *strchr(line, '#') = 0;
a0c4afa6
SS
179
180 data = line;
181 word = strsep(&data, EQS);
182
183 /* Ignore empty lines */
184 if (word == NULL || *word == 0)
185 continue;
186
187 if (data != NULL && *data != 0)
188 data = strdup(data);
189 else
190 data = NULL;
191
192 if (strcmp(word, "SMARTHOST") == 0 && data != NULL)
193 config.smarthost = data;
194 else if (strcmp(word, "PORT") == 0 && data != NULL)
195 config.port = atoi(data);
196 else if (strcmp(word, "ALIASES") == 0 && data != NULL)
197 config.aliases = data;
198 else if (strcmp(word, "SPOOLDIR") == 0 && data != NULL)
199 config.spooldir = data;
a0c4afa6
SS
200 else if (strcmp(word, "AUTHPATH") == 0 && data != NULL)
201 config.authpath= data;
202 else if (strcmp(word, "CERTFILE") == 0 && data != NULL)
203 config.certfile = data;
204 else if (strcmp(word, "MAILNAME") == 0 && data != NULL)
205 config.mailname = data;
a0c4afa6
SS
206 else if (strcmp(word, "STARTTLS") == 0 && data == NULL)
207 config.features |= STARTTLS;
4dea15f5
PP
208 else if (strcmp(word, "OPPORTUNISTIC_TLS") == 0 && data == NULL)
209 config.features |= TLS_OPP;
a0c4afa6
SS
210 else if (strcmp(word, "SECURETRANSFER") == 0 && data == NULL)
211 config.features |= SECURETRANS;
212 else if (strcmp(word, "DEFER") == 0 && data == NULL)
213 config.features |= DEFER;
214 else if (strcmp(word, "INSECURE") == 0 && data == NULL)
215 config.features |= INSECURE;
216 else if (strcmp(word, "FULLBOUNCE") == 0 && data == NULL)
217 config.features |= FULLBOUNCE;
218 else {
219 errlogx(1, "syntax error in %s:%d", config_path, lineno);
220 /* NOTREACHED */
86e4d161
MS
221 }
222 }
223
224 fclose(conf);
86e4d161 225}