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