1 | /*
|
---|
2 | Unix SMB/CIFS implementation.
|
---|
3 | Low-level sessionid.tdb access functions
|
---|
4 | Copyright (C) Volker Lendecke 2010
|
---|
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 "system/filesys.h"
|
---|
22 | #include "dbwrap.h"
|
---|
23 | #include "session.h"
|
---|
24 | #include "util_tdb.h"
|
---|
25 |
|
---|
26 | static struct db_context *session_db_ctx_ptr = NULL;
|
---|
27 |
|
---|
28 | static struct db_context *session_db_ctx(void)
|
---|
29 | {
|
---|
30 | return session_db_ctx_ptr;
|
---|
31 | }
|
---|
32 |
|
---|
33 | static struct db_context *session_db_ctx_init(bool readonly)
|
---|
34 | {
|
---|
35 | session_db_ctx_ptr = db_open(NULL, lock_path("sessionid.tdb"), 0,
|
---|
36 | TDB_CLEAR_IF_FIRST|TDB_DEFAULT|TDB_INCOMPATIBLE_HASH,
|
---|
37 | readonly ? O_RDONLY : O_RDWR | O_CREAT, 0644);
|
---|
38 | return session_db_ctx_ptr;
|
---|
39 | }
|
---|
40 |
|
---|
41 | bool sessionid_init(void)
|
---|
42 | {
|
---|
43 | if (session_db_ctx_init(false) == NULL) {
|
---|
44 | DEBUG(1,("session_init: failed to open sessionid tdb\n"));
|
---|
45 | return False;
|
---|
46 | }
|
---|
47 |
|
---|
48 | return True;
|
---|
49 | }
|
---|
50 |
|
---|
51 | bool sessionid_init_readonly(void)
|
---|
52 | {
|
---|
53 | if (session_db_ctx_init(true) == NULL) {
|
---|
54 | DEBUG(1,("session_init: failed to open sessionid tdb\n"));
|
---|
55 | return False;
|
---|
56 | }
|
---|
57 |
|
---|
58 | return True;
|
---|
59 | }
|
---|
60 |
|
---|
61 | struct db_record *sessionid_fetch_record(TALLOC_CTX *mem_ctx, const char *key)
|
---|
62 | {
|
---|
63 | struct db_context *db;
|
---|
64 |
|
---|
65 | db = session_db_ctx();
|
---|
66 | if (db == NULL) {
|
---|
67 | return NULL;
|
---|
68 | }
|
---|
69 | return db->fetch_locked(db, mem_ctx, string_term_tdb_data(key));
|
---|
70 | }
|
---|
71 |
|
---|
72 | struct sessionid_traverse_state {
|
---|
73 | int (*fn)(struct db_record *rec, const char *key,
|
---|
74 | struct sessionid *session, void *private_data);
|
---|
75 | void *private_data;
|
---|
76 | };
|
---|
77 |
|
---|
78 | static int sessionid_traverse_fn(struct db_record *rec, void *private_data)
|
---|
79 | {
|
---|
80 | struct sessionid_traverse_state *state =
|
---|
81 | (struct sessionid_traverse_state *)private_data;
|
---|
82 | struct sessionid session;
|
---|
83 |
|
---|
84 | if ((rec->key.dptr[rec->key.dsize-1] != '\0')
|
---|
85 | || (rec->value.dsize != sizeof(struct sessionid))) {
|
---|
86 | DEBUG(1, ("Found invalid record in sessionid.tdb\n"));
|
---|
87 | return 0;
|
---|
88 | }
|
---|
89 |
|
---|
90 | memcpy(&session, rec->value.dptr, sizeof(session));
|
---|
91 |
|
---|
92 | return state->fn(rec, (char *)rec->key.dptr, &session,
|
---|
93 | state->private_data);
|
---|
94 | }
|
---|
95 |
|
---|
96 | int sessionid_traverse(int (*fn)(struct db_record *rec, const char *key,
|
---|
97 | struct sessionid *session,
|
---|
98 | void *private_data),
|
---|
99 | void *private_data)
|
---|
100 | {
|
---|
101 | struct db_context *db;
|
---|
102 | struct sessionid_traverse_state state;
|
---|
103 |
|
---|
104 | db = session_db_ctx();
|
---|
105 | if (db == NULL) {
|
---|
106 | return -1;
|
---|
107 | }
|
---|
108 | state.fn = fn;
|
---|
109 | state.private_data = private_data;
|
---|
110 | return db->traverse(db, sessionid_traverse_fn, &state);
|
---|
111 | }
|
---|
112 |
|
---|
113 | struct sessionid_traverse_read_state {
|
---|
114 | int (*fn)(const char *key, struct sessionid *session,
|
---|
115 | void *private_data);
|
---|
116 | void *private_data;
|
---|
117 | };
|
---|
118 |
|
---|
119 | static int sessionid_traverse_read_fn(struct db_record *rec,
|
---|
120 | void *private_data)
|
---|
121 | {
|
---|
122 | struct sessionid_traverse_read_state *state =
|
---|
123 | (struct sessionid_traverse_read_state *)private_data;
|
---|
124 | struct sessionid session;
|
---|
125 |
|
---|
126 | if ((rec->key.dptr[rec->key.dsize-1] != '\0')
|
---|
127 | || (rec->value.dsize != sizeof(struct sessionid))) {
|
---|
128 | DEBUG(1, ("Found invalid record in sessionid.tdb\n"));
|
---|
129 | return 0;
|
---|
130 | }
|
---|
131 |
|
---|
132 | memcpy(&session, rec->value.dptr, sizeof(session));
|
---|
133 |
|
---|
134 | return state->fn((char *)rec->key.dptr, &session,
|
---|
135 | state->private_data);
|
---|
136 | }
|
---|
137 |
|
---|
138 | int sessionid_traverse_read(int (*fn)(const char *key,
|
---|
139 | struct sessionid *session,
|
---|
140 | void *private_data),
|
---|
141 | void *private_data)
|
---|
142 | {
|
---|
143 | struct db_context *db;
|
---|
144 | struct sessionid_traverse_read_state state;
|
---|
145 |
|
---|
146 | db = session_db_ctx();
|
---|
147 | if (db == NULL) {
|
---|
148 | return -1;
|
---|
149 | }
|
---|
150 | state.fn = fn;
|
---|
151 | state.private_data = private_data;
|
---|
152 | return db->traverse(db, sessionid_traverse_read_fn, &state);
|
---|
153 | }
|
---|