| 1 | /* | 
|---|
| 2 | Unix SMB/CIFS implementation. | 
|---|
| 3 | connection claim routines | 
|---|
| 4 | Copyright (C) Andrew Tridgell 1998 | 
|---|
| 5 |  | 
|---|
| 6 | This program is free software; you can redistribute it and/or modify | 
|---|
| 7 | it under the terms of the GNU General Public License as published by | 
|---|
| 8 | the Free Software Foundation; either version 3 of the License, or | 
|---|
| 9 | (at your option) any later version. | 
|---|
| 10 |  | 
|---|
| 11 | This program is distributed in the hope that it will be useful, | 
|---|
| 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of | 
|---|
| 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
|---|
| 14 | GNU General Public License for more details. | 
|---|
| 15 |  | 
|---|
| 16 | You should have received a copy of the GNU General Public License | 
|---|
| 17 | along with this program.  If not, see <http://www.gnu.org/licenses/>. | 
|---|
| 18 | */ | 
|---|
| 19 |  | 
|---|
| 20 | #include "includes.h" | 
|---|
| 21 | #include "smbd/smbd.h" | 
|---|
| 22 | #include "smbd/globals.h" | 
|---|
| 23 | #include "dbwrap.h" | 
|---|
| 24 | #include "auth.h" | 
|---|
| 25 |  | 
|---|
| 26 | /**************************************************************************** | 
|---|
| 27 | Delete a connection record. | 
|---|
| 28 | ****************************************************************************/ | 
|---|
| 29 |  | 
|---|
| 30 | bool yield_connection(connection_struct *conn, const char *name) | 
|---|
| 31 | { | 
|---|
| 32 | struct db_record *rec; | 
|---|
| 33 | NTSTATUS status; | 
|---|
| 34 |  | 
|---|
| 35 | DEBUG(3,("Yielding connection to %s\n",name)); | 
|---|
| 36 |  | 
|---|
| 37 | rec = connections_fetch_entry(talloc_tos(), conn, name); | 
|---|
| 38 | if (rec == NULL) { | 
|---|
| 39 | DEBUG(0, ("connections_fetch_entry failed\n")); | 
|---|
| 40 | return False; | 
|---|
| 41 | } | 
|---|
| 42 |  | 
|---|
| 43 | status = rec->delete_rec(rec); | 
|---|
| 44 | if (!NT_STATUS_IS_OK(status)) { | 
|---|
| 45 | DEBUG( NT_STATUS_EQUAL(status, NT_STATUS_NOT_FOUND) ? 3 : 0, | 
|---|
| 46 | ("deleting connection record returned %s\n", | 
|---|
| 47 | nt_errstr(status))); | 
|---|
| 48 | } | 
|---|
| 49 |  | 
|---|
| 50 | TALLOC_FREE(rec); | 
|---|
| 51 | return NT_STATUS_IS_OK(status); | 
|---|
| 52 | } | 
|---|
| 53 |  | 
|---|
| 54 | struct count_stat { | 
|---|
| 55 | int curr_connections; | 
|---|
| 56 | const char *name; | 
|---|
| 57 | bool Clear; | 
|---|
| 58 | }; | 
|---|
| 59 |  | 
|---|
| 60 | /**************************************************************************** | 
|---|
| 61 | Count the entries belonging to a service in the connection db. | 
|---|
| 62 | ****************************************************************************/ | 
|---|
| 63 |  | 
|---|
| 64 | static int count_fn(struct db_record *rec, | 
|---|
| 65 | const struct connections_key *ckey, | 
|---|
| 66 | const struct connections_data *crec, | 
|---|
| 67 | void *udp) | 
|---|
| 68 | { | 
|---|
| 69 | struct count_stat *cs = (struct count_stat *)udp; | 
|---|
| 70 |  | 
|---|
| 71 | if (crec->cnum == -1) { | 
|---|
| 72 | return 0; | 
|---|
| 73 | } | 
|---|
| 74 |  | 
|---|
| 75 | /* If the pid was not found delete the entry from connections.tdb */ | 
|---|
| 76 |  | 
|---|
| 77 | if (cs->Clear && !process_exists(crec->pid) && (errno == ESRCH)) { | 
|---|
| 78 | NTSTATUS status; | 
|---|
| 79 | DEBUG(2,("pid %s doesn't exist - deleting connections %d [%s]\n", | 
|---|
| 80 | procid_str_static(&crec->pid), crec->cnum, | 
|---|
| 81 | crec->servicename)); | 
|---|
| 82 |  | 
|---|
| 83 | status = rec->delete_rec(rec); | 
|---|
| 84 | if (!NT_STATUS_IS_OK(status)) { | 
|---|
| 85 | DEBUG(0,("count_fn: tdb_delete failed with error %s\n", | 
|---|
| 86 | nt_errstr(status))); | 
|---|
| 87 | } | 
|---|
| 88 | return 0; | 
|---|
| 89 | } | 
|---|
| 90 |  | 
|---|
| 91 | if (strequal(crec->servicename, cs->name)) | 
|---|
| 92 | cs->curr_connections++; | 
|---|
| 93 |  | 
|---|
| 94 | return 0; | 
|---|
| 95 | } | 
|---|
| 96 |  | 
|---|
| 97 | /**************************************************************************** | 
|---|
| 98 | Claim an entry in the connections database. | 
|---|
| 99 | ****************************************************************************/ | 
|---|
| 100 |  | 
|---|
| 101 | int count_current_connections( const char *sharename, bool clear  ) | 
|---|
| 102 | { | 
|---|
| 103 | struct count_stat cs; | 
|---|
| 104 |  | 
|---|
| 105 | cs.curr_connections = 0; | 
|---|
| 106 | cs.name = sharename; | 
|---|
| 107 | cs.Clear = clear; | 
|---|
| 108 |  | 
|---|
| 109 | /* | 
|---|
| 110 | * This has a race condition, but locking the chain before hand is worse | 
|---|
| 111 | * as it leads to deadlock. | 
|---|
| 112 | */ | 
|---|
| 113 |  | 
|---|
| 114 | if (connections_forall(count_fn, &cs) == -1) { | 
|---|
| 115 | DEBUG(0,("count_current_connections: traverse of " | 
|---|
| 116 | "connections.tdb failed\n")); | 
|---|
| 117 | return False; | 
|---|
| 118 | } | 
|---|
| 119 |  | 
|---|
| 120 | return cs.curr_connections; | 
|---|
| 121 | } | 
|---|
| 122 |  | 
|---|
| 123 | /**************************************************************************** | 
|---|
| 124 | Claim an entry in the connections database. | 
|---|
| 125 | ****************************************************************************/ | 
|---|
| 126 |  | 
|---|
| 127 | bool claim_connection(connection_struct *conn, const char *name) | 
|---|
| 128 | { | 
|---|
| 129 | struct db_record *rec; | 
|---|
| 130 | struct connections_data crec; | 
|---|
| 131 | TDB_DATA dbuf; | 
|---|
| 132 | NTSTATUS status; | 
|---|
| 133 |  | 
|---|
| 134 | DEBUG(5,("claiming [%s]\n", name)); | 
|---|
| 135 |  | 
|---|
| 136 | if (!(rec = connections_fetch_entry(talloc_tos(), conn, name))) { | 
|---|
| 137 | DEBUG(0, ("connections_fetch_entry failed\n")); | 
|---|
| 138 | return False; | 
|---|
| 139 | } | 
|---|
| 140 |  | 
|---|
| 141 | /* fill in the crec */ | 
|---|
| 142 | ZERO_STRUCT(crec); | 
|---|
| 143 | crec.magic = 0x280267; | 
|---|
| 144 | crec.pid = sconn_server_id(conn->sconn); | 
|---|
| 145 | crec.cnum = conn->cnum; | 
|---|
| 146 | crec.uid = conn->session_info->utok.uid; | 
|---|
| 147 | crec.gid = conn->session_info->utok.gid; | 
|---|
| 148 | strlcpy(crec.servicename, lp_servicename(SNUM(conn)), | 
|---|
| 149 | sizeof(crec.servicename)); | 
|---|
| 150 | crec.start = time(NULL); | 
|---|
| 151 |  | 
|---|
| 152 | strlcpy(crec.machine,get_remote_machine_name(),sizeof(crec.machine)); | 
|---|
| 153 | strlcpy(crec.addr, conn->sconn->client_id.addr, sizeof(crec.addr)); | 
|---|
| 154 |  | 
|---|
| 155 | dbuf.dptr = (uint8 *)&crec; | 
|---|
| 156 | dbuf.dsize = sizeof(crec); | 
|---|
| 157 |  | 
|---|
| 158 | status = rec->store(rec, dbuf, TDB_REPLACE); | 
|---|
| 159 |  | 
|---|
| 160 | TALLOC_FREE(rec); | 
|---|
| 161 |  | 
|---|
| 162 | if (!NT_STATUS_IS_OK(status)) { | 
|---|
| 163 | DEBUG(0,("claim_connection: tdb_store failed with error %s.\n", | 
|---|
| 164 | nt_errstr(status))); | 
|---|
| 165 | return False; | 
|---|
| 166 | } | 
|---|
| 167 |  | 
|---|
| 168 | return True; | 
|---|
| 169 | } | 
|---|