r5298: - got rid of pstring.h from includes.h. This at least makes it a bit
[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 #include "system/filesys.h"
29 #include "pstring.h"
30
31 #undef DBGC_CLASS
32 #define DBGC_CLASS DBGC_PASSDB
33
34 static struct tdb_wrap *tdb;
35
36 /**
37  * Use a TDB to store an incrementing random seed.
38  *
39  * Initialised to the current pid, the very first time Samba starts,
40  * and incremented by one each time it is needed.  
41  * 
42  * @note Not called by systems with a working /dev/urandom.
43  */
44 static void get_rand_seed(int *new_seed) 
45 {
46         *new_seed = getpid();
47         if (tdb) {
48                 tdb_change_int32_atomic(tdb->tdb, "INFO/random_seed", new_seed, 1);
49         }
50 }
51
52 /* close the secrets database */
53 void secrets_shutdown(void)
54 {
55         talloc_free(tdb);
56 }
57
58 /* open up the secrets database */
59 BOOL secrets_init(void)
60 {
61         pstring fname;
62         uint8_t dummy;
63
64         if (tdb)
65                 return True;
66
67         pstrcpy(fname, lp_private_dir());
68         pstrcat(fname,"/secrets.tdb");
69
70         tdb = tdb_wrap_open(talloc_autofree_context(), fname, 0, TDB_DEFAULT, O_RDWR|O_CREAT, 0600);
71
72         if (!tdb) {
73                 DEBUG(0,("Failed to open %s\n", fname));
74                 return False;
75         }
76
77         /**
78          * Set a reseed function for the crypto random generator 
79          * 
80          * This avoids a problem where systems without /dev/urandom
81          * could send the same challenge to multiple clients
82          */
83         set_rand_reseed_callback(get_rand_seed);
84
85         /* Ensure that the reseed is done now, while we are root, etc */
86         generate_random_buffer(&dummy, sizeof(dummy));
87
88         return True;
89 }
90
91 /* read a entry from the secrets database - the caller must free the result
92    if size is non-null then the size of the entry is put in there
93  */
94 void *secrets_fetch(const char *key, size_t *size)
95 {
96         TDB_DATA kbuf, dbuf;
97         secrets_init();
98         if (!tdb)
99                 return NULL;
100         kbuf.dptr = strdup(key);
101         kbuf.dsize = strlen(key);
102         dbuf = tdb_fetch(tdb->tdb, kbuf);
103         if (size)
104                 *size = dbuf.dsize;
105         free(kbuf.dptr);
106         return dbuf.dptr;
107 }
108
109 /************************************************************************
110  Routine to fetch the plaintext machine account password for a realm
111 the password is assumed to be a null terminated ascii string
112 ************************************************************************/
113 char *secrets_fetch_machine_password(const char *domain)
114 {
115         char *key;
116         char *ret;
117         asprintf(&key, "%s/%s", SECRETS_MACHINE_PASSWORD, domain);
118         strupper(key);
119         ret = (char *)secrets_fetch(key, NULL);
120         free(key);
121         return ret;
122 }
123
124
125
126 /*******************************************************************************
127  Lock the secrets tdb based on a string - this is used as a primitive form of mutex
128  between smbd instances.
129 *******************************************************************************/
130
131 BOOL secrets_named_mutex(const char *name, uint_t timeout, size_t *p_ref_count)
132 {
133         size_t ref_count = *p_ref_count;
134         int ret = 0;
135
136         secrets_init();
137         if (!tdb)
138                 return False;
139
140         if (ref_count == 0) {
141                 ret = tdb_lock_bystring(tdb->tdb, name, timeout);
142                 if (ret == 0)
143                         DEBUG(10,("secrets_named_mutex: got mutex for %s\n", name ));
144         }
145
146         if (ret == 0) {
147                 *p_ref_count = ++ref_count;
148                 DEBUG(10,("secrets_named_mutex: ref_count for mutex %s = %u\n", name, (uint_t)ref_count ));
149         }
150         return (ret == 0);
151 }
152
153 /*******************************************************************************
154  Unlock a named mutex.
155 *******************************************************************************/
156
157 void secrets_named_mutex_release(const char *name, size_t *p_ref_count)
158 {
159         size_t ref_count = *p_ref_count;
160
161         SMB_ASSERT(ref_count != 0);
162
163         secrets_init();
164         if (!tdb)
165                 return;
166
167         if (ref_count == 1) {
168                 tdb_unlock_bystring(tdb->tdb, name);
169                 DEBUG(10,("secrets_named_mutex: released mutex for %s\n", name ));
170         }
171
172         *p_ref_count = --ref_count;
173         DEBUG(10,("secrets_named_mutex_release: ref_count for mutex %s = %u\n", name, (uint_t)ref_count ));
174 }
175
176 /*
177   connect to the schannel ldb
178 */
179 struct ldb_wrap *secrets_db_connect(TALLOC_CTX *mem_ctx)
180 {
181         char *path;
182         struct ldb_wrap *ldb;
183
184         path = private_path(mem_ctx, "secrets.ldb");
185         if (!path) {
186                 return NULL;
187         }
188         
189         ldb = ldb_wrap_connect(mem_ctx, path, 0, NULL);
190         talloc_free(path);
191         if (!ldb) {
192                 return NULL;
193         }
194
195         return ldb;
196 }
197