xref: /illumos-gate/usr/src/lib/smbsrv/libsmb/common/smb_info.c (revision 6e6545bfaed3bab9ce836ee82d1abd8f2edba89a)
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 (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
23  * Copyright 2016 Nexenta Systems, Inc.  All rights reserved.
24  */
25 
26 #include <sys/types.h>
27 #include <sys/sockio.h>
28 #include <sys/socket.h>
29 #include <sys/utsname.h>
30 
31 #include <stdarg.h>
32 #include <unistd.h>
33 #include <stdlib.h>
34 #include <time.h>
35 #include <synch.h>
36 #include <syslog.h>
37 #include <string.h>
38 #include <strings.h>
39 #include <errno.h>
40 #include <net/if.h>
41 #include <netdb.h>
42 #include <netinet/in.h>
43 #include <arpa/nameser.h>
44 #include <resolv.h>
45 
46 #include <smbsrv/smbinfo.h>
47 #include <smbsrv/netbios.h>
48 #include <smbsrv/libsmb.h>
49 #include <assert.h>
50 
51 static mutex_t seqnum_mtx;
52 
53 /*
54  * IPC connection information that may be passed to the SMB Redirector.
55  */
56 typedef struct {
57 	char	user[SMB_USERNAME_MAXLEN];
58 	uint8_t	passwd[SMBAUTH_HASH_SZ];
59 } smb_ipc_t;
60 
61 static smb_ipc_t	ipc_info;
62 static smb_ipc_t	ipc_orig_info;
63 static rwlock_t		smb_ipc_lock;
64 
65 /*
66  * These three parameters are all related:
67  *	skc_initial_credits
68  *	skc_maximum_credits
69  *	skc_maxworkers	(max worker threads)
70  * They must be in non-decreasing order.  Get the values in order:
71  *	maxworkers, maximum_credits, initial_credits
72  * enforcing maximum values and relations as we go.  Then in the
73  * opposite order check minimum values and relations.
74  *
75  * smb_config_getnum puts a zero in the &citem if it fails getting
76  * the parameter value.  When fetch parameters for which zero is OK,
77  * the return code is intentionally ignored.
78  */
79 void
80 smb_load_kconfig(smb_kmod_cfg_t *kcfg)
81 {
82 	struct utsname uts;
83 	int64_t citem;
84 	int rc;
85 
86 	bzero(kcfg, sizeof (smb_kmod_cfg_t));
87 
88 	/*
89 	 * skc_maxworkers (max. no. of taskq worker threads)
90 	 */
91 	rc = smb_config_getnum(SMB_CI_MAX_WORKERS, &citem);
92 	if (rc != SMBD_SMF_OK)
93 		citem = SMB_PI_MAX_WORKERS_DEF;
94 	if (citem > SMB_PI_MAX_WORKERS_MAX)
95 		citem = SMB_PI_MAX_WORKERS_MAX;
96 	kcfg->skc_maxworkers = (uint32_t)citem;
97 
98 	/*
99 	 * The largest number of credits we let a single client have.
100 	 * It never makes sense for this to be > max_workers
101 	 */
102 	rc = smb_config_getnum(SMB_CI_MAXIMUM_CREDITS, &citem);
103 	if (rc != SMBD_SMF_OK)
104 		citem = SMB_PI_MAXIMUM_CREDITS_DEF;
105 	if (citem > SMB_PI_MAXIMUM_CREDITS_MAX)
106 		citem = SMB_PI_MAXIMUM_CREDITS_MAX;
107 	kcfg->skc_maximum_credits = (uint16_t)citem;
108 	if (kcfg->skc_maximum_credits > kcfg->skc_maxworkers)
109 		kcfg->skc_maximum_credits = (uint16_t)kcfg->skc_maxworkers;
110 
111 	/*
112 	 * The number of credits we give a client initially.
113 	 * Should be enough for a "light" workload, as the
114 	 * client will request additional credits when the
115 	 * workload increases.  Must be <= maximum_credits.
116 	 */
117 	rc = smb_config_getnum(SMB_CI_INITIAL_CREDITS, &citem);
118 	if (rc != SMBD_SMF_OK)
119 		citem = SMB_PI_INITIAL_CREDITS_DEF;
120 	if (citem > SMB_PI_INITIAL_CREDITS_MAX)
121 		citem = SMB_PI_INITIAL_CREDITS_MAX;
122 	kcfg->skc_initial_credits = (uint16_t)citem;
123 	if (kcfg->skc_initial_credits > kcfg->skc_maximum_credits)
124 		kcfg->skc_initial_credits = kcfg->skc_maximum_credits;
125 
126 	/*
127 	 * Now enforce minimums, smaller to larger.
128 	 */
129 	if (kcfg->skc_initial_credits < SMB_PI_INITIAL_CREDITS_MIN)
130 		kcfg->skc_initial_credits = SMB_PI_INITIAL_CREDITS_MIN;
131 
132 	if (kcfg->skc_maximum_credits < SMB_PI_MAXIMUM_CREDITS_MIN)
133 		kcfg->skc_maximum_credits = SMB_PI_MAXIMUM_CREDITS_MIN;
134 	if (kcfg->skc_maximum_credits < kcfg->skc_initial_credits)
135 		kcfg->skc_maximum_credits = kcfg->skc_initial_credits;
136 
137 	if (kcfg->skc_maxworkers < SMB_PI_MAX_WORKERS_MIN)
138 		kcfg->skc_maxworkers = SMB_PI_MAX_WORKERS_MIN;
139 	if (kcfg->skc_maxworkers < kcfg->skc_maximum_credits)
140 		kcfg->skc_maxworkers = kcfg->skc_maximum_credits;
141 
142 	(void) smb_config_getnum(SMB_CI_KEEPALIVE, &citem);
143 	kcfg->skc_keepalive = (uint32_t)citem;
144 	if ((kcfg->skc_keepalive != 0) &&
145 	    (kcfg->skc_keepalive < SMB_PI_KEEP_ALIVE_MIN))
146 		kcfg->skc_keepalive = SMB_PI_KEEP_ALIVE_MIN;
147 
148 	(void) smb_config_getnum(SMB_CI_MAX_CONNECTIONS, &citem);
149 	kcfg->skc_maxconnections = (uint32_t)citem;
150 	kcfg->skc_restrict_anon = smb_config_getbool(SMB_CI_RESTRICT_ANON);
151 	kcfg->skc_signing_enable = smb_config_getbool(SMB_CI_SIGNING_ENABLE);
152 	kcfg->skc_signing_required = smb_config_getbool(SMB_CI_SIGNING_REQD);
153 	kcfg->skc_netbios_enable = smb_config_getbool(SMB_CI_NETBIOS_ENABLE);
154 	kcfg->skc_ipv6_enable = smb_config_getbool(SMB_CI_IPV6_ENABLE);
155 	kcfg->skc_print_enable = smb_config_getbool(SMB_CI_PRINT_ENABLE);
156 	kcfg->skc_oplock_enable = smb_config_getbool(SMB_CI_OPLOCK_ENABLE);
157 	kcfg->skc_sync_enable = smb_config_getbool(SMB_CI_SYNC_ENABLE);
158 	kcfg->skc_traverse_mounts = smb_config_getbool(SMB_CI_TRAVERSE_MOUNTS);
159 	kcfg->skc_max_protocol = smb_config_get_max_protocol();
160 	kcfg->skc_secmode = smb_config_get_secmode();
161 	kcfg->skc_encrypt = smb_config_get_require(SMB_CI_ENCRYPT);
162 
163 	(void) smb_getdomainname(kcfg->skc_nbdomain,
164 	    sizeof (kcfg->skc_nbdomain));
165 	(void) smb_getfqdomainname(kcfg->skc_fqdn,
166 	    sizeof (kcfg->skc_fqdn));
167 	(void) smb_getnetbiosname(kcfg->skc_hostname,
168 	    sizeof (kcfg->skc_hostname));
169 	(void) smb_config_getstr(SMB_CI_SYS_CMNT, kcfg->skc_system_comment,
170 	    sizeof (kcfg->skc_system_comment));
171 	smb_config_get_version(&kcfg->skc_version);
172 	kcfg->skc_execflags = smb_config_get_execinfo(NULL, NULL, 0);
173 	if (smb_config_get_localuuid(kcfg->skc_machine_uuid) < 0) {
174 		syslog(LOG_ERR, "smb_load_kconfig: no machine_uuid");
175 		uuid_generate_time(kcfg->skc_machine_uuid);
176 	}
177 	/* skc_negtok, skc_negtok_len: see smbd_authsvc.c */
178 
179 	(void) uname(&uts);
180 	(void) snprintf(kcfg->skc_native_os, sizeof (kcfg->skc_native_os),
181 	    "%s %s %s", uts.sysname, uts.release, uts.version);
182 
183 	(void) strlcpy(kcfg->skc_native_lm, "Native SMB service",
184 	    sizeof (kcfg->skc_native_lm));
185 }
186 
187 /*
188  * Get the current system NetBIOS name.  The hostname is truncated at
189  * the first `.` or 15 bytes, whichever occurs first, and converted
190  * to uppercase (by smb_gethostname).  Text that appears after the
191  * first '.' is considered to be part of the NetBIOS scope.
192  *
193  * Returns 0 on success, otherwise -1 to indicate an error.
194  */
195 int
196 smb_getnetbiosname(char *buf, size_t buflen)
197 {
198 	if (smb_gethostname(buf, buflen, SMB_CASE_UPPER) != 0)
199 		return (-1);
200 
201 	if (buflen >= NETBIOS_NAME_SZ)
202 		buf[NETBIOS_NAME_SZ - 1] = '\0';
203 
204 	return (0);
205 }
206 
207 /*
208  * Get the SAM account of the current system.
209  * Returns 0 on success, otherwise, -1 to indicate an error.
210  */
211 int
212 smb_getsamaccount(char *buf, size_t buflen)
213 {
214 	if (smb_getnetbiosname(buf, buflen - 1) != 0)
215 		return (-1);
216 
217 	(void) strlcat(buf, "$", buflen);
218 	return (0);
219 }
220 
221 /*
222  * Get the current system node name.  The returned name is guaranteed
223  * to be null-terminated (gethostname may not null terminate the name).
224  * If the hostname has been fully-qualified for some reason, the domain
225  * part will be removed.  The returned hostname is converted to the
226  * specified case (lower, upper, or preserved).
227  *
228  * If gethostname fails, the returned buffer will contain an empty
229  * string.
230  */
231 int
232 smb_gethostname(char *buf, size_t buflen, smb_caseconv_t which)
233 {
234 	char *p;
235 
236 	if (buf == NULL || buflen == 0)
237 		return (-1);
238 
239 	if (gethostname(buf, buflen) != 0) {
240 		*buf = '\0';
241 		return (-1);
242 	}
243 
244 	buf[buflen - 1] = '\0';
245 
246 	if ((p = strchr(buf, '.')) != NULL)
247 		*p = '\0';
248 
249 	switch (which) {
250 	case SMB_CASE_LOWER:
251 		(void) smb_strlwr(buf);
252 		break;
253 
254 	case SMB_CASE_UPPER:
255 		(void) smb_strupr(buf);
256 		break;
257 
258 	case SMB_CASE_PRESERVE:
259 	default:
260 		break;
261 	}
262 
263 	return (0);
264 }
265 
266 /*
267  * Obtain the fully-qualified name for this machine in lower case.  If
268  * the hostname is fully-qualified, accept it.  Otherwise, try to find an
269  * appropriate domain name to append to the hostname.
270  */
271 int
272 smb_getfqhostname(char *buf, size_t buflen)
273 {
274 	char hostname[MAXHOSTNAMELEN];
275 	char domain[MAXHOSTNAMELEN];
276 
277 	hostname[0] = '\0';
278 	domain[0] = '\0';
279 
280 	if (smb_gethostname(hostname, MAXHOSTNAMELEN,
281 	    SMB_CASE_LOWER) != 0)
282 		return (-1);
283 
284 	if (smb_getfqdomainname(domain, MAXHOSTNAMELEN) != 0)
285 		return (-1);
286 
287 	if (hostname[0] == '\0')
288 		return (-1);
289 
290 	if (domain[0] == '\0') {
291 		(void) strlcpy(buf, hostname, buflen);
292 		return (0);
293 	}
294 
295 	(void) snprintf(buf, buflen, "%s.%s", hostname, domain);
296 	return (0);
297 }
298 
299 /*
300  * smb_getdomainname
301  *
302  * Returns NETBIOS name of the domain if the system is in domain
303  * mode. Or returns workgroup name if the system is in workgroup
304  * mode.
305  */
306 int
307 smb_getdomainname(char *buf, size_t buflen)
308 {
309 	int rc;
310 
311 	if (buf == NULL || buflen == 0)
312 		return (-1);
313 
314 	*buf = '\0';
315 	rc = smb_config_getstr(SMB_CI_DOMAIN_NAME, buf, buflen);
316 
317 	if ((rc != SMBD_SMF_OK) || (*buf == '\0'))
318 		return (-1);
319 
320 	return (0);
321 }
322 
323 /*
324  * smb_getfqdomainname
325  *
326  * In the system is in domain mode, the dns_domain property value
327  * is returned. Otherwise, it returns the local domain obtained via
328  * resolver.
329  *
330  * Returns 0 upon success.  Otherwise, returns -1.
331  */
332 int
333 smb_getfqdomainname(char *buf, size_t buflen)
334 {
335 	struct __res_state res_state;
336 	int rc;
337 
338 	if (buf == NULL || buflen == 0)
339 		return (-1);
340 
341 	*buf = '\0';
342 	if (smb_config_get_secmode() == SMB_SECMODE_DOMAIN) {
343 		rc = smb_config_getstr(SMB_CI_DOMAIN_FQDN, buf, buflen);
344 
345 		if ((rc != SMBD_SMF_OK) || (*buf == '\0'))
346 			return (-1);
347 	} else {
348 		bzero(&res_state, sizeof (struct __res_state));
349 		if (res_ninit(&res_state))
350 			return (-1);
351 
352 		if (*res_state.defdname == '\0') {
353 			res_ndestroy(&res_state);
354 			return (-1);
355 		}
356 
357 		(void) strlcpy(buf, res_state.defdname, buflen);
358 		res_ndestroy(&res_state);
359 		rc = 0;
360 	}
361 
362 	return (rc);
363 }
364 
365 
366 /*
367  * smb_set_machine_passwd
368  *
369  * This function should be used when setting the machine password property.
370  * The associated sequence number is incremented.
371  */
372 static int
373 smb_set_machine_passwd(char *passwd)
374 {
375 	int64_t num;
376 	int rc = -1;
377 
378 	if (smb_config_set(SMB_CI_MACHINE_PASSWD, passwd) != SMBD_SMF_OK)
379 		return (-1);
380 
381 	(void) mutex_lock(&seqnum_mtx);
382 	(void) smb_config_getnum(SMB_CI_KPASSWD_SEQNUM, &num);
383 	if (smb_config_setnum(SMB_CI_KPASSWD_SEQNUM, ++num)
384 	    == SMBD_SMF_OK)
385 		rc = 0;
386 	(void) mutex_unlock(&seqnum_mtx);
387 	return (rc);
388 }
389 
390 static int
391 smb_get_machine_passwd(uint8_t *buf, size_t buflen)
392 {
393 	char pwd[SMB_PASSWD_MAXLEN + 1];
394 	int rc;
395 
396 	if (buflen < SMBAUTH_HASH_SZ)
397 		return (-1);
398 
399 	rc = smb_config_getstr(SMB_CI_MACHINE_PASSWD, pwd, sizeof (pwd));
400 	if ((rc != SMBD_SMF_OK) || *pwd == '\0')
401 		return (-1);
402 
403 	if (smb_auth_ntlm_hash(pwd, buf) != 0)
404 		return (-1);
405 
406 	return (rc);
407 }
408 
409 /*
410  * Set up IPC connection credentials.
411  */
412 void
413 smb_ipc_init(void)
414 {
415 	int rc;
416 
417 	(void) rw_wrlock(&smb_ipc_lock);
418 	bzero(&ipc_info, sizeof (smb_ipc_t));
419 	bzero(&ipc_orig_info, sizeof (smb_ipc_t));
420 
421 	(void) smb_getsamaccount(ipc_info.user, SMB_USERNAME_MAXLEN);
422 	rc = smb_get_machine_passwd(ipc_info.passwd, SMBAUTH_HASH_SZ);
423 	if (rc != 0)
424 		*ipc_info.passwd = 0;
425 	(void) rw_unlock(&smb_ipc_lock);
426 
427 }
428 
429 /*
430  * Set the IPC username and password hash in memory.  If the domain
431  * join succeeds, the credentials will be committed for use with
432  * authenticated IPC.  Otherwise, they should be rolled back.
433  */
434 void
435 smb_ipc_set(char *plain_user, uint8_t *passwd_hash)
436 {
437 	(void) rw_wrlock(&smb_ipc_lock);
438 	(void) strlcpy(ipc_info.user, plain_user, sizeof (ipc_info.user));
439 	(void) memcpy(ipc_info.passwd, passwd_hash, SMBAUTH_HASH_SZ);
440 	(void) rw_unlock(&smb_ipc_lock);
441 
442 }
443 
444 /*
445  * Save the host credentials to be used for authenticated IPC.
446  * The credentials are also saved to the original IPC info as
447  * rollback data in case the join domain process fails later.
448  */
449 void
450 smb_ipc_commit(void)
451 {
452 	(void) rw_wrlock(&smb_ipc_lock);
453 	(void) smb_getsamaccount(ipc_info.user, SMB_USERNAME_MAXLEN);
454 	(void) smb_get_machine_passwd(ipc_info.passwd, SMBAUTH_HASH_SZ);
455 	(void) memcpy(&ipc_orig_info, &ipc_info, sizeof (smb_ipc_t));
456 	(void) rw_unlock(&smb_ipc_lock);
457 }
458 
459 /*
460  * Restore the original credentials
461  */
462 void
463 smb_ipc_rollback(void)
464 {
465 	(void) rw_wrlock(&smb_ipc_lock);
466 	(void) strlcpy(ipc_info.user, ipc_orig_info.user,
467 	    sizeof (ipc_info.user));
468 	(void) memcpy(ipc_info.passwd, ipc_orig_info.passwd,
469 	    sizeof (ipc_info.passwd));
470 	(void) rw_unlock(&smb_ipc_lock);
471 }
472 
473 void
474 smb_ipc_get_user(char *buf, size_t buflen)
475 {
476 	(void) rw_rdlock(&smb_ipc_lock);
477 	(void) strlcpy(buf, ipc_info.user, buflen);
478 	(void) rw_unlock(&smb_ipc_lock);
479 }
480 
481 void
482 smb_ipc_get_passwd(uint8_t *buf, size_t buflen)
483 {
484 	if (buflen < SMBAUTH_HASH_SZ)
485 		return;
486 
487 	(void) rw_rdlock(&smb_ipc_lock);
488 	(void) memcpy(buf, ipc_info.passwd, SMBAUTH_HASH_SZ);
489 	(void) rw_unlock(&smb_ipc_lock);
490 }
491 
492 /*
493  * smb_match_netlogon_seqnum
494  *
495  * A sequence number is associated with each machine password property
496  * update and the netlogon credential chain setup. If the
497  * sequence numbers don't match, a NETLOGON credential chain
498  * establishment is required.
499  *
500  * Returns 0 if kpasswd_seqnum equals to netlogon_seqnum. Otherwise,
501  * returns -1.
502  */
503 boolean_t
504 smb_match_netlogon_seqnum(void)
505 {
506 	int64_t setpasswd_seqnum;
507 	int64_t netlogon_seqnum;
508 
509 	(void) mutex_lock(&seqnum_mtx);
510 	(void) smb_config_getnum(SMB_CI_KPASSWD_SEQNUM, &setpasswd_seqnum);
511 	(void) smb_config_getnum(SMB_CI_NETLOGON_SEQNUM, &netlogon_seqnum);
512 	(void) mutex_unlock(&seqnum_mtx);
513 	return (setpasswd_seqnum == netlogon_seqnum);
514 }
515 
516 /*
517  * smb_setdomainprops
518  *
519  * This function should be called after joining an AD to
520  * set all the domain related SMF properties.
521  *
522  * The kpasswd_domain property is the AD domain to which the system
523  * is joined via kclient. If this function is invoked by the SMB
524  * daemon, fqdn should be set to NULL.
525  */
526 int
527 smb_setdomainprops(char *fqdn, char *server, char *passwd)
528 {
529 	if (server == NULL || passwd == NULL)
530 		return (-1);
531 
532 	if ((*server == '\0') || (*passwd == '\0'))
533 		return (-1);
534 
535 	if (fqdn && (smb_config_set(SMB_CI_KPASSWD_DOMAIN, fqdn) != 0))
536 		return (-1);
537 
538 	if (smb_config_set(SMB_CI_KPASSWD_SRV, server) != 0)
539 		return (-1);
540 
541 	if (smb_set_machine_passwd(passwd) != 0) {
542 		syslog(LOG_ERR, "smb_setdomainprops: failed to set"
543 		    " machine account password");
544 		return (-1);
545 	}
546 
547 	/*
548 	 * If we successfully create a trust account, we mark
549 	 * ourselves as a domain member in the environment so
550 	 * that we use the SAMLOGON version of the NETLOGON
551 	 * PDC location protocol.
552 	 */
553 	(void) smb_config_setbool(SMB_CI_DOMAIN_MEMB, B_TRUE);
554 
555 	return (0);
556 }
557 
558 /*
559  * smb_update_netlogon_seqnum
560  *
561  * This function should only be called upon a successful netlogon
562  * credential chain establishment to set the sequence number of the
563  * netlogon to match with that of the kpasswd.
564  */
565 void
566 smb_update_netlogon_seqnum(void)
567 {
568 	int64_t num;
569 
570 	(void) mutex_lock(&seqnum_mtx);
571 	(void) smb_config_getnum(SMB_CI_KPASSWD_SEQNUM, &num);
572 	(void) smb_config_setnum(SMB_CI_NETLOGON_SEQNUM, num);
573 	(void) mutex_unlock(&seqnum_mtx);
574 }
575 
576 
577 /*
578  * Temporary fbt for dtrace until user space sdt enabled.
579  */
580 void
581 smb_tracef(const char *fmt, ...)
582 {
583 	va_list ap;
584 	char buf[128];
585 
586 	va_start(ap, fmt);
587 	(void) vsnprintf(buf, 128, fmt, ap);
588 	va_end(ap);
589 
590 	smb_trace(buf);
591 }
592 
593 /*
594  * Temporary fbt for dtrace until user space sdt enabled.
595  *
596  * This function is designed to be used with dtrace, i.e. see:
597  * usr/src/cmd/smbsrv/dtrace/smbd-all.d
598  *
599  * Outside of dtrace, the messages passed to this function usually
600  * lack sufficient context to be useful, so we don't log them.
601  */
602 /* ARGSUSED */
603 void
604 smb_trace(const char *s)
605 {
606 }
607 
608 /*
609  * smb_tonetbiosname
610  *
611  * Creates a NetBIOS name based on the given name and suffix.
612  * NetBIOS name is 15 capital characters, padded with space if needed
613  * and the 16th byte is the suffix.
614  */
615 void
616 smb_tonetbiosname(char *name, char *nb_name, char suffix)
617 {
618 	char tmp_name[NETBIOS_NAME_SZ];
619 	smb_wchar_t wtmp_name[NETBIOS_NAME_SZ];
620 	int len;
621 	size_t rc;
622 
623 	len = 0;
624 	rc = smb_mbstowcs(wtmp_name, (const char *)name, NETBIOS_NAME_SZ);
625 
626 	if (rc != (size_t)-1) {
627 		wtmp_name[NETBIOS_NAME_SZ - 1] = 0;
628 		rc = ucstooem(tmp_name, wtmp_name, NETBIOS_NAME_SZ,
629 		    OEM_CPG_850);
630 		if (rc > 0)
631 			len = strlen(tmp_name);
632 	}
633 
634 	(void) memset(nb_name, ' ', NETBIOS_NAME_SZ - 1);
635 	if (len) {
636 		(void) smb_strupr(tmp_name);
637 		(void) memcpy(nb_name, tmp_name, len);
638 	}
639 	nb_name[NETBIOS_NAME_SZ - 1] = suffix;
640 }
641 
642 int
643 smb_get_nameservers(smb_inaddr_t *ips, int sz)
644 {
645 	union res_sockaddr_union set[MAXNS];
646 	int i, cnt;
647 	struct __res_state res_state;
648 	char ipstr[INET6_ADDRSTRLEN];
649 
650 	if (ips == NULL)
651 		return (0);
652 
653 	bzero(&res_state, sizeof (struct __res_state));
654 	if (res_ninit(&res_state) < 0)
655 		return (0);
656 
657 	cnt = res_getservers(&res_state, set, MAXNS);
658 	for (i = 0; i < cnt; i++) {
659 		if (i >= sz)
660 			break;
661 		ips[i].a_family = AF_INET;
662 		bcopy(&set[i].sin.sin_addr, &ips[i].a_ipv4, NS_INADDRSZ);
663 		if (inet_ntop(AF_INET, &ips[i].a_ipv4, ipstr,
664 		    INET_ADDRSTRLEN)) {
665 			syslog(LOG_DEBUG, "Found %s name server\n", ipstr);
666 			continue;
667 		}
668 		ips[i].a_family = AF_INET6;
669 		bcopy(&set[i].sin.sin_addr, &ips[i].a_ipv6, NS_IN6ADDRSZ);
670 		if (inet_ntop(AF_INET6, &ips[i].a_ipv6, ipstr,
671 		    INET6_ADDRSTRLEN)) {
672 			syslog(LOG_DEBUG, "Found %s name server\n", ipstr);
673 		}
674 	}
675 	res_ndestroy(&res_state);
676 	return (i);
677 }
678 
679 /*
680  * smb_gethostbyname
681  *
682  * Looks up a host by the given name. The host entry can come
683  * from any of the sources for hosts specified in the
684  * /etc/nsswitch.conf and the NetBIOS cache.
685  *
686  * XXX Invokes nbt_name_resolve API once the NBTD is integrated
687  * to look in the NetBIOS cache if getipnodebyname fails.
688  *
689  * Caller should invoke freehostent to free the returned hostent.
690  */
691 struct hostent *
692 smb_gethostbyname(const char *name, int *err_num)
693 {
694 	struct hostent *h;
695 
696 	h = getipnodebyname(name, AF_INET, 0, err_num);
697 	if ((h == NULL) || h->h_length != INADDRSZ)
698 		h = getipnodebyname(name, AF_INET6, AI_DEFAULT, err_num);
699 	return (h);
700 }
701 
702 /*
703  * smb_gethostbyaddr
704  *
705  * Looks up a host by the given IP address. The host entry can come
706  * from any of the sources for hosts specified in the
707  * /etc/nsswitch.conf and the NetBIOS cache.
708  *
709  * XXX Invokes nbt API to resolve name by IP once the NBTD is integrated
710  * to look in the NetBIOS cache if getipnodebyaddr fails.
711  *
712  * Caller should invoke freehostent to free the returned hostent.
713  */
714 struct hostent *
715 smb_gethostbyaddr(const char *addr, int len, int type, int *err_num)
716 {
717 	struct hostent *h;
718 
719 	h = getipnodebyaddr(addr, len, type, err_num);
720 
721 	return (h);
722 }
723