r5296: - only include the tdb headers where they are needed
[samba.git] / source4 / passdb / secrets.c
1 /* 
2    Unix SMB/CIFS implementation.
3    Copyright (C) Andrew Tridgell 1992-2001
4    Copyright (C) Andrew Bartlett      2002
5    Copyright (C) Rafal Szczesniak     2002
6    
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2 of the License, or
10    (at your option) any later version.
11    
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16    
17    You should have received a copy of the GNU General Public License
18    along with this program; if not, write to the Free Software
19    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 */
21
22 /* the Samba secrets database stores any generated, private information
23    such as the local SID and machine trust password */
24
25 #include "includes.h"
26 #include "lib/tdb/include/tdbutil.h"
27 #include "secrets.h"
28
29 #undef DBGC_CLASS
30 #define DBGC_CLASS DBGC_PASSDB
31
32 static struct tdb_wrap *tdb;
33
34 /**
35  * Use a TDB to store an incrementing random seed.
36  *
37  * Initialised to the current pid, the very first time Samba starts,
38  * and incremented by one each time it is needed.  
39  * 
40  * @note Not called by systems with a working /dev/urandom.
41  */
42 static void get_rand_seed(int *new_seed) 
43 {
44         *new_seed = getpid();
45         if (tdb) {
46                 tdb_change_int32_atomic(tdb->tdb, "INFO/random_seed", new_seed, 1);
47         }
48 }
49
50 /* close the secrets database */
51 void secrets_shutdown(void)
52 {
53         talloc_free(tdb);
54 }
55
56 /* open up the secrets database */
57 BOOL secrets_init(void)
58 {
59         pstring fname;
60         uint8_t dummy;
61
62         if (tdb)
63                 return True;
64
65         pstrcpy(fname, lp_private_dir());
66         pstrcat(fname,"/secrets.tdb");
67
68         tdb = tdb_wrap_open(talloc_autofree_context(), fname, 0, TDB_DEFAULT, O_RDWR|O_CREAT, 0600);
69
70         if (!tdb) {
71                 DEBUG(0,("Failed to open %s\n", fname));
72                 return False;
73         }
74
75         /**
76          * Set a reseed function for the crypto random generator 
77          * 
78          * This avoids a problem where systems without /dev/urandom
79          * could send the same challenge to multiple clients
80          */
81         set_rand_reseed_callback(get_rand_seed);
82
83         /* Ensure that the reseed is done now, while we are root, etc */
84         generate_random_buffer(&dummy, sizeof(dummy));
85
86         return True;
87 }
88
89 /* read a entry from the secrets database - the caller must free the result
90    if size is non-null then the size of the entry is put in there
91  */
92 void *secrets_fetch(const char *key, size_t *size)
93 {
94         TDB_DATA kbuf, dbuf;
95         secrets_init();
96         if (!tdb)
97                 return NULL;
98         kbuf.dptr = strdup(key);
99         kbuf.dsize = strlen(key);
100         dbuf = tdb_fetch(tdb->tdb, kbuf);
101         if (size)
102                 *size = dbuf.dsize;
103         free(kbuf.dptr);
104         return dbuf.dptr;
105 }
106
107 /************************************************************************
108  Routine to fetch the plaintext machine account password for a realm
109 the password is assumed to be a null terminated ascii string
110 ************************************************************************/
111 char *secrets_fetch_machine_password(const char *domain)
112 {
113         char *key;
114         char *ret;
115         asprintf(&key, "%s/%s", SECRETS_MACHINE_PASSWORD, domain);
116         strupper(key);
117         ret = (char *)secrets_fetch(key, NULL);
118         free(key);
119         return ret;
120 }
121
122
123
124 /*******************************************************************************
125  Lock the secrets tdb based on a string - this is used as a primitive form of mutex
126  between smbd instances.
127 *******************************************************************************/
128
129 BOOL secrets_named_mutex(const char *name, uint_t timeout, size_t *p_ref_count)
130 {
131         size_t ref_count = *p_ref_count;
132         int ret = 0;
133
134         secrets_init();
135         if (!tdb)
136                 return False;
137
138         if (ref_count == 0) {
139                 ret = tdb_lock_bystring(tdb->tdb, name, timeout);
140                 if (ret == 0)
141                         DEBUG(10,("secrets_named_mutex: got mutex for %s\n", name ));
142         }
143
144         if (ret == 0) {
145                 *p_ref_count = ++ref_count;
146                 DEBUG(10,("secrets_named_mutex: ref_count for mutex %s = %u\n", name, (uint_t)ref_count ));
147         }
148         return (ret == 0);
149 }
150
151 /*******************************************************************************
152  Unlock a named mutex.
153 *******************************************************************************/
154
155 void secrets_named_mutex_release(const char *name, size_t *p_ref_count)
156 {
157         size_t ref_count = *p_ref_count;
158
159         SMB_ASSERT(ref_count != 0);
160
161         secrets_init();
162         if (!tdb)
163                 return;
164
165         if (ref_count == 1) {
166                 tdb_unlock_bystring(tdb->tdb, name);
167                 DEBUG(10,("secrets_named_mutex: released mutex for %s\n", name ));
168         }
169
170         *p_ref_count = --ref_count;
171         DEBUG(10,("secrets_named_mutex_release: ref_count for mutex %s = %u\n", name, (uint_t)ref_count ));
172 }
173
174 /*
175   connect to the schannel ldb
176 */
177 struct ldb_wrap *secrets_db_connect(TALLOC_CTX *mem_ctx)
178 {
179         char *path;
180         struct ldb_wrap *ldb;
181
182         path = private_path(mem_ctx, "secrets.ldb");
183         if (!path) {
184                 return NULL;
185         }
186         
187         ldb = ldb_wrap_connect(mem_ctx, path, 0, NULL);
188         talloc_free(path);
189         if (!ldb) {
190                 return NULL;
191         }
192
193         return ldb;
194 }
195