summaryrefslogtreecommitdiff
path: root/parse.c
blob: c9d997ae3a28972a969c53d9154af89647f3151f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
/* $Id$ */
/*
 * Copyright (c) 2005 Dimitri Sokolyuk <demon@vhost.dyndns.org>
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#include <err.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "request.h"
#include "parse.h"
#include "base64.h"
#include "ifaddr.h"

extern int debug;

#if DEBUG
static void
print_config(struct dd_request *req)
{
	printf("auth: %s\n", req->auth);
	if (req->system != NULL)
		printf("syst: %s\n", req->system);
	printf("host: %s\n", req->hostname);
	if (req->myip != NULL)
		printf("myip: %s\n", req->myip);
	if (req->wildcard != NULL)
		printf("wild: %s\n", req->wildcard);
	if (req->mx != NULL) {
		printf("mx  : %s\n", req->mx);
		if (req->backmx != NULL)
			printf("back: %s\n", req->backmx);
	}
	if (req->offline != NULL)
		printf("offl: %s\n", req->offline);
}
#endif

static int
getconf(char **db_array, char *name, struct dd_request *req)
{
	char *buf, *user, *pass, *if_name, *if_addr;
	char auth[240];

	int f_sys = SYS_ALL;

	memset(req, '\0', sizeof(struct dd_request));

	if (cgetent(&buf, db_array, name) != 0)
		errx(1, "can't find \"%s\" in dddup config", name);

	if ((req->name =strdup(name)) == NULL)
		errx(1, "malloc");

	/* get type of service -- optional
	 * if no one is specified dyndns.org assumes `dynamic'
	 */
	if (cgetcap(buf, "dynamic", ':') != NULL) {
		req->system = "dyndns";
		f_sys = SYS_DYN;
	}
	
#define DSCERR "%s: either \"dynamic\" or \"static\" or \"custom\" " \
		"should be specified"

	if (cgetcap(buf, "static", ':') != NULL) {
		if (req->system != NULL)
			errx(1, DSCERR, name);
		req->system = "statdns";
		f_sys = SYS_STAT;
	}
	
	if (cgetcap(buf, "custom", ':') != NULL) {
		if (req->system != NULL)
			errx(1, DSCERR, name);
		req->system = "custom";
		f_sys = SYS_CUST;
	}

	/* get username -- required */
	switch (cgetstr(buf, "username", &user)) {
	case -1:
		errx(1, "%s: no username", name);
	case -2:
		errx(1, "malloc");
	}

	/* get password -- required */
	switch (cgetstr(buf, "password", &pass)) {
	case -1:
		errx(1, "%s: no password", name);
	case -2:
		errx(1, "malloc");
	}

	/* encrypt and set "req->auth" */
	snprintf(auth, sizeof(auth), "%s:%s", user, pass);
	memset(pass, '\0', strlen(pass));
	base64_encode(auth, strlen(auth), &req->auth);
	memset(&auth, '\0', sizeof(auth));

	/* interface -- determine IP and set "req->myip" */
	switch (cgetstr(buf, "interface", &if_name)) {
	case -1:
		if_name = NULL;
		break;
	case -2:
		errx(1, "malloc");
	}

	switch (cgetstr(buf, "myip", &req->myip)) {
	case -1: if_addr = NULL;
		 break;
	case -2:
		 errx(1, "malloc");
	}

	if (if_name != NULL && req->myip != NULL)
		errx(1, "%s: ither \"interface\" or \"myip\" should be specified", name);

	if (if_name != NULL)
		if_ntoa(if_name, &req->myip);

	switch (cgetstr(buf, "hostname", &req->hostname)) {
	case -1:
		errx(1, "%s: no hostname", name);
	case -2:
		errx(1, "malloc");
	}

	/* following options are all optional */
	if (f_sys & (SYS_DYN|SYS_STAT)) {
		if (cgetcap(buf, "wildcard", ':') != NULL)
			req->wildcard = "ON";
		else
			req->wildcard = "OFF";
	}

	if (f_sys & (SYS_DYN|SYS_STAT)) {
		switch (cgetstr(buf, "mx", &req->mx)) {
		case -1:
			req->mx = NULL;
			break;
		case -2:
			errx(1, "malloc");
		}
	}

	if (f_sys & (SYS_DYN|SYS_STAT) && req->mx != NULL) {
		if (cgetcap(buf, "backmx", ':') != NULL)
			req->backmx = "YES";
		else
			req->backmx = "NO";
	}

	if (f_sys & (SYS_DYN|SYS_CUST)) {
		if (cgetcap(buf, "offline", ':') != NULL)
			req->offline = "YES";
		else
			req->offline = "NO";
	}

	return 0;
}

int
parse_config(char *file, struct dd_request **req)
{

	char **db_array, *buf, *name;
	int count = 0;

	db_array = malloc(sizeof(char *));

	if (file == NULL)
		file = _PATH_DDDUP;
	*db_array = file;

	if (cgetent(&buf, db_array, "all") != 0)
		errx(1, "can't find \"all\" in dddup config");

	name = strsep(&buf, ": \t"); /* skip "all" at start */
	while ((name = strsep(&buf, ": \t")) != NULL) {
		if (*name) {
			req[count] = malloc(sizeof(struct dd_request));
			getconf(db_array, name, req[count++]);
		}
	}

	free(db_array);

#if DEBUG
	if (debug) {
		int i;
		for (i = 0; i < count; i++) {
			printf("===> No. %d\n", i);
			print_config(req[i]);
		}
	}
#endif

	return count;
}

int
free_config(int reqc, struct dd_request **reqv)
{
	while (reqc > 0)
		free(reqv[--reqc]);

	return 0;
}