s3: Remove unused args from nss_get_info_cached
[Samba.git] / source3 / smbd / session.c
blob1b391f3b8be1259d33ed49a8ba0dcd576779d6a6
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"
31 #include "dbwrap.h"
32 #include "session.h"
34 /********************************************************************
35 called when a session is created
36 ********************************************************************/
38 bool session_claim(struct smbd_server_connection *sconn, user_struct *vuser)
40 struct server_id pid = sconn_server_id(sconn);
41 TDB_DATA data;
42 int i = 0;
43 struct sessionid sessionid;
44 fstring keystr;
45 struct db_record *rec;
46 NTSTATUS status;
48 vuser->session_keystr = NULL;
50 /* don't register sessions for the guest user - its just too
51 expensive to go through pam session code for browsing etc */
52 if (vuser->session_info->guest) {
53 return True;
56 if (!sessionid_init()) {
57 return False;
60 ZERO_STRUCT(sessionid);
62 data.dptr = NULL;
63 data.dsize = 0;
65 if (lp_utmp()) {
67 for (i=1;i<MAX_SESSION_ID;i++) {
70 * This is very inefficient and needs fixing -- vl
73 struct server_id sess_pid;
75 snprintf(keystr, sizeof(keystr), "ID/%d", i);
77 rec = sessionid_fetch_record(NULL, keystr);
78 if (rec == NULL) {
79 DEBUG(1, ("Could not lock \"%s\"\n", keystr));
80 return False;
83 if (rec->value.dsize != sizeof(sessionid)) {
84 DEBUG(1, ("Re-using invalid record\n"));
85 break;
88 memcpy(&sess_pid,
89 ((char *)rec->value.dptr)
90 + offsetof(struct sessionid, pid),
91 sizeof(sess_pid));
93 if (!process_exists(sess_pid)) {
94 DEBUG(5, ("%s has died -- re-using session\n",
95 procid_str_static(&sess_pid)));
96 break;
99 TALLOC_FREE(rec);
102 if (i == MAX_SESSION_ID) {
103 SMB_ASSERT(rec == NULL);
104 DEBUG(1,("session_claim: out of session IDs "
105 "(max is %d)\n", MAX_SESSION_ID));
106 return False;
109 snprintf(sessionid.id_str, sizeof(sessionid.id_str),
110 SESSION_UTMP_TEMPLATE, i);
111 } else
113 snprintf(keystr, sizeof(keystr), "ID/%s/%u",
114 procid_str_static(&pid), vuser->vuid);
116 rec = sessionid_fetch_record(NULL, keystr);
117 if (rec == NULL) {
118 DEBUG(1, ("Could not lock \"%s\"\n", keystr));
119 return False;
122 snprintf(sessionid.id_str, sizeof(sessionid.id_str),
123 SESSION_TEMPLATE, (long unsigned int)sys_getpid(),
124 vuser->vuid);
127 SMB_ASSERT(rec != NULL);
129 /* If 'hostname lookup' == yes, then do the DNS lookup. This is
130 needed because utmp and PAM both expect DNS names
132 client_name() handles this case internally.
135 fstrcpy(sessionid.username, vuser->session_info->unix_name);
136 fstrcpy(sessionid.hostname, sconn->client_id.name);
137 sessionid.id_num = i; /* Only valid for utmp sessions */
138 sessionid.pid = pid;
139 sessionid.uid = vuser->session_info->utok.uid;
140 sessionid.gid = vuser->session_info->utok.gid;
141 fstrcpy(sessionid.remote_machine, get_remote_machine_name());
142 fstrcpy(sessionid.ip_addr_str, sconn->client_id.addr);
143 sessionid.connect_start = time(NULL);
145 if (!smb_pam_claim_session(sessionid.username, sessionid.id_str,
146 sessionid.hostname)) {
147 DEBUG(1,("pam_session rejected the session for %s [%s]\n",
148 sessionid.username, sessionid.id_str));
150 TALLOC_FREE(rec);
151 return False;
154 data.dptr = (uint8 *)&sessionid;
155 data.dsize = sizeof(sessionid);
157 status = rec->store(rec, data, TDB_REPLACE);
159 TALLOC_FREE(rec);
161 if (!NT_STATUS_IS_OK(status)) {
162 DEBUG(1,("session_claim: unable to create session id "
163 "record: %s\n", nt_errstr(status)));
164 return False;
167 if (lp_utmp()) {
168 sys_utmp_claim(sessionid.username, sessionid.hostname,
169 sessionid.ip_addr_str,
170 sessionid.id_str, sessionid.id_num);
173 vuser->session_keystr = talloc_strdup(vuser, keystr);
174 if (!vuser->session_keystr) {
175 DEBUG(0, ("session_claim: talloc_strdup() failed for session_keystr\n"));
176 return False;
178 return True;
181 /********************************************************************
182 called when a session is destroyed
183 ********************************************************************/
185 void session_yield(user_struct *vuser)
187 struct sessionid sessionid;
188 struct db_record *rec;
190 if (!vuser->session_keystr) {
191 return;
194 rec = sessionid_fetch_record(NULL, vuser->session_keystr);
195 if (rec == NULL) {
196 return;
199 if (rec->value.dsize != sizeof(sessionid))
200 return;
202 memcpy(&sessionid, rec->value.dptr, sizeof(sessionid));
204 if (lp_utmp()) {
205 sys_utmp_yield(sessionid.username, sessionid.hostname,
206 sessionid.ip_addr_str,
207 sessionid.id_str, sessionid.id_num);
210 smb_pam_close_session(sessionid.username, sessionid.id_str,
211 sessionid.hostname);
213 rec->delete_rec(rec);
215 TALLOC_FREE(rec);
218 /********************************************************************
219 ********************************************************************/
221 struct session_list {
222 TALLOC_CTX *mem_ctx;
223 int count;
224 struct sessionid *sessions;
227 static int gather_sessioninfo(const char *key, struct sessionid *session,
228 void *private_data)
230 struct session_list *sesslist = (struct session_list *)private_data;
232 sesslist->sessions = TALLOC_REALLOC_ARRAY(
233 sesslist->mem_ctx, sesslist->sessions, struct sessionid,
234 sesslist->count+1);
236 if (!sesslist->sessions) {
237 sesslist->count = 0;
238 return -1;
241 memcpy(&sesslist->sessions[sesslist->count], session,
242 sizeof(struct sessionid));
244 sesslist->count++;
246 DEBUG(7, ("gather_sessioninfo session from %s@%s\n",
247 session->username, session->remote_machine));
249 return 0;
252 /********************************************************************
253 ********************************************************************/
255 int list_sessions(TALLOC_CTX *mem_ctx, struct sessionid **session_list)
257 struct session_list sesslist;
258 int ret;
260 sesslist.mem_ctx = mem_ctx;
261 sesslist.count = 0;
262 sesslist.sessions = NULL;
264 ret = sessionid_traverse_read(gather_sessioninfo, (void *) &sesslist);
265 if (ret == -1) {
266 DEBUG(3, ("Session traverse failed\n"));
267 SAFE_FREE(sesslist.sessions);
268 *session_list = NULL;
269 return 0;
272 *session_list = sesslist.sessions;
273 return sesslist.count;