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