1*421949a3Ssevan /* $NetBSD: scaffold.c,v 1.12 2018/01/23 21:06:26 sevan Exp $ */
2f8dba09cSchristos
384b4e9c0Scjs /*
484b4e9c0Scjs * Routines for testing only. Not really industrial strength.
584b4e9c0Scjs *
684b4e9c0Scjs * Author: Wietse Venema, Eindhoven University of Technology, The Netherlands.
784b4e9c0Scjs */
884b4e9c0Scjs
9f8dba09cSchristos #include <sys/cdefs.h>
1084b4e9c0Scjs #ifndef lint
11f8dba09cSchristos #if 0
125874d1fcSitojun static char sccs_id[] = "@(#) scaffold.c 1.6 97/03/21 19:27:24";
13f8dba09cSchristos #else
14*421949a3Ssevan __RCSID("$NetBSD: scaffold.c,v 1.12 2018/01/23 21:06:26 sevan Exp $");
15f8dba09cSchristos #endif
1684b4e9c0Scjs #endif
1784b4e9c0Scjs
1884b4e9c0Scjs /* System libraries. */
1984b4e9c0Scjs
2084b4e9c0Scjs #include <sys/types.h>
2184b4e9c0Scjs #include <sys/stat.h>
2284b4e9c0Scjs #include <sys/socket.h>
2384b4e9c0Scjs #include <netinet/in.h>
2484b4e9c0Scjs #include <arpa/inet.h>
2584b4e9c0Scjs #include <netdb.h>
2684b4e9c0Scjs #include <stdio.h>
2784b4e9c0Scjs #include <syslog.h>
2884b4e9c0Scjs #include <setjmp.h>
2984b4e9c0Scjs #include <string.h>
30f8dba09cSchristos #include <stdlib.h>
3184b4e9c0Scjs
3284b4e9c0Scjs #ifndef INADDR_NONE
3384b4e9c0Scjs #define INADDR_NONE (-1) /* XXX should be 0xffffffff */
3484b4e9c0Scjs #endif
3584b4e9c0Scjs
3684b4e9c0Scjs /* Application-specific. */
3784b4e9c0Scjs
3884b4e9c0Scjs #include "tcpd.h"
3984b4e9c0Scjs #include "scaffold.h"
4084b4e9c0Scjs
4184b4e9c0Scjs /*
4284b4e9c0Scjs * These are referenced by the options module and by rfc931.c.
4384b4e9c0Scjs */
4484b4e9c0Scjs int allow_severity = SEVERITY;
4584b4e9c0Scjs int deny_severity = LOG_WARNING;
46f5024bbeSchristos extern int rfc931_timeout; /* = RFC931_TIMEOUT; */
4784b4e9c0Scjs
4884b4e9c0Scjs /* find_inet_addr - find all addresses for this host, result to free() */
4984b4e9c0Scjs
find_inet_addr(char * host,int flags)50*421949a3Ssevan struct addrinfo *find_inet_addr(char *host, int flags)
5184b4e9c0Scjs {
522f7d82e6Sitojun struct addrinfo hints, *res;
532f7d82e6Sitojun int error;
5484b4e9c0Scjs
552f7d82e6Sitojun memset(&hints, 0, sizeof(hints));
562f7d82e6Sitojun hints.ai_socktype = SOCK_DGRAM;
572f7d82e6Sitojun hints.ai_flags = AI_CANONNAME | flags;
582f7d82e6Sitojun error = getaddrinfo(host, "0", &hints, &res);
592f7d82e6Sitojun if (error) {
602f7d82e6Sitojun tcpd_warn("%s: %s", host, gai_strerror(error));
612f7d82e6Sitojun return (0);
6284b4e9c0Scjs }
6384b4e9c0Scjs
642f7d82e6Sitojun if (res->ai_canonname && STR_NE(host, res->ai_canonname)) {
6584b4e9c0Scjs tcpd_warn("%s: hostname alias", host);
662f7d82e6Sitojun tcpd_warn("(official name: %.*s)", STRING_LENGTH, res->ai_canonname);
6784b4e9c0Scjs }
682f7d82e6Sitojun return (res);
6984b4e9c0Scjs }
7084b4e9c0Scjs
7184b4e9c0Scjs /* check_dns - give each address thorough workout, return address count */
7284b4e9c0Scjs
check_dns(char * host)73*421949a3Ssevan int check_dns(char *host)
7484b4e9c0Scjs {
7584b4e9c0Scjs struct request_info request;
762f7d82e6Sitojun struct sockaddr_storage ss;
772f7d82e6Sitojun struct addrinfo *res0, *res;
7884b4e9c0Scjs int count;
7984b4e9c0Scjs
802f7d82e6Sitojun if ((res0 = find_inet_addr(host, 0)) == NULL)
8184b4e9c0Scjs return (0);
822f7d82e6Sitojun memset(&ss, 0, sizeof(ss));
832f7d82e6Sitojun request_init(&request, RQ_CLIENT_SIN, &ss, 0);
8484b4e9c0Scjs sock_methods(&request);
8584b4e9c0Scjs
862f7d82e6Sitojun count = 0;
872f7d82e6Sitojun for (res = res0; res; res = res->ai_next) {
882f7d82e6Sitojun count++;
892f7d82e6Sitojun if (res->ai_addrlen > sizeof(ss))
902f7d82e6Sitojun continue;
912f7d82e6Sitojun memcpy(&ss, res->ai_addr, res->ai_addrlen);
9284b4e9c0Scjs
9384b4e9c0Scjs /*
9484b4e9c0Scjs * Force host name and address conversions. Use the request structure
9584b4e9c0Scjs * as a cache. Detect hostname lookup problems. Any name/name or
9684b4e9c0Scjs * name/address conflicts will be reported while eval_hostname() does
9784b4e9c0Scjs * its job.
9884b4e9c0Scjs */
9984b4e9c0Scjs request_set(&request, RQ_CLIENT_ADDR, "", RQ_CLIENT_NAME, "", 0);
10084b4e9c0Scjs if (STR_EQ(eval_hostname(request.client), unknown))
10184b4e9c0Scjs tcpd_warn("host address %s->name lookup failed",
10284b4e9c0Scjs eval_hostaddr(request.client));
10384b4e9c0Scjs }
1042f7d82e6Sitojun freeaddrinfo(res0);
10584b4e9c0Scjs return (count);
10684b4e9c0Scjs }
10784b4e9c0Scjs
10884b4e9c0Scjs /* dummy function to intercept the real shell_cmd() */
10984b4e9c0Scjs
11084b4e9c0Scjs /* ARGSUSED */
11184b4e9c0Scjs
shell_cmd(char * command)112*421949a3Ssevan void shell_cmd(char *command)
11384b4e9c0Scjs {
11484b4e9c0Scjs if (hosts_access_verbose)
11584b4e9c0Scjs printf("command: %s", command);
11684b4e9c0Scjs }
11784b4e9c0Scjs
11884b4e9c0Scjs /* dummy function to intercept the real clean_exit() */
11984b4e9c0Scjs
12084b4e9c0Scjs /* ARGSUSED */
12184b4e9c0Scjs
clean_exit(struct request_info * request)122*421949a3Ssevan void clean_exit(struct request_info *request)
12384b4e9c0Scjs {
12484b4e9c0Scjs exit(0);
12584b4e9c0Scjs }
12684b4e9c0Scjs
127f8dba09cSchristos #if 0
12884b4e9c0Scjs /* dummy function to intercept the real rfc931() */
12984b4e9c0Scjs
13084b4e9c0Scjs /* ARGSUSED */
13184b4e9c0Scjs
132d6a05e11Smatt void
133d6a05e11Smatt rfc931(struct request_info *request)
13484b4e9c0Scjs {
13599f12cfaSitojun strlcpy(request->user, unknown, sizeof(request->user));
13684b4e9c0Scjs }
137f8dba09cSchristos #endif
13884b4e9c0Scjs
13984b4e9c0Scjs /* check_path - examine accessibility */
14084b4e9c0Scjs
141d6a05e11Smatt int
check_path(const char * path,struct stat * st)142d6a05e11Smatt check_path(const char *path, struct stat *st)
14384b4e9c0Scjs {
14484b4e9c0Scjs struct stat stbuf;
14584b4e9c0Scjs char buf[BUFSIZ];
14684b4e9c0Scjs
14784b4e9c0Scjs if (stat(path, st) < 0)
14884b4e9c0Scjs return (-1);
14984b4e9c0Scjs #ifdef notdef
15084b4e9c0Scjs if (st->st_uid != 0)
15184b4e9c0Scjs tcpd_warn("%s: not owned by root", path);
15284b4e9c0Scjs if (st->st_mode & 020)
15384b4e9c0Scjs tcpd_warn("%s: group writable", path);
15484b4e9c0Scjs #endif
15584b4e9c0Scjs if (st->st_mode & 002)
15684b4e9c0Scjs tcpd_warn("%s: world writable", path);
15784b4e9c0Scjs if (path[0] == '/' && path[1] != 0) {
15899f12cfaSitojun strlcpy(buf, path, sizeof(buf));
15999f12cfaSitojun strrchr(buf, '/')[0] = 0;
16084b4e9c0Scjs (void) check_path(buf[0] ? buf : "/", &stbuf);
16184b4e9c0Scjs }
16284b4e9c0Scjs return (0);
16384b4e9c0Scjs }
164