xref: /onnv-gate/usr/src/lib/libsmbfs/smb/nb_name.c (revision 11332:ed3411181494)
16007Sthurlow /*
26007Sthurlow  * Copyright (c) 2000, Boris Popov
36007Sthurlow  * All rights reserved.
46007Sthurlow  *
56007Sthurlow  * Redistribution and use in source and binary forms, with or without
66007Sthurlow  * modification, are permitted provided that the following conditions
76007Sthurlow  * are met:
86007Sthurlow  * 1. Redistributions of source code must retain the above copyright
96007Sthurlow  *    notice, this list of conditions and the following disclaimer.
106007Sthurlow  * 2. Redistributions in binary form must reproduce the above copyright
116007Sthurlow  *    notice, this list of conditions and the following disclaimer in the
126007Sthurlow  *    documentation and/or other materials provided with the distribution.
136007Sthurlow  * 3. All advertising materials mentioning features or use of this software
146007Sthurlow  *    must display the following acknowledgement:
156007Sthurlow  *    This product includes software developed by Boris Popov.
166007Sthurlow  * 4. Neither the name of the author nor the names of any co-contributors
176007Sthurlow  *    may be used to endorse or promote products derived from this software
186007Sthurlow  *    without specific prior written permission.
196007Sthurlow  *
206007Sthurlow  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
216007Sthurlow  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
226007Sthurlow  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
236007Sthurlow  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
246007Sthurlow  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
256007Sthurlow  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
266007Sthurlow  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
276007Sthurlow  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
286007Sthurlow  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
296007Sthurlow  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
306007Sthurlow  * SUCH DAMAGE.
316007Sthurlow  *
326007Sthurlow  * $Id: nb_name.c,v 1.11 2004/12/11 05:23:59 lindak Exp $
336007Sthurlow  */
346007Sthurlow 
356007Sthurlow #include <sys/param.h>
366007Sthurlow #include <sys/socket.h>
376007Sthurlow #include <ctype.h>
386007Sthurlow #include <errno.h>
396007Sthurlow #include <stdio.h>
406007Sthurlow #include <stdlib.h>
416007Sthurlow #include <string.h>
426007Sthurlow #include <strings.h>
436007Sthurlow #include <libintl.h>
446007Sthurlow #include <assert.h>
456007Sthurlow 
466007Sthurlow #include <netsmb/netbios.h>
476007Sthurlow #include <netsmb/smb_lib.h>
486007Sthurlow #include <netsmb/nb_lib.h>
496007Sthurlow #include <netsmb/mchain.h>
508271SGordon.Ross@Sun.COM #include "private.h"
516007Sthurlow 
526007Sthurlow int
nb_snballoc(struct sockaddr_nb ** dst)5310023SGordon.Ross@Sun.COM nb_snballoc(struct sockaddr_nb **dst)
546007Sthurlow {
556007Sthurlow 	struct sockaddr_nb *snb;
566007Sthurlow 	int slen;
576007Sthurlow 
586007Sthurlow 	slen = sizeof (struct sockaddr_nb);
596007Sthurlow 	snb = malloc(slen);
606007Sthurlow 	if (snb == NULL)
616007Sthurlow 		return (ENOMEM);
626007Sthurlow 	bzero(snb, slen);
636007Sthurlow 	snb->snb_family = AF_NETBIOS;
646007Sthurlow 	*dst = snb;
656007Sthurlow 	return (0);
666007Sthurlow }
676007Sthurlow 
686007Sthurlow void
nb_snbfree(struct sockaddr * snb)696007Sthurlow nb_snbfree(struct sockaddr *snb)
706007Sthurlow {
716007Sthurlow 	free(snb);
726007Sthurlow }
736007Sthurlow 
746007Sthurlow /*
756007Sthurlow  * Create a full NETBIOS address
7610023SGordon.Ross@Sun.COM  * Passed names should already be upper case.
7710023SGordon.Ross@Sun.COM  * Stores the names truncated or blank padded.
7810023SGordon.Ross@Sun.COM  * NetBIOS name encoding happens later.
796007Sthurlow  */
806007Sthurlow int
nb_sockaddr(struct sockaddr * peer,struct nb_name * np,struct sockaddr_nb ** dst)816007Sthurlow nb_sockaddr(struct sockaddr *peer, struct nb_name *np,
826007Sthurlow 	struct sockaddr_nb **dst)
836007Sthurlow 
846007Sthurlow {
856007Sthurlow 	struct sockaddr_nb *snb;
866007Sthurlow 	struct sockaddr_in *sin;
8710023SGordon.Ross@Sun.COM 	int error;
886007Sthurlow 
896007Sthurlow 	if (peer && (peer->sa_family != AF_INET))
906007Sthurlow 		return (EPROTONOSUPPORT);
9110023SGordon.Ross@Sun.COM 	error = nb_snballoc(&snb);
926007Sthurlow 	if (error)
936007Sthurlow 		return (error);
946007Sthurlow 
956007Sthurlow 	if (strcmp(np->nn_name, "*") == 0) {
966007Sthurlow 		/* Star is special: No blanks, type, etc. */
976007Sthurlow 		snb->snb_name[0] = '*';
986007Sthurlow 	} else {
996007Sthurlow 		/* Normal name: pad with blanks, add type. */
1006007Sthurlow 		snprintf(snb->snb_name, NB_NAMELEN,
1016007Sthurlow 		    "%-15.15s", np->nn_name);
1026007Sthurlow 		snb->snb_name[15] = (char)np->nn_type;
1036007Sthurlow 	}
1046007Sthurlow 
1056007Sthurlow 	if (peer) {
1066007Sthurlow 		/*LINTED*/
1076007Sthurlow 		sin = (struct sockaddr_in *)peer;
1086007Sthurlow 		snb->snb_ipaddr = sin->sin_addr.s_addr;
1096007Sthurlow 	}
1106007Sthurlow 	*dst = snb;
1116007Sthurlow 	return (0);
1126007Sthurlow }
1136007Sthurlow 
1146007Sthurlow int
nb_name_len(struct nb_name * np)1156007Sthurlow nb_name_len(struct nb_name *np)
1166007Sthurlow {
1176007Sthurlow 	char *name;
1186007Sthurlow 	int len, sclen;
1196007Sthurlow 
1206007Sthurlow 	len = 1 + NB_ENCNAMELEN;
1216007Sthurlow 	if (np->nn_scope == NULL)
1226007Sthurlow 		return (len + 1);
1236007Sthurlow 	sclen = 0;
1246007Sthurlow 	for (name = np->nn_scope; *name; name++) {
1256007Sthurlow 		if (*name == '.') {
1266007Sthurlow 			sclen = 0;
1276007Sthurlow 		} else {
1286007Sthurlow 			if (sclen < NB_MAXLABLEN) {
1296007Sthurlow 				sclen++;
1306007Sthurlow 				len++;
1316007Sthurlow 			}
1326007Sthurlow 		}
1336007Sthurlow 	}
1346007Sthurlow 	return (len + 1);
1356007Sthurlow }
1366007Sthurlow 
1376007Sthurlow int
nb_encname_len(const uchar_t * str)1386007Sthurlow nb_encname_len(const uchar_t *str)
1396007Sthurlow {
1406007Sthurlow 	const uchar_t *cp = str;
1416007Sthurlow 	int len, blen;
1426007Sthurlow 
1436007Sthurlow 	if ((cp[0] & 0xc0) == 0xc0)
1446007Sthurlow 		return (-1);	/* first two bytes are offset to name */
1456007Sthurlow 
1466007Sthurlow 	len = 1;
1476007Sthurlow 	for (;;) {
1486007Sthurlow 		blen = *cp;
1496007Sthurlow 		if (blen++ == 0)
1506007Sthurlow 			break;
1516007Sthurlow 		len += blen;
1526007Sthurlow 		cp += blen;
1536007Sthurlow 	}
1546007Sthurlow 	return (len);
1556007Sthurlow }
1566007Sthurlow 
1576007Sthurlow int
nb_name_encode(struct mbdata * mbp,struct nb_name * nn)15810023SGordon.Ross@Sun.COM nb_name_encode(struct mbdata *mbp, struct nb_name *nn)
1596007Sthurlow {
16010023SGordon.Ross@Sun.COM 	char *plen;
16110023SGordon.Ross@Sun.COM 	uchar_t ch;
16210023SGordon.Ross@Sun.COM 	char *p, namebuf[NB_NAMELEN+1];
1636007Sthurlow 	int i, lblen;
1646007Sthurlow 
16510023SGordon.Ross@Sun.COM 	bcopy(nn->nn_name, namebuf, NB_NAMELEN);
16610023SGordon.Ross@Sun.COM 	namebuf[NB_NAMELEN-1] = (char)nn->nn_type;
16710023SGordon.Ross@Sun.COM 	namebuf[NB_NAMELEN] = '\0'; /* for debug */
1686007Sthurlow 
1696007Sthurlow 	/*
1706007Sthurlow 	 * Do the NetBIOS "first-level encoding" here.
17110023SGordon.Ross@Sun.COM 	 * (RFC1002 explains this weirdness...)
1726007Sthurlow 	 *
1736007Sthurlow 	 * Here is what we marshall:
1746007Sthurlow 	 *   uint8_t NAME_LENGTH (always 32)
1756007Sthurlow 	 *   uint8_t ENCODED_NAME[32]
1766007Sthurlow 	 *   uint8_t SCOPE_LENGTH
1776007Sthurlow 	 *   Scope follows here, then another null.
1786007Sthurlow 	 */
1796007Sthurlow 
1806007Sthurlow 	/* NAME_LENGTH */
18110023SGordon.Ross@Sun.COM 	mb_put_uint8(mbp, (2 * NB_NAMELEN));
1826007Sthurlow 
1836007Sthurlow 	/* ENCODED_NAME */
1846007Sthurlow 	for (i = 0; i < NB_NAMELEN; i++) {
18510023SGordon.Ross@Sun.COM 		ch = namebuf[i];
18610023SGordon.Ross@Sun.COM 		mb_put_uint8(mbp, 'A' + ((ch >> 4) & 0xF));
18710023SGordon.Ross@Sun.COM 		mb_put_uint8(mbp, 'A' + ((ch) & 0xF));
1886007Sthurlow 	}
1896007Sthurlow 
1906007Sthurlow 	/*
1916007Sthurlow 	 * NetBIOS "scope" sting encoding,
1926007Sthurlow 	 * a.k.a second-level encoding.
1936007Sthurlow 	 * See RFC1002 for the details.
1946007Sthurlow 	 *
1956007Sthurlow 	 * Note: plen points to the length byte at the
1966007Sthurlow 	 * start of each string.  This keeps a pointer
1976007Sthurlow 	 * to the location and fills it in after the
1986007Sthurlow 	 * length of the string is determined.
19910023SGordon.Ross@Sun.COM 	 *
20010023SGordon.Ross@Sun.COM 	 * One string of length zero terminates.
20110023SGordon.Ross@Sun.COM 	 * With no scope string, the zero-length
20210023SGordon.Ross@Sun.COM 	 * string is the only thing there.
2036007Sthurlow 	 */
20410023SGordon.Ross@Sun.COM 	if (nn->nn_scope == NULL) {
20510023SGordon.Ross@Sun.COM 		mb_put_uint8(mbp, 0);
20610023SGordon.Ross@Sun.COM 		return (0);
20710023SGordon.Ross@Sun.COM 	}
20810023SGordon.Ross@Sun.COM 
209*11332SGordon.Ross@Sun.COM 	(void) mb_fit(mbp, 1, &plen);
21010023SGordon.Ross@Sun.COM 	*plen = 0; /* will update below */
21110023SGordon.Ross@Sun.COM 	lblen = 0;
21210023SGordon.Ross@Sun.COM 	for (p = nn->nn_scope; ; p++) {
21310023SGordon.Ross@Sun.COM 		if (*p == '\0') {
21410023SGordon.Ross@Sun.COM 			*plen = lblen;
21510023SGordon.Ross@Sun.COM 			if (lblen)
21610023SGordon.Ross@Sun.COM 				mb_put_uint8(mbp, 0);
21710023SGordon.Ross@Sun.COM 			break;
21810023SGordon.Ross@Sun.COM 		}
21910023SGordon.Ross@Sun.COM 		if (*p == '.') {
22010023SGordon.Ross@Sun.COM 			*plen = lblen;
221*11332SGordon.Ross@Sun.COM 			(void) mb_fit(mbp, 1, &plen);
22210023SGordon.Ross@Sun.COM 			*plen = 0;
22310023SGordon.Ross@Sun.COM 			lblen = 0;
22410023SGordon.Ross@Sun.COM 		} else {
22510023SGordon.Ross@Sun.COM 			if (lblen < NB_MAXLABLEN) {
22610023SGordon.Ross@Sun.COM 				mb_put_uint8(mbp, *p);
22710023SGordon.Ross@Sun.COM 				lblen++;
2286007Sthurlow 			}
2296007Sthurlow 		}
2306007Sthurlow 	}
2316007Sthurlow 
23210023SGordon.Ross@Sun.COM 	return (0);
2336007Sthurlow }
234