diff options
author | Sergey Poznyakoff <gray@gnu.org.ua> | 2015-12-31 13:59:18 +0200 |
---|---|---|
committer | Sergey Poznyakoff <gray@gnu.org.ua> | 2015-12-31 15:58:17 +0200 |
commit | 00e6c3c3ed06a258a02943fc49fa7c528025d747 (patch) | |
tree | 52530660be48fb5f4611bcc9393886bab8a70b72 /lib/netrc.c | |
parent | 7f204cc788de3e03a51087b1273deb5b59288cf2 (diff) | |
download | pies-00e6c3c3ed06a258a02943fc49fa7c528025d747.tar.gz pies-00e6c3c3ed06a258a02943fc49fa7c528025d747.tar.bz2 |
Command-line control interface.
* configure.ac (DEFAULT_CONTROL_URL): New subst variable.
* grecs: Upgrade.
* ident/pam.c (overwrite_and_free): Free ptr.
* lib/Makefile.am: Add new sources.
* src/addrfmt.c: Move to lib/addrfmt.c
* lib/grecsasrt.c: New file.
* lib/grecsasrt.h: New file.
* lib/mkfilename.c: New file.
* lib/netrc.c: New file.
* lib/pp.c: New file.
* lib/split3.c: New file.
* src/url.c: Move from lib/url.c
(pies_url_free_user, pies_url_free_passwd): New finctions.
* lib/libpies.h (strsplit3): New proto.
(pies_url_create, pies_url_destroy)
(pies_url_get_arg, pies_url_copy)
(pies_url_free_user, pies_url_free_passwd)
(netrc_scan)
(pp_add_option, pp_command_line, mkfilename)
(sockaddr_to_str, sockaddr_to_astr): New protos
* src/Makefile.am (bin_PROGRAMS): New program: piesctl
(pies_SOURCES): Remove addrfmt.c and url.c.
(noinst_HEADERS, BUILT_SOURCES): Add piesctl-cl.h
* src/cmdline.opt: Use pp_* function family to build
preprocessor command line.
* src/ctl.c (http_header_hash): Use case-insensitive hashing.
(ctlio_finalize_reply): Don't close connection after sending 401
response.
(input): Remove ws and wsflags. All uses changed.
(input_append): Use strsplit3 to parse the request line.
* src/pies.c: Use pp_* function family to build
preprocessor command line.
Move assert_, mkfilename and _cb+_url functions into libpies.
* src/pies.h (pies_sockaddr_storage): Move to libpies.h
* src/piesctl.c: New file.
* src/piesctl-cl.opt: New file.
Diffstat (limited to 'lib/netrc.c')
-rw-r--r-- | lib/netrc.c | 217 |
1 files changed, 217 insertions, 0 deletions
diff --git a/lib/netrc.c b/lib/netrc.c new file mode 100644 index 0000000..ed8053f --- /dev/null +++ b/lib/netrc.c @@ -0,0 +1,217 @@ +/* This file is part of GNU Pies + Copyright (C) 2015 Sergey Poznyakoff + + GNU Pies is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3, or (at your option) + any later version. + + GNU Pies is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with GNU Pies. If not, see <http://www.gnu.org/licenses/>. */ + +#ifdef HAVE_CONFIG_H +# include <config.h> +#endif +#include "libpies.h" +#include <unistd.h> +#include <pwd.h> +#include <grecs.h> +#include <wordsplit.h> +#include <errno.h> +#include <netdb.h> + +/* Compare two hostnames. Return 0 if they have the same address type, + address length *and* at least one of the addresses of A matches + B */ +static int +hostcmp (const char *a, const char *b) +{ + struct hostent *hp = gethostbyname (a); + char **addrlist; + char *dptr; + char **addr; + size_t i, count; + size_t entry_length; + int entry_type; + + if (!hp) + return 1; + + for (count = 1, addr = hp->h_addr_list; *addr; addr++) + count++; + addrlist = grecs_malloc (count * (sizeof *addrlist + hp->h_length) + - hp->h_length); + dptr = (char *) (addrlist + count); + for (i = 0; i < count - 1; i++) + { + memcpy (dptr, hp->h_addr_list[i], hp->h_length); + addrlist[i] = dptr; + dptr += hp->h_length; + } + addrlist[i] = NULL; + entry_length = hp->h_length; + entry_type = hp->h_addrtype; + + hp = gethostbyname (b); + if (!hp || entry_length != hp->h_length || entry_type != hp->h_addrtype) + { + grecs_free (addrlist); + return 1; + } + + for (addr = addrlist; *addr; addr++) + { + char **p; + + for (p = hp->h_addr_list; *p; p++) + { + if (memcmp (*addr, *p, entry_length) == 0) + { + grecs_free (addrlist); + return 0; + } + } + } + grecs_free (addrlist); + return 1; +} + +static int +match_url (size_t argc, char **argv, struct pies_url *url) +{ + if (hostcmp (argv[1], url->host ? url->host : "localhost") == 0) + { + if (argc >= 4 && strcmp (argv[2], "port") == 0) + { + unsigned long n = strtoul (argv[3], NULL, 10); + if (n == url->port) + return 1; + } + else + return 1; + } + return 0; +} + +static void +parse_args (struct grecs_locus *loc, char **argv, + char **username, char **password) +{ + if (*username) + { + grecs_free (*username); + *username = NULL; + } + if (*password) + { + grecs_free (*password); + *password = NULL; + } + + while (*argv) + { + if (!argv[1]) + { + grecs_error (loc, 0, _("incomplete sentence")); + break; + } + if (strcmp (*argv, "login") == 0) + *username = grecs_strdup (argv[1]); + else if (strcmp (*argv, "password") == 0) + *password = grecs_strdup (argv[1]); + argv += 2; + } +} + +/* Parse traditional .netrc file. Set up auth_args fields in accordance with + it. */ +void +netrc_scan_file (FILE *fp, struct grecs_locus *loc, struct pies_url *url) +{ + char *buf = NULL; + size_t n = 0; + struct wordsplit ws; + int wsflags = WRDSF_NOVAR | WRDSF_NOCMD | WRDSF_QUOTE | WRDSF_SQUEEZE_DELIMS; + char *username = NULL; + char *password = NULL; + + while (grecs_getline (&buf, &n, fp) > 0) + { + loc->beg.line++; + + if (wordsplit (buf, &ws, wsflags)) + { + grecs_error (loc, 0, "wordsplit: %s", wordsplit_strerror (&ws)); + continue; + } + wsflags |= WRDSF_REUSE; + + if (ws.ws_wordc == 0) + continue; + + if (strcmp (ws.ws_wordv[0], "machine") == 0) + { + if (match_url (ws.ws_wordc, ws.ws_wordv, url)) + { + parse_args (loc, ws.ws_wordv + 2, &username, &password); + break; + } + } + else if (strcmp (ws.ws_wordv[0], "default") == 0) + parse_args (loc, ws.ws_wordv + 1, &username, &password); + else + grecs_error (loc, 0, _("ignoring unrecognized line\n")); + } + grecs_free (buf); + + if (wsflags & WRDSF_REUSE) + wordsplit_free (&ws); + + url->user = username; + url->passwd = password; +} + +void +netrc_scan (struct pies_url *url) +{ + FILE *fp; + struct grecs_locus loc; + char *filename; + char *homedir; + + if (url->user) + return; + + homedir = getenv ("HOME"); + if (!homedir) + { + struct passwd *pwd = getpwuid (getuid ()); + if (!pwd) + return; + homedir = pwd->pw_dir; + } + + filename = mkfilename (homedir, ".netrc", NULL); + fp = fopen (filename, "r"); + if (!fp) + { + if (errno != ENOENT) + grecs_error (NULL, 0, + _("cannot open configuration file %s: %s"), + filename, strerror (errno)); + free (filename); + return; + } + + loc.beg.file = loc.end.file = (char*) filename; + loc.beg.col = loc.end.col = 0; + loc.beg.line = 0; + netrc_scan_file (fp, &loc, url); + fclose (fp); + free (filename); +} |