s3: Abstract access to sessionid.tdb, similar to conn_tdb.c
[ira/wip.git] / source3 / lib / sessionid_tdb.c
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
22 static struct db_context *session_db_ctx(void)
23 {
24         static struct db_context *session_db_ctx_ptr;
25
26         if (session_db_ctx_ptr != NULL) {
27                 return session_db_ctx_ptr;
28         }
29
30         session_db_ctx_ptr = db_open(NULL, lock_path("sessionid.tdb"), 0,
31                                      TDB_CLEAR_IF_FIRST|TDB_DEFAULT,
32                                      O_RDWR | O_CREAT, 0644);
33         return session_db_ctx_ptr;
34 }
35
36 bool sessionid_init(void)
37 {
38         if (session_db_ctx() == NULL) {
39                 DEBUG(1,("session_init: failed to open sessionid tdb\n"));
40                 return False;
41         }
42
43         return True;
44 }
45
46 struct db_record *sessionid_fetch_record(TALLOC_CTX *mem_ctx, const char *key)
47 {
48         struct db_context *db;
49
50         db = session_db_ctx();
51         if (db == NULL) {
52                 return NULL;
53         }
54         return db->fetch_locked(db, mem_ctx, string_term_tdb_data(key));
55 }
56
57 struct sessionid_traverse_state {
58         int (*fn)(struct db_record *rec, const char *key,
59                   struct sessionid *session, void *private_data);
60         void *private_data;
61 };
62
63 static int sessionid_traverse_fn(struct db_record *rec, void *private_data)
64 {
65         struct sessionid_traverse_state *state =
66                 (struct sessionid_traverse_state *)private_data;
67         struct sessionid session;
68
69         if ((rec->key.dptr[rec->key.dsize-1] != '\0')
70             || (rec->value.dsize != sizeof(struct sessionid))) {
71                 DEBUG(1, ("Found invalid record in sessionid.tdb\n"));
72                 return 0;
73         }
74
75         memcpy(&session, rec->value.dptr, sizeof(session));
76
77         return state->fn(rec, (char *)rec->key.dptr, &session,
78                          state->private_data);
79 }
80
81 int sessionid_traverse(int (*fn)(struct db_record *rec, const char *key,
82                                  struct sessionid *session,
83                                  void *private_data),
84                        void *private_data)
85 {
86         struct db_context *db;
87         struct sessionid_traverse_state state;
88
89         db = session_db_ctx();
90         if (db == NULL) {
91                 return -1;
92         }
93         state.fn = fn;
94         state.private_data = private_data;
95         return db->traverse(db, sessionid_traverse_fn, &state);
96 }
97
98 struct sessionid_traverse_read_state {
99         int (*fn)(const char *key, struct sessionid *session,
100                   void *private_data);
101         void *private_data;
102 };
103
104 static int sessionid_traverse_read_fn(struct db_record *rec,
105                                       void *private_data)
106 {
107         struct sessionid_traverse_read_state *state =
108                 (struct sessionid_traverse_read_state *)private_data;
109         struct sessionid session;
110
111         if ((rec->key.dptr[rec->key.dsize-1] != '\0')
112             || (rec->value.dsize != sizeof(struct sessionid))) {
113                 DEBUG(1, ("Found invalid record in sessionid.tdb\n"));
114                 return 0;
115         }
116
117         memcpy(&session, rec->value.dptr, sizeof(session));
118
119         return state->fn((char *)rec->key.dptr, &session,
120                          state->private_data);
121 }
122
123 int sessionid_traverse_read(int (*fn)(const char *key,
124                                       struct sessionid *session,
125                                       void *private_data),
126                             void *private_data)
127 {
128         struct db_context *db;
129         struct sessionid_traverse_read_state state;
130
131         db = session_db_ctx();
132         if (db == NULL) {
133                 return -1;
134         }
135         state.fn = fn;
136         state.private_data = private_data;
137         return db->traverse(db, sessionid_traverse_read_fn, &state);
138 }