From: Pierre Habouzit Date: Mon, 27 Aug 2007 23:20:08 +0000 (+0200) Subject: More simplifications, prepare the path to be a real daemon. X-Git-Url: http://git.madism.org/?a=commitdiff_plain;h=47c0c26c0664324a9a4836886e224f5ea870d49a;p=apps%2Fpfixtools.git More simplifications, prepare the path to be a real daemon. Signed-off-by: Pierre Habouzit --- diff --git a/srsd.c b/srsd.c index 8df7f71..7d25046 100644 --- a/srsd.c +++ b/srsd.c @@ -41,109 +41,44 @@ #include "common.h" #include "mem.h" -#define MAX_SIZE 0x10000 +static srs_t *srs = NULL; -static char **read_sfile(char *sfile) +static int read_sfile(const char *sfile) { - int fd = -1; - int pos = 0; - int nb = 0; - int len = 0; - - char *buf = NULL; - char **res = NULL; - - struct stat stat_buf; - - if (stat(sfile, &stat_buf)) { - perror("stat"); - exit(1); - } - - if (stat_buf.st_size > MAX_SIZE) { - fprintf(stderr, "the secret file is too big\n"); - exit(1); - } - - buf = p_new(char, stat_buf.st_size + 1); - - if ((fd = open(sfile, O_RDONLY)) < 0) { - perror("open"); - exit (1); - } - - for (;;) { - if ((nb = read(fd, &(buf[pos]), stat_buf.st_size)) < 0) { - if (errno == EINTR) - continue; - perror("read"); - exit(1); - } - pos += nb; - if (nb == 0 || pos == stat_buf.st_size) { - close(fd); - fd = -1; - break; - } - } - - for (nb = pos = 0; pos < stat_buf.st_size ; pos++) { - if (buf[pos] == '\n') { - nb++; - buf[pos] = 0; - } - } - - res = p_new(char*, nb + 2); - - nb = pos = 0; - while (pos < stat_buf.st_size) { - len = strlen(&(buf[pos])); - if (len) { - res[nb++] = &(buf[pos]); - } - pos += len+1; - } - - return res; -} - - -static char *encode(char *secret, char *sender, char *alias) -{ - int err = 0; - char *res = NULL; - srs_t *srs = srs_new(); - - srs_add_secret(srs, secret); - err = srs_forward_alloc(srs, &res, sender, alias); - - if (res == NULL) { - fprintf(stderr, "%s\n", srs_strerror(err)); - exit (1); + FILE *f; + char buf[BUFSIZ]; + srs_t *newsrs; + + f = fopen(sfile, "r"); + if (!f) { + UNIXERR("fopen"); + return -1; } - return res; -} + newsrs = srs_new(); -static char *decode(char **secrets, char *sender) -{ - int err = 0; - char *res = NULL; - srs_t *srs = srs_new(); + while (fgets(buf, sizeof(buf), f)) { + int n = strlen(buf); - while (*secrets) { - srs_add_secret(srs, *secrets++); + if (buf[n - 1] != '\n') + goto error; + while (n > 0 && isspace((unsigned char)buf[n - 1])) + buf[--n] = '\0'; + if (n > 0) + srs_add_secret(newsrs, buf); } + fclose(f); - err = srs_reverse_alloc(srs, &res, sender); - - if (res == NULL) { - fprintf(stderr, "%s\n", srs_strerror(err)); - exit(1); + if (srs) { + srs_free(srs); } + srs = newsrs; + return 0; - return res; + error: + fclose(f); + srs_free(newsrs); + return -1; } static void help(void) @@ -159,19 +94,19 @@ static void help(void) "\n" " -e sender the sender address we want to encode/decode\n" ); - exit (1); + exit(1); } int main(int argc, char *argv[]) { - char *buf = NULL; + char *res = NULL; char *domain = NULL; char *sender = NULL; char *sfile = NULL; int opt = 0; bool rev = false; - char **secr = NULL; + int err = 0; while ((opt = getopt(argc, argv, "d:e:f:r")) != -1) { switch (opt) { @@ -183,23 +118,22 @@ int main(int argc, char *argv[]) } if (!sender || !sfile || !(rev||domain)) { - help (); + help(); } - if (sfile) { - secr = read_sfile(sfile); - if (!secr || !secr[0]) { - fprintf(stderr, "No secret given, and secret file is empty\n"); - exit (1); - } - } + if (read_sfile(sfile) < 0) + return -1; if (rev) { - buf = decode(secr, sender); + err = srs_reverse_alloc(srs, &res, sender); } else { - buf = encode(secr[0], sender, domain); + err = srs_forward_alloc(srs, &res, sender, domain); } - puts(buf); + if (res == NULL) { + fprintf(stderr, "%s\n", srs_strerror(err)); + return -1; + } + puts(res); return 0; }