xref: /titanic_50/usr/src/lib/smbsrv/libsmb/common/libsmb.h (revision 32e0ab73531b6e6e8957e9ecdbbd42603865f2d0)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
24  */
25 
26 #ifndef	_LIBSMB_H
27 #define	_LIBSMB_H
28 
29 #ifdef	__cplusplus
30 extern "C" {
31 #endif
32 
33 #include <sys/types.h>
34 #include <sys/list.h>
35 #include <sys/avl.h>
36 #include <arpa/inet.h>
37 #include <net/if.h>
38 #include <inet/tcp.h>
39 #include <uuid/uuid.h>
40 #include <netdb.h>
41 #include <stdlib.h>
42 #include <libscf.h>
43 #include <libshare.h>
44 #include <sqlite/sqlite.h>
45 #include <uuid/uuid.h>
46 #include <synch.h>
47 
48 #include <smbsrv/string.h>
49 #include <smbsrv/smb_idmap.h>
50 #include <smbsrv/netbios.h>
51 #include <smbsrv/smb_share.h>
52 #include <smb/nterror.h>
53 #include <smb/ntstatus.h>
54 #include <smbsrv/smb_door.h>
55 #include <smbsrv/alloc.h>
56 #include <smbsrv/hash_table.h>
57 #include <smbsrv/msgbuf.h>
58 #include <smbsrv/wintypes.h>
59 #include <smbsrv/smb_xdr.h>
60 #include <smbsrv/smbinfo.h>
61 #include <smbsrv/ntifs.h>
62 
63 #define	SMB_VARSMB_DIR "/var/smb"
64 #define	SMB_VARRUN_DIR "/var/run/smb"
65 #define	SMB_CCACHE_FILE "ccache"
66 #define	SMB_CCACHE_PATH SMB_VARRUN_DIR "/" SMB_CCACHE_FILE
67 
68 
69 /* Max value length of all SMB properties */
70 #define	MAX_VALUE_BUFLEN	512
71 
72 #define	SMBD_FMRI_PREFIX		"network/smb/server"
73 #define	SMBD_DEFAULT_INSTANCE_FMRI	"svc:/network/smb/server:default"
74 #define	SMBD_PG_NAME			"smbd"
75 #define	SMBD_PROTECTED_PG_NAME		"read"
76 #define	SMBD_EXEC_PG_NAME		"exec"
77 
78 #define	SMBD_SMF_OK		0
79 #define	SMBD_SMF_NO_MEMORY	1	/* no memory for data structures */
80 #define	SMBD_SMF_SYSTEM_ERR	2	/* system error, use errno */
81 #define	SMBD_SMF_NO_PERMISSION	3	/* no permission for operation */
82 #define	SMBD_SMF_INVALID_ARG	4
83 
84 #define	SCH_STATE_UNINIT	0
85 #define	SCH_STATE_INITIALIZING	1
86 #define	SCH_STATE_INIT		2
87 
88 typedef struct smb_scfhandle {
89 	scf_handle_t		*scf_handle;
90 	int			scf_state;
91 	scf_service_t		*scf_service;
92 	scf_scope_t		*scf_scope;
93 	scf_transaction_t	*scf_trans;
94 	scf_transaction_entry_t	*scf_entry;
95 	scf_propertygroup_t	*scf_pg;
96 	scf_instance_t		*scf_instance;
97 	scf_iter_t		*scf_inst_iter;
98 	scf_iter_t		*scf_pg_iter;
99 } smb_scfhandle_t;
100 
101 /*
102  * CIFS Configuration Management
103  */
104 typedef enum {
105 	SMB_CI_VERSION = 0,
106 	SMB_CI_OPLOCK_ENABLE,
107 
108 	SMB_CI_AUTOHOME_MAP,
109 
110 	SMB_CI_DOMAIN_SID,
111 	SMB_CI_DOMAIN_MEMB,
112 	SMB_CI_DOMAIN_NAME,
113 	SMB_CI_DOMAIN_FQDN,
114 	SMB_CI_DOMAIN_FOREST,
115 	SMB_CI_DOMAIN_GUID,
116 	SMB_CI_DOMAIN_SRV,
117 
118 	SMB_CI_WINS_SRV1,
119 	SMB_CI_WINS_SRV2,
120 	SMB_CI_WINS_EXCL,
121 
122 	SMB_CI_MAX_WORKERS,
123 	SMB_CI_MAX_CONNECTIONS,
124 	SMB_CI_KEEPALIVE,
125 	SMB_CI_RESTRICT_ANON,
126 
127 	SMB_CI_SIGNING_ENABLE,
128 	SMB_CI_SIGNING_REQD,
129 
130 	SMB_CI_SYNC_ENABLE,
131 
132 	SMB_CI_SECURITY,
133 	SMB_CI_NBSCOPE,
134 	SMB_CI_SYS_CMNT,
135 	SMB_CI_LM_LEVEL,
136 
137 	SMB_CI_ADS_SITE,
138 
139 	SMB_CI_DYNDNS_ENABLE,
140 
141 	SMB_CI_MACHINE_PASSWD,
142 	SMB_CI_KPASSWD_SRV,
143 	SMB_CI_KPASSWD_DOMAIN,
144 	SMB_CI_KPASSWD_SEQNUM,
145 	SMB_CI_NETLOGON_SEQNUM,
146 	SMB_CI_IPV6_ENABLE,
147 	SMB_CI_PRINT_ENABLE,
148 	SMB_CI_MAP,
149 	SMB_CI_UNMAP,
150 	SMB_CI_DISPOSITION,
151 	SMB_CI_DFS_STDROOT_NUM,
152 	SMB_CI_MAX
153 } smb_cfg_id_t;
154 
155 /* SMF helper functions */
156 extern smb_scfhandle_t *smb_smf_scf_init(char *);
157 extern void smb_smf_scf_fini(smb_scfhandle_t *);
158 extern int smb_smf_start_transaction(smb_scfhandle_t *);
159 extern int smb_smf_end_transaction(smb_scfhandle_t *);
160 extern int smb_smf_set_string_property(smb_scfhandle_t *, char *, char *);
161 extern int smb_smf_get_string_property(smb_scfhandle_t *, char *,
162     char *, size_t);
163 extern int smb_smf_set_integer_property(smb_scfhandle_t *, char *, int64_t);
164 extern int smb_smf_get_integer_property(smb_scfhandle_t *, char *, int64_t *);
165 extern int smb_smf_set_boolean_property(smb_scfhandle_t *, char *, uint8_t);
166 extern int smb_smf_get_boolean_property(smb_scfhandle_t *, char *, uint8_t *);
167 extern int smb_smf_set_opaque_property(smb_scfhandle_t *, char *,
168     void *, size_t);
169 extern int smb_smf_get_opaque_property(smb_scfhandle_t *, char *,
170     void *, size_t);
171 extern int smb_smf_create_service_pgroup(smb_scfhandle_t *, char *);
172 extern int smb_smf_restart_service(void);
173 extern int smb_smf_maintenance_mode(void);
174 
175 /* ZFS interface */
176 int smb_getdataset(const char *, char *, size_t);
177 
178 /* Configuration management functions  */
179 extern int smb_config_get(smb_cfg_id_t, char *, int);
180 extern char *smb_config_getname(smb_cfg_id_t);
181 extern int smb_config_getstr(smb_cfg_id_t, char *, int);
182 extern int smb_config_getnum(smb_cfg_id_t, int64_t *);
183 extern boolean_t smb_config_getbool(smb_cfg_id_t);
184 
185 extern int smb_config_set(smb_cfg_id_t, char *);
186 extern int smb_config_setstr(smb_cfg_id_t, char *);
187 extern int smb_config_setnum(smb_cfg_id_t, int64_t);
188 extern int smb_config_setbool(smb_cfg_id_t, boolean_t);
189 
190 extern uint8_t smb_config_get_fg_flag(void);
191 extern char *smb_config_get_localsid(void);
192 extern int smb_config_secmode_fromstr(char *);
193 extern char *smb_config_secmode_tostr(int);
194 extern int smb_config_get_secmode(void);
195 extern int smb_config_set_secmode(int);
196 extern int smb_config_set_idmap_domain(char *);
197 extern int smb_config_refresh_idmap(void);
198 extern int smb_config_getip(smb_cfg_id_t, smb_inaddr_t *);
199 extern void smb_config_get_version(smb_version_t *);
200 uint32_t smb_config_get_execinfo(char *, char *, size_t);
201 
202 
203 extern void smb_load_kconfig(smb_kmod_cfg_t *kcfg);
204 extern uint32_t smb_crc_gen(uint8_t *, size_t);
205 
206 extern boolean_t smb_match_netlogon_seqnum(void);
207 extern int smb_setdomainprops(char *, char *, char *);
208 extern void smb_update_netlogon_seqnum(void);
209 
210 /* maximum password length on Windows 2000 and above */
211 #define	SMB_PASSWD_MAXLEN	127
212 #define	SMB_USERNAME_MAXLEN	40
213 
214 typedef struct smb_joininfo {
215 	char domain_name[MAXHOSTNAMELEN];
216 	char domain_username[SMB_USERNAME_MAXLEN + 1];
217 	char domain_passwd[SMB_PASSWD_MAXLEN + 1];
218 	uint32_t mode;
219 } smb_joininfo_t;
220 
221 /* APIs to communicate with SMB daemon via door calls */
222 uint32_t smb_join(smb_joininfo_t *info);
223 bool_t smb_joininfo_xdr(XDR *, smb_joininfo_t *);
224 boolean_t smb_find_ads_server(char *, char *, int);
225 
226 extern void smb_config_getdomaininfo(char *, char *, char *, char *, char *);
227 extern void smb_config_setdomaininfo(char *, char *, char *, char *, char *);
228 extern uint32_t smb_get_dcinfo(char *, uint32_t, smb_inaddr_t *);
229 
230 CONTEXT_HANDLE(rpc_handle) rpc_handle_t;
231 
232 typedef struct smb_spooldoc {
233 	uint32_t	sd_magic;
234 	list_node_t	sd_lnd;
235 	smb_inaddr_t	sd_ipaddr;
236 	int		sd_spool_num;
237 	char		sd_username[MAXNAMELEN];
238 	char		sd_path[MAXPATHLEN];
239 	char		sd_doc_name[MAXNAMELEN];
240 	char		sd_printer_name[MAXPATHLEN];
241 	int32_t		sd_fd;
242 	rpc_handle_t	sd_handle;
243 } smb_spooldoc_t;
244 
245 int smb_kmod_get_spool_doc(uint32_t *, char *, char *, smb_inaddr_t *);
246 
247 /*
248  * buffer context structure. This is used to keep track of the buffer
249  * context.
250  *
251  * basep:  points to the beginning of the buffer
252  * curp:   points to the current offset
253  * endp:   points to the limit of the buffer
254  */
255 typedef struct {
256 	unsigned char *basep;
257 	unsigned char *curp;
258 	unsigned char *endp;
259 } smb_ctxbuf_t;
260 
261 extern int smb_ctxbuf_init(smb_ctxbuf_t *ctx, unsigned char *buf,
262     size_t buflen);
263 extern int smb_ctxbuf_len(smb_ctxbuf_t *ctx);
264 extern int smb_ctxbuf_printf(smb_ctxbuf_t *ctx, const char *fmt, ...);
265 
266 void smb_idmap_check(const char *, idmap_stat);
267 
268 /* Miscellaneous functions */
269 extern void hexdump(unsigned char *, int);
270 extern size_t bintohex(const char *, size_t, char *, size_t);
271 extern size_t hextobin(const char *, size_t, char *, size_t);
272 extern char *strstrip(char *, const char *);
273 extern char *strtrim(char *, const char *);
274 extern char *trim_whitespace(char *);
275 extern void randomize(char *, unsigned);
276 extern void rand_hash(unsigned char *, size_t, unsigned char *, size_t);
277 
278 extern int smb_getdomainname(char *, size_t);
279 extern int smb_getfqdomainname(char *, size_t);
280 
281 typedef enum smb_caseconv {
282 	SMB_CASE_PRESERVE = 0,
283 	SMB_CASE_UPPER,
284 	SMB_CASE_LOWER
285 } smb_caseconv_t;
286 
287 extern int smb_gethostname(char *, size_t, smb_caseconv_t);
288 extern int smb_getfqhostname(char *, size_t);
289 extern int smb_getnetbiosname(char *, size_t);
290 extern struct hostent *smb_gethostbyname(const char *, int *);
291 extern struct hostent *smb_gethostbyaddr(const char *, int, int, int *);
292 
293 #define	SMB_SAMACCT_MAXLEN	(NETBIOS_NAME_SZ + 1)
294 extern int smb_getsamaccount(char *, size_t);
295 
296 extern int smb_get_nameservers(smb_inaddr_t *, int);
297 extern void smb_tonetbiosname(char *, char *, char);
298 
299 extern int smb_chk_hostaccess(smb_inaddr_t *, char *);
300 
301 extern int smb_getnameinfo(smb_inaddr_t *, char *, int, int);
302 
303 void smb_trace(const char *s);
304 void smb_tracef(const char *fmt, ...);
305 
306 const char *xlate_nt_status(unsigned int);
307 
308 /*
309  * Authentication
310  */
311 
312 #define	SMBAUTH_LM_MAGIC_STR	"KGS!@#$%"
313 
314 #define	SMBAUTH_HASH_SZ		16	/* also LM/NTLM/NTLMv2 Hash size */
315 #define	SMBAUTH_LM_RESP_SZ	24	/* also NTLM Response size */
316 #define	SMBAUTH_LM_PWD_SZ	14	/* LM password size */
317 #define	SMBAUTH_V2_CLNT_CHALLENGE_SZ 8	/* both LMv2 and NTLMv2 */
318 #define	SMBAUTH_SESSION_KEY_SZ	SMBAUTH_HASH_SZ
319 #define	SMBAUTH_HEXHASH_SZ	(SMBAUTH_HASH_SZ * 2)
320 
321 #define	SMBAUTH_FAILURE		1
322 #define	SMBAUTH_SUCCESS		0
323 #define	MD_DIGEST_LEN		16
324 
325 /*
326  * Name Types
327  *
328  * The list of names near the end of the data blob (i.e. the ndb_names
329  * field of the smb_auth_data_blob_t data structure) can be classify into
330  * the following types:
331  *
332  * 0x0000 Indicates the end of the list.
333  * 0x0001 The name is a NetBIOS machine name (e.g. server name)
334  * 0x0002 The name is an NT Domain NetBIOS name.
335  * 0x0003 The name is the server's DNS hostname.
336  * 0x0004 The name is a W2K Domain name (a DNS name).
337  */
338 #define	SMBAUTH_NAME_TYPE_LIST_END		0x0000
339 #define	SMBAUTH_NAME_TYPE_SERVER_NETBIOS 	0x0001
340 #define	SMBAUTH_NAME_TYPE_DOMAIN_NETBIOS 	0x0002
341 #define	SMBAUTH_NAME_TYPE_SERVER_DNS		0x0003
342 #define	SMBAUTH_NAME_TYPE_DOMAIN_DNS 		0x0004
343 
344 /*
345  * smb_auth_name_entry_t
346  *
347  * Each name entry in the data blob consists of the following 3 fields:
348  *
349  * nne_type - name type
350  * nne_len  - the length of the name
351  * nne_name - the name, in uppercase UCS-2LE Unicode format
352  */
353 typedef struct smb_auth_name_entry {
354 	unsigned short nne_type;
355 	unsigned short nne_len;
356 	smb_wchar_t nne_name[SMB_PI_MAX_DOMAIN * 2];
357 } smb_auth_name_entry_t;
358 
359 /*
360  * smb_auth_data_blob
361  *
362  * The format of this NTLMv2 data blob structure is as follow:
363  *
364  *	- Blob Signature 0x01010000 (4 bytes)
365  * - Reserved (0x00000000) (4 bytes)
366  * - Timestamp Little-endian, 64-bit signed value representing
367  *   the number of tenths of a microsecond since January 1, 1601.
368  *   (8 bytes)
369  * - Client Challenge (8 bytes)
370  * - Unknown1 (4 bytes)
371  * - List of Target Information (variable length)
372  * - Unknown2 (4 bytes)
373  */
374 typedef struct smb_auth_data_blob {
375 	unsigned char ndb_signature[4];
376 	unsigned char ndb_reserved[4];
377 	uint64_t ndb_timestamp;
378 	unsigned char ndb_clnt_challenge[SMBAUTH_V2_CLNT_CHALLENGE_SZ];
379 	unsigned char ndb_unknown[4];
380 	smb_auth_name_entry_t ndb_names[2];
381 	unsigned char ndb_unknown2[4];
382 } smb_auth_data_blob_t;
383 
384 #define	SMBAUTH_BLOB_MAXLEN (sizeof (smb_auth_data_blob_t))
385 #define	SMBAUTH_CI_MAXLEN   SMBAUTH_LM_RESP_SZ
386 #define	SMBAUTH_CS_MAXLEN   (SMBAUTH_BLOB_MAXLEN + SMBAUTH_HASH_SZ)
387 
388 /*
389  * smb_auth_info_t
390  *
391  * The structure contains all the authentication information
392  * needed for the preparaton of the SMBSessionSetupAndx request
393  * and the user session key.
394  *
395  * hash      - NTLM hash
396  * hash_v2   - NTLMv2 hash
397  * ci_len    - the length of the case-insensitive password
398  * ci        - case-insensitive password
399  *             (If NTLMv2 authentication mechanism is used, it
400  *              represents the LMv2 response. Otherwise, it
401  *              is empty.)
402  * cs_len    - the length of the case-sensitive password
403  * cs        - case-sensitive password
404  *             (If NTLMv2 authentication mechanism is used, it
405  *              represents the NTLMv2 response. Otherwise, it
406  *              represents the NTLM response.)
407  * data_blob - NTLMv2 data blob
408  */
409 typedef struct smb_auth_info {
410 	unsigned char hash[SMBAUTH_HASH_SZ];
411 	unsigned char hash_v2[SMBAUTH_HASH_SZ];
412 	unsigned short ci_len;
413 	unsigned char ci[SMBAUTH_CI_MAXLEN];
414 	unsigned short cs_len;
415 	unsigned char cs[SMBAUTH_CS_MAXLEN];
416 	int lmcompatibility_lvl;
417 	smb_auth_data_blob_t data_blob;
418 } smb_auth_info_t;
419 
420 /*
421  * SMB password management
422  */
423 
424 #define	SMB_PWF_LM	0x01	/* LM hash is present */
425 #define	SMB_PWF_NT	0x02	/* NT hash is present */
426 #define	SMB_PWF_DISABLE	0x04	/* Account is disabled */
427 
428 typedef struct smb_passwd {
429 	uid_t		pw_uid;
430 	uint32_t	pw_flags;
431 	char		pw_name[SMB_USERNAME_MAXLEN];
432 	uint8_t		pw_lmhash[SMBAUTH_HASH_SZ];
433 	uint8_t		pw_nthash[SMBAUTH_HASH_SZ];
434 } smb_passwd_t;
435 
436 /*
437  * Control flags passed to smb_pwd_setcntl
438  */
439 #define	SMB_PWC_DISABLE	0x01
440 #define	SMB_PWC_ENABLE	0x02
441 #define	SMB_PWC_NOLM	0x04
442 
443 #define	SMB_PWE_SUCCESS		0
444 #define	SMB_PWE_USER_UNKNOWN	1
445 #define	SMB_PWE_USER_DISABLE	2
446 #define	SMB_PWE_CLOSE_FAILED	3
447 #define	SMB_PWE_OPEN_FAILED	4
448 #define	SMB_PWE_WRITE_FAILED	6
449 #define	SMB_PWE_UPDATE_FAILED	7
450 #define	SMB_PWE_STAT_FAILED	8
451 #define	SMB_PWE_BUSY		9
452 #define	SMB_PWE_DENIED		10
453 #define	SMB_PWE_SYSTEM_ERROR	11
454 #define	SMB_PWE_INVALID_PARAM	12
455 #define	SMB_PWE_NO_MEMORY	13
456 #define	SMB_PWE_MAX		14
457 
458 typedef struct smb_pwditer {
459 	void *spi_next;
460 } smb_pwditer_t;
461 
462 typedef struct smb_luser {
463 	char *su_name;
464 	char *su_fullname;
465 	char *su_desc;
466 	uint32_t su_rid;
467 	uint32_t su_ctrl;
468 } smb_luser_t;
469 
470 extern void smb_pwd_init(boolean_t);
471 extern void smb_pwd_fini(void);
472 extern smb_passwd_t *smb_pwd_getpwnam(const char *, smb_passwd_t *);
473 extern smb_passwd_t *smb_pwd_getpwuid(uid_t, smb_passwd_t *);
474 extern int smb_pwd_setpasswd(const char *, const char *);
475 extern int smb_pwd_setcntl(const char *, int);
476 
477 extern int smb_pwd_iteropen(smb_pwditer_t *);
478 extern smb_luser_t *smb_pwd_iterate(smb_pwditer_t *);
479 extern void smb_pwd_iterclose(smb_pwditer_t *);
480 
481 extern int smb_auth_qnd_unicode(smb_wchar_t *, const char *, int);
482 extern int smb_auth_hmac_md5(unsigned char *, int, unsigned char *, int,
483     unsigned char *);
484 
485 /*
486  * A variation on HMAC-MD5 known as HMACT64 is used by Windows systems.
487  * The HMACT64() function is the same as the HMAC-MD5() except that
488  * it truncates the input key to 64 bytes rather than hashing it down
489  * to 16 bytes using the MD5() function.
490  */
491 #define	SMBAUTH_HMACT64(D, Ds, K, Ks, digest) \
492 	smb_auth_hmac_md5(D, Ds, K, (Ks > 64) ? 64 : Ks, digest)
493 
494 extern int smb_auth_DES(unsigned char *, int, unsigned char *, int,
495     unsigned char *, int);
496 
497 extern int smb_auth_md4(unsigned char *, unsigned char *, int);
498 extern int smb_auth_lm_hash(const char *, unsigned char *);
499 extern int smb_auth_ntlm_hash(const char *, unsigned char *);
500 
501 extern int smb_auth_set_info(char *, char *,
502     unsigned char *, char *, unsigned char *,
503     int, int, smb_auth_info_t *);
504 
505 extern int smb_auth_ntlmv2_hash(unsigned char *,
506 	char *, char *, unsigned char *);
507 
508 extern int smb_auth_gen_session_key(smb_auth_info_t *, unsigned char *);
509 
510 boolean_t smb_auth_validate_lm(unsigned char *, uint32_t, smb_passwd_t *,
511     unsigned char *, int, char *, char *);
512 boolean_t smb_auth_validate_nt(unsigned char *, uint32_t, smb_passwd_t *,
513     unsigned char *, int, char *, char *, uchar_t *);
514 
515 /*
516  * SMB authenticated IPC
517  */
518 extern void smb_ipc_commit(void);
519 extern void smb_ipc_get_user(char *, size_t);
520 extern void smb_ipc_get_passwd(uint8_t *, size_t);
521 extern void smb_ipc_init(void);
522 extern void smb_ipc_rollback(void);
523 extern void smb_ipc_set(char *, uint8_t *);
524 
525 /*
526  * SMB MAC Signing
527  */
528 
529 #define	SMB_MAC_KEY_SZ	(SMBAUTH_SESSION_KEY_SZ + SMBAUTH_CS_MAXLEN)
530 #define	SMB_SIG_OFFS	14	/* signature field offset within header */
531 #define	SMB_SIG_SIZE	8	/* SMB signature size */
532 
533 /*
534  * Signing flags:
535  *
536  * SMB_SCF_ENABLE                 Signing is enabled.
537  *
538  * SMB_SCF_REQUIRED               Signing is enabled and required.
539  *                                This flag shouldn't be set if
540  *                                SMB_SCF_ENABLE isn't set.
541  *
542  * SMB_SCF_STARTED                Signing will start after receiving
543  *                                the first non-anonymous SessionSetup
544  *                                request.
545  *
546  * SMB_SCF_KEY_ISSET_THIS_LOGON   Indicates whether the MAC key has just
547  *                                been set for this logon. (prior to
548  *                                sending the SMBSessionSetup request)
549  *
550  */
551 #define	SMB_SCF_ENABLE		0x01
552 #define	SMB_SCF_REQUIRED	0x02
553 #define	SMB_SCF_STARTED		0x04
554 #define	SMB_SCF_KEY_ISSET_THIS_LOGON	0x08
555 
556 /*
557  * smb_sign_ctx
558  *
559  * SMB signing context.
560  *
561  *	ssc_seqnum				sequence number
562  *	ssc_keylen				mac key length
563  *	ssc_mid					multiplex id - reserved
564  *	ssc_flags				flags
565  *	ssc_mackey				mac key
566  *	ssc_sign				mac signature
567  *
568  */
569 typedef struct smb_sign_ctx {
570 	unsigned int ssc_seqnum;
571 	unsigned short ssc_keylen;
572 	unsigned short ssc_mid;
573 	unsigned int ssc_flags;
574 	unsigned char ssc_mackey[SMB_MAC_KEY_SZ];
575 	unsigned char ssc_sign[SMB_SIG_SIZE];
576 } smb_sign_ctx_t;
577 
578 extern int smb_mac_init(smb_sign_ctx_t *sign_ctx, smb_auth_info_t *auth);
579 extern int smb_mac_calc(smb_sign_ctx_t *sign_ctx,
580     const unsigned char *buf, size_t buf_len, unsigned char *mac_sign);
581 extern int smb_mac_chk(smb_sign_ctx_t *sign_ctx,
582     const unsigned char *buf, size_t buf_len);
583 extern int smb_mac_sign(smb_sign_ctx_t *sign_ctx,
584     unsigned char *buf, size_t buf_len);
585 extern void smb_mac_inc_seqnum(smb_sign_ctx_t *sign_ctx);
586 extern void smb_mac_dec_seqnum(smb_sign_ctx_t *sign_ctx);
587 
588 /*
589  * Each domain is categorized using the enum values below.
590  * The local domain refers to the local machine and is named
591  * after the local hostname. The primary domain is the domain
592  * that the system joined. All other domains are either
593  * trusted or untrusted, as defined by the primary domain PDC.
594  */
595 typedef enum smb_domain_type {
596 	SMB_DOMAIN_NULL,
597 	SMB_DOMAIN_BUILTIN,
598 	SMB_DOMAIN_LOCAL,
599 	SMB_DOMAIN_PRIMARY,
600 	SMB_DOMAIN_ACCOUNT,
601 	SMB_DOMAIN_TRUSTED,
602 	SMB_DOMAIN_UNTRUSTED,
603 	SMB_DOMAIN_NUM_TYPES
604 } smb_domain_type_t;
605 
606 /*
607  * Information specific to trusted domains
608  */
609 typedef struct smb_domain_trust {
610 	uint32_t		dti_trust_direction;
611 	uint32_t		dti_trust_type;
612 	uint32_t		dti_trust_attrs;
613 } smb_domain_trust_t;
614 
615 /*
616  * DNS information for domain types that this info is
617  * obtained/available. Currently this is only obtained
618  * for the primary domain.
619  */
620 typedef struct smb_domain_dns {
621 	char			ddi_forest[MAXHOSTNAMELEN];
622 	char			ddi_guid[UUID_PRINTABLE_STRING_LENGTH];
623 } smb_domain_dns_t;
624 
625 /*
626  * This is the information that is held about each domain.
627  */
628 typedef struct smb_domain {
629 	list_node_t		di_lnd;
630 	smb_domain_type_t	di_type;
631 	char			di_sid[SMB_SID_STRSZ];
632 	char			di_nbname[NETBIOS_NAME_SZ];
633 	char			di_fqname[MAXHOSTNAMELEN];
634 	smb_sid_t		*di_binsid;
635 	union {
636 		smb_domain_dns_t	di_dns;
637 		smb_domain_trust_t	di_trust;
638 	} di_u;
639 } smb_domain_t;
640 
641 typedef struct smb_trusted_domains {
642 	uint32_t	td_num;
643 	smb_domain_t	*td_domains;
644 } smb_trusted_domains_t;
645 
646 #define	SMB_DOMAIN_SUCCESS		0
647 #define	SMB_DOMAIN_NOMACHINE_SID	1
648 #define	SMB_DOMAIN_NODOMAIN_SID		2
649 #define	SMB_DOMAIN_NODOMAIN_NAME	3
650 #define	SMB_DOMAIN_INTERNAL_ERR		4
651 #define	SMB_DOMAIN_INVALID_ARG		5
652 #define	SMB_DOMAIN_NO_MEMORY		6
653 #define	SMB_DOMAIN_NO_CACHE		7
654 
655 /*
656  * This structure could contain information about
657  * the primary domain the name of selected domain controller
658  * for the primary domain and a list of trusted domains if
659  * any. The "ex" in the structure name stands for extended.
660  * This is to differentiate this structure from smb_domain_t
661  * which only contains information about a single domain.
662  */
663 typedef struct smb_domainex {
664 	char			d_dc[MAXHOSTNAMELEN];
665 	smb_domain_t		d_primary;
666 	smb_trusted_domains_t	d_trusted;
667 } smb_domainex_t;
668 
669 int smb_domain_init(uint32_t);
670 void smb_domain_fini(void);
671 void smb_domain_show(void);
672 void smb_domain_save(void);
673 boolean_t smb_domain_lookup_name(char *, smb_domain_t *);
674 boolean_t smb_domain_lookup_sid(smb_sid_t *, smb_domain_t *);
675 boolean_t smb_domain_lookup_type(smb_domain_type_t, smb_domain_t *);
676 boolean_t smb_domain_getinfo(smb_domainex_t *);
677 void smb_domain_update(smb_domainex_t *);
678 uint32_t smb_domain_start_update(void);
679 void smb_domain_end_update(void);
680 void smb_domain_set_basic_info(char *, char *, char *, smb_domain_t *);
681 void smb_domain_set_dns_info(char *, char *, char *, char *, char *,
682     smb_domain_t *);
683 void smb_domain_set_trust_info(char *, char *, char *,
684     uint32_t, uint32_t, uint32_t, smb_domain_t *);
685 
686 typedef struct smb_gsid {
687 	smb_sid_t *gs_sid;
688 	uint16_t gs_type;
689 } smb_gsid_t;
690 
691 typedef struct smb_giter {
692 	sqlite_vm	*sgi_vm;
693 	sqlite		*sgi_db;
694 	uint32_t	sgi_nerr;
695 } smb_giter_t;
696 
697 typedef struct smb_group {
698 	char			*sg_name;
699 	char			*sg_cmnt;
700 	uint32_t		sg_attr;
701 	uint32_t		sg_rid;
702 	smb_gsid_t		sg_id;
703 	smb_domain_type_t	sg_domain;
704 	smb_privset_t		*sg_privs;
705 	uint32_t		sg_nmembers;
706 	smb_gsid_t		*sg_members;
707 } smb_group_t;
708 
709 int smb_lgrp_start(void);
710 void smb_lgrp_stop(void);
711 int smb_lgrp_add(char *, char *);
712 int smb_lgrp_rename(char *, char *);
713 int smb_lgrp_delete(char *);
714 int smb_lgrp_setcmnt(char *, char *);
715 int smb_lgrp_getcmnt(char *, char **);
716 int smb_lgrp_getpriv(char *, uint8_t, boolean_t *);
717 int smb_lgrp_setpriv(char *, uint8_t, boolean_t);
718 int smb_lgrp_add_member(char *, smb_sid_t *, uint16_t);
719 int smb_lgrp_del_member(char *, smb_sid_t *, uint16_t);
720 int smb_lgrp_getbyname(char *, smb_group_t *);
721 int smb_lgrp_getbyrid(uint32_t, smb_domain_type_t, smb_group_t *);
722 void smb_lgrp_free(smb_group_t *);
723 uint32_t smb_lgrp_err_to_ntstatus(uint32_t);
724 boolean_t smb_lgrp_is_member(smb_group_t *, smb_sid_t *);
725 char *smb_lgrp_strerror(int);
726 int smb_lgrp_iteropen(smb_giter_t *);
727 void smb_lgrp_iterclose(smb_giter_t *);
728 boolean_t smb_lgrp_itererror(smb_giter_t *);
729 int smb_lgrp_iterate(smb_giter_t *, smb_group_t *);
730 
731 int smb_lookup_sid(const char *, lsa_account_t *);
732 int smb_lookup_name(const char *, sid_type_t, lsa_account_t *);
733 
734 #define	SMB_LGRP_SUCCESS		0
735 #define	SMB_LGRP_INVALID_ARG		1
736 #define	SMB_LGRP_INVALID_MEMBER		2
737 #define	SMB_LGRP_INVALID_NAME		3
738 #define	SMB_LGRP_NOT_FOUND		4
739 #define	SMB_LGRP_EXISTS			5
740 #define	SMB_LGRP_NO_SID			6
741 #define	SMB_LGRP_NO_LOCAL_SID		7
742 #define	SMB_LGRP_SID_NOTLOCAL		8
743 #define	SMB_LGRP_WKSID			9
744 #define	SMB_LGRP_NO_MEMORY		10
745 #define	SMB_LGRP_DB_ERROR		11
746 #define	SMB_LGRP_DBINIT_ERROR		12
747 #define	SMB_LGRP_INTERNAL_ERROR		13
748 #define	SMB_LGRP_MEMBER_IN_GROUP	14
749 #define	SMB_LGRP_MEMBER_NOT_IN_GROUP	15
750 #define	SMB_LGRP_NO_SUCH_PRIV		16
751 #define	SMB_LGRP_NO_SUCH_DOMAIN		17
752 #define	SMB_LGRP_PRIV_HELD		18
753 #define	SMB_LGRP_PRIV_NOT_HELD		19
754 #define	SMB_LGRP_BAD_DATA		20
755 #define	SMB_LGRP_NO_MORE		21
756 #define	SMB_LGRP_DBOPEN_FAILED		22
757 #define	SMB_LGRP_DBEXEC_FAILED		23
758 #define	SMB_LGRP_DBINIT_FAILED		24
759 #define	SMB_LGRP_DOMLKP_FAILED		25
760 #define	SMB_LGRP_DOMINS_FAILED		26
761 #define	SMB_LGRP_INSERT_FAILED		27
762 #define	SMB_LGRP_DELETE_FAILED		28
763 #define	SMB_LGRP_UPDATE_FAILED		29
764 #define	SMB_LGRP_LOOKUP_FAILED		30
765 #define	SMB_LGRP_NOT_SUPPORTED		31
766 #define	SMB_LGRP_OFFLINE		32
767 #define	SMB_LGRP_POSIXCREATE_FAILED	33
768 
769 #define	SMB_LGRP_COMMENT_MAX	256
770 
771 /*
772  * values for smb_nic_t.smbflags
773  */
774 #define	SMB_NICF_NBEXCL		0x01	/* Excluded from Netbios activities */
775 #define	SMB_NICF_ALIAS		0x02	/* This is an alias */
776 
777 /*
778  * smb_nic_t
779  *     nic_host		actual host name
780  *     nic_nbname	16-byte NetBIOS host name
781  */
782 typedef struct {
783 	char		nic_host[MAXHOSTNAMELEN];
784 	char		nic_nbname[NETBIOS_NAME_SZ];
785 	char		nic_cmnt[SMB_PI_MAX_COMMENT];
786 	char		nic_ifname[LIFNAMSIZ];
787 	smb_inaddr_t	nic_ip;
788 	uint32_t	nic_mask;
789 	uint32_t	nic_bcast;
790 	uint32_t	nic_smbflags;
791 	uint64_t	nic_sysflags;
792 } smb_nic_t;
793 
794 typedef struct smb_niciter {
795 	smb_nic_t ni_nic;
796 	int ni_cookie;
797 	int ni_seqnum;
798 } smb_niciter_t;
799 
800 /* NIC config functions */
801 int smb_nic_init(void);
802 void smb_nic_fini(void);
803 int smb_nic_getnum(char *);
804 int smb_nic_addhost(const char *, const char *, int, const char **);
805 int smb_nic_delhost(const char *);
806 int smb_nic_getfirst(smb_niciter_t *);
807 int smb_nic_getnext(smb_niciter_t *);
808 boolean_t smb_nic_is_local(smb_inaddr_t *);
809 boolean_t smb_nic_is_same_subnet(smb_inaddr_t *);
810 
811 #define	SMB_NIC_SUCCESS			0
812 #define	SMB_NIC_INVALID_ARG		1
813 #define	SMB_NIC_NOT_FOUND		2
814 #define	SMB_NIC_NO_HOST			3
815 #define	SMB_NIC_NO_MEMORY		4
816 #define	SMB_NIC_DB_ERROR		5
817 #define	SMB_NIC_DBINIT_ERROR		6
818 #define	SMB_NIC_BAD_DATA		7
819 #define	SMB_NIC_NO_MORE			8
820 #define	SMB_NIC_DBOPEN_FAILED		9
821 #define	SMB_NIC_DBEXEC_FAILED		10
822 #define	SMB_NIC_DBINIT_FAILED		11
823 #define	SMB_NIC_INSERT_FAILED		12
824 #define	SMB_NIC_DELETE_FAILED		13
825 #define	SMB_NIC_SOCK			14
826 #define	SMB_NIC_IOCTL			15
827 #define	SMB_NIC_CHANGED			16
828 
829 /*
830  * Well-known account structure
831  *
832  * A security identifier (SID) is a unique value of variable length that
833  * is used to identify a security principal or security group in
834  * Windows. Well-known SIDs are a group of SIDs that identify generic
835  * users or generic groups. Their values remain constant across all
836  * operating systems.
837  *
838  * This structure is defined to store these SIDs and other related
839  * information about them (e.g. account and domain names) in a
840  * predefined table.
841  */
842 typedef struct smb_wka {
843 	uint8_t		wka_domidx;
844 	char		*wka_sid;
845 	char		*wka_name;
846 	uint16_t	wka_type;
847 	uint16_t	wka_flags;
848 	char		*wka_desc;
849 	smb_sid_t	*wka_binsid;
850 } smb_wka_t;
851 
852 /*
853  * Defined values for smb_wka.wka_flags
854  *
855  * SMB_WKAFLG_LGRP_ENABLE		Can be added as local group
856  */
857 #define	SMB_WKAFLG_LGRP_ENABLE	0x1
858 
859 /*
860  * Well-known account interfaces
861  */
862 smb_wka_t *smb_wka_lookup_builtin(const char *);
863 smb_wka_t *smb_wka_lookup_name(const char *);
864 smb_wka_t *smb_wka_lookup_sid(smb_sid_t *);
865 smb_sid_t *smb_wka_get_sid(const char *);
866 char *smb_wka_get_domain(int);
867 uint32_t smb_wka_token_groups(uint32_t, smb_ids_t *);
868 
869 /*
870  * In memory account representation
871  */
872 typedef struct smb_account {
873 	char		*a_name;
874 	char		*a_domain;
875 	uint16_t	a_type;
876 	smb_sid_t	*a_sid;
877 	smb_sid_t	*a_domsid;
878 	uint32_t	a_rid;
879 } smb_account_t;
880 
881 uint32_t smb_sam_lookup_name(char *, char *, uint16_t, smb_account_t *);
882 uint32_t smb_sam_lookup_sid(smb_sid_t *, smb_account_t *);
883 int smb_sam_usr_cnt(void);
884 uint32_t smb_sam_usr_groups(smb_sid_t *, smb_ids_t *);
885 int smb_sam_grp_cnt(smb_domain_type_t);
886 void smb_account_free(smb_account_t *);
887 boolean_t smb_account_validate(smb_account_t *);
888 
889 /*
890  * Security Descriptor functions.
891  */
892 uint32_t smb_sd_read(char *path, smb_sd_t *, uint32_t);
893 uint32_t smb_sd_write(char *path, smb_sd_t *, uint32_t);
894 uint32_t smb_sd_fromfs(smb_fssd_t *, smb_sd_t *);
895 
896 /* Kernel Module Interface */
897 int smb_kmod_bind(void);
898 int smb_kmod_setcfg(smb_kmod_cfg_t *);
899 int smb_kmod_setgmtoff(int32_t);
900 int smb_kmod_start(int, int, int);
901 void smb_kmod_stop(void);
902 int smb_kmod_event_notify(uint32_t);
903 int smb_kmod_tcplisten(int);
904 int smb_kmod_nbtlisten(int);
905 int smb_kmod_tcpreceive(void);
906 int smb_kmod_nbtreceive(void);
907 void smb_kmod_unbind(void);
908 int smb_kmod_share(nvlist_t *);
909 int smb_kmod_unshare(nvlist_t *);
910 int smb_kmod_shareinfo(char *, boolean_t *);
911 int smb_kmod_get_open_num(smb_opennum_t *);
912 int smb_kmod_enum(smb_netsvc_t *);
913 smb_netsvc_t *smb_kmod_enum_init(smb_svcenum_t *);
914 void smb_kmod_enum_fini(smb_netsvc_t *);
915 int smb_kmod_session_close(const char *, const char *);
916 int smb_kmod_file_close(uint32_t);
917 
918 void smb_name_parse(char *, char **, char **);
919 uint32_t smb_name_validate_share(const char *);
920 uint32_t smb_name_validate_account(const char *);
921 uint32_t smb_name_validate_domain(const char *);
922 uint32_t smb_name_validate_nbdomain(const char *);
923 uint32_t smb_name_validate_workgroup(const char *);
924 uint32_t smb_name_validate_rpath(const char *);
925 
926 /*
927  * Interposer library validation
928  */
929 #define	SMBEX_VERSION	1
930 #define	SMBEX_KEY	"82273fdc-e32a-18c3-3f78-827929dc23ea"
931 typedef struct smbex_version {
932 	uint32_t v_version;
933 	uuid_t v_uuid;
934 } smbex_version_t;
935 void *smb_dlopen(void);
936 void smb_dlclose(void *);
937 
938 /*
939  * General purpose multi-thread safe cache based on
940  * AVL tree
941  */
942 typedef struct smb_cache {
943 	avl_tree_t	ch_cache;
944 	rwlock_t	ch_cache_lck;
945 	uint32_t	ch_state;
946 	uint32_t	ch_nops;
947 	uint32_t	ch_wait;
948 	uint32_t	ch_sequence;
949 	size_t		ch_datasz;
950 	mutex_t		ch_mtx;
951 	cond_t		ch_cv;
952 	void		(*ch_free)(void *);
953 	void		(*ch_copy)(const void *, void *, size_t);
954 } smb_cache_t;
955 
956 typedef struct smb_cache_node {
957 	avl_node_t	cn_link;
958 	void		*cn_data;
959 } smb_cache_node_t;
960 
961 typedef struct smb_cache_cursor {
962 	void		*cc_next;
963 	uint32_t	cc_sequence;
964 } smb_cache_cursor_t;
965 
966 /*
967  * flags used with smb_cache_add()
968  *
969  * SMB_CACHE_ADD	If object doesn't exist add, otherwise fail
970  * SMB_CACHE_REPLACE	If object doesn't exist add, otherwise replace
971  */
972 #define	SMB_CACHE_ADD		1
973 #define	SMB_CACHE_REPLACE	2
974 
975 void smb_cache_create(smb_cache_t *, uint32_t,
976     int (*cmpfn) (const void *, const void *), void (*freefn)(void *),
977     void (*copyfn)(const void *, void *, size_t), size_t);
978 void smb_cache_destroy(smb_cache_t *);
979 void smb_cache_flush(smb_cache_t *);
980 uint32_t smb_cache_num(smb_cache_t *);
981 int smb_cache_refreshing(smb_cache_t *);
982 void smb_cache_ready(smb_cache_t *);
983 int smb_cache_add(smb_cache_t *, const void *, int);
984 void smb_cache_remove(smb_cache_t *, const void *);
985 void smb_cache_iterinit(smb_cache_t *, smb_cache_cursor_t *);
986 boolean_t smb_cache_iterate(smb_cache_t *, smb_cache_cursor_t *, void *);
987 
988 /*
989  * Values returned by smb_reparse_stat()
990  */
991 #define	SMB_REPARSE_NOTFOUND	1	/* object does not exist */
992 #define	SMB_REPARSE_NOTREPARSE	2	/* object is NOT a reparse point */
993 #define	SMB_REPARSE_ISREPARSE	3	/* object is a reparse point */
994 
995 /*
996  * Reparse Point API
997  */
998 int smb_reparse_stat(const char *, uint32_t *);
999 int smb_reparse_svcadd(const char *, const char *, const char *);
1000 int smb_reparse_svcdel(const char *, const char *);
1001 int smb_reparse_svcget(const char *, const char *, char **);
1002 
1003 uint32_t smb_get_txid(void);
1004 
1005 #define	SMB_LOG_LINE_SZ		256
1006 
1007 typedef uint32_t	smb_log_hdl_t;
1008 
1009 typedef struct smb_log_item {
1010 	list_node_t	li_lnd;
1011 	char		li_msg[SMB_LOG_LINE_SZ];
1012 } smb_log_item_t;
1013 
1014 typedef struct smb_log {
1015 	smb_log_hdl_t	l_handle;
1016 	int		l_cnt;
1017 	int		l_max_cnt;
1018 	mutex_t		l_mtx;
1019 	list_t		l_list;
1020 	char		l_file[MAXPATHLEN];
1021 } smb_log_t;
1022 
1023 typedef struct smb_loglist_item {
1024 	list_node_t	lli_lnd;
1025 	smb_log_t	lli_log;
1026 } smb_loglist_item_t;
1027 
1028 typedef struct smb_loglist {
1029 	mutex_t		ll_mtx;
1030 	list_t		ll_list;
1031 } smb_loglist_t;
1032 
1033 smb_log_hdl_t smb_log_create(int, char *);
1034 void smb_log(smb_log_hdl_t, int, const char *, ...);
1035 void smb_log_dumpall(void);
1036 
1037 #ifdef	__cplusplus
1038 }
1039 #endif
1040 
1041 #endif	/* _LIBSMB_H */
1042