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 "popt_common.h"
27 #include "lib/param/loadparm.h"
28 #include "lib/param/param.h"
29 #include "libcli/auth/netlogon_creds_cli.h"
30 #include "lib/dbwrap/dbwrap.h"
31 #include "lib/dbwrap/dbwrap_open.h"
32 
main(int argc,const char * argv[])33 int main(int argc, const char *argv[])
34 {
35 	TALLOC_CTX *mem_ctx = talloc_stackframe();
36 	struct tevent_context *ev;
37 	struct messaging_context *msg_ctx;
38 	struct loadparm_context *lp_ctx;
39 	struct db_context *global_db;
40 	struct netlogon_creds_cli_context *ctx;
41 	struct netlogon_creds_CredentialState *creds;
42 	NTSTATUS status;
43 	int ret = 1;
44 
45 	smb_init_locale();
46 
47 	if (!lp_load_global(get_dyn_CONFIGFILE())) {
48 		fprintf(stderr, "error opening config file %s. Error was %s\n",
49 			get_dyn_CONFIGFILE(), strerror(errno));
50 		goto done;
51 	}
52 
53 	if (argc != 4) {
54 		fprintf(stderr, "usage: %s cli_computer domain dc\n", argv[0]);
55 		goto done;
56 	}
57 
58 	lp_ctx = loadparm_init_s3(mem_ctx, loadparm_s3_helpers());
59 	if (lp_ctx == NULL) {
60 		fprintf(stderr, "loadparm_init_s3 failed\n");
61 		goto done;
62 	}
63 
64 	ev = samba_tevent_context_init(mem_ctx);
65 	if (ev == NULL) {
66 		fprintf(stderr, "samba3_tevent_context_init failed\n");
67 		goto done;
68 	}
69 	msg_ctx = messaging_init(mem_ctx, ev);
70 	if (msg_ctx == NULL) {
71 		fprintf(stderr, "messaging_init failed\n");
72 		goto done;
73 	}
74 
75 	global_db = db_open(
76 		mem_ctx,
77 		lpcfg_private_db_path(mem_ctx, lp_ctx, "netlogon_creds_cli"),
78 		0, TDB_CLEAR_IF_FIRST|TDB_INCOMPATIBLE_HASH,
79 		O_RDWR|O_CREAT, 0600, DBWRAP_LOCK_ORDER_2,
80 		DBWRAP_FLAG_OPTIMIZE_READONLY_ACCESS);
81 	if (global_db == NULL) {
82 		fprintf(stderr, "db_open failed\n");
83 		goto done;
84 	}
85 
86 	status = netlogon_creds_cli_set_global_db(&global_db);
87 	if (!NT_STATUS_IS_OK(status)) {
88 		fprintf(stderr,
89 			"netlogon_creds_cli_set_global_db failed: %s\n",
90 			nt_errstr(status));
91 		goto done;
92 	}
93 
94 	status = netlogon_creds_cli_context_global(
95 		lp_ctx,
96 		msg_ctx,
97 		talloc_asprintf(mem_ctx, "%s$", argv[1]),
98 		SEC_CHAN_WKSTA,
99 		argv[3],
100 		argv[2],
101 		"",
102 		mem_ctx,
103 		&ctx);
104 	if (!NT_STATUS_IS_OK(status)) {
105 		fprintf(stderr,
106 			"netlogon_creds_cli_context_global failed: %s\n",
107 			nt_errstr(status));
108 		goto done;
109 	}
110 
111 	status = netlogon_creds_cli_lock(ctx,
112 					 mem_ctx,
113 					 &creds);
114 	if (!NT_STATUS_IS_OK(status)) {
115 		fprintf(stderr,
116 			"netlogon_creds_cli_get failed: %s\n",
117 			nt_errstr(status));
118 		goto done;
119 	}
120 
121 	creds->session_key[0]++;
122 
123 	status = netlogon_creds_cli_store(ctx, creds);
124 	if (!NT_STATUS_IS_OK(status)) {
125 		fprintf(stderr,
126 			"netlogon_creds_cli_store failed: %s\n",
127 			nt_errstr(status));
128 		goto done;
129 	}
130 
131 	TALLOC_FREE(creds);
132 
133 	ret = 0;
134 done:
135 	TALLOC_FREE(mem_ctx);
136 	return ret;
137 }
138