1*86d7f5d3SJohn Marino /*
2*86d7f5d3SJohn Marino * Copyright (c) 2000, 2001 Boris Popov
3*86d7f5d3SJohn Marino * All rights reserved.
4*86d7f5d3SJohn Marino *
5*86d7f5d3SJohn Marino * Redistribution and use in source and binary forms, with or without
6*86d7f5d3SJohn Marino * modification, are permitted provided that the following conditions
7*86d7f5d3SJohn Marino * are met:
8*86d7f5d3SJohn Marino * 1. Redistributions of source code must retain the above copyright
9*86d7f5d3SJohn Marino * notice, this list of conditions and the following disclaimer.
10*86d7f5d3SJohn Marino * 2. Redistributions in binary form must reproduce the above copyright
11*86d7f5d3SJohn Marino * notice, this list of conditions and the following disclaimer in the
12*86d7f5d3SJohn Marino * documentation and/or other materials provided with the distribution.
13*86d7f5d3SJohn Marino * 3. All advertising materials mentioning features or use of this software
14*86d7f5d3SJohn Marino * must display the following acknowledgement:
15*86d7f5d3SJohn Marino * This product includes software developed by Boris Popov.
16*86d7f5d3SJohn Marino * 4. Neither the name of the author nor the names of any co-contributors
17*86d7f5d3SJohn Marino * may be used to endorse or promote products derived from this software
18*86d7f5d3SJohn Marino * without specific prior written permission.
19*86d7f5d3SJohn Marino *
20*86d7f5d3SJohn Marino * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21*86d7f5d3SJohn Marino * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22*86d7f5d3SJohn Marino * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23*86d7f5d3SJohn Marino * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24*86d7f5d3SJohn Marino * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25*86d7f5d3SJohn Marino * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26*86d7f5d3SJohn Marino * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27*86d7f5d3SJohn Marino * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28*86d7f5d3SJohn Marino * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29*86d7f5d3SJohn Marino * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30*86d7f5d3SJohn Marino * SUCH DAMAGE.
31*86d7f5d3SJohn Marino *
32*86d7f5d3SJohn Marino * $Id: nb.c,v 1.4 2001/04/16 04:33:01 bp Exp $
33*86d7f5d3SJohn Marino */
34*86d7f5d3SJohn Marino #include <sys/param.h>
35*86d7f5d3SJohn Marino #include <sys/socket.h>
36*86d7f5d3SJohn Marino
37*86d7f5d3SJohn Marino #include <ctype.h>
38*86d7f5d3SJohn Marino #include <netdb.h>
39*86d7f5d3SJohn Marino #include <err.h>
40*86d7f5d3SJohn Marino #include <errno.h>
41*86d7f5d3SJohn Marino #include <stdlib.h>
42*86d7f5d3SJohn Marino #include <string.h>
43*86d7f5d3SJohn Marino #include <stdio.h>
44*86d7f5d3SJohn Marino #include <unistd.h>
45*86d7f5d3SJohn Marino #include <cflib.h>
46*86d7f5d3SJohn Marino
47*86d7f5d3SJohn Marino #include <netsmb/netbios.h>
48*86d7f5d3SJohn Marino #include <netsmb/smb_lib.h>
49*86d7f5d3SJohn Marino #include <netsmb/nb_lib.h>
50*86d7f5d3SJohn Marino
51*86d7f5d3SJohn Marino int
nb_ctx_create(struct nb_ctx ** ctxpp)52*86d7f5d3SJohn Marino nb_ctx_create(struct nb_ctx **ctxpp)
53*86d7f5d3SJohn Marino {
54*86d7f5d3SJohn Marino struct nb_ctx *ctx;
55*86d7f5d3SJohn Marino
56*86d7f5d3SJohn Marino ctx = malloc(sizeof(struct nb_ctx));
57*86d7f5d3SJohn Marino if (ctx == NULL)
58*86d7f5d3SJohn Marino return ENOMEM;
59*86d7f5d3SJohn Marino bzero(ctx, sizeof(struct nb_ctx));
60*86d7f5d3SJohn Marino *ctxpp = ctx;
61*86d7f5d3SJohn Marino return 0;
62*86d7f5d3SJohn Marino }
63*86d7f5d3SJohn Marino
64*86d7f5d3SJohn Marino void
nb_ctx_done(struct nb_ctx * ctx)65*86d7f5d3SJohn Marino nb_ctx_done(struct nb_ctx *ctx)
66*86d7f5d3SJohn Marino {
67*86d7f5d3SJohn Marino if (ctx == NULL)
68*86d7f5d3SJohn Marino return;
69*86d7f5d3SJohn Marino if (ctx->nb_scope)
70*86d7f5d3SJohn Marino free(ctx->nb_scope);
71*86d7f5d3SJohn Marino }
72*86d7f5d3SJohn Marino
73*86d7f5d3SJohn Marino int
nb_ctx_setns(struct nb_ctx * ctx,const char * addr)74*86d7f5d3SJohn Marino nb_ctx_setns(struct nb_ctx *ctx, const char *addr)
75*86d7f5d3SJohn Marino {
76*86d7f5d3SJohn Marino if (addr == NULL || addr[0] == 0)
77*86d7f5d3SJohn Marino return EINVAL;
78*86d7f5d3SJohn Marino if (ctx->nb_nsname)
79*86d7f5d3SJohn Marino free(ctx->nb_nsname);
80*86d7f5d3SJohn Marino if ((ctx->nb_nsname = strdup(addr)) == NULL)
81*86d7f5d3SJohn Marino return ENOMEM;
82*86d7f5d3SJohn Marino return 0;
83*86d7f5d3SJohn Marino }
84*86d7f5d3SJohn Marino
85*86d7f5d3SJohn Marino int
nb_ctx_setscope(struct nb_ctx * ctx,const char * scope)86*86d7f5d3SJohn Marino nb_ctx_setscope(struct nb_ctx *ctx, const char *scope)
87*86d7f5d3SJohn Marino {
88*86d7f5d3SJohn Marino size_t slen = strlen(scope);
89*86d7f5d3SJohn Marino
90*86d7f5d3SJohn Marino if (slen >= 128) {
91*86d7f5d3SJohn Marino smb_error("scope '%s' is too long", 0, scope);
92*86d7f5d3SJohn Marino return ENAMETOOLONG;
93*86d7f5d3SJohn Marino }
94*86d7f5d3SJohn Marino if (ctx->nb_scope)
95*86d7f5d3SJohn Marino free(ctx->nb_scope);
96*86d7f5d3SJohn Marino ctx->nb_scope = malloc(slen + 1);
97*86d7f5d3SJohn Marino if (ctx->nb_scope == NULL)
98*86d7f5d3SJohn Marino return ENOMEM;
99*86d7f5d3SJohn Marino nls_str_upper(ctx->nb_scope, scope);
100*86d7f5d3SJohn Marino return 0;
101*86d7f5d3SJohn Marino }
102*86d7f5d3SJohn Marino
103*86d7f5d3SJohn Marino int
nb_ctx_resolve(struct nb_ctx * ctx)104*86d7f5d3SJohn Marino nb_ctx_resolve(struct nb_ctx *ctx)
105*86d7f5d3SJohn Marino {
106*86d7f5d3SJohn Marino struct sockaddr *sap;
107*86d7f5d3SJohn Marino int error;
108*86d7f5d3SJohn Marino
109*86d7f5d3SJohn Marino ctx->nb_flags &= ~NBCF_RESOLVED;
110*86d7f5d3SJohn Marino
111*86d7f5d3SJohn Marino if (ctx->nb_nsname == NULL) {
112*86d7f5d3SJohn Marino ctx->nb_ns.sin_addr.s_addr = htonl(INADDR_BROADCAST);
113*86d7f5d3SJohn Marino } else {
114*86d7f5d3SJohn Marino error = nb_resolvehost_in(ctx->nb_nsname, &sap);
115*86d7f5d3SJohn Marino if (error) {
116*86d7f5d3SJohn Marino smb_error("can't resolve %s", error, ctx->nb_nsname);
117*86d7f5d3SJohn Marino return error;
118*86d7f5d3SJohn Marino }
119*86d7f5d3SJohn Marino if (sap->sa_family != AF_INET) {
120*86d7f5d3SJohn Marino smb_error("unsupported address family %d", 0, sap->sa_family);
121*86d7f5d3SJohn Marino return EINVAL;
122*86d7f5d3SJohn Marino }
123*86d7f5d3SJohn Marino bcopy(sap, &ctx->nb_ns, sizeof(ctx->nb_ns));
124*86d7f5d3SJohn Marino free(sap);
125*86d7f5d3SJohn Marino }
126*86d7f5d3SJohn Marino ctx->nb_ns.sin_port = htons(137);
127*86d7f5d3SJohn Marino ctx->nb_ns.sin_family = AF_INET;
128*86d7f5d3SJohn Marino ctx->nb_ns.sin_len = sizeof(ctx->nb_ns);
129*86d7f5d3SJohn Marino ctx->nb_flags |= NBCF_RESOLVED;
130*86d7f5d3SJohn Marino return 0;
131*86d7f5d3SJohn Marino }
132*86d7f5d3SJohn Marino
133*86d7f5d3SJohn Marino /*
134*86d7f5d3SJohn Marino * used level values:
135*86d7f5d3SJohn Marino * 0 - default
136*86d7f5d3SJohn Marino * 1 - server
137*86d7f5d3SJohn Marino */
138*86d7f5d3SJohn Marino int
nb_ctx_readrcsection(struct rcfile * rcfile,struct nb_ctx * ctx,const char * sname,int level)139*86d7f5d3SJohn Marino nb_ctx_readrcsection(struct rcfile *rcfile, struct nb_ctx *ctx,
140*86d7f5d3SJohn Marino const char *sname, int level)
141*86d7f5d3SJohn Marino {
142*86d7f5d3SJohn Marino char *p;
143*86d7f5d3SJohn Marino int error;
144*86d7f5d3SJohn Marino
145*86d7f5d3SJohn Marino if (level > 1)
146*86d7f5d3SJohn Marino return EINVAL;
147*86d7f5d3SJohn Marino rc_getint(rcfile, sname, "nbtimeout", &ctx->nb_timo);
148*86d7f5d3SJohn Marino rc_getstringptr(rcfile, sname, "nbns", &p);
149*86d7f5d3SJohn Marino if (p) {
150*86d7f5d3SJohn Marino error = nb_ctx_setns(ctx, p);
151*86d7f5d3SJohn Marino if (error) {
152*86d7f5d3SJohn Marino smb_error("invalid address specified in the section %s", 0, sname);
153*86d7f5d3SJohn Marino return error;
154*86d7f5d3SJohn Marino }
155*86d7f5d3SJohn Marino }
156*86d7f5d3SJohn Marino rc_getstringptr(rcfile, sname, "nbscope", &p);
157*86d7f5d3SJohn Marino if (p)
158*86d7f5d3SJohn Marino nb_ctx_setscope(ctx, p);
159*86d7f5d3SJohn Marino return 0;
160*86d7f5d3SJohn Marino }
161*86d7f5d3SJohn Marino
162*86d7f5d3SJohn Marino static const char *nb_err_rcode[] = {
163*86d7f5d3SJohn Marino "bad request/response format",
164*86d7f5d3SJohn Marino "NBNS server failure",
165*86d7f5d3SJohn Marino "no such name",
166*86d7f5d3SJohn Marino "unsupported request",
167*86d7f5d3SJohn Marino "request rejected",
168*86d7f5d3SJohn Marino "name already registered"
169*86d7f5d3SJohn Marino };
170*86d7f5d3SJohn Marino
171*86d7f5d3SJohn Marino static const char *nb_err[] = {
172*86d7f5d3SJohn Marino "host not found",
173*86d7f5d3SJohn Marino "too many redirects",
174*86d7f5d3SJohn Marino "invalid response",
175*86d7f5d3SJohn Marino "NETBIOS name too long",
176*86d7f5d3SJohn Marino "no interface to broadcast on and no NBNS server specified"
177*86d7f5d3SJohn Marino };
178*86d7f5d3SJohn Marino
179*86d7f5d3SJohn Marino const char *
nb_strerror(int error)180*86d7f5d3SJohn Marino nb_strerror(int error)
181*86d7f5d3SJohn Marino {
182*86d7f5d3SJohn Marino if (error == 0)
183*86d7f5d3SJohn Marino return NULL;
184*86d7f5d3SJohn Marino if (error <= NBERR_ACTIVE)
185*86d7f5d3SJohn Marino return nb_err_rcode[error - 1];
186*86d7f5d3SJohn Marino else if (error >= NBERR_HOSTNOTFOUND && error < NBERR_MAX)
187*86d7f5d3SJohn Marino return nb_err[error - NBERR_HOSTNOTFOUND];
188*86d7f5d3SJohn Marino else
189*86d7f5d3SJohn Marino return NULL;
190*86d7f5d3SJohn Marino }
191*86d7f5d3SJohn Marino
192