xref: /freebsd/contrib/tcp_wrappers/update.c (revision 6e8394b8baa7d5d9153ab90de6824bcd19b3b4e1)
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 
16 #ifndef lint
17 static char sccsid[] = "@(#) update.c 1.1 94/12/28 17:42:56";
18 #endif
19 
20 /* System libraries */
21 
22 #include <stdio.h>
23 #include <syslog.h>
24 #include <string.h>
25 
26 /* Local stuff. */
27 
28 #include "mystdarg.h"
29 #include "tcpd.h"
30 
31 /* request_fill - request update engine */
32 
33 static struct request_info *request_fill(request, ap)
34 struct request_info *request;
35 va_list ap;
36 {
37     int     key;
38     char   *ptr;
39 
40     while ((key = va_arg(ap, int)) > 0) {
41 	switch (key) {
42 	default:
43 	    tcpd_warn("request_fill: invalid key: %d", key);
44 	    return (request);
45 	case RQ_FILE:
46 	    request->fd = va_arg(ap, int);
47 	    continue;
48 	case RQ_CLIENT_SIN:
49 	    request->client->sin = va_arg(ap, struct sockaddr_in *);
50 	    continue;
51 	case RQ_SERVER_SIN:
52 	    request->server->sin = va_arg(ap, struct sockaddr_in *);
53 	    continue;
54 
55 	    /*
56 	     * All other fields are strings with the same maximal length.
57 	     */
58 
59 	case RQ_DAEMON:
60 	    ptr = request->daemon;
61 	    break;
62 	case RQ_USER:
63 	    ptr = request->user;
64 	    break;
65 	case RQ_CLIENT_NAME:
66 	    ptr = request->client->name;
67 	    break;
68 	case RQ_CLIENT_ADDR:
69 	    ptr = request->client->addr;
70 	    break;
71 	case RQ_SERVER_NAME:
72 	    ptr = request->server->name;
73 	    break;
74 	case RQ_SERVER_ADDR:
75 	    ptr = request->server->addr;
76 	    break;
77 	}
78 	STRN_CPY(ptr, va_arg(ap, char *), STRING_LENGTH);
79     }
80     return (request);
81 }
82 
83 /* request_init - initialize request structure */
84 
85 struct request_info *VARARGS(request_init, struct request_info *, request)
86 {
87     static struct request_info default_info;
88     struct request_info *r;
89     va_list ap;
90 
91     /*
92      * Initialize data members. We do not assign default function pointer
93      * members, to avoid pulling in the whole socket module when it is not
94      * really needed.
95      */
96     VASTART(ap, struct request_info *, request);
97     *request = default_info;
98     request->fd = -1;
99     strcpy(request->daemon, unknown);
100     sprintf(request->pid, "%d", getpid());
101     request->client->request = request;
102     request->server->request = request;
103     r = request_fill(request, ap);
104     VAEND(ap);
105     return (r);
106 }
107 
108 /* request_set - update request structure */
109 
110 struct request_info *VARARGS(request_set, struct request_info *, request)
111 {
112     struct request_info *r;
113     va_list ap;
114 
115     VASTART(ap, struct request_info *, request);
116     r = request_fill(request, ap);
117     VAEND(ap);
118     return (r);
119 }
120