Added "use mmap" for HPUX.
[samba.git] / source / smbd / session.c
1 /* 
2    Unix SMB/Netbios implementation.
3    Version 2.0
4    session handling for utmp and PAM
5    Copyright (C) tridge@samba.org 2001
6    Copyright (C) abartlet@pcug.org.au 2001
7    
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 2 of the License, or
11    (at your option) any later version.
12    
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17    
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
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
31 #if defined(WITH_PAM) || defined(WITH_UTMP)
32
33 static TDB_CONTEXT *tdb;
34 struct sessionid {
35         fstring username;
36         fstring hostname;
37         fstring id_str;
38         uint32  id_num;
39         uint32  pid;
40 };
41
42 /* called when a session is created */
43 BOOL session_claim(uint16 vuid)
44 {
45         user_struct *vuser = get_valid_user_struct(vuid);
46         int i;
47         TDB_DATA data;
48         struct sessionid sessionid;
49         pstring dbuf;
50         int dlen;
51         uint32 pid = (uint32)sys_getpid();
52         TDB_DATA key;           
53         fstring keystr;
54         char * hostname;
55
56         vuser->session_id = 0;
57
58         /* don't register sessions for the guest user - its just too
59            expensive to go through pam session code for browsing etc */
60         if (strequal(vuser->user.unix_name,lp_guestaccount(-1))) {
61                 return True;
62         }
63
64         if (!tdb) {
65                 tdb = tdb_open_log(lock_path("sessionid.tdb"), 0, TDB_CLEAR_IF_FIRST|USE_TDB_MMAP_FLAG, 
66                                O_RDWR | O_CREAT, 0644);
67                 if (!tdb) {
68                         DEBUG(1,("session_claim: failed to open sessionid tdb\n"));
69                         return False;
70                 }
71         }
72
73         ZERO_STRUCT(sessionid);
74
75         data.dptr = NULL;
76         data.dsize = 0;
77
78         for (i=1;i<MAX_SESSION_ID;i++) {
79                 slprintf(keystr, sizeof(keystr)-1, "ID/%d", i);
80                 key.dptr = keystr;
81                 key.dsize = strlen(keystr)+1;
82
83                 if (tdb_store(tdb, key, data, TDB_INSERT) == 0) break;
84         }
85
86         if (i == MAX_SESSION_ID) {
87                 DEBUG(1,("session_claim: out of session IDs (max is %d)\n", 
88                          MAX_SESSION_ID));
89                 return False;
90         }
91
92         hostname = client_name();
93         if (strequal(hostname,"UNKNOWN"))
94                 hostname = client_addr();
95
96         fstrcpy(sessionid.username, vuser->user.unix_name);
97         fstrcpy(sessionid.hostname, hostname);
98         slprintf(sessionid.id_str, sizeof(sessionid.id_str)-1, SESSION_TEMPLATE, i);
99         sessionid.id_num = i;
100         sessionid.pid = pid;
101
102         if (!smb_pam_claim_session(sessionid.username, sessionid.id_str, sessionid.hostname)) {
103                 DEBUG(1,("pam_session rejected the session for %s [%s]\n",
104                                 sessionid.username, sessionid.id_str));
105                 tdb_delete(tdb, key);
106                 return False;
107         }
108
109         dlen = tdb_pack(dbuf, sizeof(dbuf), "fffdd",
110                         sessionid.username, sessionid.hostname, sessionid.id_str,
111                         sessionid.id_num, sessionid.pid);
112
113         data.dptr = dbuf;
114         data.dsize = dlen;
115         if (tdb_store(tdb, key, data, TDB_MODIFY) != 0) {
116                 DEBUG(1,("session_claim: unable to create session id record\n"));
117                 return False;
118         }
119
120 #if WITH_UTMP   
121         if (lp_utmp()) {
122                 sys_utmp_claim(sessionid.username, sessionid.hostname, 
123                                sessionid.id_str, sessionid.id_num);
124         }
125 #endif
126
127         vuser->session_id = i;
128         return True;
129 }
130
131 /* called when a session is destroyed */
132 void session_yield(uint16 vuid)
133 {
134         user_struct *vuser = get_valid_user_struct(vuid);
135         TDB_DATA data;
136         struct sessionid sessionid;
137         TDB_DATA key;           
138         fstring keystr;
139
140         if (!tdb) return;
141
142         if (vuser->session_id == 0) {
143                 return;
144         }
145
146         slprintf(keystr, sizeof(keystr)-1, "ID/%d", vuser->session_id);
147
148         key.dptr = keystr;
149         key.dsize = strlen(keystr)+1;
150
151         data = tdb_fetch(tdb, key);
152         if (data.dptr == NULL) {
153                 return;
154         }
155
156         tdb_unpack(data.dptr, data.dsize, "fffdd",
157                    &sessionid.username, &sessionid.hostname, &sessionid.id_str,
158                    &sessionid.id_num, &sessionid.pid);
159
160         safe_free(data.dptr);
161         data.dptr = NULL;
162
163 #if WITH_UTMP   
164         if (lp_utmp()) {
165                 sys_utmp_yield(sessionid.username, sessionid.hostname, 
166                                sessionid.id_str, sessionid.id_num);
167         }
168 #endif
169
170         smb_pam_close_session(sessionid.username, sessionid.id_str, sessionid.hostname);
171
172         tdb_delete(tdb, key);
173 }
174
175 #else
176  /* null functions - no session support needed */
177  BOOL session_claim(uint16 vuid) { return True; }
178  void session_yield(uint16 vuid) {} 
179 #endif