xref: /dragonfly/contrib/smbfs/lib/smb/nb_name.c (revision 0ce716ae28bcdb9d17311fa9884267ad78c27dc6)
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 #include <sys/param.h>
35 #include <sys/socket.h>
36 #include <sys/endian.h>
37 
38 #include <ctype.h>
39 #include <err.h>
40 #include <errno.h>
41 #include <stdio.h>
42 #include <stdlib.h>
43 #include <string.h>
44 
45 #include <netsmb/netbios.h>
46 #include <netsmb/smb_lib.h>
47 #include <netsmb/nb_lib.h>
48 
49 int
nb_snballoc(int namelen,struct sockaddr_nb ** dst)50 nb_snballoc(int namelen, struct sockaddr_nb **dst)
51 {
52           struct sockaddr_nb *snb;
53           int slen;
54 
55           slen = namelen + sizeof(*snb) - sizeof(snb->snb_name);
56           snb = malloc(slen);
57           if (snb == NULL)
58                     return ENOMEM;
59           bzero(snb, slen);
60           snb->snb_family = AF_NETBIOS;
61           snb->snb_len = slen;
62           *dst = snb;
63           return 0;
64 }
65 
66 void
nb_snbfree(struct sockaddr * snb)67 nb_snbfree(struct sockaddr *snb)
68 {
69           free(snb);
70 }
71 
72 /*
73  * Create a full NETBIOS address
74  */
75 int
nb_sockaddr(struct sockaddr * peer,struct nb_name * np,struct sockaddr_nb ** dst)76 nb_sockaddr(struct sockaddr *peer, struct nb_name *np,
77           struct sockaddr_nb **dst)
78 
79 {
80           struct sockaddr_nb *snb;
81           int nmlen, error;
82 
83           if (peer && (peer->sa_family != AF_INET && peer->sa_family != AF_IPX))
84                     return EPROTONOSUPPORT;
85           nmlen = nb_name_len(np);
86           if (nmlen < NB_ENCNAMELEN)
87                     return EINVAL;
88           error = nb_snballoc(nmlen, &snb);
89           if (error)
90                     return error;
91           if (nmlen != nb_name_encode(np, snb->snb_name))
92                     printf("a bug somewhere in the nb_name* code\n");
93           if (peer)
94                     memcpy(&snb->snb_tran, peer, peer->sa_len);
95           *dst = snb;
96           return 0;
97 }
98 
99 int
nb_name_len(struct nb_name * np)100 nb_name_len(struct nb_name *np)
101 {
102           u_char *name;
103           int len, sclen;
104 
105           len = 1 + NB_ENCNAMELEN;
106           if (np->nn_scope == NULL)
107                     return len + 1;
108           sclen = 0;
109           for (name = np->nn_scope; *name; name++) {
110                     if (*name == '.') {
111                               sclen = 0;
112                     } else {
113                               if (sclen < NB_MAXLABLEN) {
114                                         sclen++;
115                                         len++;
116                               }
117                     }
118           }
119           return len + 1;
120 }
121 
122 int
nb_encname_len(const char * str)123 nb_encname_len(const char *str)
124 {
125           const u_char *cp = (const u_char *)str;
126           int len, blen;
127 
128           if ((cp[0] & 0xc0) == 0xc0)
129                     return -1;          /* first two bytes are offset to name */
130 
131           len = 1;
132           for (;;) {
133                     blen = *cp;
134                     if (blen++ == 0)
135                               break;
136                     len += blen;
137                     cp += blen;
138           }
139           return len;
140 }
141 
142 #define   NBENCODE(c)         (htole16((u_short)(((u_char)(c) >> 4) | \
143                                (((u_char)(c) & 0xf) << 8)) + 0x4141))
144 
145 static void
memsetw(char * dst,int n,u_short word)146 memsetw(char *dst, int n, u_short word)
147 {
148           while (n--) {
149                     *(u_short*)dst = word;
150                     dst += 2;
151           }
152 }
153 
154 int
nb_name_encode(struct nb_name * np,u_char * dst)155 nb_name_encode(struct nb_name *np, u_char *dst)
156 {
157           u_char *name, *plen;
158           u_char *cp = dst;
159           int i, lblen;
160 
161           *cp++ = NB_ENCNAMELEN;
162           name = np->nn_name;
163           if (name[0] == '*' && name[1] == 0) {
164                     *(u_short*)cp = NBENCODE('*');
165                     memsetw(cp + 2, NB_NAMELEN - 1, NBENCODE(' '));
166                     cp += NB_ENCNAMELEN;
167           } else {
168                     for (i = 0; *name && i < NB_NAMELEN; i++, cp += 2, name++)
169                               *(u_short*)cp = NBENCODE(toupper(*name));
170                     i = NB_NAMELEN - i - 1;
171                     if (i > 0) {
172                               memsetw(cp, i, NBENCODE(' '));
173                               cp += i * 2;
174                     }
175                     *(u_short*)cp = NBENCODE(np->nn_type);
176                     cp += 2;
177           }
178           *cp = 0;
179           if (np->nn_scope == NULL)
180                     return nb_encname_len(dst);
181           plen = cp++;
182           lblen = 0;
183           for (name = np->nn_scope; ; name++) {
184                     if (*name == '.' || *name == 0) {
185                               *plen = lblen;
186                               plen = cp++;
187                               *plen = 0;
188                               if (*name == 0)
189                                         break;
190                     } else {
191                               if (lblen < NB_MAXLABLEN) {
192                                         *cp++ = *name;
193                                         lblen++;
194                               }
195                     }
196           }
197           return nb_encname_len(dst);
198 }
199 
200