summaryrefslogtreecommitdiff
path: root/usr/src/cmd/smbsrv/smbd/smbd_logon.c
blob: ba3de5ce939752a570344573f677c7045a80bfc3 (plain)
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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
/*
 * 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 2022 Tintri by DDN, Inc. All rights reserved.
 */

#include <sys/types.h>
#include <errno.h>
#include <synch.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <strings.h>
#include <syslog.h>
#include <fcntl.h>
#include <bsm/adt.h>
#include <bsm/adt_event.h>
#include <bsm/audit_uevents.h>
#include <pwd.h>
#include <nss_dbdefs.h>
#include <sys/idmap.h>
#include "smbd.h"


/*
 * An audit session is established at user logon and terminated at user
 * logoff.
 *
 * SMB audit handles are allocated when users logon (SmbSessionSetupX)
 * and deallocted when a user logs off (SmbLogoffX).  Each time an SMB
 * audit handle is allocated it is added to a global list.
 */
typedef struct smb_audit {
	struct smb_audit *sa_next;
	adt_session_data_t *sa_handle;
	uid_t sa_uid;
	gid_t sa_gid;
	uint32_t sa_audit_sid;
	uint32_t sa_refcnt;
	char *sa_domain;
	char *sa_username;
} smb_audit_t;

static smb_audit_t *smbd_audit_list;
static mutex_t smbd_audit_lock;

/*
 * Unique identifier for audit sessions in the audit list.
 * Used to lookup an audit session on logoff.
 */
static uint32_t smbd_audit_sid;

static void smbd_audit_link(smb_audit_t *);
static smb_audit_t *smbd_audit_unlink(uint32_t);


/*
 * Invoked at user logon due to SmbSessionSetupX.  Authenticate the
 * user.
 *
 * On error, returns NULL, and status in user_info->lg_status.
 *
 * Equivalent to smbd_krb5ssp_work().
 */
smb_token_t *
smbd_user_auth_logon(smb_logon_t *user_info)
{
	smb_token_t *token = NULL;
	smb_logon_t tmp_user;
	char *p;
	char *buf = NULL;

	if (user_info->lg_username == NULL ||
	    user_info->lg_domain == NULL ||
	    user_info->lg_workstation == NULL) {
		user_info->lg_status = NT_STATUS_INVALID_PARAMETER;
		return (NULL);
	}

	/*
	 * Avoid modifying the caller-provided struct because it
	 * may or may not point to allocated strings etc.
	 * Copy to tmp_user, auth, then copy the (out) lg_status
	 * member back to the caller-provided struct.
	 */
	tmp_user = *user_info;
	if (tmp_user.lg_username[0] == '\0') {
		tmp_user.lg_flags |= SMB_ATF_ANON;
		tmp_user.lg_e_username = "anonymous";
	} else {
		tmp_user.lg_e_username = tmp_user.lg_username;
	}

	/* Handle user@domain format. */
	if (tmp_user.lg_domain[0] == '\0' &&
	    (p = strchr(tmp_user.lg_e_username, '@')) != NULL) {
		buf = strdup(tmp_user.lg_e_username);
		if (buf == NULL) {
			user_info->lg_status = NT_STATUS_NO_MEMORY;
			return (NULL);
		}
		p = buf + (p - tmp_user.lg_e_username);
		*p = '\0';
		tmp_user.lg_e_domain = p + 1;
		tmp_user.lg_e_username = buf;
	} else {
		tmp_user.lg_e_domain = tmp_user.lg_domain;
	}

	token = smb_logon(&tmp_user);

	if (token == NULL && tmp_user.lg_status == 0) /* should not happen */
		user_info->lg_status = NT_STATUS_INTERNAL_ERROR;
	else
		user_info->lg_status = tmp_user.lg_status;

	user_info->lg_status = smbd_logon_final(token,
	    &user_info->lg_clnt_ipaddr, tmp_user.lg_e_username,
	    tmp_user.lg_e_domain, user_info->lg_status);

	free(buf);

	if (user_info->lg_status != 0) {
		smb_token_destroy(token);
		token = NULL;
	}
	return (token);
}

