summaryrefslogtreecommitdiff
path: root/source3/lib/util_cmdline.c
blob: 90ee67c4cb7663e1a5f28f16092ebebbd4a7fd1e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
/*
   Unix SMB/CIFS implementation.
   Samba utility functions
   Copyright (C) Andrew Tridgell 1992-1998
   Copyright (C) Jeremy Allison 2001-2007
   Copyright (C) Simo Sorce 2001
   Copyright (C) Jim McDonough <jmcd@us.ibm.com> 2003
   Copyright (C) James Peach 2006

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 3 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "includes.h"
#include "auth_info.h"
#include "secrets.h"
#include "param/param.h"
#include "librpc/gen_ndr/samr.h"
#include "auth/credentials/credentials.h"
#include "auth/gensec/gensec.h"

/**************************************************************************n
  Code to cope with username/password auth options from the commandline.
  Used mainly in client tools.
****************************************************************************/

struct user_auth_info {
	struct cli_credentials *creds;
	struct loadparm_context *lp_ctx;
	bool got_username;
	bool got_pass;
	int signing_state;
	bool smb_encrypt;
	bool use_machine_account;
	bool use_pw_nt_hash;
	char *pw_nt_hash;
};

struct user_auth_info *user_auth_info_init(TALLOC_CTX *mem_ctx)
{
	struct user_auth_info *result = NULL;

	result = talloc_zero(mem_ctx, struct user_auth_info);
	if (result == NULL) {
		return NULL;
	}

	result->lp_ctx = loadparm_init_s3(result, loadparm_s3_helpers());
	if (result->lp_ctx == NULL) {
		TALLOC_FREE(result);
		return NULL;
	}

	result->creds = cli_credentials_init(result);
	if (result->creds == NULL) {
		TALLOC_FREE(result);
		return NULL;
	}

	cli_credentials_set_conf(result->creds, result->lp_ctx);

	result->signing_state = SMB_SIGNING_DEFAULT;
	return result;
}

void set_cmdline_auth_info_guess(struct user_auth_info *auth_info)
{
	/*
	 * Note that cli_credentials_guess() calls
	 * cli_credentials_set_conf() again, which will
	 * hopefully cope with a reloaded smb.conf.
	 */
	cli_credentials_set_username(auth_info->creds, "GUEST", CRED_GUESS_ENV);
	cli_credentials_guess(auth_info->creds, auth_info->lp_ctx);
}

void set_cmdline_auth_info_from_file(struct user_auth_info *auth_info,
				     const char *filename)
{
	bool ok;

	ok = cli_credentials_parse_file(auth_info->creds, filename,
					CRED_SPECIFIED);
	if (!ok) {
		exit(EIO);
	}
	auth_info->got_username = true;
}

const char *get_cmdline_auth_info_username(const struct user_auth_info *auth_info)
{
	const char *username = NULL;

	username = cli_credentials_get_username(auth_info->creds);
	if (username == NULL) {
		return "";
	}

	return username;
}

void set_cmdline_auth_info_username(struct user_auth_info *auth_info,
				    const char *username)
{
	const char *new_val = NULL;

	if (username == NULL) {
		return;
	}
	cli_credentials_parse_string(auth_info->creds,
				     username,
				     CRED_SPECIFIED);
	new_val = cli_credentials_get_username(auth_info->creds);
	if (new_val == NULL) {
		exit(ENOMEM);
	}

	auth_info->got_username = true;
	if (strchr_m(username, '%') != NULL) {
		auth_info->got_pass = true;
	}
}

void reset_cmdline_auth_info_username(struct user_auth_info *auth_info)
{
	const char *username = NULL;
	const char *new_val = NULL;

	if (!auth_info->got_username) {
		return;
	}

	username = cli_credentials_get_username(auth_info->creds);
	if (username == NULL) {
		return;
	}
	if (username[0] == '\0') {
		return;
	}

	cli_credentials_parse_string(auth_info->creds,
				     username,
				     CRED_SPECIFIED);
	new_val = cli_credentials_get_username(auth_info->creds);
	if (new_val == NULL) {
		exit(ENOMEM);
	}
}

const char *get_cmdline_auth_info_domain(const struct user_auth_info *auth_info)
{
	const char *domain = NULL;

	domain = cli_credentials_get_domain(auth_info->creds);
	if (domain == NULL) {
		return "";
	}

	return domain;
}

void set_cmdline_auth_info_domain(struct user_auth_info *auth_info,
				  const char *domain)
{
	bool ok;

	ok = cli_credentials_set_domain(auth_info->creds, domain, CRED_SPECIFIED);
	if (!ok) {
		exit(ENOMEM);
	}
}

