1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
|
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
* Copyright 2015 Nexenta Systems, Inc. All rights reserved.
*/
#include <syslog.h>
#include <synch.h>
#include <pthread.h>
#include <unistd.h>
#include <string.h>
#include <strings.h>
#include <errno.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <smbsrv/libsmb.h>
#include <smbsrv/libsmbns.h>
#include <smbsrv/libmlsvc.h>
#include <smbsrv/smbinfo.h>
#include "smbd.h"
#define SMBD_DC_MONITOR_ATTEMPTS 3
#define SMBD_DC_MONITOR_RETRY_INTERVAL 3 /* seconds */
#define SMBD_DC_MONITOR_INTERVAL 60 /* seconds */
extern smbd_t smbd;
static mutex_t smbd_dc_mutex;
static cond_t smbd_dc_cv;
static void *smbd_dc_monitor(void *);
static void smbd_dc_update(void);
static int smbd_dc_check(smb_domainex_t *);
/* Todo: static boolean_t smbd_set_netlogon_cred(void); */
static void smbd_join_workgroup(smb_joininfo_t *, smb_joinres_t *);
static void smbd_join_domain(smb_joininfo_t *, smb_joinres_t *);
/*
* Launch the DC discovery and monitor thread.
*/
int
smbd_dc_monitor_init(void)
{
pthread_attr_t attr;
int rc;
(void) smb_config_getstr(SMB_CI_ADS_SITE, smbd.s_site,
MAXHOSTNAMELEN);
(void) smb_config_getip(SMB_CI_DOMAIN_SRV, &smbd.s_pdc);
smb_ads_init();
if (smbd.s_secmode != SMB_SECMODE_DOMAIN)
return (0);
(void) pthread_attr_init(&attr);
(void) pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
rc = pthread_create(&smbd.s_dc_monitor_tid, &attr, smbd_dc_monitor,
NULL);
(void) pthread_attr_destroy(&attr);
return (rc);
}
/*
* Refresh the DC monitor. Called from SMF refresh and when idmap
* finds a different DC from what we were using previously.
* Update our domain (and current DC) information.
*/
void
smbd_dc_monitor_refresh(void)
{
syslog(LOG_INFO, "smbd_dc_monitor_refresh");
smb_ddiscover_refresh();
(void) mutex_lock(&smbd_dc_mutex);
smbd.s_pdc_changed = B_TRUE;
(void) cond_signal(&smbd_dc_cv);
(void) mutex_unlock(&smbd_dc_mutex);
}
/*ARGSUSED*/
static void *
smbd_dc_monitor(void *arg)
{
smb_domainex_t di;
boolean_t ds_not_responding;
boolean_t ds_cfg_changed;
timestruc_t delay;
int i;
/* Wait for smb_dclocator_init() to complete. */
smbd_online_wait("smbd_dc_monitor");
smbd_dc_update();
while (smbd_online()) {
ds_not_responding = B_FALSE;
ds_cfg_changed = B_FALSE;
delay.tv_sec = SMBD_DC_MONITOR_INTERVAL;
delay.tv_nsec = 0;
(void) mutex_lock(&smbd_dc_mutex);
(void) cond_reltimedwait(&smbd_dc_cv, &smbd_dc_mutex, &delay);
if (smbd.s_pdc_changed) {
smbd.s_pdc_changed = B_FALSE;
ds_cfg_changed = B_TRUE;
/* NB: smb_ddiscover_refresh was called. */
}
(void) mutex_unlock(&smbd_dc_mutex);
if (ds_cfg_changed) {
syslog(LOG_DEBUG, "smbd_dc_monitor: config changed");
goto rediscover;
}
if (!smb_domain_getinfo(&di)) {
syslog(LOG_DEBUG, "smbd_dc_monitor: no domain info");
goto rediscover;
}
if (di.d_dci.dc_name[0] == '\0') {
syslog(LOG_DEBUG, "smbd_dc_monitor: no DC name");
goto rediscover;
}
for (i = 0; i < SMBD_DC_MONITOR_ATTEMPTS; ++i) {
if (smbd_dc_check(&di) == 0) {
ds_not_responding = B_FALSE;
break;
}
ds_not_responding = B_TRUE;
(void) sleep(SMBD_DC_MONITOR_RETRY_INTERVAL);
}
if (ds_not_responding) {
syslog(LOG_NOTICE,
"smbd_dc_monitor: DC not responding: %s",
di.d_dci.dc_name);
smb_ddiscover_bad_dc(di.d_dci.dc_name);
}
if (ds_not_responding || ds_cfg_changed) {
rediscover:
/*
* An smb_ads_refresh will be done by the
* smb_ddiscover_service when necessary.
* Note: smbd_dc_monitor_refresh was already
* called if appropriate.
*/
smbd_dc_update();
}
}
smbd.s_dc_monitor_tid = 0;
return (NULL);
}
/*
* Simply attempt a connection to the DC.
*/
static int
smbd_dc_check(smb_domainex_t *di)
{
struct sockaddr_storage sa;
int salen = 0;
int sock = -1;
int tmo = 5 * 1000; /* 5 sec. */
int rc;
bzero(&sa, sizeof (sa));
switch (di->d_dci.dc_addr.a_family) {
case AF_INET: {
struct sockaddr_in *sin = (void *)&sa;
sin->sin_family = AF_INET;
sin->sin_port = htons(IPPORT_SMB);
sin->sin_addr.s_addr = di->d_dci.dc_addr.a_ipv4;
salen = sizeof (*sin);
break;
}
case AF_INET6: {
struct sockaddr_in6 *sin6 = (void *)&sa;
sin6->sin6_family = AF_INET6;
sin6->sin6_port = htons(IPPORT_SMB);
(void) memcpy(&sin6->sin6_addr,
&di->d_dci.dc_addr.a_ipv6,
sizeof (in6_addr_t));
salen = sizeof (*sin6);
break;
}
default:
return (-1);
}
sock = socket(di->d_dci.dc_addr.a_family, SOCK_STREAM, 0);
if (sock < 0)
return (errno);
(void) setsockopt(sock, IPPROTO_TCP,
TCP_CONN_ABORT_THRESHOLD, &tmo, sizeof (tmo));
rc = connect(sock, (const struct sockaddr *)&sa, salen);
if (rc < 0)
rc = errno;
(void) close(sock);
return (rc);
}
/*
* Locate a domain controller in the current resource domain and Update
* the Netlogon credential chain.
*
* The domain configuration will be updated upon successful DC discovery.
*/
static void
smbd_dc_update(void)
{
char domain[MAXHOSTNAMELEN];
smb_domainex_t info;
smb_domain_t *di;
DWORD status;
/*
* Don't want this active until we're a domain member.
*/
if (smb_config_get_secmode() != SMB_SECMODE_DOMAIN)
return;
if (smb_getfqdomainname(domain, MAXHOSTNAMELEN) != 0)
return;
if (domain[0] == '\0') {
syslog(LOG_NOTICE,
"smbd_dc_update: no domain name set");
return;
}
if (!smb_locate_dc(domain, &info)) {
syslog(LOG_NOTICE,
"smbd_dc_update: %s: locate failed", domain);
return;
}
di = &info.d_primary;
syslog(LOG_INFO,
"smbd_dc_update: %s: located %s", domain, info.d_dci.dc_name);
status = mlsvc_netlogon(info.d_dci.dc_name, di->di_nbname);
if (status != NT_STATUS_SUCCESS) {
syslog(LOG_NOTICE,
"failed to establish NETLOGON credential chain");
syslog(LOG_NOTICE, " with server %s for domain %s (%s)",
info.d_dci.dc_name, domain,
xlate_nt_status(status));
}
}
/*
* smbd_join
*
* Joins the specified domain/workgroup.
*
* If the security mode or domain name is being changed,
* the caller must restart the service.
*/
void
smbd_join(smb_joininfo_t *info, smb_joinres_t *res)
{
dssetup_clear_domain_info();
if (info->mode == SMB_SECMODE_WORKGRP)
smbd_join_workgroup(info, res);
else
smbd_join_domain(info, res);
}
static void
smbd_join_workgroup(smb_joininfo_t *info, smb_joinres_t *res)
{
char nb_domain[SMB_PI_MAX_DOMAIN];
syslog(LOG_DEBUG, "smbd: join workgroup: %s", info->domain_name);
(void) smb_config_getstr(SMB_CI_DOMAIN_NAME, nb_domain,
sizeof (nb_domain));
smbd_set_secmode(SMB_SECMODE_WORKGRP);
smb_config_setdomaininfo(info->domain_name, "", "", "", "");
(void) smb_config_set_idmap_domain("");
(void) smb_config_refresh_idmap();
if (strcasecmp(nb_domain, info->domain_name))
smb_browser_reconfig();
res->status = NT_STATUS_SUCCESS;
}
static void
smbd_join_domain(smb_joininfo_t *info, smb_joinres_t *res)
{
syslog(LOG_DEBUG, "smbd: join domain: %s", info->domain_name);
/* info->domain_name could either be NetBIOS domain name or FQDN */
mlsvc_join(info, res);
if (res->status == 0) {
smbd_set_secmode(SMB_SECMODE_DOMAIN);
} else {
syslog(LOG_ERR, "smbd: failed joining %s (%s)",
info->domain_name, xlate_nt_status(res->status));
}
}
|