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