1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate *
4*7c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate * with the License.
8*7c478bd9Sstevel@tonic-gate *
9*7c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate *
14*7c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate *
20*7c478bd9Sstevel@tonic-gate * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate */
22*7c478bd9Sstevel@tonic-gate /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
23*7c478bd9Sstevel@tonic-gate /* All Rights Reserved */
24*7c478bd9Sstevel@tonic-gate
25*7c478bd9Sstevel@tonic-gate
26*7c478bd9Sstevel@tonic-gate /*
27*7c478bd9Sstevel@tonic-gate * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
28*7c478bd9Sstevel@tonic-gate * Use is subject to license terms.
29*7c478bd9Sstevel@tonic-gate */
30*7c478bd9Sstevel@tonic-gate
31*7c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
32*7c478bd9Sstevel@tonic-gate
33*7c478bd9Sstevel@tonic-gate /*
34*7c478bd9Sstevel@tonic-gate * nlsenv.c:
35*7c478bd9Sstevel@tonic-gate *
36*7c478bd9Sstevel@tonic-gate * Utilities for servers to access environment set by listener.
37*7c478bd9Sstevel@tonic-gate *
38*7c478bd9Sstevel@tonic-gate * nlsgetcall: Returns pointer to t_call structure listener recieved during
39*7c478bd9Sstevel@tonic-gate * the t_listen. Gets data from environment and converts
40*7c478bd9Sstevel@tonic-gate * the data to internal address form.
41*7c478bd9Sstevel@tonic-gate *
42*7c478bd9Sstevel@tonic-gate * nlsprovider: Returns name of provider from environment.
43*7c478bd9Sstevel@tonic-gate *
44*7c478bd9Sstevel@tonic-gate */
45*7c478bd9Sstevel@tonic-gate
46*7c478bd9Sstevel@tonic-gate #include <ctype.h>
47*7c478bd9Sstevel@tonic-gate #include <strings.h>
48*7c478bd9Sstevel@tonic-gate #include <sys/tiuser.h>
49*7c478bd9Sstevel@tonic-gate #include "listen.h"
50*7c478bd9Sstevel@tonic-gate
51*7c478bd9Sstevel@tonic-gate /*
52*7c478bd9Sstevel@tonic-gate * define DEBUGMODE for diagnostic printf's to stderr
53*7c478bd9Sstevel@tonic-gate */
54*7c478bd9Sstevel@tonic-gate
55*7c478bd9Sstevel@tonic-gate /* #define DEBUGMODE */
56*7c478bd9Sstevel@tonic-gate
57*7c478bd9Sstevel@tonic-gate #ifdef DEBUGMODE
58*7c478bd9Sstevel@tonic-gate #include <stdio.h>
59*7c478bd9Sstevel@tonic-gate #endif
60*7c478bd9Sstevel@tonic-gate
61*7c478bd9Sstevel@tonic-gate /*
62*7c478bd9Sstevel@tonic-gate * nlsenv: (static)
63*7c478bd9Sstevel@tonic-gate *
64*7c478bd9Sstevel@tonic-gate * Given an environment variable name, a receiving buffer and the length
65*7c478bd9Sstevel@tonic-gate * of the receiving buffer, getenv gets the environment variable, decodes
66*7c478bd9Sstevel@tonic-gate * it and places the decoded data in addr. The return value is the length
67*7c478bd9Sstevel@tonic-gate * of "addr" if succesful, or a negative value if unsuccessful.
68*7c478bd9Sstevel@tonic-gate */
69*7c478bd9Sstevel@tonic-gate
70*7c478bd9Sstevel@tonic-gate extern char *getenv();
71*7c478bd9Sstevel@tonic-gate
72*7c478bd9Sstevel@tonic-gate int
nlsenv(struct netbuf * buf,char * envname)73*7c478bd9Sstevel@tonic-gate nlsenv(struct netbuf *buf, char *envname)
74*7c478bd9Sstevel@tonic-gate {
75*7c478bd9Sstevel@tonic-gate char *charaddr;
76*7c478bd9Sstevel@tonic-gate extern char *calloc();
77*7c478bd9Sstevel@tonic-gate extern int nlsc2addr();
78*7c478bd9Sstevel@tonic-gate int length;
79*7c478bd9Sstevel@tonic-gate
80*7c478bd9Sstevel@tonic-gate if (!(charaddr = getenv(envname)))
81*7c478bd9Sstevel@tonic-gate return(-11);
82*7c478bd9Sstevel@tonic-gate
83*7c478bd9Sstevel@tonic-gate #ifdef DEBUGMODE
84*7c478bd9Sstevel@tonic-gate fprintf(stderr, "nlsenv: environ %s = %s len = %d\n",
85*7c478bd9Sstevel@tonic-gate envname, charaddr, strlen(charaddr));
86*7c478bd9Sstevel@tonic-gate #endif
87*7c478bd9Sstevel@tonic-gate
88*7c478bd9Sstevel@tonic-gate if ((int)strlen(charaddr) & 1)
89*7c478bd9Sstevel@tonic-gate return(-12);
90*7c478bd9Sstevel@tonic-gate
91*7c478bd9Sstevel@tonic-gate length = (strlen(charaddr) + 1) / 2;
92*7c478bd9Sstevel@tonic-gate if (!(buf->buf = calloc(1, length)))
93*7c478bd9Sstevel@tonic-gate return(-13);
94*7c478bd9Sstevel@tonic-gate else
95*7c478bd9Sstevel@tonic-gate buf->maxlen = length;
96*7c478bd9Sstevel@tonic-gate return(nlsc2addr(buf->buf, buf->maxlen, charaddr));
97*7c478bd9Sstevel@tonic-gate }
98*7c478bd9Sstevel@tonic-gate
99*7c478bd9Sstevel@tonic-gate
100*7c478bd9Sstevel@tonic-gate /*
101*7c478bd9Sstevel@tonic-gate * nlsgetcall: Get calling data provided by the client via the listener.
102*7c478bd9Sstevel@tonic-gate *
103*7c478bd9Sstevel@tonic-gate * nlsgetcall allows network server processes started by the
104*7c478bd9Sstevel@tonic-gate * network listener process to access the callers 't_call'
105*7c478bd9Sstevel@tonic-gate * structure provided in the client's t_connect primitive.
106*7c478bd9Sstevel@tonic-gate *
107*7c478bd9Sstevel@tonic-gate * This routine gets this data from the environment
108*7c478bd9Sstevel@tonic-gate * via putenv(3C), interprets the data and places the data
109*7c478bd9Sstevel@tonic-gate * in a t_call structure allocated via t_alloc.
110*7c478bd9Sstevel@tonic-gate *
111*7c478bd9Sstevel@tonic-gate * synopsis:
112*7c478bd9Sstevel@tonic-gate *
113*7c478bd9Sstevel@tonic-gate * struct t_call *nlsgetcall(fd);
114*7c478bd9Sstevel@tonic-gate * int fd; arg now ignored
115*7c478bd9Sstevel@tonic-gate *
116*7c478bd9Sstevel@tonic-gate *
117*7c478bd9Sstevel@tonic-gate * returns: Address of an allocated t_call structure
118*7c478bd9Sstevel@tonic-gate * or
119*7c478bd9Sstevel@tonic-gate * NULL for failure. (calloc failed)
120*7c478bd9Sstevel@tonic-gate * If calloc succeeds, non-existant
121*7c478bd9Sstevel@tonic-gate * env. variables or data is indicated
122*7c478bd9Sstevel@tonic-gate * by a negative 'len' field in the approp.
123*7c478bd9Sstevel@tonic-gate * netbuf structure. A length of zero in the
124*7c478bd9Sstevel@tonic-gate * netbuf structure is valid.
125*7c478bd9Sstevel@tonic-gate *
126*7c478bd9Sstevel@tonic-gate */
127*7c478bd9Sstevel@tonic-gate
128*7c478bd9Sstevel@tonic-gate struct t_call *
nlsgetcall(int fd)129*7c478bd9Sstevel@tonic-gate nlsgetcall(int fd)
130*7c478bd9Sstevel@tonic-gate {
131*7c478bd9Sstevel@tonic-gate struct t_call *call;
132*7c478bd9Sstevel@tonic-gate extern char *calloc();
133*7c478bd9Sstevel@tonic-gate
134*7c478bd9Sstevel@tonic-gate if (!(call = (struct t_call *) calloc(1, sizeof(struct t_call))))
135*7c478bd9Sstevel@tonic-gate return((struct t_call *)0);
136*7c478bd9Sstevel@tonic-gate
137*7c478bd9Sstevel@tonic-gate /*
138*7c478bd9Sstevel@tonic-gate * Note: space for buffers gets allocated by nlsenv on the fly
139*7c478bd9Sstevel@tonic-gate */
140*7c478bd9Sstevel@tonic-gate
141*7c478bd9Sstevel@tonic-gate call->addr.len = nlsenv(&call->addr, NLSADDR);
142*7c478bd9Sstevel@tonic-gate call->opt.len = nlsenv(&call->opt, NLSOPT);
143*7c478bd9Sstevel@tonic-gate call->udata.len = nlsenv(&call->udata, NLSUDATA);
144*7c478bd9Sstevel@tonic-gate
145*7c478bd9Sstevel@tonic-gate return (call);
146*7c478bd9Sstevel@tonic-gate }
147*7c478bd9Sstevel@tonic-gate
148*7c478bd9Sstevel@tonic-gate
149*7c478bd9Sstevel@tonic-gate /*
150*7c478bd9Sstevel@tonic-gate * nlsprovider: Return the name of the transport provider
151*7c478bd9Sstevel@tonic-gate * as placed in the environment by the Network listener
152*7c478bd9Sstevel@tonic-gate * process. If the variable is not defined in the
153*7c478bd9Sstevel@tonic-gate * environment, a NULL pointer is returned.
154*7c478bd9Sstevel@tonic-gate *
155*7c478bd9Sstevel@tonic-gate * If the provider is "/dev/starlan", nlsprovider
156*7c478bd9Sstevel@tonic-gate * returns a pointer to the null terminated character string:
157*7c478bd9Sstevel@tonic-gate * "/dev/starlan" if this calling process is a child of the
158*7c478bd9Sstevel@tonic-gate * network listener process.
159*7c478bd9Sstevel@tonic-gate */
160*7c478bd9Sstevel@tonic-gate
161*7c478bd9Sstevel@tonic-gate char *
nlsprovider()162*7c478bd9Sstevel@tonic-gate nlsprovider()
163*7c478bd9Sstevel@tonic-gate {
164*7c478bd9Sstevel@tonic-gate return(getenv(NLSPROVIDER));
165*7c478bd9Sstevel@tonic-gate }
166*7c478bd9Sstevel@tonic-gate
167*7c478bd9Sstevel@tonic-gate
168*7c478bd9Sstevel@tonic-gate /*
169*7c478bd9Sstevel@tonic-gate * nlsc2addr: Convert external address to internal form.
170*7c478bd9Sstevel@tonic-gate * (from nlsaddr.c)
171*7c478bd9Sstevel@tonic-gate */
172*7c478bd9Sstevel@tonic-gate
173*7c478bd9Sstevel@tonic-gate /*
174*7c478bd9Sstevel@tonic-gate * asctohex(X): convert char X to integer value
175*7c478bd9Sstevel@tonic-gate * assumes isxdigit(X). returns integer value.
176*7c478bd9Sstevel@tonic-gate * Note that 'a' > 'A'. See usage in code below.
177*7c478bd9Sstevel@tonic-gate */
178*7c478bd9Sstevel@tonic-gate
179*7c478bd9Sstevel@tonic-gate #define asctohex(X) \
180*7c478bd9Sstevel@tonic-gate ((int)(isdigit(X) ? (int)(X-'0') : (X>='a') ? (X-'a')+10 : (X-'A')+10))
181*7c478bd9Sstevel@tonic-gate
182*7c478bd9Sstevel@tonic-gate /*
183*7c478bd9Sstevel@tonic-gate * nlsc2addr: Given a buffer containing the hex/ascii representation
184*7c478bd9Sstevel@tonic-gate * of a logical address, the buffer's size and an address
185*7c478bd9Sstevel@tonic-gate * of a receiving buffer, char2addr converts the logical
186*7c478bd9Sstevel@tonic-gate * addr to internal format and returns the size of the logical
187*7c478bd9Sstevel@tonic-gate * address. A negative value is returned and the receiving
188*7c478bd9Sstevel@tonic-gate * buffers contents are undefined if:
189*7c478bd9Sstevel@tonic-gate *
190*7c478bd9Sstevel@tonic-gate * A. The receiving buffer is not large enough. (rc = -1)
191*7c478bd9Sstevel@tonic-gate * B. If 'charaddr' does not contain a series of octets
192*7c478bd9Sstevel@tonic-gate * (strlen(charaddr) must be even). (rc = -2)
193*7c478bd9Sstevel@tonic-gate * C. Any character in 'charaddr' is not an ASCII hex digit.
194*7c478bd9Sstevel@tonic-gate * (rc = -3)
195*7c478bd9Sstevel@tonic-gate *
196*7c478bd9Sstevel@tonic-gate * NOTE: that even if the internal representation of an address is
197*7c478bd9Sstevel@tonic-gate * an ASCII string, there is no guarantee that the output will be
198*7c478bd9Sstevel@tonic-gate * null terminated, thus the returned length must be used when
199*7c478bd9Sstevel@tonic-gate * accessing the internal address.
200*7c478bd9Sstevel@tonic-gate */
201*7c478bd9Sstevel@tonic-gate
202*7c478bd9Sstevel@tonic-gate
203*7c478bd9Sstevel@tonic-gate int
nlsc2addr(char * addr,int maxlen,char * charaddr)204*7c478bd9Sstevel@tonic-gate nlsc2addr(char *addr, int maxlen, char *charaddr)
205*7c478bd9Sstevel@tonic-gate {
206*7c478bd9Sstevel@tonic-gate int len;
207*7c478bd9Sstevel@tonic-gate int i;
208*7c478bd9Sstevel@tonic-gate char c;
209*7c478bd9Sstevel@tonic-gate unsigned char val;
210*7c478bd9Sstevel@tonic-gate
211*7c478bd9Sstevel@tonic-gate if (strlen(charaddr) & 1)
212*7c478bd9Sstevel@tonic-gate return(-1);
213*7c478bd9Sstevel@tonic-gate
214*7c478bd9Sstevel@tonic-gate for (len = 0; ((maxlen--) && (*charaddr)); ++len) {
215*7c478bd9Sstevel@tonic-gate for (i = 2, val = 0; i--; ) {
216*7c478bd9Sstevel@tonic-gate c = *charaddr++;
217*7c478bd9Sstevel@tonic-gate if (!(isxdigit(c)))
218*7c478bd9Sstevel@tonic-gate return(-3);
219*7c478bd9Sstevel@tonic-gate val = (val << 4) | (unsigned char)asctohex(c);
220*7c478bd9Sstevel@tonic-gate }
221*7c478bd9Sstevel@tonic-gate
222*7c478bd9Sstevel@tonic-gate *addr++ = (char)val;
223*7c478bd9Sstevel@tonic-gate }
224*7c478bd9Sstevel@tonic-gate
225*7c478bd9Sstevel@tonic-gate #ifdef DEBUGMODE
226*7c478bd9Sstevel@tonic-gate fprintf(stderr, "nlsc2addr: returned length = %d\n", len);
227*7c478bd9Sstevel@tonic-gate #endif
228*7c478bd9Sstevel@tonic-gate
229*7c478bd9Sstevel@tonic-gate return(*charaddr ? -2 : len);
230*7c478bd9Sstevel@tonic-gate }
231