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