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