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