s3/utils: Detect (and report) failure to parse sddl
[samba.git] / source3 / utils / destroy_netlogon_creds_cli.c
1 /*
2  * Unix SMB/CIFS implementation.
3  * Garble the netlogon_creds_cli key for testing purposes
4  * Copyright (C) Volker Lendecke 2018
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19
20 #include "includes.h"
21 #include "system/filesys.h"
22 #include <talloc.h>
23 #include <tevent.h>
24 #include "messages.h"
25 #include "lib/util/talloc_stack.h"
26 #include "lib/param/loadparm.h"
27 #include "lib/param/param.h"
28 #include "libcli/auth/netlogon_creds_cli.h"
29 #include "lib/dbwrap/dbwrap.h"
30 #include "lib/dbwrap/dbwrap_open.h"
31
32 int main(int argc, const char *argv[])
33 {
34         TALLOC_CTX *mem_ctx = talloc_stackframe();
35         struct tevent_context *ev;
36         struct messaging_context *msg_ctx;
37         struct loadparm_context *lp_ctx;
38         struct db_context *global_db;
39         struct netlogon_creds_cli_context *ctx;
40         struct netlogon_creds_CredentialState *creds;
41         NTSTATUS status;
42         int ret = 1;
43
44         smb_init_locale();
45
46         if (!lp_load_global(get_dyn_CONFIGFILE())) {
47                 fprintf(stderr, "error opening config file %s. Error was %s\n",
48                         get_dyn_CONFIGFILE(), strerror(errno));
49                 goto done;
50         }
51
52         if (argc != 4) {
53                 fprintf(stderr, "usage: %s cli_computer domain dc\n", argv[0]);
54                 goto done;
55         }
56
57         lp_ctx = loadparm_init_s3(mem_ctx, loadparm_s3_helpers());
58         if (lp_ctx == NULL) {
59                 fprintf(stderr, "loadparm_init_s3 failed\n");
60                 goto done;
61         }
62
63         ev = samba_tevent_context_init(mem_ctx);
64         if (ev == NULL) {
65                 fprintf(stderr, "samba3_tevent_context_init failed\n");
66                 goto done;
67         }
68         msg_ctx = messaging_init(mem_ctx, ev);
69         if (msg_ctx == NULL) {
70                 fprintf(stderr, "messaging_init failed\n");
71                 goto done;
72         }
73
74         global_db = db_open(
75                 mem_ctx,
76                 lpcfg_private_db_path(mem_ctx, lp_ctx, "netlogon_creds_cli"),
77                 0, TDB_CLEAR_IF_FIRST|TDB_INCOMPATIBLE_HASH,
78                 O_RDWR|O_CREAT, 0600, DBWRAP_LOCK_ORDER_2,
79                 DBWRAP_FLAG_OPTIMIZE_READONLY_ACCESS);
80         if (global_db == NULL) {
81                 fprintf(stderr, "db_open failed\n");
82                 goto done;
83         }
84
85         status = netlogon_creds_cli_set_global_db(lp_ctx, &global_db);
86         if (!NT_STATUS_IS_OK(status)) {
87                 fprintf(stderr,
88                         "netlogon_creds_cli_set_global_db failed: %s\n",
89                         nt_errstr(status));
90                 goto done;
91         }
92
93         status = netlogon_creds_cli_context_global(
94                 lp_ctx,
95                 msg_ctx,
96                 talloc_asprintf(mem_ctx, "%s$", argv[1]),
97                 SEC_CHAN_WKSTA,
98                 argv[3],
99                 argv[2],
100                 "",
101                 mem_ctx,
102                 &ctx);
103         if (!NT_STATUS_IS_OK(status)) {
104                 fprintf(stderr,
105                         "netlogon_creds_cli_context_global failed: %s\n",
106                         nt_errstr(status));
107                 goto done;
108         }
109
110         status = netlogon_creds_cli_lock(ctx,
111                                          mem_ctx,
112                                          &creds);
113         if (!NT_STATUS_IS_OK(status)) {
114                 fprintf(stderr,
115                         "netlogon_creds_cli_get failed: %s\n",
116                         nt_errstr(status));
117                 goto done;
118         }
119
120         creds->session_key[0]++;
121
122         status = netlogon_creds_cli_store(ctx, creds);
123         if (!NT_STATUS_IS_OK(status)) {
124                 fprintf(stderr,
125                         "netlogon_creds_cli_store failed: %s\n",
126                         nt_errstr(status));
127                 goto done;
128         }
129
130         TALLOC_FREE(creds);
131
132         ret = 0;
133 done:
134         TALLOC_FREE(mem_ctx);
135         return ret;
136 }