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