xref: /freebsd/contrib/smbfs/lib/smb/nb_name.c (revision 2be1a816b9ff69588e55be0a84cbe2a31efc0f2f)
1 /*
2  * Copyright (c) 2000-2001, Boris Popov
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *    This product includes software developed by Boris Popov.
16  * 4. Neither the name of the author nor the names of any co-contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  *
32  * $Id: nb_name.c,v 1.2 2001/08/22 03:31:36 bp Exp $
33  */
34 
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
37 
38 #include <sys/param.h>
39 #include <sys/endian.h>
40 #include <sys/socket.h>
41 
42 #include <ctype.h>
43 #include <err.h>
44 #include <errno.h>
45 #include <stdio.h>
46 #include <stdlib.h>
47 #include <string.h>
48 
49 #include <netsmb/netbios.h>
50 #include <netsmb/smb_lib.h>
51 #include <netsmb/nb_lib.h>
52 
53 int
54 nb_snballoc(int namelen, struct sockaddr_nb **dst)
55 {
56 	struct sockaddr_nb *snb;
57 	int slen;
58 
59 	slen = namelen + sizeof(*snb) - sizeof(snb->snb_name);
60 	snb = malloc(slen);
61 	if (snb == NULL)
62 		return ENOMEM;
63 	bzero(snb, slen);
64 	snb->snb_family = AF_NETBIOS;
65 	snb->snb_len = slen;
66 	*dst = snb;
67 	return 0;
68 }
69 
70 void
71 nb_snbfree(struct sockaddr *snb)
72 {
73 	free(snb);
74 }
75 
76 /*
77  * Create a full NETBIOS address
78  */
79 int
80 nb_sockaddr(struct sockaddr *peer, struct nb_name *np,
81 	struct sockaddr_nb **dst)
82 
83 {
84 	struct sockaddr_nb *snb;
85 	int nmlen, error;
86 
87 	if (peer && (peer->sa_family != AF_INET && peer->sa_family != AF_IPX))
88 		return EPROTONOSUPPORT;
89 	nmlen = nb_name_len(np);
90 	if (nmlen < NB_ENCNAMELEN)
91 		return EINVAL;
92 	error = nb_snballoc(nmlen, &snb);
93 	if (error)
94 		return error;
95 	if (nmlen != nb_name_encode(np, snb->snb_name))
96 		printf("a bug somewhere in the nb_name* code\n");
97 	if (peer)
98 		memcpy(&snb->snb_tran, peer, peer->sa_len);
99 	*dst = snb;
100 	return 0;
101 }
102 
103 int
104 nb_name_len(struct nb_name *np)
105 {
106 	u_char *name;
107 	int len, sclen;
108 
109 	len = 1 + NB_ENCNAMELEN;
110 	if (np->nn_scope == NULL)
111 		return len + 1;
112 	sclen = 0;
113 	for (name = np->nn_scope; *name; name++) {
114 		if (*name == '.') {
115 			sclen = 0;
116 		} else {
117 			if (sclen < NB_MAXLABLEN) {
118 				sclen++;
119 				len++;
120 			}
121 		}
122 	}
123 	return len + 1;
124 }
125 
126 int
127 nb_encname_len(const char *str)
128 {
129 	const u_char *cp = (const u_char *)str;
130 	int len, blen;
131 
132 	if ((cp[0] & 0xc0) == 0xc0)
133 		return -1;	/* first two bytes are offset to name */
134 
135 	len = 1;
136 	for (;;) {
137 		blen = *cp;
138 		if (blen++ == 0)
139 			break;
140 		len += blen;
141 		cp += blen;
142 	}
143 	return len;
144 }
145 
146 #define	NBENCODE(c)	(htole16((u_short)(((u_char)(c) >> 4) | \
147 			 (((u_char)(c) & 0xf) << 8)) + 0x4141))
148 
149 static void
150 memsetw(char *dst, int n, u_short word)
151 {
152 	while (n--) {
153 		*(u_short*)dst = word;
154 		dst += 2;
155 	}
156 }
157 
158 int
159 nb_name_encode(struct nb_name *np, u_char *dst)
160 {
161 	u_char *name, *plen;
162 	u_char *cp = dst;
163 	int i, lblen;
164 
165 	*cp++ = NB_ENCNAMELEN;
166 	name = np->nn_name;
167 	if (name[0] == '*' && name[1] == 0) {
168 		*(u_short*)cp = NBENCODE('*');
169 		memsetw(cp + 2, NB_NAMELEN - 1, NBENCODE(' '));
170 		cp += NB_ENCNAMELEN;
171 	} else {
172 		for (i = 0; *name && i < NB_NAMELEN; i++, cp += 2, name++)
173 			*(u_short*)cp = NBENCODE(toupper(*name));
174 		i = NB_NAMELEN - i - 1;
175 		if (i > 0) {
176 			memsetw(cp, i, NBENCODE(' '));
177 			cp += i * 2;
178 		}
179 		*(u_short*)cp = NBENCODE(np->nn_type);
180 		cp += 2;
181 	}
182 	*cp = 0;
183 	if (np->nn_scope == NULL)
184 		return nb_encname_len(dst);
185 	plen = cp++;
186 	lblen = 0;
187 	for (name = np->nn_scope; ; name++) {
188 		if (*name == '.' || *name == 0) {
189 			*plen = lblen;
190 			plen = cp++;
191 			*plen = 0;
192 			if (*name == 0)
193 				break;
194 		} else {
195 			if (lblen < NB_MAXLABLEN) {
196 				*cp++ = *name;
197 				lblen++;
198 			}
199 		}
200 	}
201 	return nb_encname_len(dst);
202 }
203 
204