1/*
2 * Copyright (c) 2000-2001 Boris Popov
3 * All rights reserved.
4 *
5 * Portions Copyright (C) 2001 - 2011 Apple Inc. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 *    notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 *    notice, this list of conditions and the following disclaimer in the
14 *    documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 *    must display the following acknowledgement:
17 *    This product includes software developed by Boris Popov.
18 * 4. Neither the name of the author nor the names of any co-contributors
19 *    may be used to endorse or promote products derived from this software
20 *    without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
33 *
34 */
35#ifndef _NETSMB_SMB_LIB_H_
36#define _NETSMB_SMB_LIB_H_
37
38#include <sys/types.h>
39#include <sys/socket.h>
40#include <netinet/in.h>
41#include <arpa/inet.h>
42#include <CoreFoundation/CoreFoundation.h>
43#include <pthread.h>
44#include <sys/mount.h>
45
46#include <netsmb/smb.h>
47#include <netsmb/smb_dev.h>
48#include <netsmb/netbios.h>
49#include <asl.h>
50#include "preference.h"
51
52#define SMB_BonjourServiceNameType "_smb._tcp."
53#define NetBIOS_SMBSERVER		"*SMBSERVER"
54
55/* Used by mount_smbfs to pass mount option into the smb_mount call */
56#define kNotifyOffMountKey	CFSTR("SMBNotifyOffMount")
57#define kStreamstMountKey	CFSTR("SMBStreamsMount")
58#define kdirModeKey			CFSTR("SMBDirModes")
59#define kfileModeKey		CFSTR("SMBFileModes")
60
61/* This one should be defined in NetFS.h, but we want it to be private */
62#define kTimeMachineMountKey	CFSTR("TimeMachineMount")
63
64#define SMB_PASSWORD_KEY "Password"
65
66struct connectAddress {
67	int	so;
68	union {
69		struct sockaddr addr;
70		struct sockaddr_in in4;
71		struct sockaddr_in6 in6;
72		struct sockaddr_nb nb;
73		struct sockaddr_storage storage;	/* Make we always have enough room */
74	};
75};
76
77/*
78 * nb environment
79 */
80struct nb_ctx {
81	struct sockaddr_in	nb_ns;			/* ip addr of name server */
82	struct sockaddr_in	nb_sender;		/* address of the system that responded */
83};
84
85/*
86 * SMB work context. Used to store all values which are necessary
87 * to establish connection to an SMB server.
88 */
89struct smb_ctx {
90	pthread_mutex_t ctx_mutex;
91	CFURLRef		ct_url;
92	uint32_t		ct_flags;	/* SMBCF_ */
93	int				ct_fd;		/* handle of connection */
94	uint16_t		ct_cancel;
95	CFStringRef		serverNameRef; /* Display Server name obtain from URL or Bonjour Service Name */
96	char *			serverName;		/* Server name obtain from the URL */
97	struct nb_ctx		ct_nb;
98	struct smbioc_ossn	ct_ssn;
99	struct smbioc_setup ct_setup;
100	struct smbioc_share	ct_sh;
101	struct sockaddr	*ct_saddr;
102	char *			ct_origshare;
103	CFStringRef		mountPath;
104	uint32_t		ct_vc_uid;
105	uint32_t		ct_vc_caps;		/* Obtained from the negotiate message */
106	uint32_t		ct_vc_smb2_caps;
107	uint32_t		ct_vc_flags;	/* Obtained from the negotiate message */
108    uint64_t		ct_vc_misc_flags;
109	uint32_t		ct_vc_hflags;
110	uint32_t		ct_vc_hflags2;
111	uint32_t		ct_vc_shared;	/* Obtained from the negotiate message, currently only tells if the vc is shared */
112	uint64_t		ct_vc_txmax;
113	uint64_t		ct_vc_rxmax;
114    uint64_t		ct_vc_wxmax;
115	int				forceNewSession;
116	int				inCallback;
117	int				serverIsDomainController;
118	CFDictionaryRef mechDict;
119	struct smb_prefs prefs;
120    char *          model_info;     /* SMB 2/3 Server model string, only MAC to MAC */
121};
122
123#define	SMBCF_RESOLVED			0x00000001	/* We have reolved the address and name */
124#define	SMBCF_CONNECTED			0x00000002	/* The negoticate message was succesful */
125#define	SMBCF_AUTHORIZED		0x00000004	/* We have completed the security phase */
126#define	SMBCF_SHARE_CONN		0x00000008	/* We have a tree connection */
127#define	SMBCF_READ_PREFS		0x00000010	/* We already read the preference */
128#define SMBCF_RAW_NTLMSSP		0x00000040	/* Server only supports RAW NTLMSSP */
129#define SMBCF_EXPLICITPWD		0x00010000	/* The password set by the url */
130
131#define SMBCF_CONNECT_STATE	SMBCF_CONNECTED | SMBCF_AUTHORIZED | SMBCF_SHARE_CONN
132
133__BEGIN_DECLS
134
135struct sockaddr;
136
137int smb_load_library(void);
138
139#define SMB_BTMM_DOMAIN "com.apple.filesharing.smb.btmm-connection"
140
141void LogToMessageTracer(const char *domain, const char *signature,
142						const char *optResult, const char *optValue,
143						const char *fmt,...) __printflike(5, 6);
144void smb_log_info(const char *, int,...) __printflike(1, 3);
145void smb_ctx_hexdump(const char */* func */, const char */* comments */, unsigned char */* buf */, size_t /* inlen */);
146
147/*
148 * Context management
149 */
150CFMutableDictionaryRef CreateAuthDictionary(struct smb_ctx *ctx, uint32_t authFlags,
151											const char * clientPrincipal,
152											uint32_t clientNameType);
153int smb_ctx_clone(struct smb_ctx *new_ctx, struct smb_ctx *old_ctx,
154				  CFMutableDictionaryRef openOptions);
155int findMountPointVC(void *inRef, const char *mntPoint);
156void *create_smb_ctx(void);
157int  create_smb_ctx_with_url(struct smb_ctx **out_ctx, const char *url);
158void smb_ctx_cancel_connection(struct smb_ctx *ctx);
159void smb_ctx_done(void *);
160int already_mounted(struct smb_ctx *ctx, char *uppercaseShareName, struct statfs *fs,
161					int fs_cnt, CFMutableDictionaryRef mdict, int requestMntFlags);
162
163Boolean SMBGetDictBooleanValue(CFDictionaryRef Dict, const void * KeyValue, Boolean DefaultValue);
164
165int smb_get_server_info(struct smb_ctx *ctx, CFURLRef url, CFDictionaryRef OpenOptions, CFDictionaryRef *ServerParams);
166int smb_open_session(struct smb_ctx *ctx, CFURLRef url, CFDictionaryRef OpenOptions, CFDictionaryRef *sessionInfo);
167int smb_mount(struct smb_ctx *in_ctx, CFStringRef mpoint,
168			  CFDictionaryRef mOptions, CFDictionaryRef *mInfo,
169			  void (*)(void  *, void *), void *);
170
171void smb_get_vc_properties(struct smb_ctx *ctx);
172int smb_share_disconnect(struct smb_ctx *ctx);
173int smb_share_connect(struct smb_ctx *ctx);
174uint16_t smb_tree_conn_optional_support_flags(struct smb_ctx *ctx);
175uint32_t smb_tree_conn_fstype(struct smb_ctx *ctx);
176int  smb_ctx_setuser(struct smb_ctx *, const char *);
177int  smb_ctx_setshare(struct smb_ctx *, const char *);
178int  smb_ctx_setdomain(struct smb_ctx *, const char *);
179int  smb_ctx_setpassword(struct smb_ctx *, const char *, int /*setFlags*/);
180
181uint16_t smb_ctx_connstate(struct smb_ctx *ctx);
182int  smb_smb_open_print_file(struct smb_ctx *, int, int, const char *, smbfh*);
183int  smb_smb_close_print_file(struct smb_ctx *, smbfh);
184int  smb_read(struct smb_ctx *, smbfh, off_t, uint32_t, char *);
185int  smb_write(struct smb_ctx *, smbfh, off_t, uint32_t, const char *);
186void smb_ctx_get_user_mount_info(const char * /*mntonname */, CFMutableDictionaryRef);
187
188CF_RETURNS_RETAINED CFArrayRef smb_resolve_domain(CFStringRef serverNameRef);
189
190__END_DECLS
191
192#endif /* _NETSMB_SMB_LIB_H_ */
193