xref: /freebsd/contrib/tcp_wrappers/update.c (revision b740c88bfb6453416926271c089262e7164dace3)
1  /*
2   * Routines for controlled update/initialization of request structures.
3   *
4   * request_init() initializes its argument. Pointers and string-valued members
5   * are initialized to zero, to indicate that no lookup has been attempted.
6   *
7   * request_set() adds information to an already initialized request structure.
8   *
9   * Both functions take a variable-length name-value list.
10   *
11   * Diagnostics are reported through syslog(3).
12   *
13   * Author: Wietse Venema, Eindhoven University of Technology, The Netherlands.
14   *
15   * $FreeBSD$
16   */
17 
18 #ifndef lint
19 static char sccsid[] = "@(#) update.c 1.1 94/12/28 17:42:56";
20 #endif
21 
22 /* System libraries */
23 
24 #include <stdio.h>
25 #include <syslog.h>
26 #include <string.h>
27 #include <unistd.h>
28 
29 /* Local stuff. */
30 
31 #include "mystdarg.h"
32 #include "tcpd.h"
33 
34 /* request_fill - request update engine */
35 
36 static struct request_info *request_fill(request, ap)
37 struct request_info *request;
38 va_list ap;
39 {
40     int     key;
41     char   *ptr;
42 
43     while ((key = va_arg(ap, int)) > 0) {
44 	switch (key) {
45 	default:
46 	    tcpd_warn("request_fill: invalid key: %d", key);
47 	    return (request);
48 	case RQ_FILE:
49 	    request->fd = va_arg(ap, int);
50 	    continue;
51 	case RQ_CLIENT_SIN:
52 #ifdef INET6
53 	    request->client->sin = va_arg(ap, struct sockaddr *);
54 #else
55 	    request->client->sin = va_arg(ap, struct sockaddr_in *);
56 #endif
57 	    continue;
58 	case RQ_SERVER_SIN:
59 #ifdef INET6
60 	    request->server->sin = va_arg(ap, struct sockaddr *);
61 #else
62 	    request->server->sin = va_arg(ap, struct sockaddr_in *);
63 #endif
64 	    continue;
65 
66 	    /*
67 	     * All other fields are strings with the same maximal length.
68 	     */
69 
70 	case RQ_DAEMON:
71 	    ptr = request->daemon;
72 	    break;
73 	case RQ_USER:
74 	    ptr = request->user;
75 	    break;
76 	case RQ_CLIENT_NAME:
77 	    ptr = request->client->name;
78 	    break;
79 	case RQ_CLIENT_ADDR:
80 	    ptr = request->client->addr;
81 	    break;
82 	case RQ_SERVER_NAME:
83 	    ptr = request->server->name;
84 	    break;
85 	case RQ_SERVER_ADDR:
86 	    ptr = request->server->addr;
87 	    break;
88 	}
89 	STRN_CPY(ptr, va_arg(ap, char *), STRING_LENGTH);
90     }
91     return (request);
92 }
93 
94 /* request_init - initialize request structure */
95 
96 struct request_info *VARARGS(request_init, struct request_info *, request)
97 {
98     static struct request_info default_info;
99     struct request_info *r;
100     va_list ap;
101 
102     /*
103      * Initialize data members. We do not assign default function pointer
104      * members, to avoid pulling in the whole socket module when it is not
105      * really needed.
106      */
107     VASTART(ap, struct request_info *, request);
108     *request = default_info;
109     request->fd = -1;
110     strcpy(request->daemon, unknown);
111     sprintf(request->pid, "%d", getpid());
112     request->client->request = request;
113     request->server->request = request;
114     r = request_fill(request, ap);
115     VAEND(ap);
116     return (r);
117 }
118 
119 /* request_set - update request structure */
120 
121 struct request_info *VARARGS(request_set, struct request_info *, request)
122 {
123     struct request_info *r;
124     va_list ap;
125 
126     VASTART(ap, struct request_info *, request);
127     r = request_fill(request, ap);
128     VAEND(ap);
129     return (r);
130 }
131