2 Unix SMB/Netbios implementation.
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 #include "rpc_parse.h"
25 #include "rpc_client.h"
27 extern int DEBUGLEVEL
;
29 BOOL
tdb_delete_secret(TDB_CONTEXT
* tdb
, const UNISTR2
* 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))
46 prs_tdb_delete(tdb
, &key
);
53 BOOL
tdb_lookup_secret(TDB_CONTEXT
* tdb
, const UNISTR2
* uk
,
65 (*usr
) = g_new(LSA_SECRET
, 1);
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))
84 prs_tdb_fetch(tdb
, &key
, &data
);
86 if (prs_buf_len(&data
) == 0x0)
100 if (!lsa_io_secret("usr", (*usr
), &data
, 0))
103 prs_free_data(&data
);
110 prs_free_data(&data
);
115 BOOL
tdb_store_secret(TDB_CONTEXT
* tdb
, const UNISTR2
* uk
, LSA_SECRET
* usr
)
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)
136 prs_free_data(&data
);
141 prs_free_data(&data
);
145 TDB_CONTEXT
*open_secret_db(int perms
)
147 extern fstring global_myworkgroup
;
148 extern pstring global_myname
;
153 fstrcpy(domname
, global_myworkgroup
);
154 fstrcpy(srvname
, global_myname
);
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];
172 char *an
= "$MACHINE.ACC";
177 fstrcpy(domname
, global_myworkgroup
);
178 fstrcpy(srvname
, global_myname
);
182 tdb
= open_secret_db(O_RDWR
);
186 DEBUG(10, ("secret_init_db: opened\n"));
190 tdb
= open_secret_db(O_RDWR
| O_CREAT
);
194 DEBUG(0, ("secret_init_db: failed\n"));
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
));
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
);