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