Merge branch 'v4-0-test' of ssh://git.samba.org/data/git/samba into v4-0-gmake3
[ira/wip.git] / source4 / utils / net / net_vampire.c
1 /* 
2    Samba Unix/Linux SMB client library 
3    Distributed SMB/CIFS Server Management Utility 
4
5    Copyright (C) 2004 Stefan Metzmacher <metze@samba.org>
6    Copyright (C) 2005 Andrew Bartlett <abartlet@samba.org>
7
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 3 of the License, or
11    (at your option) any later version.
12    
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.
17    
18    You should have received a copy of the GNU General Public License
19    along with this program.  If not, see <http://www.gnu.org/licenses/>.
20 */
21
22 #include "includes.h"
23 #include "utils/net/net.h"
24 #include "libnet/libnet.h"
25 #include "librpc/gen_ndr/samr.h"
26 #include "auth/auth.h"
27 #include "param/param.h"
28 #include "lib/events/events.h"
29
30 static int net_samdump_keytab_usage(struct net_context *ctx, int argc, const char **argv)
31 {
32         d_printf("net samdump keytab <keytab>\n");
33         return 0;       
34 }
35
36 static int net_samdump_keytab_help(struct net_context *ctx, int argc, const char **argv)
37 {
38         d_printf("Dumps kerberos keys of a domain into a keytab.\n");
39         return 0;       
40 }
41
42 static int net_samdump_keytab(struct net_context *ctx, int argc, const char **argv) 
43 {
44         NTSTATUS status;
45         struct libnet_context *libnetctx;
46         struct libnet_SamDump_keytab r;
47
48         switch (argc) {
49         case 0:
50                 return net_samdump_keytab_usage(ctx, argc, argv);
51                 break;
52         case 1:
53                 r.in.keytab_name = argv[0];
54                 break;
55         }
56
57         libnetctx = libnet_context_init(ctx->event_ctx, ctx->lp_ctx);
58         if (!libnetctx) {
59                 return -1;      
60         }
61         libnetctx->cred = ctx->credentials;
62
63         r.out.error_string = NULL;
64         r.in.machine_account = NULL;
65         r.in.binding_string = NULL;
66
67         status = libnet_SamDump_keytab(libnetctx, ctx, &r);
68         if (!NT_STATUS_IS_OK(status)) {
69                 DEBUG(0,("libnet_SamDump returned %s: %s\n",
70                          nt_errstr(status),
71                          r.out.error_string));
72                 return -1;
73         }
74
75         talloc_free(libnetctx);
76
77         return 0;
78 }
79
80 /* main function table */
81 static const struct net_functable net_samdump_functable[] = {
82         {"keytab", "dump keys into a keytab\n", net_samdump_keytab, net_samdump_keytab_usage},
83         {NULL, NULL, NULL, NULL}
84 };
85
86 int net_samdump(struct net_context *ctx, int argc, const char **argv) 
87 {
88         NTSTATUS status;
89         struct libnet_context *libnetctx;
90         struct libnet_SamDump r;
91         int rc;
92
93         switch (argc) {
94         case 0:
95                 break;
96         case 1:
97         default:
98                 rc = net_run_function(ctx, argc, argv, net_samdump_functable, 
99                                       net_samdump_usage);
100                 return rc;
101         }
102
103         libnetctx = libnet_context_init(ctx->event_ctx, ctx->lp_ctx);
104         if (!libnetctx) {
105                 return -1;      
106         }
107         libnetctx->cred = ctx->credentials;
108
109         r.out.error_string = NULL;
110         r.in.machine_account = NULL;
111         r.in.binding_string = NULL;
112
113         status = libnet_SamDump(libnetctx, ctx, &r);
114         if (!NT_STATUS_IS_OK(status)) {
115                 DEBUG(0,("libnet_SamDump returned %s: %s\n",
116                          nt_errstr(status),
117                          r.out.error_string));
118                 return -1;
119         }
120
121         talloc_free(libnetctx);
122
123         return 0;
124 }
125
126 int net_samdump_usage(struct net_context *ctx, int argc, const char **argv)
127 {
128         d_printf("net samdump\n");
129         d_printf("net samdump keytab <keytab>\n");
130         return 0;       
131 }
132
133 int net_samdump_help(struct net_context *ctx, int argc, const char **argv)
134 {
135         d_printf("Dumps the sam of the domain we are joined to.\n");
136         return 0;       
137 }
138
139 int net_samsync_ldb(struct net_context *ctx, int argc, const char **argv) 
140 {
141         NTSTATUS status;
142         struct libnet_context *libnetctx;
143         struct libnet_samsync_ldb r;
144
145         libnetctx = libnet_context_init(ctx->event_ctx, ctx->lp_ctx);
146         if (!libnetctx) {
147                 return -1;      
148         }
149         libnetctx->cred = ctx->credentials;
150
151         r.out.error_string = NULL;
152         r.in.machine_account = NULL;
153         r.in.binding_string = NULL;
154
155         /* Needed to override the ACLs on ldb */
156         r.in.session_info = system_session(libnetctx, ctx->lp_ctx);
157
158         status = libnet_samsync_ldb(libnetctx, libnetctx, &r);
159         if (!NT_STATUS_IS_OK(status)) {
160                 DEBUG(0,("libnet_samsync_ldb returned %s: %s\n",
161                          nt_errstr(status),
162                          r.out.error_string));
163                 return -1;
164         }
165
166         talloc_free(libnetctx);
167
168         return 0;
169 }
170
171 int net_samsync_ldb_usage(struct net_context *ctx, int argc, const char **argv)
172 {
173         d_printf("net samsync\n");
174         return 0;       
175 }
176
177 int net_samsync_ldb_help(struct net_context *ctx, int argc, const char **argv)
178 {
179         d_printf("Synchronise into the local ldb the SAM of a domain.\n");
180         return 0;       
181 }