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