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