14bff34e3Sthurlow /*
24bff34e3Sthurlow * Copyright (c) 2000, Boris Popov
34bff34e3Sthurlow * All rights reserved.
44bff34e3Sthurlow *
54bff34e3Sthurlow * Redistribution and use in source and binary forms, with or without
64bff34e3Sthurlow * modification, are permitted provided that the following conditions
74bff34e3Sthurlow * are met:
84bff34e3Sthurlow * 1. Redistributions of source code must retain the above copyright
94bff34e3Sthurlow * notice, this list of conditions and the following disclaimer.
104bff34e3Sthurlow * 2. Redistributions in binary form must reproduce the above copyright
114bff34e3Sthurlow * notice, this list of conditions and the following disclaimer in the
124bff34e3Sthurlow * documentation and/or other materials provided with the distribution.
134bff34e3Sthurlow * 3. All advertising materials mentioning features or use of this software
144bff34e3Sthurlow * must display the following acknowledgement:
154bff34e3Sthurlow * This product includes software developed by Boris Popov.
164bff34e3Sthurlow * 4. Neither the name of the author nor the names of any co-contributors
174bff34e3Sthurlow * may be used to endorse or promote products derived from this software
184bff34e3Sthurlow * without specific prior written permission.
194bff34e3Sthurlow *
204bff34e3Sthurlow * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
214bff34e3Sthurlow * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
224bff34e3Sthurlow * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
234bff34e3Sthurlow * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
244bff34e3Sthurlow * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
254bff34e3Sthurlow * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
264bff34e3Sthurlow * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
274bff34e3Sthurlow * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
284bff34e3Sthurlow * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
294bff34e3Sthurlow * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
304bff34e3Sthurlow * SUCH DAMAGE.
314bff34e3Sthurlow *
324bff34e3Sthurlow * $Id: nb_name.c,v 1.11 2004/12/11 05:23:59 lindak Exp $
334bff34e3Sthurlow */
344bff34e3Sthurlow
354bff34e3Sthurlow #include <sys/param.h>
364bff34e3Sthurlow #include <sys/socket.h>
374bff34e3Sthurlow #include <ctype.h>
384bff34e3Sthurlow #include <errno.h>
394bff34e3Sthurlow #include <stdio.h>
404bff34e3Sthurlow #include <stdlib.h>
414bff34e3Sthurlow #include <string.h>
424bff34e3Sthurlow #include <strings.h>
434bff34e3Sthurlow #include <libintl.h>
444bff34e3Sthurlow #include <assert.h>
454bff34e3Sthurlow
464bff34e3Sthurlow #include <netsmb/netbios.h>
474bff34e3Sthurlow #include <netsmb/smb_lib.h>
484bff34e3Sthurlow #include <netsmb/nb_lib.h>
494bff34e3Sthurlow #include <netsmb/mchain.h>
509c9af259SGordon Ross #include "private.h"
514bff34e3Sthurlow
524bff34e3Sthurlow int
nb_snballoc(struct sockaddr_nb ** dst)53613a2f6bSGordon Ross nb_snballoc(struct sockaddr_nb **dst)
544bff34e3Sthurlow {
554bff34e3Sthurlow struct sockaddr_nb *snb;
564bff34e3Sthurlow int slen;
574bff34e3Sthurlow
584bff34e3Sthurlow slen = sizeof (struct sockaddr_nb);
594bff34e3Sthurlow snb = malloc(slen);
604bff34e3Sthurlow if (snb == NULL)
614bff34e3Sthurlow return (ENOMEM);
624bff34e3Sthurlow bzero(snb, slen);
634bff34e3Sthurlow snb->snb_family = AF_NETBIOS;
644bff34e3Sthurlow *dst = snb;
654bff34e3Sthurlow return (0);
664bff34e3Sthurlow }
674bff34e3Sthurlow
684bff34e3Sthurlow void
nb_snbfree(struct sockaddr * snb)694bff34e3Sthurlow nb_snbfree(struct sockaddr *snb)
704bff34e3Sthurlow {
714bff34e3Sthurlow free(snb);
724bff34e3Sthurlow }
734bff34e3Sthurlow
744bff34e3Sthurlow /*
754bff34e3Sthurlow * Create a full NETBIOS address
76613a2f6bSGordon Ross * Passed names should already be upper case.
77613a2f6bSGordon Ross * Stores the names truncated or blank padded.
78613a2f6bSGordon Ross * NetBIOS name encoding happens later.
794bff34e3Sthurlow */
804bff34e3Sthurlow int
nb_sockaddr(struct sockaddr * peer,struct nb_name * np,struct sockaddr_nb ** dst)814bff34e3Sthurlow nb_sockaddr(struct sockaddr *peer, struct nb_name *np,
824bff34e3Sthurlow struct sockaddr_nb **dst)
834bff34e3Sthurlow
844bff34e3Sthurlow {
854bff34e3Sthurlow struct sockaddr_nb *snb;
864bff34e3Sthurlow struct sockaddr_in *sin;
87613a2f6bSGordon Ross int error;
884bff34e3Sthurlow
894bff34e3Sthurlow if (peer && (peer->sa_family != AF_INET))
904bff34e3Sthurlow return (EPROTONOSUPPORT);
91613a2f6bSGordon Ross error = nb_snballoc(&snb);
924bff34e3Sthurlow if (error)
934bff34e3Sthurlow return (error);
944bff34e3Sthurlow
954bff34e3Sthurlow if (strcmp(np->nn_name, "*") == 0) {
964bff34e3Sthurlow /* Star is special: No blanks, type, etc. */
974bff34e3Sthurlow snb->snb_name[0] = '*';
984bff34e3Sthurlow } else {
994bff34e3Sthurlow /* Normal name: pad with blanks, add type. */
1004bff34e3Sthurlow snprintf(snb->snb_name, NB_NAMELEN,
1014bff34e3Sthurlow "%-15.15s", np->nn_name);
1024bff34e3Sthurlow snb->snb_name[15] = (char)np->nn_type;
1034bff34e3Sthurlow }
1044bff34e3Sthurlow
1054bff34e3Sthurlow if (peer) {
1064bff34e3Sthurlow /*LINTED*/
1074bff34e3Sthurlow sin = (struct sockaddr_in *)peer;
1084bff34e3Sthurlow snb->snb_ipaddr = sin->sin_addr.s_addr;
1094bff34e3Sthurlow }
1104bff34e3Sthurlow *dst = snb;
1114bff34e3Sthurlow return (0);
1124bff34e3Sthurlow }
1134bff34e3Sthurlow
1144bff34e3Sthurlow int
nb_name_len(struct nb_name * np)1154bff34e3Sthurlow nb_name_len(struct nb_name *np)
1164bff34e3Sthurlow {
1174bff34e3Sthurlow char *name;
1184bff34e3Sthurlow int len, sclen;
1194bff34e3Sthurlow
1204bff34e3Sthurlow len = 1 + NB_ENCNAMELEN;
1214bff34e3Sthurlow if (np->nn_scope == NULL)
1224bff34e3Sthurlow return (len + 1);
1234bff34e3Sthurlow sclen = 0;
1244bff34e3Sthurlow for (name = np->nn_scope; *name; name++) {
1254bff34e3Sthurlow if (*name == '.') {
1264bff34e3Sthurlow sclen = 0;
1274bff34e3Sthurlow } else {
1284bff34e3Sthurlow if (sclen < NB_MAXLABLEN) {
1294bff34e3Sthurlow sclen++;
1304bff34e3Sthurlow len++;
1314bff34e3Sthurlow }
1324bff34e3Sthurlow }
1334bff34e3Sthurlow }
1344bff34e3Sthurlow return (len + 1);
1354bff34e3Sthurlow }
1364bff34e3Sthurlow
1374bff34e3Sthurlow int
nb_encname_len(const uchar_t * str)1384bff34e3Sthurlow nb_encname_len(const uchar_t *str)
1394bff34e3Sthurlow {
1404bff34e3Sthurlow const uchar_t *cp = str;
1414bff34e3Sthurlow int len, blen;
1424bff34e3Sthurlow
1434bff34e3Sthurlow if ((cp[0] & 0xc0) == 0xc0)
1444bff34e3Sthurlow return (-1); /* first two bytes are offset to name */
1454bff34e3Sthurlow
1464bff34e3Sthurlow len = 1;
1474bff34e3Sthurlow for (;;) {
1484bff34e3Sthurlow blen = *cp;
1494bff34e3Sthurlow if (blen++ == 0)
1504bff34e3Sthurlow break;
1514bff34e3Sthurlow len += blen;
1524bff34e3Sthurlow cp += blen;
1534bff34e3Sthurlow }
1544bff34e3Sthurlow return (len);
1554bff34e3Sthurlow }
1564bff34e3Sthurlow
1574bff34e3Sthurlow int
nb_name_encode(struct mbdata * mbp,struct nb_name * nn)158613a2f6bSGordon Ross nb_name_encode(struct mbdata *mbp, struct nb_name *nn)
1594bff34e3Sthurlow {
160613a2f6bSGordon Ross char *plen;
161613a2f6bSGordon Ross uchar_t ch;
162613a2f6bSGordon Ross char *p, namebuf[NB_NAMELEN+1];
1634bff34e3Sthurlow int i, lblen;
1644bff34e3Sthurlow
165613a2f6bSGordon Ross bcopy(nn->nn_name, namebuf, NB_NAMELEN);
166613a2f6bSGordon Ross namebuf[NB_NAMELEN-1] = (char)nn->nn_type;
167613a2f6bSGordon Ross namebuf[NB_NAMELEN] = '\0'; /* for debug */
1684bff34e3Sthurlow
1694bff34e3Sthurlow /*
1704bff34e3Sthurlow * Do the NetBIOS "first-level encoding" here.
171613a2f6bSGordon Ross * (RFC1002 explains this weirdness...)
1724bff34e3Sthurlow *
1734bff34e3Sthurlow * Here is what we marshall:
1744bff34e3Sthurlow * uint8_t NAME_LENGTH (always 32)
1754bff34e3Sthurlow * uint8_t ENCODED_NAME[32]
1764bff34e3Sthurlow * uint8_t SCOPE_LENGTH
1774bff34e3Sthurlow * Scope follows here, then another null.
1784bff34e3Sthurlow */
1794bff34e3Sthurlow
1804bff34e3Sthurlow /* NAME_LENGTH */
181613a2f6bSGordon Ross mb_put_uint8(mbp, (2 * NB_NAMELEN));
1824bff34e3Sthurlow
1834bff34e3Sthurlow /* ENCODED_NAME */
1844bff34e3Sthurlow for (i = 0; i < NB_NAMELEN; i++) {
185613a2f6bSGordon Ross ch = namebuf[i];
186613a2f6bSGordon Ross mb_put_uint8(mbp, 'A' + ((ch >> 4) & 0xF));
187613a2f6bSGordon Ross mb_put_uint8(mbp, 'A' + ((ch) & 0xF));
1884bff34e3Sthurlow }
1894bff34e3Sthurlow
1904bff34e3Sthurlow /*
1914bff34e3Sthurlow * NetBIOS "scope" sting encoding,
1924bff34e3Sthurlow * a.k.a second-level encoding.
1934bff34e3Sthurlow * See RFC1002 for the details.
1944bff34e3Sthurlow *
1954bff34e3Sthurlow * Note: plen points to the length byte at the
1964bff34e3Sthurlow * start of each string. This keeps a pointer
1974bff34e3Sthurlow * to the location and fills it in after the
1984bff34e3Sthurlow * length of the string is determined.
199613a2f6bSGordon Ross *
200613a2f6bSGordon Ross * One string of length zero terminates.
201613a2f6bSGordon Ross * With no scope string, the zero-length
202613a2f6bSGordon Ross * string is the only thing there.
2034bff34e3Sthurlow */
204613a2f6bSGordon Ross if (nn->nn_scope == NULL) {
205613a2f6bSGordon Ross mb_put_uint8(mbp, 0);
206613a2f6bSGordon Ross return (0);
207613a2f6bSGordon Ross }
208613a2f6bSGordon Ross
209*02d09e03SGordon Ross (void) mb_fit(mbp, 1, &plen);
210613a2f6bSGordon Ross *plen = 0; /* will update below */
2114bff34e3Sthurlow lblen = 0;
212613a2f6bSGordon Ross for (p = nn->nn_scope; ; p++) {
213613a2f6bSGordon Ross if (*p == '\0') {
2144bff34e3Sthurlow *plen = lblen;
215613a2f6bSGordon Ross if (lblen)
216613a2f6bSGordon Ross mb_put_uint8(mbp, 0);
2174bff34e3Sthurlow break;
218613a2f6bSGordon Ross }
219613a2f6bSGordon Ross if (*p == '.') {
220613a2f6bSGordon Ross *plen = lblen;
221*02d09e03SGordon Ross (void) mb_fit(mbp, 1, &plen);
2224bff34e3Sthurlow *plen = 0;
2234bff34e3Sthurlow lblen = 0;
2244bff34e3Sthurlow } else {
2254bff34e3Sthurlow if (lblen < NB_MAXLABLEN) {
226613a2f6bSGordon Ross mb_put_uint8(mbp, *p);
2274bff34e3Sthurlow lblen++;
2284bff34e3Sthurlow }
2294bff34e3Sthurlow }
2304bff34e3Sthurlow }
2314bff34e3Sthurlow
232613a2f6bSGordon Ross return (0);
2334bff34e3Sthurlow }
234