187866Ssheldonh/* 287866Ssheldonh * Copyright (c) 2000-2001 Boris Popov 387866Ssheldonh * All rights reserved. 487866Ssheldonh * 587866Ssheldonh * Redistribution and use in source and binary forms, with or without 687866Ssheldonh * modification, are permitted provided that the following conditions 787866Ssheldonh * are met: 887866Ssheldonh * 1. Redistributions of source code must retain the above copyright 987866Ssheldonh * notice, this list of conditions and the following disclaimer. 1087866Ssheldonh * 2. Redistributions in binary form must reproduce the above copyright 1187866Ssheldonh * notice, this list of conditions and the following disclaimer in the 1287866Ssheldonh * documentation and/or other materials provided with the distribution. 1387866Ssheldonh * 3. All advertising materials mentioning features or use of this software 1487866Ssheldonh * must display the following acknowledgement: 1587866Ssheldonh * This product includes software developed by Boris Popov. 1687866Ssheldonh * 4. Neither the name of the author nor the names of any co-contributors 1787866Ssheldonh * may be used to endorse or promote products derived from this software 1887866Ssheldonh * without specific prior written permission. 1987866Ssheldonh * 2087866Ssheldonh * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 2187866Ssheldonh * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 2287866Ssheldonh * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2387866Ssheldonh * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 2487866Ssheldonh * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2587866Ssheldonh * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2687866Ssheldonh * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2787866Ssheldonh * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2887866Ssheldonh * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2987866Ssheldonh * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 3087866Ssheldonh * SUCH DAMAGE. 3187866Ssheldonh * 3288282Ssheldonh * $Id: smb_lib.h,v 1.24 2001/12/20 15:19:43 bp Exp $ 33150313Simura * $FreeBSD$ 3487866Ssheldonh */ 3587866Ssheldonh#ifndef _NETSMB_SMB_LIB_H_ 3687866Ssheldonh#define _NETSMB_SMB_LIB_H_ 3787866Ssheldonh 3887866Ssheldonh#include <netsmb/smb.h> 3987866Ssheldonh#include <netsmb/smb_dev.h> 4087866Ssheldonh 4188282Ssheldonh#ifndef SMB_CFG_FILE 4287866Ssheldonh#define SMB_CFG_FILE "/usr/local/etc/nsmb.conf" 4388282Ssheldonh#endif 4487866Ssheldonh 4587866Ssheldonh#define STDPARAM_ARGS 'A':case 'B':case 'C':case 'E':case 'I': \ 4687866Ssheldonh case 'L':case 'M': \ 4787866Ssheldonh case 'N':case 'U':case 'R':case 'S':case 'T': \ 4887866Ssheldonh case 'W':case 'O':case 'P' 4987866Ssheldonh 5087866Ssheldonh#define STDPARAM_OPT "A:BCE:I:L:M:NO:P:U:R:S:T:W:" 5187866Ssheldonh 5287866Ssheldonh/* 5387866Ssheldonh * bits to indicate the source of error 5487866Ssheldonh */ 5587866Ssheldonh#define SMB_ERRTYPE_MASK 0xf0000 5687866Ssheldonh#define SMB_SYS_ERROR 0x00000 5787866Ssheldonh#define SMB_RAP_ERROR 0x10000 5887866Ssheldonh#define SMB_NB_ERROR 0x20000 5987866Ssheldonh 6087866Ssheldonh#ifndef min 6187866Ssheldonh#define min(a,b) (((a)<(b)) ? (a) : (b)) 6287866Ssheldonh#endif 6387866Ssheldonh 6487866Ssheldonh#define getb(buf,ofs) (((const u_int8_t *)(buf))[ofs]) 6587866Ssheldonh#define setb(buf,ofs,val) (((u_int8_t*)(buf))[ofs])=val 6687866Ssheldonh#define getbw(buf,ofs) ((u_int16_t)(getb(buf,ofs))) 6787866Ssheldonh#define getw(buf,ofs) (*((u_int16_t*)(&((u_int8_t*)(buf))[ofs]))) 6887866Ssheldonh#define getdw(buf,ofs) (*((u_int32_t*)(&((u_int8_t*)(buf))[ofs]))) 6987866Ssheldonh 7087866Ssheldonh#if (BYTE_ORDER == LITTLE_ENDIAN) 7187866Ssheldonh 7287866Ssheldonh#define getwle(buf,ofs) (*((u_int16_t*)(&((u_int8_t*)(buf))[ofs]))) 7387866Ssheldonh#define getdle(buf,ofs) (*((u_int32_t*)(&((u_int8_t*)(buf))[ofs]))) 7487866Ssheldonh#define getwbe(buf,ofs) (ntohs(getwle(buf,ofs))) 7587866Ssheldonh#define getdbe(buf,ofs) (ntohl(getdle(buf,ofs))) 7687866Ssheldonh 7787866Ssheldonh#define setwle(buf,ofs,val) getwle(buf,ofs)=val 7887866Ssheldonh#define setwbe(buf,ofs,val) getwle(buf,ofs)=htons(val) 7987866Ssheldonh#define setdle(buf,ofs,val) getdle(buf,ofs)=val 8087866Ssheldonh#define setdbe(buf,ofs,val) getdle(buf,ofs)=htonl(val) 8187866Ssheldonh 8287866Ssheldonh#else /* (BYTE_ORDER == LITTLE_ENDIAN) */ 8387866Ssheldonh 84150313Simura#define getwbe(buf,ofs) (*((u_int16_t*)(&((u_int8_t*)(buf))[ofs]))) 85150313Simura#define getdbe(buf,ofs) (*((u_int32_t*)(&((u_int8_t*)(buf))[ofs]))) 86150313Simura#define getwle(buf,ofs) (bswap16(getwbe(buf,ofs))) 87150313Simura#define getdle(buf,ofs) (bswap32(getdbe(buf,ofs))) 88150313Simura 89150313Simura#define setwbe(buf,ofs,val) getwbe(buf,ofs)=val 90150313Simura#define setwle(buf,ofs,val) getwbe(buf,ofs)=bswap16(val) 91150313Simura#define setdbe(buf,ofs,val) getdbe(buf,ofs)=val 92150313Simura#define setdle(buf,ofs,val) getdbe(buf,ofs)=bswap32(val) 93150313Simura 9487866Ssheldonh#endif /* (BYTE_ORDER == LITTLE_ENDIAN) */ 9587866Ssheldonh 9687866Ssheldonh/* 9787866Ssheldonh * SMB work context. Used to store all values which is necessary 9887866Ssheldonh * to establish connection to an SMB server. 9987866Ssheldonh */ 10087866Ssheldonhstruct smb_ctx { 10187866Ssheldonh int ct_flags; /* SMBCF_ */ 10287866Ssheldonh int ct_fd; /* handle of connection */ 10387866Ssheldonh int ct_parsedlevel; 10487866Ssheldonh int ct_minlevel; 10587866Ssheldonh int ct_maxlevel; 10687866Ssheldonh char * ct_srvaddr; /* hostname or IP address of server */ 10787866Ssheldonh char ct_locname[SMB_MAXUSERNAMELEN + 1]; 10887866Ssheldonh const char * ct_uncnext; 10987866Ssheldonh struct nb_ctx * ct_nb; 11087866Ssheldonh struct smbioc_ossn ct_ssn; 11187866Ssheldonh struct smbioc_oshare ct_sh; 112150802Sbp long ct_smbtcpport; 11387866Ssheldonh}; 11487866Ssheldonh 11587866Ssheldonh#define SMBCF_NOPWD 0x0001 /* don't ask for a password */ 11687866Ssheldonh#define SMBCF_SRIGHTS 0x0002 /* share access rights was supplied */ 11787866Ssheldonh#define SMBCF_LOCALE 0x0004 /* use current locale */ 11887866Ssheldonh#define SMBCF_RESOLVED 0x8000 /* structure has been verified */ 11987866Ssheldonh 12087866Ssheldonh/* 12187866Ssheldonh * request handling structures 12287866Ssheldonh */ 12387866Ssheldonhstruct mbuf { 12487866Ssheldonh int m_len; 12587866Ssheldonh int m_maxlen; 12687866Ssheldonh char * m_data; 12787866Ssheldonh struct mbuf * m_next; 12887866Ssheldonh}; 12987866Ssheldonh 13087866Ssheldonhstruct mbdata { 13187866Ssheldonh struct mbuf * mb_top; 13287866Ssheldonh struct mbuf * mb_cur; 13387866Ssheldonh char * mb_pos; 13487866Ssheldonh int mb_count; 13587866Ssheldonh}; 13687866Ssheldonh 13787866Ssheldonh#define M_ALIGNFACTOR (sizeof(long)) 13887866Ssheldonh#define M_ALIGN(len) (((len) + M_ALIGNFACTOR - 1) & ~(M_ALIGNFACTOR - 1)) 13987866Ssheldonh#define M_BASESIZE (sizeof(struct mbuf)) 14087866Ssheldonh#define M_MINSIZE (256 - M_BASESIZE) 14187866Ssheldonh#define M_TOP(m) ((char*)(m) + M_BASESIZE) 14287866Ssheldonh#define mtod(m,t) ((t)(m)->m_data) 14387866Ssheldonh#define M_TRAILINGSPACE(m) ((m)->m_maxlen - (m)->m_len) 14487866Ssheldonh 14587866Ssheldonhstruct smb_rq { 14687866Ssheldonh u_char rq_cmd; 14787866Ssheldonh struct mbdata rq_rq; 14887866Ssheldonh struct mbdata rq_rp; 14987866Ssheldonh struct smb_ctx *rq_ctx; 15087866Ssheldonh int rq_wcount; 15187866Ssheldonh int rq_bcount; 15287866Ssheldonh}; 15387866Ssheldonh 15487866Ssheldonhstruct smb_bitname { 15587866Ssheldonh u_int bn_bit; 15687866Ssheldonh char *bn_name; 15787866Ssheldonh}; 15887866Ssheldonh 15987866Ssheldonhextern struct rcfile *smb_rc; 16087866Ssheldonh 16187866Ssheldonh__BEGIN_DECLS 16287866Ssheldonh 16387866Ssheldonhstruct sockaddr; 16487866Ssheldonh 16587866Ssheldonhint smb_lib_init(void); 16687866Ssheldonhint smb_open_rcfile(void); 16787866Ssheldonhvoid smb_error(const char *, int,...); 16887866Ssheldonhchar *smb_printb(char *, int, const struct smb_bitname *); 16987866Ssheldonhvoid *smb_dumptree(void); 17087866Ssheldonh 17187866Ssheldonh/* 17287866Ssheldonh * Context management 17387866Ssheldonh */ 17487866Ssheldonhint smb_ctx_init(struct smb_ctx *, int, char *[], int, int, int); 17587866Ssheldonhvoid smb_ctx_done(struct smb_ctx *); 17687866Ssheldonhint smb_ctx_parseunc(struct smb_ctx *, const char *, int, const char **); 17787866Ssheldonhint smb_ctx_setcharset(struct smb_ctx *, const char *); 17887866Ssheldonhint smb_ctx_setserver(struct smb_ctx *, const char *); 179150802Sbpint smb_ctx_setnbport(struct smb_ctx *, int); 180150802Sbpint smb_ctx_setsmbport(struct smb_ctx *, int); 18187866Ssheldonhint smb_ctx_setuser(struct smb_ctx *, const char *); 18287866Ssheldonhint smb_ctx_setshare(struct smb_ctx *, const char *, int); 18387866Ssheldonhint smb_ctx_setscope(struct smb_ctx *, const char *); 18487866Ssheldonhint smb_ctx_setworkgroup(struct smb_ctx *, const char *); 18587866Ssheldonhint smb_ctx_setpassword(struct smb_ctx *, const char *); 18687866Ssheldonhint smb_ctx_setsrvaddr(struct smb_ctx *, const char *); 18787866Ssheldonhint smb_ctx_opt(struct smb_ctx *, int, const char *); 18887866Ssheldonhint smb_ctx_lookup(struct smb_ctx *, int, int); 18987866Ssheldonhint smb_ctx_login(struct smb_ctx *); 19087866Ssheldonhint smb_ctx_readrc(struct smb_ctx *); 19187866Ssheldonhint smb_ctx_resolve(struct smb_ctx *); 19287866Ssheldonhint smb_ctx_setflags(struct smb_ctx *, int, int, int); 19387866Ssheldonh 19487866Ssheldonhint smb_smb_open_print_file(struct smb_ctx *, int, int, const char *, smbfh*); 19587866Ssheldonhint smb_smb_close_print_file(struct smb_ctx *, smbfh); 19687866Ssheldonh 19787866Ssheldonhint smb_read(struct smb_ctx *, smbfh, off_t, size_t, char *); 19887866Ssheldonhint smb_write(struct smb_ctx *, smbfh, off_t, size_t, const char *); 19987866Ssheldonh 20087866Ssheldonh#define smb_rq_getrequest(rqp) (&(rqp)->rq_rq) 20187866Ssheldonh#define smb_rq_getreply(rqp) (&(rqp)->rq_rp) 20287866Ssheldonh 20387866Ssheldonhint smb_rq_init(struct smb_ctx *, u_char, size_t, struct smb_rq **); 20487866Ssheldonhvoid smb_rq_done(struct smb_rq *); 20587866Ssheldonhvoid smb_rq_wend(struct smb_rq *); 20687866Ssheldonhint smb_rq_simple(struct smb_rq *); 20787866Ssheldonhint smb_rq_dmem(struct mbdata *, const char *, size_t); 20887866Ssheldonhint smb_rq_dstring(struct mbdata *, const char *); 20987866Ssheldonh 21087866Ssheldonhint smb_t2_request(struct smb_ctx *, int, int, const char *, 21187866Ssheldonh int, void *, int, void *, int *, void *, int *, void *); 21287866Ssheldonh 21388282Ssheldonhchar* smb_simplecrypt(char *dst, const char *src); 21487866Ssheldonhint smb_simpledecrypt(char *dst, const char *src); 21587866Ssheldonh 21687866Ssheldonhint m_getm(struct mbuf *, size_t, struct mbuf **); 21787866Ssheldonhint m_lineup(struct mbuf *, struct mbuf **); 21887866Ssheldonhint mb_init(struct mbdata *, size_t); 21987866Ssheldonhint mb_initm(struct mbdata *, struct mbuf *); 22087866Ssheldonhint mb_done(struct mbdata *); 22187866Ssheldonhint mb_fit(struct mbdata *mbp, size_t size, char **pp); 22287866Ssheldonhint mb_put_uint8(struct mbdata *, u_int8_t); 22387866Ssheldonhint mb_put_uint16be(struct mbdata *, u_int16_t); 22487866Ssheldonhint mb_put_uint16le(struct mbdata *, u_int16_t); 22587866Ssheldonhint mb_put_uint32be(struct mbdata *, u_int32_t); 22687866Ssheldonhint mb_put_uint32le(struct mbdata *, u_int32_t); 22787866Ssheldonhint mb_put_int64be(struct mbdata *, int64_t); 22887866Ssheldonhint mb_put_int64le(struct mbdata *, int64_t); 22987866Ssheldonhint mb_put_mem(struct mbdata *, const char *, size_t); 23087866Ssheldonhint mb_put_pstring(struct mbdata *mbp, const char *s); 23187866Ssheldonhint mb_put_mbuf(struct mbdata *, struct mbuf *); 23287866Ssheldonh 23387866Ssheldonhint mb_get_uint8(struct mbdata *, u_int8_t *); 23487866Ssheldonhint mb_get_uint16(struct mbdata *, u_int16_t *); 23587866Ssheldonhint mb_get_uint16le(struct mbdata *, u_int16_t *); 23687866Ssheldonhint mb_get_uint16be(struct mbdata *, u_int16_t *); 23787866Ssheldonhint mb_get_uint32(struct mbdata *, u_int32_t *); 23887866Ssheldonhint mb_get_uint32be(struct mbdata *, u_int32_t *); 23987866Ssheldonhint mb_get_uint32le(struct mbdata *, u_int32_t *); 24087866Ssheldonhint mb_get_int64(struct mbdata *, int64_t *); 24187866Ssheldonhint mb_get_int64be(struct mbdata *, int64_t *); 24287866Ssheldonhint mb_get_int64le(struct mbdata *, int64_t *); 24387866Ssheldonhint mb_get_mem(struct mbdata *, char *, size_t); 24487866Ssheldonh 24587866Ssheldonhextern u_char nls_lower[256], nls_upper[256]; 24687866Ssheldonh 24787866Ssheldonhint nls_setrecode(const char *, const char *); 24887866Ssheldonhint nls_setlocale(const char *); 24987866Ssheldonhchar* nls_str_toext(char *, const char *); 25087866Ssheldonhchar* nls_str_toloc(char *, const char *); 25187866Ssheldonhvoid* nls_mem_toext(void *, const void *, int); 25287866Ssheldonhvoid* nls_mem_toloc(void *, const void *, int); 25387866Ssheldonhchar* nls_str_upper(char *, const char *); 25487866Ssheldonhchar* nls_str_lower(char *, const char *); 25587866Ssheldonh 25687866Ssheldonh__END_DECLS 25787866Ssheldonh 25887866Ssheldonh#endif /* _NETSMB_SMB_LIB_H_ */ 259