1 /* $NetBSD: scaffold.c,v 1.12 2018/01/23 21:06:26 sevan Exp $ */
2
3 /*
4 * Routines for testing only. Not really industrial strength.
5 *
6 * Author: Wietse Venema, Eindhoven University of Technology, The Netherlands.
7 */
8
9 #include <sys/cdefs.h>
10 #ifndef lint
11 #if 0
12 static char sccs_id[] = "@(#) scaffold.c 1.6 97/03/21 19:27:24";
13 #else
14 __RCSID("$NetBSD: scaffold.c,v 1.12 2018/01/23 21:06:26 sevan Exp $");
15 #endif
16 #endif
17
18 /* System libraries. */
19
20 #include <sys/types.h>
21 #include <sys/stat.h>
22 #include <sys/socket.h>
23 #include <netinet/in.h>
24 #include <arpa/inet.h>
25 #include <netdb.h>
26 #include <stdio.h>
27 #include <syslog.h>
28 #include <setjmp.h>
29 #include <string.h>
30 #include <stdlib.h>
31
32 #ifndef INADDR_NONE
33 #define INADDR_NONE (-1) /* XXX should be 0xffffffff */
34 #endif
35
36 /* Application-specific. */
37
38 #include "tcpd.h"
39 #include "scaffold.h"
40
41 /*
42 * These are referenced by the options module and by rfc931.c.
43 */
44 int allow_severity = SEVERITY;
45 int deny_severity = LOG_WARNING;
46 extern int rfc931_timeout; /* = RFC931_TIMEOUT; */
47
48 /* find_inet_addr - find all addresses for this host, result to free() */
49
find_inet_addr(char * host,int flags)50 struct addrinfo *find_inet_addr(char *host, int flags)
51 {
52 struct addrinfo hints, *res;
53 int error;
54
55 memset(&hints, 0, sizeof(hints));
56 hints.ai_socktype = SOCK_DGRAM;
57 hints.ai_flags = AI_CANONNAME | flags;
58 error = getaddrinfo(host, "0", &hints, &res);
59 if (error) {
60 tcpd_warn("%s: %s", host, gai_strerror(error));
61 return (0);
62 }
63
64 if (res->ai_canonname && STR_NE(host, res->ai_canonname)) {
65 tcpd_warn("%s: hostname alias", host);
66 tcpd_warn("(official name: %.*s)", STRING_LENGTH, res->ai_canonname);
67 }
68 return (res);
69 }
70
71 /* check_dns - give each address thorough workout, return address count */
72
check_dns(char * host)73 int check_dns(char *host)
74 {
75 struct request_info request;
76 struct sockaddr_storage ss;
77 struct addrinfo *res0, *res;
78 int count;
79
80 if ((res0 = find_inet_addr(host, 0)) == NULL)
81 return (0);
82 memset(&ss, 0, sizeof(ss));
83 request_init(&request, RQ_CLIENT_SIN, &ss, 0);
84 sock_methods(&request);
85
86 count = 0;
87 for (res = res0; res; res = res->ai_next) {
88 count++;
89 if (res->ai_addrlen > sizeof(ss))
90 continue;
91 memcpy(&ss, res->ai_addr, res->ai_addrlen);
92
93 /*
94 * Force host name and address conversions. Use the request structure
95 * as a cache. Detect hostname lookup problems. Any name/name or
96 * name/address conflicts will be reported while eval_hostname() does
97 * its job.
98 */
99 request_set(&request, RQ_CLIENT_ADDR, "", RQ_CLIENT_NAME, "", 0);
100 if (STR_EQ(eval_hostname(request.client), unknown))
101 tcpd_warn("host address %s->name lookup failed",
102 eval_hostaddr(request.client));
103 }
104 freeaddrinfo(res0);
105 return (count);
106 }
107
108 /* dummy function to intercept the real shell_cmd() */
109
110 /* ARGSUSED */
111
shell_cmd(char * command)112 void shell_cmd(char *command)
113 {
114 if (hosts_access_verbose)
115 printf("command: %s", command);
116 }
117
118 /* dummy function to intercept the real clean_exit() */
119
120 /* ARGSUSED */
121
clean_exit(struct request_info * request)122 void clean_exit(struct request_info *request)
123 {
124 exit(0);
125 }
126
127 #if 0
128 /* dummy function to intercept the real rfc931() */
129
130 /* ARGSUSED */
131
132 void
133 rfc931(struct request_info *request)
134 {
135 strlcpy(request->user, unknown, sizeof(request->user));
136 }
137 #endif
138
139 /* check_path - examine accessibility */
140
141 int
check_path(const char * path,struct stat * st)142 check_path(const char *path, struct stat *st)
143 {
144 struct stat stbuf;
145 char buf[BUFSIZ];
146
147 if (stat(path, st) < 0)
148 return (-1);
149 #ifdef notdef
150 if (st->st_uid != 0)
151 tcpd_warn("%s: not owned by root", path);
152 if (st->st_mode & 020)
153 tcpd_warn("%s: group writable", path);
154 #endif
155 if (st->st_mode & 002)
156 tcpd_warn("%s: world writable", path);
157 if (path[0] == '/' && path[1] != 0) {
158 strlcpy(buf, path, sizeof(buf));
159 strrchr(buf, '/')[0] = 0;
160 (void) check_path(buf[0] ? buf : "/", &stbuf);
161 }
162 return (0);
163 }
164