xref: /illumos-gate/usr/src/lib/libsmbfs/netsmb/smb_lib.h (revision 4bff34e37def8a90f9194d81bc345c52ba20086a)
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.21.82.2 2005/06/02 00:55:39 lindak Exp $
33  */
34 
35 #ifndef _NETSMB_SMB_LIB_H_
36 #define	_NETSMB_SMB_LIB_H_
37 
38 #pragma ident	"%Z%%M%	%I%	%E% SMI"
39 
40 #include <sys/types.h>
41 #include <sys/socket.h>
42 #include <netinet/in.h>
43 #include <arpa/inet.h>
44 #include <sys/byteorder.h>
45 
46 #include <netsmb/smb.h>
47 #include <netsmb/smb_dev.h>
48 
49 #define	SMB_CFG_FILE	"/etc/nsmb.conf"
50 #define	OLD_SMB_CFG_FILE	"/usr/local/etc/nsmb.conf"
51 
52 #define	STDPARAM_ARGS	\
53 	'A':case 'B':case 'C':case 'E':case 'I':case 'L':case \
54 	'M':case 'N':case 'U':case 'R':case 'S':case 'T':case \
55 	'W':case 'O':case 'P'
56 
57 #define	STDPARAM_OPT	"ABCE:I:L:M:NO:P:U:R:S:T:W:"
58 
59 /*
60  * bits to indicate the source of error
61  */
62 #define	SMB_ERRTYPE_MASK	0xf0000
63 #define	SMB_SYS_ERROR		0x00000
64 #define	SMB_RAP_ERROR		0x10000
65 #define	SMB_NB_ERROR		0x20000
66 
67 /*
68  * These get/set macros do not handle mis-aligned data.
69  * The data are all supposed to be aligned, but that's
70  * up to the server.  If we ever encounter a server that
71  * doesn't obey this rule, a "strict alignment" client
72  * (i.e. SPARC) may get an alignment trap in one of these.
73  * If that ever happens, make these macros into functions
74  * that can handle mis-aligned data.  (Or catch traps.)
75  */
76 #define	getb(buf, ofs) 		(((const uint8_t *)(buf))[ofs])
77 #define	setb(buf, ofs, val)	(((uint8_t *)(buf))[ofs]) = val
78 #define	getbw(buf, ofs)		((uint16_t)(getb(buf, ofs)))
79 #define	getw(buf, ofs)		(*((uint16_t *)(&((uint8_t *)(buf))[ofs])))
80 #define	getdw(buf, ofs)		(*((uint32_t *)(&((uint8_t *)(buf))[ofs])))
81 
82 #ifdef _LITTLE_ENDIAN
83 
84 #define	getwle(buf, ofs)	(*((uint16_t *)(&((uint8_t *)(buf))[ofs])))
85 #define	getdle(buf, ofs)	(*((uint32_t *)(&((uint8_t *)(buf))[ofs])))
86 #define	getwbe(buf, ofs)	(ntohs(getwle(buf, ofs)))
87 #define	getdbe(buf, ofs)	(ntohl(getdle(buf, ofs)))
88 
89 #define	setwle(buf, ofs, val) getwle(buf, ofs) = val
90 #define	setwbe(buf, ofs, val) getwle(buf, ofs) = htons(val)
91 #define	setdle(buf, ofs, val) getdle(buf, ofs) = val
92 #define	setdbe(buf, ofs, val) getdle(buf, ofs) = htonl(val)
93 
94 #else	/* _LITTLE_ENDIAN */
95 
96 #define	getwbe(buf, ofs) (*((uint16_t *)(&((uint8_t *)(buf))[ofs])))
97 #define	getdbe(buf, ofs) (*((uint32_t *)(&((uint8_t *)(buf))[ofs])))
98 #define	getwle(buf, ofs) (BSWAP_16(getwbe(buf, ofs)))
99 #define	getdle(buf, ofs) (BSWAP_32(getdbe(buf, ofs)))
100 
101 #define	setwbe(buf, ofs, val) getwbe(buf, ofs) = val
102 #define	setwle(buf, ofs, val) getwbe(buf, ofs) = BSWAP_16(val)
103 #define	setdbe(buf, ofs, val) getdbe(buf, ofs) = val
104 #define	setdle(buf, ofs, val) getdbe(buf, ofs) = BSWAP_32(val)
105 
106 #endif	/* _LITTLE_ENDIAN */
107 
108 /*
109  * SMB work context. Used to store all values which are necessary
110  * to establish connection to an SMB server.
111  */
112 struct smb_ctx {
113 	int		ct_flags;	/* SMBCF_ */
114 	int		ct_fd;		/* handle of connection */
115 	int		ct_parsedlevel;
116 	int		ct_minlevel;
117 	int		ct_maxlevel;
118 	char		*ct_fullserver; /* original server name from cmd line */
119 	char		*ct_srvaddr;	/* hostname or IP address of server */
120 	struct sockaddr_in ct_srvinaddr; /* IP address of server */
121 	char		ct_locname[SMB_MAXUSERNAMELEN + 1];
122 	struct nb_ctx	*ct_nb;
123 	struct smbioc_ossn	ct_ssn;
124 	struct smbioc_oshare	ct_sh;
125 	char		*ct_origshare;
126 	char		*ct_home;
127 #ifdef APPLE
128 	/* temporary automount hack */
129 	char	**ct_xxx;
130 	int	ct_maxxxx;	/* max # to mount (-x arg) */
131 #endif
132 	void		*ct_secblob;
133 	int		ct_secbloblen;
134 	/* krb5 stuff: all anonymous struct pointers here. */
135 	struct _krb5_context *ct_krb5ctx;
136 	struct _krb5_ccache *ct_krb5cc; 	/* credentials cache */
137 	struct krb5_principal_data *ct_krb5cp;	/* client principal */
138 };
139 typedef struct smb_ctx smb_ctx_t;
140 
141 #define	SMBCF_NOPWD		    0x0001 /* don't ask for a password */
142 #define	SMBCF_SRIGHTS		    0x0002 /* share access rights supplied */
143 #define	SMBCF_LOCALE		    0x0004 /* use current locale */
144 #define	SMBCF_CMD_DOM		    0x0010 /* CMD specified domain */
145 #define	SMBCF_CMD_USR		    0x0020 /* CMD specified user */
146 #define	SMBCF_CMD_PW		    0x0040 /* CMD specified password */
147 #define	SMBCF_RESOLVED		    0x8000 /* structure has been verified */
148 #define	SMBCF_KCBAD		0x00080000 /* keychain password failed */
149 #define	SMBCF_KCFOUND		0x00100000 /* password is from keychain */
150 #define	SMBCF_BROWSEOK		0x00200000 /* browser dialogue may be used */
151 #define	SMBCF_AUTHREQ		0x00400000 /* auth. dialog requested */
152 #define	SMBCF_KCSAVE		0x00800000 /* add to keychain requested */
153 #define	SMBCF_XXX		0x01000000 /* mount-all, a very bad thing */
154 #define	SMBCF_SSNACTIVE		0x02000000 /* session setup succeeded */
155 #define	SMBCF_KCDOMAIN		0x04000000 /* use domain in KC lookup */
156 
157 /*
158  * access modes (see also smb_dev.h)
159  */
160 #define	SMBM_READ	S_IRUSR	/* read conn attrs. (like list shares) */
161 #define	SMBM_WRITE	S_IWUSR	/* modify conn attrs */
162 #define	SMBM_EXEC	S_IXUSR	/* can send SMB requests */
163 #define	SMBM_READGRP	S_IRGRP
164 #define	SMBM_WRITEGRP	S_IWGRP
165 #define	SMBM_EXECGRP	S_IXGRP
166 #define	SMBM_READOTH	S_IROTH
167 #define	SMBM_WRITEOTH	S_IWOTH
168 #define	SMBM_EXECOTH	S_IXOTH
169 #define	SMBM_ALL	S_IRWXU
170 #define	SMBM_DEFAULT	S_IRWXU
171 
172 
173 /*
174  * Share type for smb_ctx_init
175  */
176 #define	SMB_ST_DISK		STYPE_DISKTREE
177 #define	SMB_ST_PRINTER		STYPE_PRINTQ
178 #define	SMB_ST_COMM		STYPE_DEVICE
179 #define	SMB_ST_PIPE		STYPE_IPC
180 #define	SMB_ST_ANY		STYPE_UNKNOWN
181 #define	SMB_ST_MAX		STYPE_UNKNOWN
182 #define	SMB_ST_NONE		0xff	/* not a part of protocol */
183 
184 
185 /*
186  * request handling structures
187  */
188 struct mbuf {
189 	int		m_len;
190 	int		m_maxlen;
191 	char		*m_data;
192 	struct mbuf	*m_next;
193 };
194 typedef struct mbuf mbuf_t;
195 
196 struct mbdata {
197 	struct mbuf	*mb_top;
198 	struct mbuf	*mb_cur;
199 	char		*mb_pos;
200 	int		mb_count;
201 };
202 typedef struct mbdata mbdata_t;
203 
204 #define	M_ALIGNFACTOR	(sizeof (long))
205 #define	M_ALIGN(len)	(((len) + M_ALIGNFACTOR - 1) & ~(M_ALIGNFACTOR - 1))
206 #define	M_BASESIZE	(sizeof (struct mbuf))
207 #define	M_MINSIZE	(256 - M_BASESIZE)
208 #define	M_TOP(m)	((char *)(m) + M_BASESIZE)
209 #define	M_TRAILINGSPACE(m) ((m)->m_maxlen - (m)->m_len)
210 #define	mtod(m, t)	((t)(m)->m_data)
211 
212 struct smb_rq {
213 	uchar_t		rq_cmd;
214 	struct mbdata	rq_rq;
215 	struct mbdata	rq_rp;
216 	struct smb_ctx *rq_ctx;
217 	int		rq_wcount;
218 	int		rq_bcount;
219 };
220 typedef struct smb_rq smb_rq_t;
221 
222 struct smb_bitname {
223 	uint_t	bn_bit;
224 	char	*bn_name;
225 };
226 typedef struct smb_bitname smb_bitname_t;
227 
228 extern int smb_debug, smb_verbose;
229 extern struct rcfile *smb_rc;
230 
231 #ifdef __cplusplus
232 extern "C" {
233 #endif
234 
235 struct sockaddr;
236 
237 int  smb_lib_init(void);
238 int  smb_open_driver(void);
239 int  smb_open_rcfile(struct smb_ctx *ctx);
240 void smb_error(const char *, int, ...);
241 char *smb_printb(char *, int, const struct smb_bitname *);
242 
243 /*
244  * Context management
245  */
246 int  smb_ctx_init(struct smb_ctx *, int, char *[], int, int, int);
247 void smb_ctx_done(struct smb_ctx *);
248 int  smb_ctx_parseunc(struct smb_ctx *, const char *, int, const char **);
249 int  smb_ctx_setcharset(struct smb_ctx *, const char *);
250 int  smb_ctx_setfullserver(struct smb_ctx *, const char *);
251 void  smb_ctx_setserver(struct smb_ctx *, const char *);
252 int  smb_ctx_setuser(struct smb_ctx *, const char *, int);
253 int  smb_ctx_setshare(struct smb_ctx *, const char *, int);
254 int  smb_ctx_setscope(struct smb_ctx *, const char *);
255 int  smb_ctx_setworkgroup(struct smb_ctx *, const char *, int);
256 int  smb_ctx_setpassword(struct smb_ctx *, const char *, int);
257 int  smb_ctx_setsrvaddr(struct smb_ctx *, const char *);
258 int  smb_ctx_opt(struct smb_ctx *, int, const char *);
259 int  smb_ctx_negotiate(struct smb_ctx *, int, int, char *);
260 int  smb_ctx_tdis(struct smb_ctx *ctx);
261 int  smb_ctx_lookup(struct smb_ctx *, int, int);
262 int  smb_ctx_login(struct smb_ctx *);
263 int  smb_ctx_readrc(struct smb_ctx *);
264 int  smb_ctx_resolve(struct smb_ctx *);
265 int  smb_ctx_setflags(struct smb_ctx *, int, int, int);
266 int  smb_ctx_flags2(struct smb_ctx *);
267 
268 int  smb_smb_open_print_file(struct smb_ctx *, int, int, const char *, smbfh*);
269 int  smb_smb_close_print_file(struct smb_ctx *, smbfh);
270 
271 int  smb_read(struct smb_ctx *, smbfh, off_t, size_t, char *);
272 int  smb_write(struct smb_ctx *, smbfh, off_t, size_t, const char *);
273 
274 #define	smb_rq_getrequest(rqp)	(&(rqp)->rq_rq)
275 #define	smb_rq_getreply(rqp)	(&(rqp)->rq_rp)
276 
277 int  smb_rq_init(struct smb_ctx *, uchar_t, size_t, struct smb_rq **);
278 void smb_rq_done(struct smb_rq *);
279 void smb_rq_wend(struct smb_rq *);
280 int  smb_rq_simple(struct smb_rq *);
281 int  smb_rq_dmem(struct mbdata *, const char *, size_t);
282 int  smb_rq_dstring(struct mbdata *, const char *);
283 
284 int  smb_t2_request(struct smb_ctx *, int, uint16_t *, const char *,
285 	int, void *, int, void *, int *, void *, int *, void *, int *);
286 
287 void smb_simplecrypt(char *dst, const char *src);
288 int  smb_simpledecrypt(char *dst, const char *src);
289 
290 int  m_getm(struct mbuf *, size_t, struct mbuf **);
291 int  m_lineup(struct mbuf *, struct mbuf **);
292 int  mb_init(struct mbdata *, size_t);
293 int  mb_initm(struct mbdata *, struct mbuf *);
294 int  mb_done(struct mbdata *);
295 int  mb_fit(struct mbdata *mbp, size_t size, char **pp);
296 int  mb_put_uint8(struct mbdata *, uint8_t);
297 int  mb_put_uint16be(struct mbdata *, uint16_t);
298 int  mb_put_uint16le(struct mbdata *, uint16_t);
299 int  mb_put_uint32be(struct mbdata *, uint32_t);
300 int  mb_put_uint32le(struct mbdata *, uint32_t);
301 int  mb_put_uint64be(struct mbdata *, uint64_t);
302 int  mb_put_uint64le(struct mbdata *, uint64_t);
303 int  mb_put_mem(struct mbdata *, const char *, size_t);
304 int  mb_put_pstring(struct mbdata *mbp, const char *s);
305 int  mb_put_mbuf(struct mbdata *, struct mbuf *);
306 
307 int  mb_get_uint8(struct mbdata *, uint8_t *);
308 int  mb_get_uint16(struct mbdata *, uint16_t *);
309 int  mb_get_uint16le(struct mbdata *, uint16_t *);
310 int  mb_get_uint16be(struct mbdata *, uint16_t *);
311 int  mb_get_uint32(struct mbdata *, uint32_t *);
312 int  mb_get_uint32be(struct mbdata *, uint32_t *);
313 int  mb_get_uint32le(struct mbdata *, uint32_t *);
314 int  mb_get_uint64(struct mbdata *, uint64_t *);
315 int  mb_get_uint64be(struct mbdata *, uint64_t *);
316 int  mb_get_uint64le(struct mbdata *, uint64_t *);
317 int  mb_get_mem(struct mbdata *, char *, size_t);
318 
319 extern uchar_t nls_lower[256], nls_upper[256];
320 
321 int	nls_setrecode(const char *, const char *);
322 int	nls_setlocale(const char *);
323 char	*nls_str_toext(char *, const char *);
324 char	*nls_str_toloc(char *, const char *);
325 void	*nls_mem_toext(void *, const void *, int);
326 void	*nls_mem_toloc(void *, const void *, int);
327 char	*nls_str_upper(char *, const char *);
328 char	*nls_str_lower(char *, const char *);
329 
330 int smb_get_authentication(char *, size_t, char *, size_t, char *, size_t,
331 	const char *, struct smb_ctx *);
332 int smb_browse(struct smb_ctx *, int);
333 void smb_save2keychain(struct smb_ctx *);
334 #define	smb_autherr(e) ((e) == EAUTH || (e) == EACCES || (e) == EPERM)
335 char *smb_strerror(int);
336 char *smb_getprogname();
337 #define	__progname smb_getprogname()
338 
339 extern char *unpercent(char *component);
340 
341 #ifdef __cplusplus
342 }
343 #endif
344 
345 #endif /* _NETSMB_SMB_LIB_H_ */
346