/* Start an audit session and audit the event. */
static boolean_t
smbd_logon_audit(smb_token_t *token, smb_inaddr_t *ipaddr, char *username,
    char *domain)
{
	smb_audit_t *entry;
	adt_session_data_t *ah = NULL;
	adt_event_data_t *event;
	au_tid_addr_t termid;
	char sidbuf[SMB_SID_STRSZ];
	uid_t uid;
	gid_t gid;
	char *sid;
	int status;
	int retval;

	assert(username != NULL);
	assert(domain != NULL);

	if (token == NULL) {
		uid = ADT_NO_ATTRIB;
		gid = ADT_NO_ATTRIB;
		sid = NT_NULL_SIDSTR;
		/* use the 'default' username and domain we were given */
		status = ADT_FAILURE;
		retval = ADT_FAIL_VALUE_AUTH;
	} else {
		uid = token->tkn_user.i_id;
		gid = token->tkn_primary_grp.i_id;
		smb_sid_tostr(token->tkn_user.i_sid, sidbuf);
		sid = sidbuf;
		username = token->tkn_account_name;
		domain = token->tkn_domain_name;
		status = ADT_SUCCESS;
		retval = ADT_SUCCESS;
	}

	if (adt_start_session(&ah, NULL, 0)) {
		syslog(LOG_AUTH | LOG_ALERT, "adt_start_session: %m");
		goto errout;
	}

	if ((event = adt_alloc_event(ah, ADT_smbd_session)) == NULL) {
		syslog(LOG_AUTH | LOG_ALERT,
		    "adt_alloc_event(ADT_smbd_session): %m");
		goto errout;
	}

	(void) memset(&termid, 0, sizeof (au_tid_addr_t));
	termid.at_port = IPPORT_SMB;

	if (ipaddr->a_family == AF_INET) {
		termid.at_addr[0] = ipaddr->a_ipv4;
		termid.at_type = AU_IPv4;
	} else {
		bcopy(&ipaddr->a_ip, termid.at_addr,
		    sizeof (in6_addr_t));
		termid.at_type = AU_IPv6;
	}
	adt_set_termid(ah, &termid);

	if (adt_set_user(ah, uid, gid, uid, gid, NULL, ADT_NEW)) {
		syslog(LOG_AUTH | LOG_ALERT, "adt_set_user: %m");
		adt_free_event(event);
		goto errout;
	}

	event->adt_smbd_session.domain = domain;
	event->adt_smbd_session.username = username;
	event->adt_smbd_session.sid = sid;

	if (adt_put_event(event, status, retval))
		syslog(LOG_AUTH | LOG_ALERT, "adt_put_event: %m");

	adt_free_event(event);

	if (token) {
		if ((entry = malloc(sizeof (smb_audit_t))) == NULL) {
			syslog(LOG_ERR, "smbd_user_auth_logon: %m");
			goto errout;
		}

		entry->sa_handle = ah;
		entry->sa_uid = uid;
		entry->sa_gid = gid;
		entry->sa_username = strdup(username);
		entry->sa_domain = strdup(domain);

		smbd_audit_link(entry);
		token->tkn_audit_sid = entry->sa_audit_sid;
	}

	return (B_TRUE);
errout:
	(void) adt_end_session(ah);
	return (B_FALSE);
}

/*
 * Handles all of the work needed to be done after SMB authentication,
 * regardless of the auth flavor (Kerberos or NTLM).
 *
 * This should return the original status to the caller, unless something
 * here causes us to turn what would be a success into a failure
 * (or we decide we should override the original error for some reason).
 */
uint32_t
smbd_logon_final(smb_token_t *token, smb_inaddr_t *ipaddr, char *username,
    char *domain, uint32_t status)
{
	assert(token != NULL || status != 0);

	if (!smbd_logon_audit(token, ipaddr, username, domain) && status == 0)
		return (NT_STATUS_AUDIT_FAILED);

	if (status == 0)
		smb_autohome_add(token);

	return (status);
}

