s3-build: use dbwrap.h only where needed.
[samba.git] / source3 / smbd / session.c
1 /* 
2    Unix SMB/CIFS implementation.
3    session handling for utmp and PAM
4
5    Copyright (C) tridge@samba.org       2001
6    Copyright (C) abartlet@samba.org     2001
7    Copyright (C) Gerald (Jerry) Carter  2006   
8    
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.
13    
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.
18    
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/>.
21 */
22
23 /* a "session" is claimed when we do a SessionSetupX operation
24    and is yielded when the corresponding vuid is destroyed.
25
26    sessions are used to populate utmp and PAM session structures
27 */
28
29 #include "includes.h"
30 #include "smbd/globals.h"
31 #include "dbwrap.h"
32
33 /********************************************************************
34  called when a session is created
35 ********************************************************************/
36
37 bool session_claim(struct smbd_server_connection *sconn, user_struct *vuser)
38 {
39         struct server_id pid = sconn_server_id(sconn);
40         TDB_DATA data;
41         int i = 0;
42         struct sessionid sessionid;
43         fstring keystr;
44         struct db_record *rec;
45         NTSTATUS status;
46
47         vuser->session_keystr = NULL;
48
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;
53         }
54
55         if (!sessionid_init()) {
56                 return False;
57         }
58
59         ZERO_STRUCT(sessionid);
60
61         data.dptr = NULL;
62         data.dsize = 0;
63
64         if (lp_utmp()) {
65
66                 for (i=1;i<MAX_SESSION_ID;i++) {
67
68                         /*
69                          * This is very inefficient and needs fixing -- vl
70                          */
71
72                         struct server_id sess_pid;
73
74                         snprintf(keystr, sizeof(keystr), "ID/%d", i);
75
76                         rec = sessionid_fetch_record(NULL, keystr);
77                         if (rec == NULL) {
78                                 DEBUG(1, ("Could not lock \"%s\"\n", keystr));
79                                 return False;
80                         }
81
82                         if (rec->value.dsize != sizeof(sessionid)) {
83                                 DEBUG(1, ("Re-using invalid record\n"));
84                                 break;
85                         }
86
87                         memcpy(&sess_pid,
88                                ((char *)rec->value.dptr)
89                                + offsetof(struct sessionid, pid),
90                                sizeof(sess_pid));
91
92                         if (!process_exists(sess_pid)) {
93                                 DEBUG(5, ("%s has died -- re-using session\n",
94                                           procid_str_static(&sess_pid)));
95                                 break;
96                         }
97
98                         TALLOC_FREE(rec);
99                 }
100
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;
106                 }
107
108                 snprintf(sessionid.id_str, sizeof(sessionid.id_str),
109                          SESSION_UTMP_TEMPLATE, i);
110         } else
111         {
112                 snprintf(keystr, sizeof(keystr), "ID/%s/%u",
113                          procid_str_static(&pid), vuser->vuid);
114
115                 rec = sessionid_fetch_record(NULL, keystr);
116                 if (rec == NULL) {
117                         DEBUG(1, ("Could not lock \"%s\"\n", keystr));
118                         return False;
119                 }
120
121                 snprintf(sessionid.id_str, sizeof(sessionid.id_str),
122                          SESSION_TEMPLATE, (long unsigned int)sys_getpid(),
123                          vuser->vuid);
124         }
125
126         SMB_ASSERT(rec != NULL);
127
128         /* If 'hostname lookup' == yes, then do the DNS lookup.  This is
129            needed because utmp and PAM both expect DNS names
130
131            client_name() handles this case internally.
132         */
133
134         fstrcpy(sessionid.username, vuser->server_info->unix_name);
135         fstrcpy(sessionid.hostname, sconn->client_id.name);
136         sessionid.id_num = i;  /* Only valid for utmp sessions */
137         sessionid.pid = pid;
138         sessionid.uid = vuser->server_info->utok.uid;
139         sessionid.gid = vuser->server_info->utok.gid;
140         fstrcpy(sessionid.remote_machine, get_remote_machine_name());
141         fstrcpy(sessionid.ip_addr_str, sconn->client_id.addr);
142         sessionid.connect_start = time(NULL);
143
144         if (!smb_pam_claim_session(sessionid.username, sessionid.id_str,
145                                    sessionid.hostname)) {
146                 DEBUG(1,("pam_session rejected the session for %s [%s]\n",
147                                 sessionid.username, sessionid.id_str));
148
149                 TALLOC_FREE(rec);
150                 return False;
151         }
152
153         data.dptr = (uint8 *)&sessionid;
154         data.dsize = sizeof(sessionid);
155
156         status = rec->store(rec, data, TDB_REPLACE);
157
158         TALLOC_FREE(rec);
159
160         if (!NT_STATUS_IS_OK(status)) {
161                 DEBUG(1,("session_claim: unable to create session id "
162                          "record: %s\n", nt_errstr(status)));
163                 return False;
164         }
165
166         if (lp_utmp()) {
167                 sys_utmp_claim(sessionid.username, sessionid.hostname,
168                                sessionid.ip_addr_str,
169                                sessionid.id_str, sessionid.id_num);
170         }
171
172         vuser->session_keystr = talloc_strdup(vuser, keystr);
173         if (!vuser->session_keystr) {
174                 DEBUG(0, ("session_claim:  talloc_strdup() failed for session_keystr\n"));
175                 return False;
176         }
177         return True;
178 }
179
180 /********************************************************************
181  called when a session is destroyed
182 ********************************************************************/
183
184 void session_yield(user_struct *vuser)
185 {
186         struct sessionid sessionid;
187         struct db_record *rec;
188
189         if (!vuser->session_keystr) {
190                 return;
191         }
192
193         rec = sessionid_fetch_record(NULL, vuser->session_keystr);
194         if (rec == NULL) {
195                 return;
196         }
197
198         if (rec->value.dsize != sizeof(sessionid))
199                 return;
200
201         memcpy(&sessionid, rec->value.dptr, sizeof(sessionid));
202
203         if (lp_utmp()) {
204                 sys_utmp_yield(sessionid.username, sessionid.hostname, 
205                                sessionid.ip_addr_str,
206                                sessionid.id_str, sessionid.id_num);
207         }
208
209         smb_pam_close_session(sessionid.username, sessionid.id_str,
210                               sessionid.hostname);
211
212         rec->delete_rec(rec);
213
214         TALLOC_FREE(rec);
215 }
216
217 /********************************************************************
218 ********************************************************************/
219
220 struct session_list {
221         TALLOC_CTX *mem_ctx;
222         int count;
223         struct sessionid *sessions;
224 };
225
226 static int gather_sessioninfo(const char *key, struct sessionid *session,
227                               void *private_data)
228 {
229         struct session_list *sesslist = (struct session_list *)private_data;
230
231         sesslist->sessions = TALLOC_REALLOC_ARRAY(
232                 sesslist->mem_ctx, sesslist->sessions, struct sessionid,
233                 sesslist->count+1);
234
235         if (!sesslist->sessions) {
236                 sesslist->count = 0;
237                 return -1;
238         }
239
240         memcpy(&sesslist->sessions[sesslist->count], session,
241                sizeof(struct sessionid));
242
243         sesslist->count++;
244
245         DEBUG(7, ("gather_sessioninfo session from %s@%s\n",
246                   session->username, session->remote_machine));
247
248         return 0;
249 }
250
251 /********************************************************************
252 ********************************************************************/
253
254 int list_sessions(TALLOC_CTX *mem_ctx, struct sessionid **session_list)
255 {
256         struct session_list sesslist;
257         int ret;
258
259         sesslist.mem_ctx = mem_ctx;
260         sesslist.count = 0;
261         sesslist.sessions = NULL;
262
263         ret = sessionid_traverse_read(gather_sessioninfo, (void *) &sesslist);
264         if (ret == -1) {
265                 DEBUG(3, ("Session traverse failed\n"));
266                 SAFE_FREE(sesslist.sessions);
267                 *session_list = NULL;
268                 return 0;
269         }
270
271         *session_list = sesslist.sessions;
272         return sesslist.count;
273 }