Fix the mess with ldb includes.
[Samba/ekacnet.git] / source4 / utils / net / net_machinepw.c
blob390eb8df0b04de4d20fa4a58cdbd15e6c22dd03f
1 /*
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/>.
21 #include "includes.h"
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");
33 return -1;
36 int net_machinepw(struct net_context *ctx, int argc, const char **argv)
38 struct ldb_context *secrets;
39 TALLOC_CTX *mem_ctx;
40 struct tevent_context *ev;
41 struct ldb_message **msgs;
42 int num_records;
43 const char *attrs[] = { "secret", NULL };
44 const char *secret;
46 if (argc != 1) {
47 net_machinepw_usage(ctx, argc, argv);
48 return -1;
51 mem_ctx = talloc_new(ctx);
52 if (mem_ctx == NULL) {
53 d_fprintf(stderr, "talloc_new failed\n");
54 return -1;
57 ev = event_context_init(mem_ctx);
58 if (ev == NULL) {
59 d_fprintf(stderr, "event_context_init failed\n");
60 goto fail;
63 secrets = secrets_db_connect(mem_ctx, ev, ctx->lp_ctx);
64 if (secrets == NULL) {
65 d_fprintf(stderr, "secrets_db_connect failed\n");
66 goto fail;
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);
75 goto fail;
78 secret = ldb_msg_find_attr_as_string(msgs[0], "secret", NULL);
79 if (secret == NULL) {
80 d_fprintf(stderr, "machine account contains no secret\n");
81 goto fail;
84 printf("%s\n", secret);
85 talloc_free(mem_ctx);
86 return 0;
88 fail:
89 talloc_free(mem_ctx);
90 return -1;