const char *get_cmdline_auth_info_password(const struct user_auth_info *auth_info)
{
	const char *password = NULL;

	if (auth_info->pw_nt_hash != NULL) {
		return auth_info->pw_nt_hash;
	}

	if (auth_info->use_pw_nt_hash) {
		struct user_auth_info *ai =
			discard_const_p(struct user_auth_info, auth_info);
		struct samr_Password *nt_hash = NULL;

		nt_hash = cli_credentials_get_nt_hash(ai->creds,
						      ai);
		if (nt_hash == NULL) {
			return "";
		}

		ai->pw_nt_hash = hex_encode_talloc(ai,
						   nt_hash->hash,
						   sizeof(nt_hash->hash));
		TALLOC_FREE(nt_hash);
		if (ai->pw_nt_hash == NULL) {
			return "";
		}

		return auth_info->pw_nt_hash;
	}

	password = cli_credentials_get_password(auth_info->creds);
	if (password == NULL) {
		return "";
	}

	return password;
}

void set_cmdline_auth_info_password(struct user_auth_info *auth_info,
				    const char *password)
{
	bool ok;

	auth_info->got_pass = true;

	if (password != NULL && strlen(password) == 0) {
		password = NULL;
	}

	ok = cli_credentials_set_password(auth_info->creds,
					  password,
					  CRED_SPECIFIED);
	if (!ok) {
		exit(ENOMEM);
	}
}

bool set_cmdline_auth_info_signing_state(struct user_auth_info *auth_info,
					 const char *arg)
{
	auth_info->signing_state = SMB_SIGNING_DEFAULT;
	if (strequal(arg, "off") || strequal(arg, "no") ||
			strequal(arg, "false")) {
		auth_info->signing_state = SMB_SIGNING_OFF;
	} else if (strequal(arg, "on") || strequal(arg, "yes") ||
			strequal(arg, "if_required") ||
			strequal(arg, "true") || strequal(arg, "auto")) {
		auth_info->signing_state = SMB_SIGNING_IF_REQUIRED;
	} else if (strequal(arg, "force") || strequal(arg, "required") ||
			strequal(arg, "forced")) {
		auth_info->signing_state = SMB_SIGNING_REQUIRED;
	} else {
		return false;
	}
	return true;
}

void set_cmdline_auth_info_signing_state_raw(struct user_auth_info *auth_info,
					     int signing_state)
{
	auth_info->signing_state = signing_state;
}

int get_cmdline_auth_info_signing_state(const struct user_auth_info *auth_info)
{
	if (auth_info->smb_encrypt) {
		return SMB_SIGNING_REQUIRED;
	}
	return auth_info->signing_state;
}

void set_cmdline_auth_info_use_ccache(struct user_auth_info *auth_info, bool b)
{
	uint32_t gensec_features;

	gensec_features = cli_credentials_get_gensec_features(auth_info->creds);
	gensec_features |= GENSEC_FEATURE_NTLM_CCACHE;
	cli_credentials_set_gensec_features(auth_info->creds, gensec_features);
}

bool get_cmdline_auth_info_use_ccache(const struct user_auth_info *auth_info)
{
	uint32_t gensec_features;

	gensec_features = cli_credentials_get_gensec_features(auth_info->creds);
	if (gensec_features & GENSEC_FEATURE_NTLM_CCACHE) {
		return true;
	}

	return false;
}

void set_cmdline_auth_info_use_pw_nt_hash(struct user_auth_info *auth_info,
					  bool b)
{
	TALLOC_FREE(auth_info->pw_nt_hash);
	auth_info->use_pw_nt_hash = b;
	cli_credentials_set_password_will_be_nt_hash(auth_info->creds, b);
}

bool get_cmdline_auth_info_use_pw_nt_hash(
	const struct user_auth_info *auth_info)
{
	return auth_info->use_pw_nt_hash;
}

void set_cmdline_auth_info_use_kerberos(struct user_auth_info *auth_info,
					bool b)
{
	enum credentials_use_kerberos krb5_state;

	if (b) {
		krb5_state = CRED_MUST_USE_KERBEROS;
	} else {
		krb5_state = CRED_DONT_USE_KERBEROS;
	}

	cli_credentials_set_kerberos_state(auth_info->creds, krb5_state);
}

bool get_cmdline_auth_info_use_kerberos(const struct user_auth_info *auth_info)
{
	enum credentials_use_kerberos krb5_state;

	krb5_state = cli_credentials_get_kerberos_state(auth_info->creds);

	if (krb5_state == CRED_MUST_USE_KERBEROS) {
		return true;
	}

	return false;
}

