2 Samba Unix/Linux SMB client library
3 Distributed SMB/CIFS Server Management Utility
5 Copyright (C) 2008 Volker Lendecke
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 3 of the License, or
10 (at your option) any later version.
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.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
22 #include "lib/events/events.h"
23 #include "utils/net/net.h"
24 #include "libnet/libnet.h"
25 #include "libcli/security/security.h"
26 #include "param/secrets.h"
27 #include "param/param.h"
28 #include "lib/util/util_ldb.h"
30 int net_machinepw_usage(struct net_context
*ctx
, int argc
, const char **argv
)
32 d_printf("net machinepw <accountname>\n");
36 int net_machinepw(struct net_context
*ctx
, int argc
, const char **argv
)
38 struct ldb_context
*secrets
;
40 struct tevent_context
*ev
;
41 struct ldb_message
**msgs
;
43 const char *attrs
[] = { "secret", NULL
};
47 net_machinepw_usage(ctx
, argc
, argv
);
51 mem_ctx
= talloc_new(ctx
);
52 if (mem_ctx
== NULL
) {
53 d_fprintf(stderr
, "talloc_new failed\n");
57 ev
= event_context_init(mem_ctx
);
59 d_fprintf(stderr
, "event_context_init failed\n");
63 secrets
= secrets_db_connect(mem_ctx
, ev
, ctx
->lp_ctx
);
64 if (secrets
== NULL
) {
65 d_fprintf(stderr
, "secrets_db_connect failed\n");
69 num_records
= gendb_search(secrets
, mem_ctx
, NULL
, &msgs
, attrs
,
70 "(&(objectclass=primaryDomain)"
71 "(samaccountname=%s))", argv
[0]);
72 if (num_records
!= 1) {
73 d_fprintf(stderr
, "gendb_search returned %d records, "
74 "expected 1\n", num_records
);
78 secret
= ldb_msg_find_attr_as_string(msgs
[0], "secret", NULL
);
80 d_fprintf(stderr
, "machine account contains no secret\n");
84 printf("%s\n", secret
);