xref: /freebsd/contrib/smbfs/include/netsmb/smb_lib.h (revision dd41de95a84d979615a2ef11df6850622bf6184e)
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 	long		ct_smbtcpport;
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  */
123 struct mbuf {
124 	int		m_len;
125 	int		m_maxlen;
126 	char *		m_data;
127 	struct mbuf *	m_next;
128 };
129 
130 struct 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 
145 struct 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 
154 struct smb_bitname {
155 	u_int	bn_bit;
156 	char	*bn_name;
157 };
158 
159 extern struct rcfile *smb_rc;
160 
161 __BEGIN_DECLS
162 
163 struct sockaddr;
164 
165 int  smb_lib_init(void);
166 int  smb_open_rcfile(void);
167 void smb_error(const char *, int,...);
168 char *smb_printb(char *, int, const struct smb_bitname *);
169 void *smb_dumptree(void);
170 
171 /*
172  * Context management
173  */
174 int  smb_ctx_init(struct smb_ctx *, int, char *[], int, int, int);
175 void smb_ctx_done(struct smb_ctx *);
176 int  smb_ctx_parseunc(struct smb_ctx *, const char *, int, const char **);
177 int  smb_ctx_setcharset(struct smb_ctx *, const char *);
178 int  smb_ctx_setserver(struct smb_ctx *, const char *);
179 int  smb_ctx_setnbport(struct smb_ctx *, int);
180 int  smb_ctx_setsmbport(struct smb_ctx *, int);
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