/* $Id$ */ /* * Copyright (c) 2005 Dimitri Sokolyuk * * 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 #include #include #include #include "request.h" #include "parse.h" #include "base64.h" #include "ifaddr.h" #define SYS_DYN 0x1 #define SYS_STAT 0x2 #define SYS_CUST 0x4 #define SYS_ALL SYS_DYN|SYS_STAT|SYS_CUST 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; }