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