merge from HEAD
[Samba.git] / source / lsarpcd / secret_db.c
blobd560d7fcaf4cf761b66560171d20ca59247627f2
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.
23 #include "includes.h"
24 #include "rpc_parse.h"
25 #include "rpc_client.h"
27 extern int DEBUGLEVEL;
29 BOOL tdb_delete_secret(TDB_CONTEXT * tdb, const UNISTR2 * uk)
31 prs_struct key;
32 UNISTR2 k;
33 pstring tmp;
35 copy_unistr2(&k, uk);
37 unistr2_to_ascii(tmp, uk, sizeof(tmp) - 1);
38 DEBUG(10, ("delete secret %s\n", tmp));
40 prs_init(&key, 0, 4, False);
41 if (!smb_io_unistr2("key", &k, 1, &key, 0))
43 return False;
46 prs_tdb_delete(tdb, &key);
48 prs_free_data(&key);
50 return True;
53 BOOL tdb_lookup_secret(TDB_CONTEXT * tdb, const UNISTR2 * uk,
54 LSA_SECRET ** usr)
56 prs_struct key;
57 prs_struct data;
58 UNISTR2 k = *uk;
59 pstring tmp;
61 copy_unistr2(&k, uk);
63 if (usr != NULL)
65 (*usr) = g_new(LSA_SECRET, 1);
66 if ((*usr) == NULL)
68 return False;
70 ZERO_STRUCTP((*usr));
73 unistr2_to_ascii(tmp, uk, sizeof(tmp) - 1);
74 DEBUG(10, ("lookup secret %s\n", tmp));
76 prs_init(&key, 0, 4, False);
77 if (!smb_io_unistr2("key", &k, 1, &key, 0))
79 prs_free_data(&key);
80 safe_free((*usr));
81 return False;
84 prs_tdb_fetch(tdb, &key, &data);
86 if (prs_buf_len(&data) == 0x0)
88 if (usr != NULL)
90 safe_free((*usr));
92 prs_free_data(&key);
93 prs_free_data(&data);
94 return False;
98 if (usr != NULL)
100 if (!lsa_io_secret("usr", (*usr), &data, 0))
102 prs_free_data(&key);
103 prs_free_data(&data);
104 safe_free((*usr));
105 return False;
109 prs_free_data(&key);
110 prs_free_data(&data);
112 return True;
115 BOOL tdb_store_secret(TDB_CONTEXT * tdb, const UNISTR2 * uk, LSA_SECRET * usr)
117 prs_struct key;
118 prs_struct data;
119 UNISTR2 k;
120 pstring tmp;
122 copy_unistr2(&k, uk);
124 unistr2_to_ascii(tmp, uk, sizeof(tmp) - 1);
125 DEBUG(10, ("storing secret %s\n", tmp));
128 prs_init(&key, 0, 4, False);
129 prs_init(&data, 0, 4, False);
131 if (!smb_io_unistr2("key", &k, 1, &key, 0) ||
132 !lsa_io_secret("usr", usr, &data, 0) ||
133 prs_tdb_store(tdb, TDB_REPLACE, &key, &data) != 0)
135 prs_free_data(&key);
136 prs_free_data(&data);
137 return False;
140 prs_free_data(&key);
141 prs_free_data(&data);
142 return True;
145 TDB_CONTEXT *open_secret_db(int perms)
147 extern fstring global_myworkgroup;
148 extern pstring global_myname;
149 fstring domsec;
150 fstring domname;
151 fstring srvname;
153 fstrcpy(domname, global_myworkgroup);
154 fstrcpy(srvname, global_myname);
155 strupper(domname);
156 strupper(srvname);
158 slprintf(domsec, sizeof(domsec) - 1, "%s.%s.tdb", domname, srvname);
160 return tdb_open(lock_path(domsec), 0, 0, perms, 0600);
163 BOOL secret_init_db(void)
165 extern fstring global_myworkgroup;
166 extern pstring global_myname;
167 uchar trust_passwd[16];
168 fstring domname;
169 fstring srvname;
170 NTTIME crt;
171 UNISTR2 name;
172 char *an = "$MACHINE.ACC";
173 LSA_SECRET sec;
174 TDB_CONTEXT *tdb;
175 BOOL ret = False;
177 fstrcpy(domname, global_myworkgroup);
178 fstrcpy(srvname, global_myname);
179 strupper(domname);
180 strupper(srvname);
182 tdb = open_secret_db(O_RDWR);
184 if (tdb != NULL)
186 DEBUG(10, ("secret_init_db: opened\n"));
187 return True;
190 tdb = open_secret_db(O_RDWR | O_CREAT);
192 if (tdb == NULL)
194 DEBUG(0, ("secret_init_db: failed\n"));
195 return False;
198 DEBUG(10, ("secret_init_db: opened first time: initialising.\n"));
200 generate_random_buffer(trust_passwd, 16, True);
201 unix_to_nt_time(&crt, time(NULL));
203 make_unistr2(&name, an, strlen(an));
204 ZERO_STRUCT(sec);
206 sec.curinfo.ptr_value = 1;
207 sec.curinfo.value.ptr_secret = 0x1;
208 make_strhdr2(&sec.curinfo.value.hdr_secret, 24, 24, 1);
210 secret_store_data( &sec.curinfo.value.enc_secret, trust_passwd, 16);
212 sec.oldinfo.ptr_update = 1;
213 sec.oldinfo.last_update = crt;
215 sec.curinfo.ptr_update = 1;
216 sec.curinfo.last_update = crt;
218 ret = tdb_store_secret(tdb, &name, &sec);
220 tdb_close(tdb);
222 return ret;