smb_lib.h revision 87866
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: smb_lib.h,v 1.22 2001/04/10 05:37:22 bp Exp $
33 */
34#ifndef _NETSMB_SMB_LIB_H_
35#define _NETSMB_SMB_LIB_H_
36
37#include <netsmb/smb.h>
38#include <netsmb/smb_dev.h>
39
40#define	SMB_CFG_FILE	"/usr/local/etc/nsmb.conf"
41
42#define	STDPARAM_ARGS	'A':case 'B':case 'C':case 'E':case 'I': \
43		   case 'L':case 'M': \
44		   case 'N':case 'U':case 'R':case 'S':case 'T': \
45		   case 'W':case 'O':case 'P'
46
47#define STDPARAM_OPT	"A:BCE:I:L:M:NO:P:U:R:S:T:W:"
48
49/*
50 * bits to indicate the source of error
51 */
52#define	SMB_ERRTYPE_MASK	0xf0000
53#define	SMB_SYS_ERROR		0x00000
54#define SMB_RAP_ERROR		0x10000
55#define SMB_NB_ERROR		0x20000
56
57#ifndef min
58#define	min(a,b)	(((a)<(b)) ? (a) : (b))
59#endif
60
61#define getb(buf,ofs) 		(((const u_int8_t *)(buf))[ofs])
62#define setb(buf,ofs,val)	(((u_int8_t*)(buf))[ofs])=val
63#define getbw(buf,ofs)		((u_int16_t)(getb(buf,ofs)))
64#define getw(buf,ofs)		(*((u_int16_t*)(&((u_int8_t*)(buf))[ofs])))
65#define getdw(buf,ofs)		(*((u_int32_t*)(&((u_int8_t*)(buf))[ofs])))
66
67#if (BYTE_ORDER == LITTLE_ENDIAN)
68
69#define getwle(buf,ofs)	(*((u_int16_t*)(&((u_int8_t*)(buf))[ofs])))
70#define getdle(buf,ofs)	(*((u_int32_t*)(&((u_int8_t*)(buf))[ofs])))
71#define getwbe(buf,ofs)	(ntohs(getwle(buf,ofs)))
72#define getdbe(buf,ofs)	(ntohl(getdle(buf,ofs)))
73
74#define setwle(buf,ofs,val) getwle(buf,ofs)=val
75#define setwbe(buf,ofs,val) getwle(buf,ofs)=htons(val)
76#define setdle(buf,ofs,val) getdle(buf,ofs)=val
77#define setdbe(buf,ofs,val) getdle(buf,ofs)=htonl(val)
78
79#else	/* (BYTE_ORDER == LITTLE_ENDIAN) */
80#error "Macros for Big-Endians are incomplete"
81#define getwle(buf,ofs) ((u_int16_t)(getb(buf, ofs) | (getb(buf, ofs + 1) << 8)))
82#define getdle(buf,ofs) ((u_int32_t)(getb(buf, ofs) | \
83				    (getb(buf, ofs + 1) << 8) | \
84				    (getb(buf, ofs + 2) << 16) | \
85				    (getb(buf, ofs + 3) << 24)))
86#define getwbe(buf,ofs) (*((u_int16_t*)(&((u_int8_t*)(buf))[ofs])))
87#define getdbe(buf,ofs) (*((u_int32_t*)(&((u_int8_t*)(buf))[ofs])))
88/*
89#define setwle(buf,ofs,val) getwle(buf,ofs)=val
90#define setdle(buf,ofs,val) getdle(buf,ofs)=val
91*/
92#define setwbe(buf,ofs,val) getwle(buf,ofs)=val
93#define setdbe(buf,ofs,val) getdle(buf,ofs)=val
94
95#endif	/* (BYTE_ORDER == LITTLE_ENDIAN) */
96
97/*
98 * SMB work context. Used to store all values which is necessary
99 * to establish connection to an SMB server.
100 */
101struct smb_ctx {
102	int		ct_flags;	/* SMBCF_ */
103	int		ct_fd;		/* handle of connection */
104	int		ct_parsedlevel;
105	int		ct_minlevel;
106	int		ct_maxlevel;
107	char *		ct_srvaddr;	/* hostname or IP address of server */
108	char		ct_locname[SMB_MAXUSERNAMELEN + 1];
109	const char *	ct_uncnext;
110	struct nb_ctx *	ct_nb;
111	struct smbioc_ossn	ct_ssn;
112	struct smbioc_oshare	ct_sh;
113};
114
115#define	SMBCF_NOPWD		0x0001	/* don't ask for a password */
116#define	SMBCF_SRIGHTS		0x0002	/* share access rights was supplied */
117#define	SMBCF_LOCALE		0x0004	/* use current locale */
118#define	SMBCF_RESOLVED		0x8000	/* structure has been verified */
119
120/*
121 * request handling structures
122 */
123struct mbuf {
124	int		m_len;
125	int		m_maxlen;
126	char *		m_data;
127	struct mbuf *	m_next;
128};
129
130struct mbdata {
131	struct mbuf *	mb_top;
132	struct mbuf * 	mb_cur;
133	char *		mb_pos;
134	int		mb_count;
135};
136
137#define	M_ALIGNFACTOR	(sizeof(long))
138#define M_ALIGN(len)	(((len) + M_ALIGNFACTOR - 1) & ~(M_ALIGNFACTOR - 1))
139#define	M_BASESIZE	(sizeof(struct mbuf))
140#define	M_MINSIZE	(256 - M_BASESIZE)
141#define M_TOP(m)	((char*)(m) + M_BASESIZE)
142#define mtod(m,t)	((t)(m)->m_data)
143#define M_TRAILINGSPACE(m) ((m)->m_maxlen - (m)->m_len)
144
145struct smb_rq {
146	u_char		rq_cmd;
147	struct mbdata	rq_rq;
148	struct mbdata	rq_rp;
149	struct smb_ctx *rq_ctx;
150	int		rq_wcount;
151	int		rq_bcount;
152};
153
154struct smb_bitname {
155	u_int	bn_bit;
156	char	*bn_name;
157};
158
159extern struct rcfile *smb_rc;
160
161__BEGIN_DECLS
162
163struct sockaddr;
164
165int  smb_lib_init(void);
166int  smb_open_rcfile(void);
167void smb_error(const char *, int,...);
168char *smb_printb(char *, int, const struct smb_bitname *);
169void *smb_dumptree(void);
170
171/*
172 * Context management
173 */
174int  smb_ctx_init(struct smb_ctx *, int, char *[], int, int, int);
175void smb_ctx_done(struct smb_ctx *);
176int  smb_ctx_parseunc(struct smb_ctx *, const char *, int, const char **);
177int  smb_ctx_setcharset(struct smb_ctx *, const char *);
178int  smb_ctx_setserver(struct smb_ctx *, const char *);
179int  smb_ctx_setuser(struct smb_ctx *, const char *);
180int  smb_ctx_setshare(struct smb_ctx *, const char *, int);
181int  smb_ctx_setscope(struct smb_ctx *, const char *);
182int  smb_ctx_setworkgroup(struct smb_ctx *, const char *);
183int  smb_ctx_setpassword(struct smb_ctx *, const char *);
184int  smb_ctx_setsrvaddr(struct smb_ctx *, const char *);
185int  smb_ctx_opt(struct smb_ctx *, int, const char *);
186int  smb_ctx_lookup(struct smb_ctx *, int, int);
187int  smb_ctx_login(struct smb_ctx *);
188int  smb_ctx_readrc(struct smb_ctx *);
189int  smb_ctx_resolve(struct smb_ctx *);
190int  smb_ctx_setflags(struct smb_ctx *, int, int, int);
191
192int  smb_smb_open_print_file(struct smb_ctx *, int, int, const char *, smbfh*);
193int  smb_smb_close_print_file(struct smb_ctx *, smbfh);
194
195int  smb_read(struct smb_ctx *, smbfh, off_t, size_t, char *);
196int  smb_write(struct smb_ctx *, smbfh, off_t, size_t, const char *);
197
198#define smb_rq_getrequest(rqp)	(&(rqp)->rq_rq)
199#define smb_rq_getreply(rqp)	(&(rqp)->rq_rp)
200
201int  smb_rq_init(struct smb_ctx *, u_char, size_t, struct smb_rq **);
202void smb_rq_done(struct smb_rq *);
203void smb_rq_wend(struct smb_rq *);
204int  smb_rq_simple(struct smb_rq *);
205int  smb_rq_dmem(struct mbdata *, const char *, size_t);
206int  smb_rq_dstring(struct mbdata *, const char *);
207
208int  smb_t2_request(struct smb_ctx *, int, int, const char *,
209	int, void *, int, void *, int *, void *, int *, void *);
210
211void smb_simplecrypt(char *dst, const char *src);
212int  smb_simpledecrypt(char *dst, const char *src);
213
214int  m_getm(struct mbuf *, size_t, struct mbuf **);
215int  m_lineup(struct mbuf *, struct mbuf **);
216int  mb_init(struct mbdata *, size_t);
217int  mb_initm(struct mbdata *, struct mbuf *);
218int  mb_done(struct mbdata *);
219int  mb_fit(struct mbdata *mbp, size_t size, char **pp);
220int  mb_put_uint8(struct mbdata *, u_int8_t);
221int  mb_put_uint16be(struct mbdata *, u_int16_t);
222int  mb_put_uint16le(struct mbdata *, u_int16_t);
223int  mb_put_uint32be(struct mbdata *, u_int32_t);
224int  mb_put_uint32le(struct mbdata *, u_int32_t);
225int  mb_put_int64be(struct mbdata *, int64_t);
226int  mb_put_int64le(struct mbdata *, int64_t);
227int  mb_put_mem(struct mbdata *, const char *, size_t);
228int  mb_put_pstring(struct mbdata *mbp, const char *s);
229int  mb_put_mbuf(struct mbdata *, struct mbuf *);
230
231int  mb_get_uint8(struct mbdata *, u_int8_t *);
232int  mb_get_uint16(struct mbdata *, u_int16_t *);
233int  mb_get_uint16le(struct mbdata *, u_int16_t *);
234int  mb_get_uint16be(struct mbdata *, u_int16_t *);
235int  mb_get_uint32(struct mbdata *, u_int32_t *);
236int  mb_get_uint32be(struct mbdata *, u_int32_t *);
237int  mb_get_uint32le(struct mbdata *, u_int32_t *);
238int  mb_get_int64(struct mbdata *, int64_t *);
239int  mb_get_int64be(struct mbdata *, int64_t *);
240int  mb_get_int64le(struct mbdata *, int64_t *);
241int  mb_get_mem(struct mbdata *, char *, size_t);
242
243extern u_char nls_lower[256], nls_upper[256];
244
245int   nls_setrecode(const char *, const char *);
246int   nls_setlocale(const char *);
247char* nls_str_toext(char *, const char *);
248char* nls_str_toloc(char *, const char *);
249void* nls_mem_toext(void *, const void *, int);
250void* nls_mem_toloc(void *, const void *, int);
251char* nls_str_upper(char *, const char *);
252char* nls_str_lower(char *, const char *);
253
254__END_DECLS
255
256#endif /* _NETSMB_SMB_LIB_H_ */
257