| 1 | /*
|
|---|
| 2 | Unix SMB/CIFS implementation.
|
|---|
| 3 | Utility functions for the dbwrap API
|
|---|
| 4 | Copyright (C) Volker Lendecke 2007
|
|---|
| 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 2 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, write to the Free Software
|
|---|
| 18 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
|---|
| 19 | */
|
|---|
| 20 |
|
|---|
| 21 | #include "includes.h"
|
|---|
| 22 |
|
|---|
| 23 | int32_t dbwrap_fetch_int32(struct db_context *db, const char *keystr)
|
|---|
| 24 | {
|
|---|
| 25 | TDB_DATA dbuf;
|
|---|
| 26 | int32 ret;
|
|---|
| 27 |
|
|---|
| 28 | if (db->fetch(db, NULL, string_term_tdb_data(keystr), &dbuf) != 0) {
|
|---|
| 29 | return -1;
|
|---|
| 30 | }
|
|---|
| 31 |
|
|---|
| 32 | if ((dbuf.dptr == NULL) || (dbuf.dsize != sizeof(int32_t))) {
|
|---|
| 33 | TALLOC_FREE(dbuf.dptr);
|
|---|
| 34 | return -1;
|
|---|
| 35 | }
|
|---|
| 36 |
|
|---|
| 37 | ret = IVAL(dbuf.dptr, 0);
|
|---|
| 38 | TALLOC_FREE(dbuf.dptr);
|
|---|
| 39 | return ret;
|
|---|
| 40 | }
|
|---|
| 41 |
|
|---|
| 42 | int dbwrap_store_int32(struct db_context *db, const char *keystr, int32_t v)
|
|---|
| 43 | {
|
|---|
| 44 | struct db_record *rec;
|
|---|
| 45 | int32 v_store;
|
|---|
| 46 | NTSTATUS status;
|
|---|
| 47 |
|
|---|
| 48 | rec = db->fetch_locked(db, NULL, string_term_tdb_data(keystr));
|
|---|
| 49 | if (rec == NULL) {
|
|---|
| 50 | return -1;
|
|---|
| 51 | }
|
|---|
| 52 |
|
|---|
| 53 | SIVAL(&v_store, 0, v);
|
|---|
| 54 |
|
|---|
| 55 | status = rec->store(rec, make_tdb_data((const uint8 *)&v_store,
|
|---|
| 56 | sizeof(v_store)),
|
|---|
| 57 | TDB_REPLACE);
|
|---|
| 58 | TALLOC_FREE(rec);
|
|---|
| 59 | return NT_STATUS_IS_OK(status) ? 0 : -1;
|
|---|
| 60 | }
|
|---|
| 61 |
|
|---|
| 62 | bool dbwrap_fetch_uint32(struct db_context *db, const char *keystr,
|
|---|
| 63 | uint32_t *val)
|
|---|
| 64 | {
|
|---|
| 65 | TDB_DATA dbuf;
|
|---|
| 66 |
|
|---|
| 67 | if (db->fetch(db, NULL, string_term_tdb_data(keystr), &dbuf) != 0) {
|
|---|
| 68 | return false;
|
|---|
| 69 | }
|
|---|
| 70 |
|
|---|
| 71 | if ((dbuf.dptr == NULL) || (dbuf.dsize != sizeof(uint32_t))) {
|
|---|
| 72 | TALLOC_FREE(dbuf.dptr);
|
|---|
| 73 | return false;
|
|---|
| 74 | }
|
|---|
| 75 |
|
|---|
| 76 | *val = IVAL(dbuf.dptr, 0);
|
|---|
| 77 | TALLOC_FREE(dbuf.dptr);
|
|---|
| 78 | return true;
|
|---|
| 79 | }
|
|---|
| 80 |
|
|---|
| 81 | int dbwrap_store_uint32(struct db_context *db, const char *keystr, uint32_t v)
|
|---|
| 82 | {
|
|---|
| 83 | struct db_record *rec;
|
|---|
| 84 | uint32 v_store;
|
|---|
| 85 | NTSTATUS status;
|
|---|
| 86 |
|
|---|
| 87 | rec = db->fetch_locked(db, NULL, string_term_tdb_data(keystr));
|
|---|
| 88 | if (rec == NULL) {
|
|---|
| 89 | return -1;
|
|---|
| 90 | }
|
|---|
| 91 |
|
|---|
| 92 | SIVAL(&v_store, 0, v);
|
|---|
| 93 |
|
|---|
| 94 | status = rec->store(rec, make_tdb_data((const uint8 *)&v_store,
|
|---|
| 95 | sizeof(v_store)),
|
|---|
| 96 | TDB_REPLACE);
|
|---|
| 97 | TALLOC_FREE(rec);
|
|---|
| 98 | return NT_STATUS_IS_OK(status) ? 0 : -1;
|
|---|
| 99 | }
|
|---|
| 100 |
|
|---|
| 101 | /**
|
|---|
| 102 | * Atomic unsigned integer change (addition):
|
|---|
| 103 | *
|
|---|
| 104 | * if value does not exist yet in the db, use *oldval as initial old value.
|
|---|
| 105 | * return old value in *oldval.
|
|---|
| 106 | * store *oldval + change_val to db.
|
|---|
| 107 | */
|
|---|
| 108 | uint32_t dbwrap_change_uint32_atomic(struct db_context *db, const char *keystr,
|
|---|
| 109 | uint32_t *oldval, uint32_t change_val)
|
|---|
| 110 | {
|
|---|
| 111 | struct db_record *rec;
|
|---|
| 112 | uint32 val = -1;
|
|---|
| 113 | TDB_DATA data;
|
|---|
| 114 |
|
|---|
| 115 | if (!(rec = db->fetch_locked(db, NULL,
|
|---|
| 116 | string_term_tdb_data(keystr)))) {
|
|---|
| 117 | return -1;
|
|---|
| 118 | }
|
|---|
| 119 |
|
|---|
| 120 | if (rec->value.dptr == NULL) {
|
|---|
| 121 | val = *oldval;
|
|---|
| 122 | } else if (rec->value.dsize == sizeof(val)) {
|
|---|
| 123 | val = IVAL(rec->value.dptr, 0);
|
|---|
| 124 | *oldval = val;
|
|---|
| 125 | } else {
|
|---|
| 126 | return -1;
|
|---|
| 127 | }
|
|---|
| 128 |
|
|---|
| 129 | val += change_val;
|
|---|
| 130 |
|
|---|
| 131 | data.dsize = sizeof(val);
|
|---|
| 132 | data.dptr = (uint8 *)&val;
|
|---|
| 133 |
|
|---|
| 134 | rec->store(rec, data, TDB_REPLACE);
|
|---|
| 135 |
|
|---|
| 136 | TALLOC_FREE(rec);
|
|---|
| 137 |
|
|---|
| 138 | return 0;
|
|---|
| 139 | }
|
|---|
| 140 |
|
|---|
| 141 | /**
|
|---|
| 142 | * Atomic integer change (addition):
|
|---|
| 143 | *
|
|---|
| 144 | * if value does not exist yet in the db, use *oldval as initial old value.
|
|---|
| 145 | * return old value in *oldval.
|
|---|
| 146 | * store *oldval + change_val to db.
|
|---|
| 147 | */
|
|---|
| 148 | int32 dbwrap_change_int32_atomic(struct db_context *db, const char *keystr,
|
|---|
| 149 | int32 *oldval, int32 change_val)
|
|---|
| 150 | {
|
|---|
| 151 | struct db_record *rec;
|
|---|
| 152 | int32 val = -1;
|
|---|
| 153 | TDB_DATA data;
|
|---|
| 154 |
|
|---|
| 155 | if (!(rec = db->fetch_locked(db, NULL,
|
|---|
| 156 | string_term_tdb_data(keystr)))) {
|
|---|
| 157 | return -1;
|
|---|
| 158 | }
|
|---|
| 159 |
|
|---|
| 160 | if (rec->value.dptr == NULL) {
|
|---|
| 161 | val = *oldval;
|
|---|
| 162 | } else if (rec->value.dsize == sizeof(val)) {
|
|---|
| 163 | val = IVAL(rec->value.dptr, 0);
|
|---|
| 164 | *oldval = val;
|
|---|
| 165 | } else {
|
|---|
| 166 | return -1;
|
|---|
| 167 | }
|
|---|
| 168 |
|
|---|
| 169 | val += change_val;
|
|---|
| 170 |
|
|---|
| 171 | data.dsize = sizeof(val);
|
|---|
| 172 | data.dptr = (uint8 *)&val;
|
|---|
| 173 |
|
|---|
| 174 | rec->store(rec, data, TDB_REPLACE);
|
|---|
| 175 |
|
|---|
| 176 | TALLOC_FREE(rec);
|
|---|
| 177 |
|
|---|
| 178 | return 0;
|
|---|
| 179 | }
|
|---|
| 180 |
|
|---|
| 181 | NTSTATUS dbwrap_trans_store(struct db_context *db, TDB_DATA key, TDB_DATA dbuf,
|
|---|
| 182 | int flag)
|
|---|
| 183 | {
|
|---|
| 184 | int res;
|
|---|
| 185 | struct db_record *rec = NULL;
|
|---|
| 186 | NTSTATUS status;
|
|---|
| 187 |
|
|---|
| 188 | res = db->transaction_start(db);
|
|---|
| 189 | if (res != 0) {
|
|---|
| 190 | DEBUG(5, ("transaction_start failed\n"));
|
|---|
| 191 | return NT_STATUS_INTERNAL_DB_CORRUPTION;
|
|---|
| 192 | }
|
|---|
| 193 |
|
|---|
| 194 | rec = db->fetch_locked(db, talloc_tos(), key);
|
|---|
| 195 | if (rec == NULL) {
|
|---|
| 196 | DEBUG(5, ("fetch_locked failed\n"));
|
|---|
| 197 | status = NT_STATUS_NO_MEMORY;
|
|---|
| 198 | goto cancel;
|
|---|
| 199 | }
|
|---|
| 200 |
|
|---|
| 201 | status = rec->store(rec, dbuf, flag);
|
|---|
| 202 | if (!NT_STATUS_IS_OK(status)) {
|
|---|
| 203 | DEBUG(5, ("store returned %s\n", nt_errstr(status)));
|
|---|
| 204 | goto cancel;
|
|---|
| 205 | }
|
|---|
| 206 |
|
|---|
| 207 | TALLOC_FREE(rec);
|
|---|
| 208 |
|
|---|
| 209 | res = db->transaction_commit(db);
|
|---|
| 210 | if (res != 0) {
|
|---|
| 211 | DEBUG(5, ("tdb_transaction_commit failed\n"));
|
|---|
| 212 | status = NT_STATUS_INTERNAL_DB_CORRUPTION;
|
|---|
| 213 | goto cancel;
|
|---|
| 214 | }
|
|---|
| 215 |
|
|---|
| 216 | return NT_STATUS_OK;
|
|---|
| 217 |
|
|---|
| 218 | cancel:
|
|---|
| 219 | TALLOC_FREE(rec);
|
|---|
| 220 |
|
|---|
| 221 | if (db->transaction_cancel(db) != 0) {
|
|---|
| 222 | smb_panic("Cancelling transaction failed");
|
|---|
| 223 | }
|
|---|
| 224 | return status;
|
|---|
| 225 | }
|
|---|
| 226 |
|
|---|
| 227 | NTSTATUS dbwrap_trans_delete(struct db_context *db, TDB_DATA key)
|
|---|
| 228 | {
|
|---|
| 229 | int res;
|
|---|
| 230 | struct db_record *rec = NULL;
|
|---|
| 231 | NTSTATUS status;
|
|---|
| 232 |
|
|---|
| 233 | res = db->transaction_start(db);
|
|---|
| 234 | if (res != 0) {
|
|---|
| 235 | DEBUG(5, ("transaction_start failed\n"));
|
|---|
| 236 | return NT_STATUS_INTERNAL_DB_CORRUPTION;
|
|---|
| 237 | }
|
|---|
| 238 |
|
|---|
| 239 | rec = db->fetch_locked(db, talloc_tos(), key);
|
|---|
| 240 | if (rec == NULL) {
|
|---|
| 241 | DEBUG(5, ("fetch_locked failed\n"));
|
|---|
| 242 | status = NT_STATUS_NO_MEMORY;
|
|---|
| 243 | goto cancel;
|
|---|
| 244 | }
|
|---|
| 245 |
|
|---|
| 246 | status = rec->delete_rec(rec);
|
|---|
| 247 | if (!NT_STATUS_IS_OK(status)) {
|
|---|
| 248 | DEBUG(5, ("delete_rec returned %s\n", nt_errstr(status)));
|
|---|
| 249 | goto cancel;
|
|---|
| 250 | }
|
|---|
| 251 |
|
|---|
| 252 | TALLOC_FREE(rec);
|
|---|
| 253 |
|
|---|
| 254 | res = db->transaction_commit(db);
|
|---|
| 255 | if (res != 0) {
|
|---|
| 256 | DEBUG(5, ("tdb_transaction_commit failed\n"));
|
|---|
| 257 | status = NT_STATUS_INTERNAL_DB_CORRUPTION;
|
|---|
| 258 | goto cancel;
|
|---|
| 259 | }
|
|---|
| 260 |
|
|---|
| 261 | return NT_STATUS_OK;
|
|---|
| 262 |
|
|---|
| 263 | cancel:
|
|---|
| 264 | TALLOC_FREE(rec);
|
|---|
| 265 |
|
|---|
| 266 | if (db->transaction_cancel(db) != 0) {
|
|---|
| 267 | smb_panic("Cancelling transaction failed");
|
|---|
| 268 | }
|
|---|
| 269 | return status;
|
|---|
| 270 | }
|
|---|
| 271 |
|
|---|
| 272 | NTSTATUS dbwrap_trans_store_int32(struct db_context *db, const char *keystr,
|
|---|
| 273 | int32_t v)
|
|---|
| 274 | {
|
|---|
| 275 | int32 v_store;
|
|---|
| 276 |
|
|---|
| 277 | SIVAL(&v_store, 0, v);
|
|---|
| 278 |
|
|---|
| 279 | return dbwrap_trans_store(db, string_term_tdb_data(keystr),
|
|---|
| 280 | make_tdb_data((const uint8 *)&v_store,
|
|---|
| 281 | sizeof(v_store)),
|
|---|
| 282 | TDB_REPLACE);
|
|---|
| 283 | }
|
|---|
| 284 |
|
|---|
| 285 | NTSTATUS dbwrap_trans_store_uint32(struct db_context *db, const char *keystr,
|
|---|
| 286 | uint32_t v)
|
|---|
| 287 | {
|
|---|
| 288 | uint32 v_store;
|
|---|
| 289 |
|
|---|
| 290 | SIVAL(&v_store, 0, v);
|
|---|
| 291 |
|
|---|
| 292 | return dbwrap_trans_store(db, string_term_tdb_data(keystr),
|
|---|
| 293 | make_tdb_data((const uint8 *)&v_store,
|
|---|
| 294 | sizeof(v_store)),
|
|---|
| 295 | TDB_REPLACE);
|
|---|
| 296 | }
|
|---|
| 297 |
|
|---|
| 298 | NTSTATUS dbwrap_trans_store_bystring(struct db_context *db, const char *key,
|
|---|
| 299 | TDB_DATA data, int flags)
|
|---|
| 300 | {
|
|---|
| 301 | return dbwrap_trans_store(db, string_term_tdb_data(key), data, flags);
|
|---|
| 302 | }
|
|---|
| 303 |
|
|---|
| 304 | NTSTATUS dbwrap_trans_delete_bystring(struct db_context *db, const char *key)
|
|---|
| 305 | {
|
|---|
| 306 | return dbwrap_trans_delete(db, string_term_tdb_data(key));
|
|---|
| 307 | }
|
|---|