s3-winbind: Implemented samr backend function common_enum_local_groups.
[Samba/gebeck_regimport.git] / source3 / smbd / session.c
blob64274b771e2c590113e8915820341ebca60df992
1 /*
2 Unix SMB/CIFS implementation.
3 session handling for utmp and PAM
5 Copyright (C) tridge@samba.org 2001
6 Copyright (C) abartlet@samba.org 2001
7 Copyright (C) Gerald (Jerry) Carter 2006
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>.
23 /* a "session" is claimed when we do a SessionSetupX operation
24 and is yielded when the corresponding vuid is destroyed.
26 sessions are used to populate utmp and PAM session structures
29 #include "includes.h"
30 #include "smbd/globals.h"
32 /********************************************************************
33 called when a session is created
34 ********************************************************************/
36 bool session_claim(struct server_id pid, user_struct *vuser)
38 TDB_DATA data;
39 int i = 0;
40 struct sessionid sessionid;
41 fstring keystr;
42 const char * hostname;
43 struct db_record *rec;
44 NTSTATUS status;
45 char addr[INET6_ADDRSTRLEN];
47 vuser->session_keystr = NULL;
49 /* don't register sessions for the guest user - its just too
50 expensive to go through pam session code for browsing etc */
51 if (vuser->server_info->guest) {
52 return True;
55 if (!sessionid_init()) {
56 return False;
59 ZERO_STRUCT(sessionid);
61 data.dptr = NULL;
62 data.dsize = 0;
64 if (lp_utmp()) {
66 for (i=1;i<MAX_SESSION_ID;i++) {
69 * This is very inefficient and needs fixing -- vl
72 struct server_id sess_pid;
74 snprintf(keystr, sizeof(keystr), "ID/%d", i);
76 rec = sessionid_fetch_record(NULL, keystr);
77 if (rec == NULL) {
78 DEBUG(1, ("Could not lock \"%s\"\n", keystr));
79 return False;
82 if (rec->value.dsize != sizeof(sessionid)) {
83 DEBUG(1, ("Re-using invalid record\n"));
84 break;
87 memcpy(&sess_pid,
88 ((char *)rec->value.dptr)
89 + offsetof(struct sessionid, pid),
90 sizeof(sess_pid));
92 if (!process_exists(sess_pid)) {
93 DEBUG(5, ("%s has died -- re-using session\n",
94 procid_str_static(&sess_pid)));
95 break;
98 TALLOC_FREE(rec);
101 if (i == MAX_SESSION_ID) {
102 SMB_ASSERT(rec == NULL);
103 DEBUG(1,("session_claim: out of session IDs "
104 "(max is %d)\n", MAX_SESSION_ID));
105 return False;
108 snprintf(sessionid.id_str, sizeof(sessionid.id_str),
109 SESSION_UTMP_TEMPLATE, i);
110 } else
112 snprintf(keystr, sizeof(keystr), "ID/%s/%u",
113 procid_str_static(&pid), vuser->vuid);
115 rec = sessionid_fetch_record(NULL, keystr);
116 if (rec == NULL) {
117 DEBUG(1, ("Could not lock \"%s\"\n", keystr));
118 return False;
121 snprintf(sessionid.id_str, sizeof(sessionid.id_str),
122 SESSION_TEMPLATE, (long unsigned int)sys_getpid(),
123 vuser->vuid);
126 SMB_ASSERT(rec != NULL);
128 /* If 'hostname lookup' == yes, then do the DNS lookup. This is
129 needed because utmp and PAM both expect DNS names
131 client_name() handles this case internally.
134 hostname = client_name(get_client_fd());
135 if (strcmp(hostname, "UNKNOWN") == 0) {
136 hostname = client_addr(get_client_fd(),addr,sizeof(addr));
139 fstrcpy(sessionid.username, vuser->server_info->unix_name);
140 fstrcpy(sessionid.hostname, hostname);
141 sessionid.id_num = i; /* Only valid for utmp sessions */
142 sessionid.pid = pid;
143 sessionid.uid = vuser->server_info->utok.uid;
144 sessionid.gid = vuser->server_info->utok.gid;
145 fstrcpy(sessionid.remote_machine, get_remote_machine_name());
146 fstrcpy(sessionid.ip_addr_str,
147 client_addr(get_client_fd(),addr,sizeof(addr)));
148 sessionid.connect_start = time(NULL);
150 if (!smb_pam_claim_session(sessionid.username, sessionid.id_str,
151 sessionid.hostname)) {
152 DEBUG(1,("pam_session rejected the session for %s [%s]\n",
153 sessionid.username, sessionid.id_str));
155 TALLOC_FREE(rec);
156 return False;
159 data.dptr = (uint8 *)&sessionid;
160 data.dsize = sizeof(sessionid);
162 status = rec->store(rec, data, TDB_REPLACE);
164 TALLOC_FREE(rec);
166 if (!NT_STATUS_IS_OK(status)) {
167 DEBUG(1,("session_claim: unable to create session id "
168 "record: %s\n", nt_errstr(status)));
169 return False;
172 if (lp_utmp()) {
173 sys_utmp_claim(sessionid.username, sessionid.hostname,
174 sessionid.ip_addr_str,
175 sessionid.id_str, sessionid.id_num);
178 vuser->session_keystr = talloc_strdup(vuser, keystr);
179 if (!vuser->session_keystr) {
180 DEBUG(0, ("session_claim: talloc_strdup() failed for session_keystr\n"));
181 return False;
183 return True;
186 /********************************************************************
187 called when a session is destroyed
188 ********************************************************************/
190 void session_yield(user_struct *vuser)
192 struct sessionid sessionid;
193 struct db_record *rec;
195 if (!vuser->session_keystr) {
196 return;
199 rec = sessionid_fetch_record(NULL, vuser->session_keystr);
200 if (rec == NULL) {
201 return;
204 if (rec->value.dsize != sizeof(sessionid))
205 return;
207 memcpy(&sessionid, rec->value.dptr, sizeof(sessionid));
209 if (lp_utmp()) {
210 sys_utmp_yield(sessionid.username, sessionid.hostname,
211 sessionid.ip_addr_str,
212 sessionid.id_str, sessionid.id_num);
215 smb_pam_close_session(sessionid.username, sessionid.id_str,
216 sessionid.hostname);
218 rec->delete_rec(rec);
220 TALLOC_FREE(rec);
223 /********************************************************************
224 ********************************************************************/
226 struct session_list {
227 TALLOC_CTX *mem_ctx;
228 int count;
229 struct sessionid *sessions;
232 static int gather_sessioninfo(const char *key, struct sessionid *session,
233 void *private_data)
235 struct session_list *sesslist = (struct session_list *)private_data;
237 sesslist->sessions = TALLOC_REALLOC_ARRAY(
238 sesslist->mem_ctx, sesslist->sessions, struct sessionid,
239 sesslist->count+1);
241 if (!sesslist->sessions) {
242 sesslist->count = 0;
243 return -1;
246 memcpy(&sesslist->sessions[sesslist->count], session,
247 sizeof(struct sessionid));
249 sesslist->count++;
251 DEBUG(7, ("gather_sessioninfo session from %s@%s\n",
252 session->username, session->remote_machine));
254 return 0;
257 /********************************************************************
258 ********************************************************************/
260 int list_sessions(TALLOC_CTX *mem_ctx, struct sessionid **session_list)
262 struct session_list sesslist;
263 int ret;
265 sesslist.mem_ctx = mem_ctx;
266 sesslist.count = 0;
267 sesslist.sessions = NULL;
269 ret = sessionid_traverse_read(gather_sessioninfo, (void *) &sesslist);
270 if (ret == -1) {
271 DEBUG(3, ("Session traverse failed\n"));
272 SAFE_FREE(sesslist.sessions);
273 *session_list = NULL;
274 return 0;
277 *session_list = sesslist.sessions;
278 return sesslist.count;