/*
 * Logon due to a subsequent SmbSessionSetupX on an existing session.
 * The user was authenticated during the initial session setup.
 */
void
smbd_user_nonauth_logon(uint32_t audit_sid)
{
	smb_audit_t *entry;

	(void) mutex_lock(&smbd_audit_lock);
	entry = smbd_audit_list;

	while (entry) {
		if (entry->sa_audit_sid == audit_sid) {
			++entry->sa_refcnt;
			break;
		}

		entry = entry->sa_next;
	}

	(void) mutex_unlock(&smbd_audit_lock);
}

/*
 * Invoked at user logoff due to SMB Logoff.  If this is the final
 * logoff for this user on the session, audit the event and terminate
 * the audit session.
 *
 * This is called to logoff both NTLMSSP and KRB5SSP authentications.
 */
void
smbd_user_auth_logoff(uint32_t audit_sid)
{
	smb_audit_t *entry;
	adt_session_data_t *ah;
	adt_event_data_t *event;
	struct passwd pw;
	char buf[NSS_LINELEN_PASSWD];

	if ((entry = smbd_audit_unlink(audit_sid)) == NULL)
		return;

	if (IDMAP_ID_IS_EPHEMERAL(entry->sa_uid)) {
		smb_autohome_remove(entry->sa_username);
	} else {
		if (getpwuid_r(entry->sa_uid, &pw, buf, sizeof (buf)) == NULL)
			return;

		smb_autohome_remove(pw.pw_name);
	}

	ah = entry->sa_handle;

	if ((event = adt_alloc_event(ah, ADT_smbd_logoff)) == NULL) {
		syslog(LOG_AUTH | LOG_ALERT,
		    "adt_alloc_event(ADT_smbd_logoff): %m");
	} else {
		event->adt_smbd_logoff.domain = entry->sa_domain;
		event->adt_smbd_logoff.username = entry->sa_username;

		if (adt_put_event(event, ADT_SUCCESS, ADT_SUCCESS))
			syslog(LOG_AUTH | LOG_ALERT, "adt_put_event: %m");

		adt_free_event(event);
	}

	(void) adt_end_session(ah);

	free(entry->sa_username);
	free(entry->sa_domain);
	free(entry);
}

/*
 * Allocate an id and link an audit handle onto the global list.
 */
static void
smbd_audit_link(smb_audit_t *entry)
{
	(void) mutex_lock(&smbd_audit_lock);

	do {
		++smbd_audit_sid;
	} while ((smbd_audit_sid == 0) || (smbd_audit_sid == (uint32_t)-1));

	entry->sa_audit_sid = smbd_audit_sid;
	entry->sa_refcnt = 1;
	entry->sa_next = smbd_audit_list;
	smbd_audit_list = entry;

	(void) mutex_unlock(&smbd_audit_lock);
}

/*
 * Unlink an audit handle.  If the reference count reaches 0, the entry
 * is removed from the list and returned.  Otherwise the entry remains
 * on the list and a null pointer is returned.
 */
static smb_audit_t *
smbd_audit_unlink(uint32_t audit_sid)
{
	smb_audit_t *entry;
	smb_audit_t **ppe;

	(void) mutex_lock(&smbd_audit_lock);
	ppe = &smbd_audit_list;

	while (*ppe) {
		entry = *ppe;

		if (entry->sa_audit_sid == audit_sid) {
			if (entry->sa_refcnt == 0)
				break;

			if ((--entry->sa_refcnt) != 0)
				break;

			*ppe = entry->sa_next;
			(void) mutex_unlock(&smbd_audit_lock);
			return (entry);
		}

		ppe = &(*ppe)->sa_next;
	}

	(void) mutex_unlock(&smbd_audit_lock);
	return (NULL);
}