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