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