void set_cmdline_auth_info_fallback_after_kerberos(struct user_auth_info *auth_info,
					bool b)
{
	enum credentials_use_kerberos krb5_state;

	krb5_state = cli_credentials_get_kerberos_state(auth_info->creds);

	switch (krb5_state) {
	case CRED_MUST_USE_KERBEROS:
		if (b) {
			krb5_state = CRED_AUTO_USE_KERBEROS;
		}
		break;
	case CRED_AUTO_USE_KERBEROS:
		if (!b) {
			krb5_state = CRED_MUST_USE_KERBEROS;
		}
		break;
	case CRED_DONT_USE_KERBEROS:
		/* nothing to do */
		break;
	}

	cli_credentials_set_kerberos_state(auth_info->creds, krb5_state);
}

bool get_cmdline_auth_info_fallback_after_kerberos(const struct user_auth_info *auth_info)
{
	enum credentials_use_kerberos krb5_state;

	krb5_state = cli_credentials_get_kerberos_state(auth_info->creds);

	if (krb5_state == CRED_AUTO_USE_KERBEROS) {
		return true;
	}

	return false;
}

/* This should only be used by lib/popt_common.c JRA */
void set_cmdline_auth_info_use_krb5_ticket(struct user_auth_info *auth_info)
{
	set_cmdline_auth_info_use_kerberos(auth_info, true);
	auth_info->got_pass = true;
}

/* This should only be used by lib/popt_common.c JRA */
void set_cmdline_auth_info_smb_encrypt(struct user_auth_info *auth_info)
{
	auth_info->smb_encrypt = true;
}

void set_cmdline_auth_info_use_machine_account(struct user_auth_info *auth_info)
{
	cli_credentials_set_machine_account_pending(auth_info->creds,
						    auth_info->lp_ctx);
	auth_info->use_machine_account = true;
}

bool get_cmdline_auth_info_got_pass(const struct user_auth_info *auth_info)
{
	return auth_info->got_pass;
}

bool get_cmdline_auth_info_smb_encrypt(const struct user_auth_info *auth_info)
{
	return auth_info->smb_encrypt;
}

bool get_cmdline_auth_info_use_machine_account(const struct user_auth_info *auth_info)
{
	return auth_info->use_machine_account;
}

bool set_cmdline_auth_info_machine_account_creds(struct user_auth_info *auth_info)
{
	struct db_context *db_ctx = NULL;
	NTSTATUS status;

	if (!get_cmdline_auth_info_use_machine_account(auth_info)) {
		return false;
	}

	db_ctx = secrets_db_ctx();
	if (db_ctx == NULL) {
		d_printf("ERROR: Unable to open secrets database\n");
		return false;
	}

	cli_credentials_set_domain(auth_info->creds, lpcfg_workgroup(auth_info->lp_ctx),
				   CRED_SPECIFIED);

	status = cli_credentials_set_machine_account_db_ctx(auth_info->creds,
							    auth_info->lp_ctx,
							    db_ctx);
	if (!NT_STATUS_IS_OK(status)) {
		d_printf("ERROR: Unable to fetch machine password for "
			 "%s in domain %s - %s\n",
			 lpcfg_netbios_name(auth_info->lp_ctx),
			 lpcfg_workgroup(auth_info->lp_ctx),
			 nt_errstr(status));
		return false;
	}

	return true;
}

static const char *cmdline_auth_info_pw_callback(struct cli_credentials *creds)
{
	TALLOC_CTX *frame = talloc_stackframe();
	const char *name = NULL;
	char *label = NULL;
	char *ret = NULL;
	char pwd[256] = {0};
	int rc;

	name = cli_credentials_get_unparsed_name(creds, frame);
	if (name == NULL) {
		goto fail;
	}
	label = talloc_asprintf(frame, "Enter %s's password: ", name);
	if (label == NULL) {
		goto fail;
	}
	rc = samba_getpass(label, pwd, sizeof(pwd), false, false);
	if (rc != 0) {
		goto fail;
	}
	ret = talloc_strdup(creds, pwd);
	if (ret == NULL) {
		goto fail;
	}
	talloc_set_name_const(ret, __location__);
fail:
	ZERO_STRUCT(pwd);
	TALLOC_FREE(frame);
	return ret;
}

/****************************************************************************
 Ensure we have a password if one not given.
****************************************************************************/

void set_cmdline_auth_info_getpass(struct user_auth_info *auth_info)
{
	if (get_cmdline_auth_info_got_pass(auth_info) ||
	    get_cmdline_auth_info_use_ccache(auth_info) ||
	    get_cmdline_auth_info_use_kerberos(auth_info)) {
		/* Already got one... */
		return;
	}

	cli_credentials_set_password_callback(auth_info->creds,
					cmdline_auth_info_pw_callback);
}

struct cli_credentials *get_cmdline_auth_info_creds(
	const struct user_auth_info *auth_info)
{
	return auth_info->creds;
}