s4:lib/tevent: rename structs
[kai/samba.git] / source4 / utils / net / net_machinepw.c
1 /*
2    Samba Unix/Linux SMB client library
3    Distributed SMB/CIFS Server Management Utility
4
5    Copyright (C) 2008 Volker Lendecke
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 3 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, see <http://www.gnu.org/licenses/>.
19 */
20
21 #include "includes.h"
22 #include "utils/net/net.h"
23 #include "libnet/libnet.h"
24 #include "libcli/security/security.h"
25 #include "param/secrets.h"
26 #include "param/param.h"
27 #include "lib/events/events.h"
28 #include "lib/util/util_ldb.h"
29
30 int net_machinepw_usage(struct net_context *ctx, int argc, const char **argv)
31 {
32         d_printf("net machinepw <accountname>\n");
33         return -1;
34 }
35
36 int net_machinepw(struct net_context *ctx, int argc, const char **argv)
37 {
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;
45
46         if (argc != 1) {
47                 net_machinepw_usage(ctx, argc, argv);
48                 return -1;
49         }
50
51         mem_ctx = talloc_new(ctx);
52         if (mem_ctx == NULL) {
53                 d_fprintf(stderr, "talloc_new failed\n");
54                 return -1;
55         }
56
57         ev = event_context_init(mem_ctx);
58         if (ev == NULL) {
59                 d_fprintf(stderr, "event_context_init failed\n");
60                 goto fail;
61         }
62
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;
67         }
68
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;
76         }
77
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;
82         }
83
84         printf("%s\n", secret);
85         talloc_free(mem_ctx);
86         return 0;
87
88  fail:
89         talloc_free(mem_ctx);
90         return -1;
91 }