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