- got fed up of vuser_db spewing 150 lines of trash, made it possible
[Samba.git] / source / lib / vuser_db.c
blobf8d3ea5a83d4b9ebaa4b5ae133f856d6f71052be
1 /*
2 Unix SMB/Netbios implementation.
3 Version 1.9.
4 Samba utility functions
5 Copyright (C) Andrew Tridgell 1992-2000
6 Copyright (C) Luke Kenneth Casson Leighton 1996-2000
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 * this module stores a user_struct which can be globally accessed (by root)
25 * across process boundaries, in order to obtain info about users currently
26 * accessing * samba.
28 * the database key is the process number (pid) of the creater of the
29 * structure plus the vuid - SMB virtual user id. NORMALLY, this would
30 * be smbd pid and an smbd-generated vuid.
34 #include "includes.h"
36 extern int DEBUGLEVEL;
38 static TDB_CONTEXT *tdb = NULL;
40 BOOL tdb_delete_vuid( const vuser_key *uk)
42 prs_struct key;
43 vuser_key k = *uk;
45 if (tdb == NULL)
47 if (!vuid_init_db())
49 return False;
53 DEBUG(10,("delete user %x,%x\n", uk->pid, uk->vuid));
55 prs_init(&key, 0, 4, False);
56 if (!vuid_io_key("key", &k, &key, 0))
58 return False;
61 prs_tdb_delete(tdb, &key);
63 prs_free_data(&key);
65 return True;
68 BOOL tdb_lookup_vuid( const vuser_key *uk, user_struct **usr)
70 prs_struct key;
71 prs_struct data;
72 vuser_key k = *uk;
74 if (usr != NULL)
76 (*usr) = (user_struct *)malloc(sizeof(**usr));
77 if ((*usr) == NULL)
79 return False;
81 ZERO_STRUCTP((*usr));
83 if (tdb == NULL)
85 if (!vuid_init_db())
87 safe_free((*usr));
88 return False;
92 DEBUG(10,("lookup user %x,%x\n", uk->pid, uk->vuid));
94 prs_init(&key, 0, 4, False);
95 if (!vuid_io_key("key", &k, &key, 0))
97 prs_free_data(&key);
98 safe_free((*usr));
99 return False;
102 prs_tdb_fetch(tdb, &key, &data);
104 if (usr != NULL)
106 if (!vuid_io_user_struct("usr", (*usr), &data, 100))
108 prs_free_data(&key);
109 prs_free_data(&data);
110 safe_free((*usr));
111 return False;
115 prs_free_data(&key);
116 prs_free_data(&data);
118 return True;
121 BOOL tdb_store_vuid( const vuser_key *uk, user_struct *usr)
123 prs_struct key;
124 prs_struct data;
125 vuser_key k = *uk;
127 if (tdb == NULL)
129 if (!vuid_init_db())
131 return False;
135 DEBUG(10,("storing user %x,%x\n", uk->pid, uk->vuid));
137 prs_init(&key, 0, 4, False);
138 prs_init(&data, 0, 4, False);
140 if (!vuid_io_key("key", &k, &key, 0) ||
141 !vuid_io_user_struct("usr", usr, &data, 100) ||
142 prs_tdb_store(tdb, TDB_REPLACE, &key, &data) != 0)
144 prs_free_data(&key);
145 prs_free_data(&data);
146 return False;
149 prs_free_data(&key);
150 prs_free_data(&data);
151 return True;
154 BOOL vuid_init_db(void)
156 tdb = tdb_open(lock_path("vuid.tdb"), 0, 0, O_RDWR | O_CREAT, 0600);
158 if (tdb == NULL)
160 DEBUG(0,("vuid_init_db: failed\n"));
161 return False;
164 DEBUG(10,("vuid_init_db: opened\n"));
166 return True;