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