summaryrefslogtreecommitdiff
path: root/usr/src/lib/libnls/common/nlsenv.c
blob: 45c5f9f7606ead28bafdc6c498abb9308e37f22b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License, Version 1.0 only
 * (the "License").  You may not use this file except in compliance
 * with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */
/*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
/*	  All Rights Reserved  	*/


/*
 * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

#pragma ident	"%Z%%M%	%I%	%E% SMI"

/*
 * nlsenv.c:
 *
 * Utilities for servers to access environment set by listener.
 *
 * nlsgetcall:	Returns pointer to t_call structure listener recieved during
 *		the t_listen.  Gets data from environment and converts
 *		the data to internal address form.
 *
 * nlsprovider:	Returns name of provider from environment.
 *
 */

#include <ctype.h>
#include <strings.h>
#include <sys/tiuser.h>
#include "listen.h"

/*
 * define DEBUGMODE for diagnostic printf's to stderr
 */

/* #define	DEBUGMODE */

#ifdef	DEBUGMODE
#include <stdio.h>
#endif

/*
 * nlsenv: (static)
 *
 * Given an environment variable name, a receiving buffer and the length
 * of the receiving buffer, getenv gets the environment variable, decodes
 * it and places the decoded data in addr.  The return value is the length
 * of "addr" if succesful, or a negative value if unsuccessful.
 */

extern char *getenv();

int
nlsenv(struct netbuf *buf, char *envname)
{
	char *charaddr;
	extern char *calloc();
	extern int nlsc2addr();
	int length;

	if (!(charaddr = getenv(envname)))
		return(-11);

#ifdef	DEBUGMODE
	fprintf(stderr, "nlsenv: environ %s = %s len = %d\n", 
		envname, charaddr, strlen(charaddr));
#endif

	if ((int)strlen(charaddr) & 1)
		return(-12);

	length = (strlen(charaddr) + 1) / 2;
	if (!(buf->buf = calloc(1, length)))
		return(-13);
	else
		buf->maxlen = length;
	return(nlsc2addr(buf->buf, buf->maxlen, charaddr));
}


/*
 * nlsgetcall:	Get calling data provided by the client via the listener.
 *
 *		nlsgetcall allows network server processes started by the
 *		network listener process to access the callers 't_call'
 *		structure provided in the client's t_connect primitive.
 *
 *		This routine gets this data from the environment
 *		via putenv(3C), interprets the data and places the data
 *		in a t_call structure allocated via t_alloc.
 *
 *		synopsis:
 *
 *		struct t_call *nlsgetcall(fd);
 *		int fd;		arg now ignored
 *
 *
 *		returns:	Address of an allocated t_call structure
 *				or
 *				NULL for failure. (calloc failed)
 *				If calloc succeeds, non-existant
 *				env. variables or data is indicated
 *				by a negative 'len' field in the approp.
 *				netbuf structure.  A length of zero in the
 *				netbuf structure is valid.
 *
 */

struct t_call *
nlsgetcall(int fd)
{
	struct t_call *call;
	extern char *calloc();

	if (!(call = (struct t_call *) calloc(1, sizeof(struct t_call))))
		return((struct t_call *)0);

/*
 * Note: space for buffers gets allocated by nlsenv on the fly
 */

	call->addr.len = nlsenv(&call->addr, NLSADDR);
	call->opt.len = nlsenv(&call->opt, NLSOPT);
	call->udata.len = nlsenv(&call->udata, NLSUDATA);

	return (call);
}


/*
 * nlsprovider:	Return the name of the transport provider
 *		as placed in the environment by the Network listener
 *		process.  If the variable is not defined in the
 *		environment, a NULL pointer is returned.
 *
 *		If the provider is "/dev/starlan", nlsprovider
 *		returns a pointer to the null terminated character string:
 *		"/dev/starlan" if this calling process is a child of the 
 *		network listener process.
 */

char *
nlsprovider()
{
	return(getenv(NLSPROVIDER));
}


/*
 * nlsc2addr:	Convert external address to internal form.
 *	(from nlsaddr.c)
 */

/*
 * asctohex(X):  convert char X to integer value
 *		 assumes isxdigit(X). returns integer value.
 *		 Note that 'a' > 'A'.  See usage in code below.
 */

#define asctohex(X)	\
    ((int)(isdigit(X) ? (int)(X-'0') : (X>='a') ? (X-'a')+10 : (X-'A')+10))

/*
 * nlsc2addr:	Given a buffer containing the hex/ascii representation
 *		of a logical address, the buffer's size and an address
 *		of a receiving buffer, char2addr converts the logical
 *		addr to internal format and returns the size of the logical
 *		address.  A negative value is returned and the receiving
 *		buffers contents are undefined if:
 *
 *		A.  The receiving buffer is not large enough. (rc = -1)
 *		B.  If 'charaddr' does not contain a series of octets 
 *		    (strlen(charaddr) must be even). (rc = -2)
 *		C.  Any character in 'charaddr' is not an ASCII hex digit.
 *		    (rc = -3)
 *
 *	NOTE: that even if the internal representation of an address is
 *	an ASCII string, there is no guarantee that the output will be
 *	null terminated, thus the returned length must be used when
 *	accessing the internal address.
 */


int
nlsc2addr(char *addr, int maxlen, char *charaddr)
{
	int len;
	int i;
	char c;
	unsigned char val;

	if (strlen(charaddr) & 1)
		return(-1);

	for (len = 0; ((maxlen--) && (*charaddr)); ++len)  {
		for (i = 2,  val = 0;  i--;  )  {
			c = *charaddr++;
			if (!(isxdigit(c)))
				return(-3);
			val = (val << 4) | (unsigned char)asctohex(c);
	    	}

		*addr++ = (char)val;
	}

#ifdef	DEBUGMODE
	fprintf(stderr, "nlsc2addr: returned length = %d\n", len);
#endif

	return(*charaddr ? -2 : len);
}