dma(8): Upgrade to v0.7.
[dragonfly.git] / libexec / dma / conf.c
CommitLineData
f67beddd
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.
f67beddd
MS
34 */
35
36#include <err.h>
e2c88018 37#include <errno.h>
f67beddd
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
ca259d14
SS
46#define DP ": \t"
47#define EQS " \t"
f67beddd 48
f67beddd
MS
49
50/*
51 * Remove trailing \n's
52 */
53void
54trim_line(char *line)
55{
56 size_t linelen;
57 char *p;
58
f67beddd
MS
59 if ((p = strchr(line, '\n')))
60 *p = (char)0;
61
62 /* Escape leading dot in every case */
63 linelen = strlen(line);
64 if (line[0] == '.') {
65 if ((linelen + 2) > 1000) {
66 syslog(LOG_CRIT, "Cannot escape leading dot. Buffer overflow");
67 exit(1);
68 }
69 memmove((line + 1), line, (linelen + 1));
70 line[0] = '.';
71 }
72}
73
f67beddd 74static void
ca259d14 75chomp(char *str)
f67beddd 76{
ca259d14 77 size_t len = strlen(str);
f67beddd 78
ca259d14
SS
79 if (len == 0)
80 return;
81 if (str[len - 1] == '\n')
82 str[len - 1] = 0;
f67beddd
MS
83}
84
85/*
f67beddd 86 * Read the SMTP authentication config file
ca259d14
SS
87 *
88 * file format is:
89 * user|host:password
90 *
91 * A line starting with # is treated as comment and ignored.
f67beddd 92 */
ca259d14 93void
f67beddd
MS
94parse_authfile(const char *path)
95{
ca259d14
SS
96 char line[2048];
97 struct authuser *au;
f67beddd 98 FILE *a;
f67beddd 99 char *data;
ca259d14 100 int lineno = 0;
f67beddd
MS
101
102 a = fopen(path, "r");
ca259d14
SS
103 if (a == NULL) {
104 errlog(1, "can not open auth file `%s'", path);
105 /* NOTREACHED */
106 }
f67beddd
MS
107
108 while (!feof(a)) {
2922fd2b
SS
109 if (fgets(line, sizeof(line), a) == NULL)
110 break;
ca259d14
SS
111 lineno++;
112
113 chomp(line);
114
f67beddd 115 /* We hit a comment */
ca259d14
SS
116 if (*line == '#')
117 continue;
118 /* Ignore empty lines */
119 if (*line == 0)
120 continue;
121
122 au = calloc(1, sizeof(*au));
123 if (au == NULL)
124 errlog(1, NULL);
125
126 data = strdup(line);
127 au->login = strsep(&data, "|");
128 au->host = strsep(&data, DP);
129 au->password = data;
130
131 if (au->login == NULL ||
132 au->host == NULL ||
133 au->password == NULL) {
134 errlogx(1, "syntax error in authfile %s:%d",
135 path, lineno);
136 /* NOTREACHED */
f67beddd 137 }
ca259d14
SS
138
139 SLIST_INSERT_HEAD(&authusers, au, next);
f67beddd
MS
140 }
141
142 fclose(a);
f67beddd
MS
143}
144
145/*
146 * XXX TODO
f67beddd
MS
147 * Check for bad things[TM]
148 */
ca259d14 149void
f4e61a9f 150parse_conf(const char *config_path)
f67beddd
MS
151{
152 char *word;
153 char *data;
154 FILE *conf;
155 char line[2048];
ca259d14 156 int lineno = 0;
f67beddd
MS
157
158 conf = fopen(config_path, "r");
ca259d14
SS
159 if (conf == NULL) {
160 /* Don't treat a non-existing config file as error */
161 if (errno == ENOENT)
162 return;
163 errlog(1, "can not open config `%s'", config_path);
164 /* NOTREACHED */
165 }
f67beddd
MS
166
167 while (!feof(conf)) {
2922fd2b
SS
168 if (fgets(line, sizeof(line), conf) == NULL)
169 break;
ca259d14
SS
170 lineno++;
171
172 chomp(line);
173
f67beddd
MS
174 /* We hit a comment */
175 if (strchr(line, '#'))
176 *strchr(line, '#') = 0;
ca259d14
SS
177
178 data = line;
179 word = strsep(&data, EQS);
180
181 /* Ignore empty lines */
182 if (word == NULL || *word == 0)
183 continue;
184
185 if (data != NULL && *data != 0)
186 data = strdup(data);
187 else
188 data = NULL;
189
190 if (strcmp(word, "SMARTHOST") == 0 && data != NULL)
191 config.smarthost = data;
192 else if (strcmp(word, "PORT") == 0 && data != NULL)
193 config.port = atoi(data);
194 else if (strcmp(word, "ALIASES") == 0 && data != NULL)
195 config.aliases = data;
196 else if (strcmp(word, "SPOOLDIR") == 0 && data != NULL)
197 config.spooldir = data;
ca259d14
SS
198 else if (strcmp(word, "AUTHPATH") == 0 && data != NULL)
199 config.authpath= data;
200 else if (strcmp(word, "CERTFILE") == 0 && data != NULL)
201 config.certfile = data;
202 else if (strcmp(word, "MAILNAME") == 0 && data != NULL)
203 config.mailname = data;
c8b07ee5
SW
204 else if (strcmp(word, "MASQUERADE") == 0 && data != NULL) {
205 char *user = NULL, *host = NULL;
206 if (strrchr(data, '@')) {
207 host = strrchr(data, '@');
208 *host = 0;
209 host++;
210 user = data;
211 } else {
212 host = data;
213 }
214 if (host && *host == 0)
215 host = NULL;
216 if (user && *user == 0)
217 user = NULL;
218 config.masquerade_host = host;
219 config.masquerade_user = user;
220 } else if (strcmp(word, "STARTTLS") == 0 && data == NULL)
ca259d14 221 config.features |= STARTTLS;
c8b07ee5
SW
222 else if (strcmp(word, "OPPORTUNISTIC_TLS") == 0 && data == NULL)
223 config.features |= TLS_OPP;
ca259d14
SS
224 else if (strcmp(word, "SECURETRANSFER") == 0 && data == NULL)
225 config.features |= SECURETRANS;
226 else if (strcmp(word, "DEFER") == 0 && data == NULL)
227 config.features |= DEFER;
228 else if (strcmp(word, "INSECURE") == 0 && data == NULL)
229 config.features |= INSECURE;
230 else if (strcmp(word, "FULLBOUNCE") == 0 && data == NULL)
231 config.features |= FULLBOUNCE;
232 else {
233 errlogx(1, "syntax error in %s:%d", config_path, lineno);
234 /* NOTREACHED */
f67beddd
MS
235 }
236 }
237
238 fclose(conf);
f67beddd 239}