xref: /titanic_50/usr/src/cmd/smbsrv/smbd/smbd_logon.c (revision 12b65585e720714b31036daaa2b30eb76014048e)
1da6c28aaSamw /*
2da6c28aaSamw  * CDDL HEADER START
3da6c28aaSamw  *
4da6c28aaSamw  * The contents of this file are subject to the terms of the
5da6c28aaSamw  * Common Development and Distribution License (the "License").
6da6c28aaSamw  * You may not use this file except in compliance with the License.
7da6c28aaSamw  *
8da6c28aaSamw  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9da6c28aaSamw  * or http://www.opensolaris.org/os/licensing.
10da6c28aaSamw  * See the License for the specific language governing permissions
11da6c28aaSamw  * and limitations under the License.
12da6c28aaSamw  *
13da6c28aaSamw  * When distributing Covered Code, include this CDDL HEADER in each
14da6c28aaSamw  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15da6c28aaSamw  * If applicable, add the following below this CDDL HEADER, with the
16da6c28aaSamw  * fields enclosed by brackets "[]" replaced with your own identifying
17da6c28aaSamw  * information: Portions Copyright [yyyy] [name of copyright owner]
18da6c28aaSamw  *
19da6c28aaSamw  * CDDL HEADER END
20da6c28aaSamw  */
21da6c28aaSamw /*
22c5866007SKeyur Desai  * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
23*12b65585SGordon Ross  * Copyright 2014 Nexenta Systems, Inc.  All rights reserved.
24da6c28aaSamw  */
25da6c28aaSamw 
26da6c28aaSamw #include <sys/types.h>
27da6c28aaSamw #include <errno.h>
28da6c28aaSamw #include <synch.h>
29da6c28aaSamw #include <stdio.h>
30da6c28aaSamw #include <stdlib.h>
31da6c28aaSamw #include <unistd.h>
32da6c28aaSamw #include <string.h>
33da6c28aaSamw #include <strings.h>
34da6c28aaSamw #include <syslog.h>
35da6c28aaSamw #include <fcntl.h>
36da6c28aaSamw #include <bsm/adt.h>
37da6c28aaSamw #include <bsm/adt_event.h>
38da6c28aaSamw #include <bsm/audit_uevents.h>
39c5866007SKeyur Desai #include <pwd.h>
40c5866007SKeyur Desai #include <nss_dbdefs.h>
41c5866007SKeyur Desai #include <sys/idmap.h>
42da6c28aaSamw #include "smbd.h"
43da6c28aaSamw 
44da6c28aaSamw 
45da6c28aaSamw /*
46da6c28aaSamw  * An audit session is established at user logon and terminated at user
47da6c28aaSamw  * logoff.
48da6c28aaSamw  *
49da6c28aaSamw  * SMB audit handles are allocated when users logon (SmbSessionSetupX)
50da6c28aaSamw  * and deallocted when a user logs off (SmbLogoffX).  Each time an SMB
51da6c28aaSamw  * audit handle is allocated it is added to a global list.
52da6c28aaSamw  */
53da6c28aaSamw typedef struct smb_audit {
54da6c28aaSamw 	struct smb_audit *sa_next;
55da6c28aaSamw 	adt_session_data_t *sa_handle;
56da6c28aaSamw 	uid_t sa_uid;
57da6c28aaSamw 	gid_t sa_gid;
58da6c28aaSamw 	uint32_t sa_audit_sid;
59da6c28aaSamw 	uint32_t sa_refcnt;
60da6c28aaSamw 	char *sa_domain;
61da6c28aaSamw 	char *sa_username;
62da6c28aaSamw } smb_audit_t;
63da6c28aaSamw 
64da6c28aaSamw static smb_audit_t *smbd_audit_list;
65da6c28aaSamw static mutex_t smbd_audit_lock;
66da6c28aaSamw 
67da6c28aaSamw /*
68da6c28aaSamw  * Unique identifier for audit sessions in the audit list.
69da6c28aaSamw  * Used to lookup an audit session on logoff.
70da6c28aaSamw  */
71da6c28aaSamw static uint32_t smbd_audit_sid;
72da6c28aaSamw 
73da6c28aaSamw static void smbd_audit_link(smb_audit_t *);
74da6c28aaSamw static smb_audit_t *smbd_audit_unlink(uint32_t);
75da6c28aaSamw 
76da6c28aaSamw 
77da6c28aaSamw /*
78da6c28aaSamw  * Invoked at user logon due to SmbSessionSetupX.  Authenticate the
79da6c28aaSamw  * user, start an audit session and audit the event.
80da6c28aaSamw  */
81da6c28aaSamw smb_token_t *
smbd_user_auth_logon(smb_logon_t * user_info)829fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States smbd_user_auth_logon(smb_logon_t *user_info)
83da6c28aaSamw {
84da6c28aaSamw 	smb_token_t *token;
85da6c28aaSamw 	smb_audit_t *entry;
86da6c28aaSamw 	adt_session_data_t *ah;
87da6c28aaSamw 	adt_event_data_t *event;
88*12b65585SGordon Ross 	smb_logon_t tmp_user;
89da6c28aaSamw 	au_tid_addr_t termid;
906537f381Sas200622 	char sidbuf[SMB_SID_STRSZ];
917f667e74Sjose borrego 	char *username;
927f667e74Sjose borrego 	char *domain;
93da6c28aaSamw 	uid_t uid;
94da6c28aaSamw 	gid_t gid;
95da6c28aaSamw 	char *sid;
96da6c28aaSamw 	int status;
97da6c28aaSamw 	int retval;
98da6c28aaSamw 
99*12b65585SGordon Ross 	if (user_info->lg_username == NULL ||
100*12b65585SGordon Ross 	    user_info->lg_domain == NULL ||
101*12b65585SGordon Ross 	    user_info->lg_workstation == NULL) {
102*12b65585SGordon Ross 		return (NULL);
103*12b65585SGordon Ross 	}
104*12b65585SGordon Ross 
105*12b65585SGordon Ross 	tmp_user = *user_info;
106*12b65585SGordon Ross 	if (tmp_user.lg_username[0] == '\0') {
107*12b65585SGordon Ross 		tmp_user.lg_flags |= SMB_ATF_ANON;
108*12b65585SGordon Ross 		tmp_user.lg_e_username = "anonymous";
109*12b65585SGordon Ross 	} else {
110*12b65585SGordon Ross 		tmp_user.lg_e_username = tmp_user.lg_username;
111*12b65585SGordon Ross 	}
112*12b65585SGordon Ross 	tmp_user.lg_e_domain = tmp_user.lg_domain;
113*12b65585SGordon Ross 
114*12b65585SGordon Ross 	if ((token = smb_logon(&tmp_user)) == NULL) {
115da6c28aaSamw 		uid = ADT_NO_ATTRIB;
116da6c28aaSamw 		gid = ADT_NO_ATTRIB;
1176537f381Sas200622 		sid = NT_NULL_SIDSTR;
118*12b65585SGordon Ross 		username = tmp_user.lg_e_username;
119*12b65585SGordon Ross 		domain = tmp_user.lg_e_domain;
120da6c28aaSamw 		status = ADT_FAILURE;
121da6c28aaSamw 		retval = ADT_FAIL_VALUE_AUTH;
122da6c28aaSamw 	} else {
1237f667e74Sjose borrego 		uid = token->tkn_user.i_id;
1247f667e74Sjose borrego 		gid = token->tkn_primary_grp.i_id;
1257f667e74Sjose borrego 		smb_sid_tostr(token->tkn_user.i_sid, sidbuf);
1266537f381Sas200622 		sid = sidbuf;
1277f667e74Sjose borrego 		username = token->tkn_account_name;
1287f667e74Sjose borrego 		domain = token->tkn_domain_name;
129da6c28aaSamw 		status = ADT_SUCCESS;
130da6c28aaSamw 		retval = ADT_SUCCESS;
131da6c28aaSamw 	}
132da6c28aaSamw 
133da6c28aaSamw 	if (adt_start_session(&ah, NULL, 0)) {
134da6c28aaSamw 		syslog(LOG_AUTH | LOG_ALERT, "adt_start_session: %m");
135da6c28aaSamw 		smb_token_destroy(token);
136da6c28aaSamw 		return (NULL);
137da6c28aaSamw 	}
138da6c28aaSamw 
139da6c28aaSamw 	if ((event = adt_alloc_event(ah, ADT_smbd_session)) == NULL) {
140da6c28aaSamw 		syslog(LOG_AUTH | LOG_ALERT,
141da6c28aaSamw 		    "adt_alloc_event(ADT_smbd_session): %m");
142da6c28aaSamw 		(void) adt_end_session(ah);
143da6c28aaSamw 		smb_token_destroy(token);
144da6c28aaSamw 		return (NULL);
145da6c28aaSamw 	}
146da6c28aaSamw 
147da6c28aaSamw 	(void) memset(&termid, 0, sizeof (au_tid_addr_t));
1489fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 	termid.at_port = user_info->lg_local_port;
1497f667e74Sjose borrego 
1509fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 	if (user_info->lg_clnt_ipaddr.a_family == AF_INET) {
1519fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 		termid.at_addr[0] = user_info->lg_clnt_ipaddr.a_ipv4;
152da6c28aaSamw 		termid.at_type = AU_IPv4;
1537f667e74Sjose borrego 	} else {
1549fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 		bcopy(&user_info->lg_clnt_ipaddr.a_ip, termid.at_addr,
155b819cea2SGordon Ross 		    sizeof (in6_addr_t));
1567f667e74Sjose borrego 		termid.at_type = AU_IPv6;
1577f667e74Sjose borrego 	}
158da6c28aaSamw 	adt_set_termid(ah, &termid);
159da6c28aaSamw 
160da6c28aaSamw 	if (adt_set_user(ah, uid, gid, uid, gid, NULL, ADT_NEW)) {
161da6c28aaSamw 		syslog(LOG_AUTH | LOG_ALERT, "adt_set_user: %m");
162da6c28aaSamw 		adt_free_event(event);
163da6c28aaSamw 		(void) adt_end_session(ah);
164da6c28aaSamw 		smb_token_destroy(token);
165da6c28aaSamw 		return (NULL);
166da6c28aaSamw 	}
167da6c28aaSamw 
1687f667e74Sjose borrego 	event->adt_smbd_session.domain = domain;
1697f667e74Sjose borrego 	event->adt_smbd_session.username = username;
170da6c28aaSamw 	event->adt_smbd_session.sid = sid;
171da6c28aaSamw 
172da6c28aaSamw 	if (adt_put_event(event, status, retval))
173da6c28aaSamw 		syslog(LOG_AUTH | LOG_ALERT, "adt_put_event: %m");
174da6c28aaSamw 
175da6c28aaSamw 	adt_free_event(event);
176da6c28aaSamw 
177da6c28aaSamw 	if (token) {
178da6c28aaSamw 		if ((entry = malloc(sizeof (smb_audit_t))) == NULL) {
179da6c28aaSamw 			syslog(LOG_ERR, "smbd_user_auth_logon: %m");
180da6c28aaSamw 			(void) adt_end_session(ah);
181da6c28aaSamw 			smb_token_destroy(token);
182da6c28aaSamw 			return (NULL);
183da6c28aaSamw 		}
184da6c28aaSamw 
185da6c28aaSamw 		entry->sa_handle = ah;
186da6c28aaSamw 		entry->sa_uid = uid;
187da6c28aaSamw 		entry->sa_gid = gid;
1887f667e74Sjose borrego 		entry->sa_username = strdup(username);
1897f667e74Sjose borrego 		entry->sa_domain = strdup(domain);
190da6c28aaSamw 
191fe1c642dSBill Krier 		smb_autohome_add(token);
192da6c28aaSamw 		smbd_audit_link(entry);
193da6c28aaSamw 		token->tkn_audit_sid = entry->sa_audit_sid;
194da6c28aaSamw 	}
195da6c28aaSamw 
196da6c28aaSamw 	return (token);
197da6c28aaSamw }
198da6c28aaSamw 
199da6c28aaSamw /*
200da6c28aaSamw  * Logon due to a subsequent SmbSessionSetupX on an existing session.
201da6c28aaSamw  * The user was authenticated during the initial session setup.
202da6c28aaSamw  */
203da6c28aaSamw void
smbd_user_nonauth_logon(uint32_t audit_sid)204da6c28aaSamw smbd_user_nonauth_logon(uint32_t audit_sid)
205da6c28aaSamw {
206da6c28aaSamw 	smb_audit_t *entry;
207da6c28aaSamw 
208da6c28aaSamw 	(void) mutex_lock(&smbd_audit_lock);
209da6c28aaSamw 	entry = smbd_audit_list;
210da6c28aaSamw 
211da6c28aaSamw 	while (entry) {
212da6c28aaSamw 		if (entry->sa_audit_sid == audit_sid) {
213da6c28aaSamw 			++entry->sa_refcnt;
214da6c28aaSamw 			break;
215da6c28aaSamw 		}
216da6c28aaSamw 
217da6c28aaSamw 		entry = entry->sa_next;
218da6c28aaSamw 	}
219da6c28aaSamw 
220da6c28aaSamw 	(void) mutex_unlock(&smbd_audit_lock);
221da6c28aaSamw }
222da6c28aaSamw 
223da6c28aaSamw /*
224da6c28aaSamw  * Invoked at user logoff due to SmbLogoffX.  If this is the final
225da6c28aaSamw  * logoff for this user on the session, audit the event and terminate
226da6c28aaSamw  * the audit session.
227da6c28aaSamw  */
228da6c28aaSamw void
smbd_user_auth_logoff(uint32_t audit_sid)229da6c28aaSamw smbd_user_auth_logoff(uint32_t audit_sid)
230da6c28aaSamw {
231da6c28aaSamw 	smb_audit_t *entry;
232da6c28aaSamw 	adt_session_data_t *ah;
233da6c28aaSamw 	adt_event_data_t *event;
234c5866007SKeyur Desai 	struct passwd pw;
235c5866007SKeyur Desai 	char buf[NSS_LINELEN_PASSWD];
236da6c28aaSamw 
237da6c28aaSamw 	if ((entry = smbd_audit_unlink(audit_sid)) == NULL)
238da6c28aaSamw 		return;
239da6c28aaSamw 
240c5866007SKeyur Desai 	if (IDMAP_ID_IS_EPHEMERAL(entry->sa_uid)) {
2413ad684d6Sjb150015 		smb_autohome_remove(entry->sa_username);
242c5866007SKeyur Desai 	} else {
243c5866007SKeyur Desai 		if (getpwuid_r(entry->sa_uid, &pw, buf, sizeof (buf)) == NULL)
244c5866007SKeyur Desai 			return;
245c5866007SKeyur Desai 
246c5866007SKeyur Desai 		smb_autohome_remove(pw.pw_name);
247c5866007SKeyur Desai 	}
248da6c28aaSamw 
249da6c28aaSamw 	ah = entry->sa_handle;
250da6c28aaSamw 
251da6c28aaSamw 	if ((event = adt_alloc_event(ah, ADT_smbd_logoff)) == NULL) {
252da6c28aaSamw 		syslog(LOG_AUTH | LOG_ALERT,
253da6c28aaSamw 		    "adt_alloc_event(ADT_smbd_logoff): %m");
254da6c28aaSamw 	} else {
255da6c28aaSamw 		event->adt_smbd_logoff.domain = entry->sa_domain;
256da6c28aaSamw 		event->adt_smbd_logoff.username = entry->sa_username;
257da6c28aaSamw 
258da6c28aaSamw 		if (adt_put_event(event, ADT_SUCCESS, ADT_SUCCESS))
259da6c28aaSamw 			syslog(LOG_AUTH | LOG_ALERT, "adt_put_event: %m");
260da6c28aaSamw 
261da6c28aaSamw 		adt_free_event(event);
262da6c28aaSamw 	}
263da6c28aaSamw 
264da6c28aaSamw 	(void) adt_end_session(ah);
265da6c28aaSamw 
266da6c28aaSamw 	free(entry->sa_username);
267da6c28aaSamw 	free(entry->sa_domain);
268da6c28aaSamw 	free(entry);
269da6c28aaSamw }
270da6c28aaSamw 
271da6c28aaSamw /*
272da6c28aaSamw  * Allocate an id and link an audit handle onto the global list.
273da6c28aaSamw  */
274da6c28aaSamw static void
smbd_audit_link(smb_audit_t * entry)275da6c28aaSamw smbd_audit_link(smb_audit_t *entry)
276da6c28aaSamw {
277da6c28aaSamw 	(void) mutex_lock(&smbd_audit_lock);
278da6c28aaSamw 
279da6c28aaSamw 	do {
280da6c28aaSamw 		++smbd_audit_sid;
281da6c28aaSamw 	} while ((smbd_audit_sid == 0) || (smbd_audit_sid == (uint32_t)-1));
282da6c28aaSamw 
283da6c28aaSamw 	entry->sa_audit_sid = smbd_audit_sid;
284da6c28aaSamw 	entry->sa_refcnt = 1;
285da6c28aaSamw 	entry->sa_next = smbd_audit_list;
286da6c28aaSamw 	smbd_audit_list = entry;
287da6c28aaSamw 
288da6c28aaSamw 	(void) mutex_unlock(&smbd_audit_lock);
289da6c28aaSamw }
290da6c28aaSamw 
291da6c28aaSamw /*
292da6c28aaSamw  * Unlink an audit handle.  If the reference count reaches 0, the entry
293da6c28aaSamw  * is removed from the list and returned.  Otherwise the entry remains
294da6c28aaSamw  * on the list and a null pointer is returned.
295da6c28aaSamw  */
296da6c28aaSamw static smb_audit_t *
smbd_audit_unlink(uint32_t audit_sid)297da6c28aaSamw smbd_audit_unlink(uint32_t audit_sid)
298da6c28aaSamw {
299da6c28aaSamw 	smb_audit_t *entry;
300da6c28aaSamw 	smb_audit_t **ppe;
301da6c28aaSamw 
302da6c28aaSamw 	(void) mutex_lock(&smbd_audit_lock);
303da6c28aaSamw 	ppe = &smbd_audit_list;
304da6c28aaSamw 
305da6c28aaSamw 	while (*ppe) {
306da6c28aaSamw 		entry = *ppe;
307da6c28aaSamw 
308da6c28aaSamw 		if (entry->sa_audit_sid == audit_sid) {
309da6c28aaSamw 			if (entry->sa_refcnt == 0)
310da6c28aaSamw 				break;
311da6c28aaSamw 
312da6c28aaSamw 			if ((--entry->sa_refcnt) != 0)
313da6c28aaSamw 				break;
314da6c28aaSamw 
315da6c28aaSamw 			*ppe = entry->sa_next;
316da6c28aaSamw 			(void) mutex_unlock(&smbd_audit_lock);
317da6c28aaSamw 			return (entry);
318da6c28aaSamw 		}
319da6c28aaSamw 
320da6c28aaSamw 		ppe = &(*ppe)->sa_next;
321da6c28aaSamw 	}
322da6c28aaSamw 
323da6c28aaSamw 	(void) mutex_unlock(&smbd_audit_lock);
324da6c28aaSamw 	return (NULL);
325da6c28aaSamw }
326