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