summaryrefslogtreecommitdiff
path: root/parse.c
diff options
context:
space:
mode:
Diffstat (limited to 'parse.c')
-rw-r--r--parse.c219
1 files changed, 219 insertions, 0 deletions
diff --git a/parse.c b/parse.c
new file mode 100644
index 0000000..8f6e687
--- /dev/null
+++ b/parse.c
@@ -0,0 +1,219 @@
+/* $Id$ */
+/*
+ * Copyright (c) 2004 demon <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);
+
+ /* get type of service -- optional */
+ if (cgetcap(buf, "dyndns", ':') != NULL) {
+ req->system = "dyndns";
+ f_sys = SYS_DYN;
+ }
+
+#define DSCERR "either \"dyndns\" or \"statdns\" or \"custdns\" " \
+ "should be specifed"
+
+ if (cgetcap(buf, "statdns", ':') != NULL) {
+ if (req->system != NULL)
+ errx(1, DSCERR);
+ req->system = "statdns";
+ f_sys = SYS_STAT;
+ }
+
+ if (cgetcap(buf, "custdns", ':') != NULL) {
+ if (req->system != NULL)
+ errx(1, DSCERR);
+ req->system = "custdns";
+ f_sys = SYS_CUST;
+ }
+
+ /* get username -- required */
+ switch (cgetstr(buf, "username", &user)) {
+ case -1:
+ errx(1, "No username");
+ case -2:
+ errx(1, "malloc");
+ }
+
+ /* get password -- required */
+ switch (cgetstr(buf, "password", &pass)) {
+ case -1:
+ errx(1, "No password");
+ 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 -> 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, "either \"interface\" or \"myip\" should be specifed");
+
+ if (if_name != NULL)
+ if_ntoa(if_name, &req->myip);
+
+ switch (cgetstr(buf, "hostname", &req->hostname)) {
+ case -1:
+ errx(1, "No hostname");
+ case -2:
+ errx(1, "malloc");
+ }
+
+ 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]);
+ count++;
+ }
+ }
+
+ free(db_array);
+
+#if DEBUG
+ if (debug) {
+ 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)
+{
+ int i;
+
+ for (i = 0; i < reqc; i++)
+ free(reqv[i]);
+ return 0;
+}