xref: /illumos-gate/usr/src/lib/libnls/common/nlsenv.c (revision 1da57d55)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
227c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
237c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
247c478bd9Sstevel@tonic-gate 
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate /*
277c478bd9Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
287c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
297c478bd9Sstevel@tonic-gate  */
307c478bd9Sstevel@tonic-gate 
317c478bd9Sstevel@tonic-gate /*
327c478bd9Sstevel@tonic-gate  * nlsenv.c:
337c478bd9Sstevel@tonic-gate  *
347c478bd9Sstevel@tonic-gate  * Utilities for servers to access environment set by listener.
357c478bd9Sstevel@tonic-gate  *
367c478bd9Sstevel@tonic-gate  * nlsgetcall:	Returns pointer to t_call structure listener recieved during
377c478bd9Sstevel@tonic-gate  *		the t_listen.  Gets data from environment and converts
387c478bd9Sstevel@tonic-gate  *		the data to internal address form.
397c478bd9Sstevel@tonic-gate  *
407c478bd9Sstevel@tonic-gate  * nlsprovider:	Returns name of provider from environment.
417c478bd9Sstevel@tonic-gate  *
427c478bd9Sstevel@tonic-gate  */
437c478bd9Sstevel@tonic-gate 
447c478bd9Sstevel@tonic-gate #include <ctype.h>
457c478bd9Sstevel@tonic-gate #include <strings.h>
467c478bd9Sstevel@tonic-gate #include <sys/tiuser.h>
477c478bd9Sstevel@tonic-gate #include "listen.h"
487c478bd9Sstevel@tonic-gate 
497c478bd9Sstevel@tonic-gate /*
507c478bd9Sstevel@tonic-gate  * define DEBUGMODE for diagnostic printf's to stderr
517c478bd9Sstevel@tonic-gate  */
527c478bd9Sstevel@tonic-gate 
537c478bd9Sstevel@tonic-gate /* #define	DEBUGMODE */
547c478bd9Sstevel@tonic-gate 
557c478bd9Sstevel@tonic-gate #ifdef	DEBUGMODE
567c478bd9Sstevel@tonic-gate #include <stdio.h>
577c478bd9Sstevel@tonic-gate #endif
587c478bd9Sstevel@tonic-gate 
597c478bd9Sstevel@tonic-gate /*
607c478bd9Sstevel@tonic-gate  * nlsenv: (static)
617c478bd9Sstevel@tonic-gate  *
627c478bd9Sstevel@tonic-gate  * Given an environment variable name, a receiving buffer and the length
637c478bd9Sstevel@tonic-gate  * of the receiving buffer, getenv gets the environment variable, decodes
647c478bd9Sstevel@tonic-gate  * it and places the decoded data in addr.  The return value is the length
657c478bd9Sstevel@tonic-gate  * of "addr" if succesful, or a negative value if unsuccessful.
667c478bd9Sstevel@tonic-gate  */
677c478bd9Sstevel@tonic-gate 
687c478bd9Sstevel@tonic-gate extern char *getenv();
697c478bd9Sstevel@tonic-gate 
707c478bd9Sstevel@tonic-gate int
nlsenv(struct netbuf * buf,char * envname)717c478bd9Sstevel@tonic-gate nlsenv(struct netbuf *buf, char *envname)
727c478bd9Sstevel@tonic-gate {
737c478bd9Sstevel@tonic-gate 	char *charaddr;
747c478bd9Sstevel@tonic-gate 	extern char *calloc();
757c478bd9Sstevel@tonic-gate 	extern int nlsc2addr();
767c478bd9Sstevel@tonic-gate 	int length;
777c478bd9Sstevel@tonic-gate 
787c478bd9Sstevel@tonic-gate 	if (!(charaddr = getenv(envname)))
797c478bd9Sstevel@tonic-gate 		return(-11);
807c478bd9Sstevel@tonic-gate 
817c478bd9Sstevel@tonic-gate #ifdef	DEBUGMODE
82*1da57d55SToomas Soome 	fprintf(stderr, "nlsenv: environ %s = %s len = %d\n",
837c478bd9Sstevel@tonic-gate 		envname, charaddr, strlen(charaddr));
847c478bd9Sstevel@tonic-gate #endif
857c478bd9Sstevel@tonic-gate 
867c478bd9Sstevel@tonic-gate 	if ((int)strlen(charaddr) & 1)
877c478bd9Sstevel@tonic-gate 		return(-12);
887c478bd9Sstevel@tonic-gate 
897c478bd9Sstevel@tonic-gate 	length = (strlen(charaddr) + 1) / 2;
907c478bd9Sstevel@tonic-gate 	if (!(buf->buf = calloc(1, length)))
917c478bd9Sstevel@tonic-gate 		return(-13);
927c478bd9Sstevel@tonic-gate 	else
937c478bd9Sstevel@tonic-gate 		buf->maxlen = length;
947c478bd9Sstevel@tonic-gate 	return(nlsc2addr(buf->buf, buf->maxlen, charaddr));
957c478bd9Sstevel@tonic-gate }
967c478bd9Sstevel@tonic-gate 
977c478bd9Sstevel@tonic-gate 
987c478bd9Sstevel@tonic-gate /*
997c478bd9Sstevel@tonic-gate  * nlsgetcall:	Get calling data provided by the client via the listener.
1007c478bd9Sstevel@tonic-gate  *
1017c478bd9Sstevel@tonic-gate  *		nlsgetcall allows network server processes started by the
1027c478bd9Sstevel@tonic-gate  *		network listener process to access the callers 't_call'
1037c478bd9Sstevel@tonic-gate  *		structure provided in the client's t_connect primitive.
1047c478bd9Sstevel@tonic-gate  *
1057c478bd9Sstevel@tonic-gate  *		This routine gets this data from the environment
1067c478bd9Sstevel@tonic-gate  *		via putenv(3C), interprets the data and places the data
1077c478bd9Sstevel@tonic-gate  *		in a t_call structure allocated via t_alloc.
1087c478bd9Sstevel@tonic-gate  *
1097c478bd9Sstevel@tonic-gate  *		synopsis:
1107c478bd9Sstevel@tonic-gate  *
1117c478bd9Sstevel@tonic-gate  *		struct t_call *nlsgetcall(fd);
1127c478bd9Sstevel@tonic-gate  *		int fd;		arg now ignored
1137c478bd9Sstevel@tonic-gate  *
1147c478bd9Sstevel@tonic-gate  *
1157c478bd9Sstevel@tonic-gate  *		returns:	Address of an allocated t_call structure
1167c478bd9Sstevel@tonic-gate  *				or
1177c478bd9Sstevel@tonic-gate  *				NULL for failure. (calloc failed)
1187c478bd9Sstevel@tonic-gate  *				If calloc succeeds, non-existant
1197c478bd9Sstevel@tonic-gate  *				env. variables or data is indicated
1207c478bd9Sstevel@tonic-gate  *				by a negative 'len' field in the approp.
1217c478bd9Sstevel@tonic-gate  *				netbuf structure.  A length of zero in the
1227c478bd9Sstevel@tonic-gate  *				netbuf structure is valid.
1237c478bd9Sstevel@tonic-gate  *
1247c478bd9Sstevel@tonic-gate  */
1257c478bd9Sstevel@tonic-gate 
1267c478bd9Sstevel@tonic-gate struct t_call *
nlsgetcall(int fd)1277c478bd9Sstevel@tonic-gate nlsgetcall(int fd)
1287c478bd9Sstevel@tonic-gate {
1297c478bd9Sstevel@tonic-gate 	struct t_call *call;
1307c478bd9Sstevel@tonic-gate 	extern char *calloc();
1317c478bd9Sstevel@tonic-gate 
1327c478bd9Sstevel@tonic-gate 	if (!(call = (struct t_call *) calloc(1, sizeof(struct t_call))))
1337c478bd9Sstevel@tonic-gate 		return((struct t_call *)0);
1347c478bd9Sstevel@tonic-gate 
1357c478bd9Sstevel@tonic-gate /*
1367c478bd9Sstevel@tonic-gate  * Note: space for buffers gets allocated by nlsenv on the fly
1377c478bd9Sstevel@tonic-gate  */
1387c478bd9Sstevel@tonic-gate 
1397c478bd9Sstevel@tonic-gate 	call->addr.len = nlsenv(&call->addr, NLSADDR);
1407c478bd9Sstevel@tonic-gate 	call->opt.len = nlsenv(&call->opt, NLSOPT);
1417c478bd9Sstevel@tonic-gate 	call->udata.len = nlsenv(&call->udata, NLSUDATA);
1427c478bd9Sstevel@tonic-gate 
1437c478bd9Sstevel@tonic-gate 	return (call);
1447c478bd9Sstevel@tonic-gate }
1457c478bd9Sstevel@tonic-gate 
1467c478bd9Sstevel@tonic-gate 
1477c478bd9Sstevel@tonic-gate /*
1487c478bd9Sstevel@tonic-gate  * nlsprovider:	Return the name of the transport provider
1497c478bd9Sstevel@tonic-gate  *		as placed in the environment by the Network listener
1507c478bd9Sstevel@tonic-gate  *		process.  If the variable is not defined in the
1517c478bd9Sstevel@tonic-gate  *		environment, a NULL pointer is returned.
1527c478bd9Sstevel@tonic-gate  *
1537c478bd9Sstevel@tonic-gate  *		If the provider is "/dev/starlan", nlsprovider
1547c478bd9Sstevel@tonic-gate  *		returns a pointer to the null terminated character string:
155*1da57d55SToomas Soome  *		"/dev/starlan" if this calling process is a child of the
1567c478bd9Sstevel@tonic-gate  *		network listener process.
1577c478bd9Sstevel@tonic-gate  */
1587c478bd9Sstevel@tonic-gate 
1597c478bd9Sstevel@tonic-gate char *
nlsprovider()1607c478bd9Sstevel@tonic-gate nlsprovider()
1617c478bd9Sstevel@tonic-gate {
1627c478bd9Sstevel@tonic-gate 	return(getenv(NLSPROVIDER));
1637c478bd9Sstevel@tonic-gate }
1647c478bd9Sstevel@tonic-gate 
1657c478bd9Sstevel@tonic-gate 
1667c478bd9Sstevel@tonic-gate /*
1677c478bd9Sstevel@tonic-gate  * nlsc2addr:	Convert external address to internal form.
1687c478bd9Sstevel@tonic-gate  *	(from nlsaddr.c)
1697c478bd9Sstevel@tonic-gate  */
1707c478bd9Sstevel@tonic-gate 
1717c478bd9Sstevel@tonic-gate /*
1727c478bd9Sstevel@tonic-gate  * asctohex(X):  convert char X to integer value
1737c478bd9Sstevel@tonic-gate  *		 assumes isxdigit(X). returns integer value.
1747c478bd9Sstevel@tonic-gate  *		 Note that 'a' > 'A'.  See usage in code below.
1757c478bd9Sstevel@tonic-gate  */
1767c478bd9Sstevel@tonic-gate 
1777c478bd9Sstevel@tonic-gate #define asctohex(X)	\
1787c478bd9Sstevel@tonic-gate     ((int)(isdigit(X) ? (int)(X-'0') : (X>='a') ? (X-'a')+10 : (X-'A')+10))
1797c478bd9Sstevel@tonic-gate 
1807c478bd9Sstevel@tonic-gate /*
1817c478bd9Sstevel@tonic-gate  * nlsc2addr:	Given a buffer containing the hex/ascii representation
1827c478bd9Sstevel@tonic-gate  *		of a logical address, the buffer's size and an address
1837c478bd9Sstevel@tonic-gate  *		of a receiving buffer, char2addr converts the logical
1847c478bd9Sstevel@tonic-gate  *		addr to internal format and returns the size of the logical
1857c478bd9Sstevel@tonic-gate  *		address.  A negative value is returned and the receiving
1867c478bd9Sstevel@tonic-gate  *		buffers contents are undefined if:
1877c478bd9Sstevel@tonic-gate  *
1887c478bd9Sstevel@tonic-gate  *		A.  The receiving buffer is not large enough. (rc = -1)
189*1da57d55SToomas Soome  *		B.  If 'charaddr' does not contain a series of octets
1907c478bd9Sstevel@tonic-gate  *		    (strlen(charaddr) must be even). (rc = -2)
1917c478bd9Sstevel@tonic-gate  *		C.  Any character in 'charaddr' is not an ASCII hex digit.
1927c478bd9Sstevel@tonic-gate  *		    (rc = -3)
1937c478bd9Sstevel@tonic-gate  *
1947c478bd9Sstevel@tonic-gate  *	NOTE: that even if the internal representation of an address is
1957c478bd9Sstevel@tonic-gate  *	an ASCII string, there is no guarantee that the output will be
1967c478bd9Sstevel@tonic-gate  *	null terminated, thus the returned length must be used when
1977c478bd9Sstevel@tonic-gate  *	accessing the internal address.
1987c478bd9Sstevel@tonic-gate  */
1997c478bd9Sstevel@tonic-gate 
2007c478bd9Sstevel@tonic-gate 
2017c478bd9Sstevel@tonic-gate int
nlsc2addr(char * addr,int maxlen,char * charaddr)2027c478bd9Sstevel@tonic-gate nlsc2addr(char *addr, int maxlen, char *charaddr)
2037c478bd9Sstevel@tonic-gate {
2047c478bd9Sstevel@tonic-gate 	int len;
2057c478bd9Sstevel@tonic-gate 	int i;
2067c478bd9Sstevel@tonic-gate 	char c;
2077c478bd9Sstevel@tonic-gate 	unsigned char val;
2087c478bd9Sstevel@tonic-gate 
2097c478bd9Sstevel@tonic-gate 	if (strlen(charaddr) & 1)
2107c478bd9Sstevel@tonic-gate 		return(-1);
2117c478bd9Sstevel@tonic-gate 
2127c478bd9Sstevel@tonic-gate 	for (len = 0; ((maxlen--) && (*charaddr)); ++len)  {
2137c478bd9Sstevel@tonic-gate 		for (i = 2,  val = 0;  i--;  )  {
2147c478bd9Sstevel@tonic-gate 			c = *charaddr++;
2157c478bd9Sstevel@tonic-gate 			if (!(isxdigit(c)))
2167c478bd9Sstevel@tonic-gate 				return(-3);
2177c478bd9Sstevel@tonic-gate 			val = (val << 4) | (unsigned char)asctohex(c);
2187c478bd9Sstevel@tonic-gate 	    	}
2197c478bd9Sstevel@tonic-gate 
2207c478bd9Sstevel@tonic-gate 		*addr++ = (char)val;
2217c478bd9Sstevel@tonic-gate 	}
2227c478bd9Sstevel@tonic-gate 
2237c478bd9Sstevel@tonic-gate #ifdef	DEBUGMODE
2247c478bd9Sstevel@tonic-gate 	fprintf(stderr, "nlsc2addr: returned length = %d\n", len);
2257c478bd9Sstevel@tonic-gate #endif
2267c478bd9Sstevel@tonic-gate 
2277c478bd9Sstevel@tonic-gate 	return(*charaddr ? -2 : len);
2287c478bd9Sstevel@tonic-gate }
229