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