| 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 2 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, write to the Free Software
|
|---|
| 21 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
|---|
| 22 | */
|
|---|
| 23 |
|
|---|
| 24 | /* a "session" is claimed when we do a SessionSetupX operation
|
|---|
| 25 | and is yielded when the corresponding vuid is destroyed.
|
|---|
| 26 |
|
|---|
| 27 | sessions are used to populate utmp and PAM session structures
|
|---|
| 28 | */
|
|---|
| 29 |
|
|---|
| 30 | #include "includes.h"
|
|---|
| 31 |
|
|---|
| 32 | static TDB_CONTEXT *tdb;
|
|---|
| 33 |
|
|---|
| 34 | /********************************************************************
|
|---|
| 35 | ********************************************************************/
|
|---|
| 36 |
|
|---|
| 37 | BOOL session_init(void)
|
|---|
| 38 | {
|
|---|
| 39 | if (tdb)
|
|---|
| 40 | return True;
|
|---|
| 41 |
|
|---|
| 42 | tdb = tdb_open_log(lock_path("sessionid.tdb"), 0, TDB_CLEAR_IF_FIRST|TDB_DEFAULT,
|
|---|
| 43 | O_RDWR | O_CREAT, 0644);
|
|---|
| 44 | if (!tdb) {
|
|---|
| 45 | DEBUG(1,("session_init: failed to open sessionid tdb\n"));
|
|---|
| 46 | return False;
|
|---|
| 47 | }
|
|---|
| 48 |
|
|---|
| 49 | return True;
|
|---|
| 50 | }
|
|---|
| 51 |
|
|---|
| 52 | /********************************************************************
|
|---|
| 53 | called when a session is created
|
|---|
| 54 | ********************************************************************/
|
|---|
| 55 |
|
|---|
| 56 | BOOL session_claim(user_struct *vuser)
|
|---|
| 57 | {
|
|---|
| 58 | int i = 0;
|
|---|
| 59 | TDB_DATA data;
|
|---|
| 60 | struct sockaddr sa;
|
|---|
| 61 | struct in_addr *client_ip;
|
|---|
| 62 | struct sessionid sessionid;
|
|---|
| 63 | uint32 pid = (uint32)sys_getpid();
|
|---|
| 64 | TDB_DATA key;
|
|---|
| 65 | fstring keystr;
|
|---|
| 66 | char * hostname;
|
|---|
| 67 | int tdb_store_flag; /* If using utmp, we do an inital 'lock hold' store,
|
|---|
| 68 | but we don't need this if we are just using the
|
|---|
| 69 | (unique) pid/vuid combination */
|
|---|
| 70 |
|
|---|
| 71 | vuser->session_keystr = NULL;
|
|---|
| 72 |
|
|---|
| 73 | /* don't register sessions for the guest user - its just too
|
|---|
| 74 | expensive to go through pam session code for browsing etc */
|
|---|
| 75 | if (vuser->guest) {
|
|---|
| 76 | return True;
|
|---|
| 77 | }
|
|---|
| 78 |
|
|---|
| 79 | if (!session_init())
|
|---|
| 80 | return False;
|
|---|
| 81 |
|
|---|
| 82 | ZERO_STRUCT(sessionid);
|
|---|
| 83 |
|
|---|
| 84 | data.dptr = NULL;
|
|---|
| 85 | data.dsize = 0;
|
|---|
| 86 |
|
|---|
| 87 | if (lp_utmp()) {
|
|---|
| 88 | for (i=1;i<MAX_SESSION_ID;i++) {
|
|---|
| 89 | slprintf(keystr, sizeof(keystr)-1, "ID/%d", i);
|
|---|
| 90 | key.dptr = keystr;
|
|---|
| 91 | key.dsize = strlen(keystr)+1;
|
|---|
| 92 |
|
|---|
| 93 | if (tdb_store(tdb, key, data, TDB_INSERT) == 0) break;
|
|---|
| 94 | }
|
|---|
| 95 |
|
|---|
| 96 | if (i == MAX_SESSION_ID) {
|
|---|
| 97 | DEBUG(1,("session_claim: out of session IDs (max is %d)\n",
|
|---|
| 98 | MAX_SESSION_ID));
|
|---|
| 99 | return False;
|
|---|
| 100 | }
|
|---|
| 101 | slprintf(sessionid.id_str, sizeof(sessionid.id_str)-1, SESSION_UTMP_TEMPLATE, i);
|
|---|
| 102 | tdb_store_flag = TDB_MODIFY;
|
|---|
| 103 | } else
|
|---|
| 104 | {
|
|---|
| 105 | slprintf(keystr, sizeof(keystr)-1, "ID/%lu/%u",
|
|---|
| 106 | (long unsigned int)sys_getpid(),
|
|---|
| 107 | vuser->vuid);
|
|---|
| 108 | slprintf(sessionid.id_str, sizeof(sessionid.id_str)-1,
|
|---|
| 109 | SESSION_TEMPLATE, (long unsigned int)sys_getpid(),
|
|---|
| 110 | vuser->vuid);
|
|---|
| 111 |
|
|---|
| 112 | key.dptr = keystr;
|
|---|
| 113 | key.dsize = strlen(keystr)+1;
|
|---|
| 114 |
|
|---|
| 115 | tdb_store_flag = TDB_REPLACE;
|
|---|
| 116 | }
|
|---|
| 117 |
|
|---|
| 118 | /* If 'hostname lookup' == yes, then do the DNS lookup. This is
|
|---|
| 119 | needed because utmp and PAM both expect DNS names
|
|---|
| 120 |
|
|---|
| 121 | client_name() handles this case internally.
|
|---|
| 122 | */
|
|---|
| 123 |
|
|---|
| 124 | hostname = client_name();
|
|---|
| 125 | if (strcmp(hostname, "UNKNOWN") == 0) {
|
|---|
| 126 | hostname = client_addr();
|
|---|
| 127 | }
|
|---|
| 128 |
|
|---|
| 129 | fstrcpy(sessionid.username, vuser->user.unix_name);
|
|---|
| 130 | fstrcpy(sessionid.hostname, hostname);
|
|---|
| 131 | sessionid.id_num = i; /* Only valid for utmp sessions */
|
|---|
| 132 | sessionid.pid = pid;
|
|---|
| 133 | sessionid.uid = vuser->uid;
|
|---|
| 134 | sessionid.gid = vuser->gid;
|
|---|
| 135 | fstrcpy(sessionid.remote_machine, get_remote_machine_name());
|
|---|
| 136 | fstrcpy(sessionid.ip_addr, client_addr());
|
|---|
| 137 | sessionid.connect_start = time(NULL);
|
|---|
| 138 |
|
|---|
| 139 | client_ip = client_inaddr(&sa);
|
|---|
| 140 |
|
|---|
| 141 | if (!smb_pam_claim_session(sessionid.username, sessionid.id_str, sessionid.hostname)) {
|
|---|
| 142 | DEBUG(1,("pam_session rejected the session for %s [%s]\n",
|
|---|
| 143 | sessionid.username, sessionid.id_str));
|
|---|
| 144 | if (tdb_store_flag == TDB_MODIFY) {
|
|---|
| 145 | tdb_delete(tdb, key);
|
|---|
| 146 | }
|
|---|
| 147 | return False;
|
|---|
| 148 | }
|
|---|
| 149 |
|
|---|
| 150 | data.dptr = (char *)&sessionid;
|
|---|
| 151 | data.dsize = sizeof(sessionid);
|
|---|
| 152 | if (tdb_store(tdb, key, data, tdb_store_flag) != 0) {
|
|---|
| 153 | DEBUG(1,("session_claim: unable to create session id record\n"));
|
|---|
| 154 | return False;
|
|---|
| 155 | }
|
|---|
| 156 |
|
|---|
| 157 | if (lp_utmp()) {
|
|---|
| 158 | sys_utmp_claim(sessionid.username, sessionid.hostname,
|
|---|
| 159 | client_ip,
|
|---|
| 160 | sessionid.id_str, sessionid.id_num);
|
|---|
| 161 | }
|
|---|
| 162 |
|
|---|
| 163 | vuser->session_keystr = SMB_STRDUP(keystr);
|
|---|
| 164 | if (!vuser->session_keystr) {
|
|---|
| 165 | DEBUG(0, ("session_claim: strdup() failed for session_keystr\n"));
|
|---|
| 166 | return False;
|
|---|
| 167 | }
|
|---|
| 168 | return True;
|
|---|
| 169 | }
|
|---|
| 170 |
|
|---|
| 171 | /********************************************************************
|
|---|
| 172 | called when a session is destroyed
|
|---|
| 173 | ********************************************************************/
|
|---|
| 174 |
|
|---|
| 175 | void session_yield(user_struct *vuser)
|
|---|
| 176 | {
|
|---|
| 177 | TDB_DATA dbuf;
|
|---|
| 178 | struct sessionid sessionid;
|
|---|
| 179 | struct in_addr *client_ip;
|
|---|
| 180 | TDB_DATA key;
|
|---|
| 181 |
|
|---|
| 182 | if (!tdb) return;
|
|---|
| 183 |
|
|---|
| 184 | if (!vuser->session_keystr) {
|
|---|
| 185 | return;
|
|---|
| 186 | }
|
|---|
| 187 |
|
|---|
| 188 | key.dptr = vuser->session_keystr;
|
|---|
| 189 | key.dsize = strlen(vuser->session_keystr)+1;
|
|---|
| 190 |
|
|---|
| 191 | dbuf = tdb_fetch(tdb, key);
|
|---|
| 192 |
|
|---|
| 193 | if (dbuf.dsize != sizeof(sessionid))
|
|---|
| 194 | return;
|
|---|
| 195 |
|
|---|
| 196 | memcpy(&sessionid, dbuf.dptr, sizeof(sessionid));
|
|---|
| 197 |
|
|---|
| 198 | client_ip = interpret_addr2(sessionid.ip_addr);
|
|---|
| 199 |
|
|---|
| 200 | SAFE_FREE(dbuf.dptr);
|
|---|
| 201 |
|
|---|
| 202 | if (lp_utmp()) {
|
|---|
| 203 | sys_utmp_yield(sessionid.username, sessionid.hostname,
|
|---|
| 204 | client_ip,
|
|---|
| 205 | sessionid.id_str, sessionid.id_num);
|
|---|
| 206 | }
|
|---|
| 207 |
|
|---|
| 208 | smb_pam_close_session(sessionid.username, sessionid.id_str, sessionid.hostname);
|
|---|
| 209 |
|
|---|
| 210 | tdb_delete(tdb, key);
|
|---|
| 211 | }
|
|---|
| 212 |
|
|---|
| 213 | /********************************************************************
|
|---|
| 214 | ********************************************************************/
|
|---|
| 215 |
|
|---|
| 216 | BOOL session_traverse(int (*fn)(TDB_CONTEXT *, TDB_DATA, TDB_DATA, void *),
|
|---|
| 217 | void *state)
|
|---|
| 218 | {
|
|---|
| 219 | if (!session_init()) {
|
|---|
| 220 | DEBUG(3, ("No tdb opened\n"));
|
|---|
| 221 | return False;
|
|---|
| 222 | }
|
|---|
| 223 |
|
|---|
| 224 | tdb_traverse(tdb, fn, state);
|
|---|
| 225 | return True;
|
|---|
| 226 | }
|
|---|
| 227 |
|
|---|
| 228 | /********************************************************************
|
|---|
| 229 | ********************************************************************/
|
|---|
| 230 |
|
|---|
| 231 | struct session_list {
|
|---|
| 232 | int count;
|
|---|
| 233 | struct sessionid *sessions;
|
|---|
| 234 | };
|
|---|
| 235 |
|
|---|
| 236 | static int gather_sessioninfo(TDB_CONTEXT *stdb, TDB_DATA kbuf, TDB_DATA dbuf, void *state)
|
|---|
| 237 | {
|
|---|
| 238 | uint32 i;
|
|---|
| 239 | struct session_list *sesslist = (struct session_list *) state;
|
|---|
| 240 | const struct sessionid *current = (const struct sessionid *) dbuf.dptr;
|
|---|
| 241 |
|
|---|
| 242 | i = sesslist->count;
|
|---|
| 243 |
|
|---|
| 244 | sesslist->sessions = SMB_REALLOC_ARRAY(sesslist->sessions, struct sessionid, i+1);
|
|---|
| 245 | if (!sesslist->sessions) {
|
|---|
| 246 | sesslist->count = 0;
|
|---|
| 247 | return -1;
|
|---|
| 248 | }
|
|---|
| 249 |
|
|---|
| 250 | memcpy(&sesslist->sessions[i], current, sizeof(struct sessionid));
|
|---|
| 251 | sesslist->count++;
|
|---|
| 252 |
|
|---|
| 253 | DEBUG(7,("gather_sessioninfo session from %s@%s\n",
|
|---|
| 254 | current->username, current->remote_machine));
|
|---|
| 255 |
|
|---|
| 256 | return 0;
|
|---|
| 257 | }
|
|---|
| 258 |
|
|---|
| 259 | /********************************************************************
|
|---|
| 260 | ********************************************************************/
|
|---|
| 261 |
|
|---|
| 262 | int list_sessions(struct sessionid **session_list)
|
|---|
| 263 | {
|
|---|
| 264 | struct session_list sesslist;
|
|---|
| 265 |
|
|---|
| 266 | sesslist.count = 0;
|
|---|
| 267 | sesslist.sessions = NULL;
|
|---|
| 268 |
|
|---|
| 269 | if (!session_traverse(gather_sessioninfo, (void *) &sesslist)) {
|
|---|
| 270 | DEBUG(3, ("Session traverse failed\n"));
|
|---|
| 271 | SAFE_FREE(sesslist.sessions);
|
|---|
| 272 | *session_list = NULL;
|
|---|
| 273 | return 0;
|
|---|
| 274 | }
|
|---|
| 275 |
|
|---|
| 276 | *session_list = sesslist.sessions;
|
|---|
| 277 | return sesslist.count;
|
|---|
| 278 | }
|
|---|