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