r23792: convert Samba4 to GPLv3
[amitay/samba.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
28 static int net_samdump_keytab_usage(struct net_context *ctx, int argc, const char **argv)
29 {
30         d_printf("net samdump keytab <keytab>\n");
31         return 0;       
32 }
33
34 static int net_samdump_keytab_help(struct net_context *ctx, int argc, const char **argv)
35 {
36         d_printf("Dumps kerberos keys of a domain into a keytab.\n");
37         return 0;       
38 }
39
40 static int net_samdump_keytab(struct net_context *ctx, int argc, const char **argv) 
41 {
42         NTSTATUS status;
43         struct libnet_context *libnetctx;
44         struct libnet_SamDump_keytab r;
45
46         switch (argc) {
47         case 0:
48                 return net_samdump_keytab_usage(ctx, argc, argv);
49                 break;
50         case 1:
51                 r.in.keytab_name = argv[0];
52                 break;
53         }
54
55         libnetctx = libnet_context_init(NULL);
56         if (!libnetctx) {
57                 return -1;      
58         }
59         libnetctx->cred = ctx->credentials;
60
61         r.out.error_string = NULL;
62         r.in.machine_account = NULL;
63         r.in.binding_string = NULL;
64
65         status = libnet_SamDump_keytab(libnetctx, ctx->mem_ctx, &r);
66         if (!NT_STATUS_IS_OK(status)) {
67                 DEBUG(0,("libnet_SamDump returned %s: %s\n",
68                          nt_errstr(status),
69                          r.out.error_string));
70                 return -1;
71         }
72
73         talloc_free(libnetctx);
74
75         return 0;
76 }
77
78 /* main function table */
79 static const struct net_functable net_samdump_functable[] = {
80         {"keytab", "dump keys into a keytab\n", net_samdump_keytab, net_samdump_keytab_usage},
81         {NULL, NULL, NULL, NULL}
82 };
83
84 int net_samdump(struct net_context *ctx, int argc, const char **argv) 
85 {
86         NTSTATUS status;
87         struct libnet_context *libnetctx;
88         struct libnet_SamDump r;
89         int rc;
90
91         switch (argc) {
92         case 0:
93                 break;
94         case 1:
95         default:
96                 rc = net_run_function(ctx, argc, argv, net_samdump_functable, 
97                                       net_samdump_usage);
98                 return rc;
99         }
100
101         libnetctx = libnet_context_init(NULL);
102         if (!libnetctx) {
103                 return -1;      
104         }
105         libnetctx->cred = ctx->credentials;
106
107         r.out.error_string = NULL;
108         r.in.machine_account = NULL;
109         r.in.binding_string = NULL;
110
111         status = libnet_SamDump(libnetctx, ctx->mem_ctx, &r);
112         if (!NT_STATUS_IS_OK(status)) {
113                 DEBUG(0,("libnet_SamDump returned %s: %s\n",
114                          nt_errstr(status),
115                          r.out.error_string));
116                 return -1;
117         }
118
119         talloc_free(libnetctx);
120
121         return 0;
122 }
123
124 int net_samdump_usage(struct net_context *ctx, int argc, const char **argv)
125 {
126         d_printf("net samdump\n");
127         d_printf("net samdump keytab <keytab>\n");
128         return 0;       
129 }
130
131 int net_samdump_help(struct net_context *ctx, int argc, const char **argv)
132 {
133         d_printf("Dumps the sam of the domain we are joined to.\n");
134         return 0;       
135 }
136
137 int net_samsync_ldb(struct net_context *ctx, int argc, const char **argv) 
138 {
139         NTSTATUS status;
140         struct libnet_context *libnetctx;
141         struct libnet_samsync_ldb r;
142
143         libnetctx = libnet_context_init(NULL);
144         if (!libnetctx) {
145                 return -1;      
146         }
147         libnetctx->cred = ctx->credentials;
148
149         r.out.error_string = NULL;
150         r.in.machine_account = NULL;
151         r.in.binding_string = NULL;
152
153         /* Needed to override the ACLs on ldb */
154         r.in.session_info = system_session(libnetctx);
155
156         status = libnet_samsync_ldb(libnetctx, libnetctx, &r);
157         if (!NT_STATUS_IS_OK(status)) {
158                 DEBUG(0,("libnet_samsync_ldb returned %s: %s\n",
159                          nt_errstr(status),
160                          r.out.error_string));
161                 return -1;
162         }
163
164         talloc_free(libnetctx);
165
166         return 0;
167 }
168
169 int net_samsync_ldb_usage(struct net_context *ctx, int argc, const char **argv)
170 {
171         d_printf("net samsync_ldb\n");
172         return 0;       
173 }
174
175 int net_samsync_ldb_help(struct net_context *ctx, int argc, const char **argv)
176 {
177         d_printf("Synchronise into the local ldb the SAM of a domain.\n");
178         return 0;       
179 }