dc4945
From 6dcf7dcc04af4b77829f182a698beb59fc6f4341 Mon Sep 17 00:00:00 2001
dc4945
From: Rob Crittenden <rcritten@redhat.com>
dc4945
Date: Fri, 5 Apr 2019 11:17:22 -0400
dc4945
Subject: [PATCH] Add interactive prompt for the LDAP bind password to
dc4945
 ipa-getkeytab
dc4945
dc4945
This provides a mechanism to bind over LDAP without exposing
dc4945
the password on the command-line.
dc4945
dc4945
https://pagure.io/freeipa/issue/631
dc4945
dc4945
Signed-off-by: Rob Crittenden <rcritten@redhat.com>
dc4945
Reviewed-By: Alexander Bokovoy <abokovoy@redhat.com>
dc4945
Reviewed-By: Robbie Harwood <rharwood@redhat.com>
dc4945
Reviewed-By: Christian Heimes <cheimes@redhat.com>
dc4945
Reviewed-By: Florence Blanc-Renaud <frenaud@redhat.com>
dc4945
Reviewed-By: Simo Sorce <ssorce@redhat.com>
dc4945
---
dc4945
 client/ipa-getkeytab.c     | 53 ++++++++++++++++++++++++++++++--------
dc4945
 client/man/ipa-getkeytab.1 |  9 ++++---
dc4945
 2 files changed, 48 insertions(+), 14 deletions(-)
dc4945
dc4945
diff --git a/client/ipa-getkeytab.c b/client/ipa-getkeytab.c
dc4945
index 6713a0c5f6352dc63dc0ec24d4ccaec4c3ba31ae..8a5e98bed1947344247f9d6146e595d5f7f7a963 100644
dc4945
--- a/client/ipa-getkeytab.c
dc4945
+++ b/client/ipa-getkeytab.c
dc4945
@@ -626,7 +626,16 @@ done:
dc4945
     return ret;
dc4945
 }
dc4945
 
dc4945
-static char *ask_password(krb5_context krbctx)
dc4945
+/* Prompt for either a password.
dc4945
+ * This can be either asking for a new or existing password.
dc4945
+ *
dc4945
+ * To set a new password provide values for both prompt1 and prompt2 and
dc4945
+ * set match=true to enforce that the two entered passwords match.
dc4945
+ *
dc4945
+ * To prompt for an existing password provide prompt1 and set match=false.
dc4945
+ */
dc4945
+static char *ask_password(krb5_context krbctx, char *prompt1, char *prompt2,
dc4945
+                          bool match)
dc4945
 {
dc4945
     krb5_prompt ap_prompts[2];
dc4945
     krb5_data k5d_pw0;
dc4945
@@ -634,24 +643,27 @@ static char *ask_password(krb5_context krbctx)
dc4945
     char pw0[256];
dc4945
     char pw1[256];
dc4945
     char *password;
dc4945
+    int num_prompts = match ? 2:1;
dc4945
 
dc4945
     k5d_pw0.length = sizeof(pw0);
dc4945
     k5d_pw0.data = pw0;
dc4945
-    ap_prompts[0].prompt = _("New Principal Password");
dc4945
+    ap_prompts[0].prompt = prompt1;
dc4945
     ap_prompts[0].hidden = 1;
dc4945
     ap_prompts[0].reply = &k5d_pw0;
dc4945
 
dc4945
-    k5d_pw1.length = sizeof(pw1);
dc4945
-    k5d_pw1.data = pw1;
dc4945
-    ap_prompts[1].prompt = _("Verify Principal Password");
dc4945
-    ap_prompts[1].hidden = 1;
dc4945
-    ap_prompts[1].reply = &k5d_pw1;
dc4945
+    if (match) {
dc4945
+        k5d_pw1.length = sizeof(pw1);
dc4945
+        k5d_pw1.data = pw1;
dc4945
+        ap_prompts[1].prompt = prompt2;
dc4945
+        ap_prompts[1].hidden = 1;
dc4945
+        ap_prompts[1].reply = &k5d_pw1;
dc4945
+    }
dc4945
 
dc4945
     krb5_prompter_posix(krbctx, NULL,
dc4945
                 NULL, NULL,
dc4945
-                2, ap_prompts);
dc4945
+                num_prompts, ap_prompts);
dc4945
 
dc4945
-    if (strcmp(pw0, pw1)) {
dc4945
+    if (match && (strcmp(pw0, pw1))) {
dc4945
         fprintf(stderr, _("Passwords do not match!"));
dc4945
         return NULL;
dc4945
     }
dc4945
@@ -752,6 +764,7 @@ int main(int argc, const char *argv[])
dc4945
 	static const char *ca_cert_file = NULL;
dc4945
 	int quiet = 0;
dc4945
 	int askpass = 0;
dc4945
+	int askbindpw = 0;
dc4945
 	int permitted_enctypes = 0;
dc4945
 	int retrieve = 0;
dc4945
         struct poptOption options[] = {
dc4945
@@ -778,6 +791,8 @@ int main(int argc, const char *argv[])
dc4945
               _("LDAP DN"), _("DN to bind as if not using kerberos") },
dc4945
 	    { "bindpw", 'w', POPT_ARG_STRING, &bindpw, 0,
dc4945
               _("LDAP password"), _("password to use if not using kerberos") },
dc4945
+	    { NULL, 'W', POPT_ARG_NONE, &askbindpw, 0,
dc4945
+              _("Prompt for LDAP password"), NULL },
dc4945
 	    { "cacert", 0, POPT_ARG_STRING, &ca_cert_file, 0,
dc4945
               _("Path to the IPA CA certificate"), _("IPA CA certificate")},
dc4945
 	    { "ldapuri", 'H', POPT_ARG_STRING, &ldap_uri, 0,
dc4945
@@ -849,9 +864,24 @@ int main(int argc, const char *argv[])
dc4945
 		exit(2);
dc4945
 	}
dc4945
 
dc4945
+    if (askbindpw && bindpw != NULL) {
dc4945
+		fprintf(stderr, _("Bind password already provided (-w).\n"));
dc4945
+		if (!quiet) {
dc4945
+			poptPrintUsage(pc, stderr, 0);
dc4945
+		}
dc4945
+		exit(2);
dc4945
+    }
dc4945
+
dc4945
+    if (askbindpw) {
dc4945
+		bindpw = ask_password(krbctx, _("Enter LDAP password"), NULL, false);
dc4945
+		if (!bindpw) {
dc4945
+			exit(2);
dc4945
+		}
dc4945
+    }
dc4945
+
dc4945
 	if (NULL!=binddn && NULL==bindpw) {
dc4945
 		fprintf(stderr,
dc4945
-                        _("Bind password required when using a bind DN.\n"));
dc4945
+                        _("Bind password required when using a bind DN (-w or -W).\n"));
dc4945
 		if (!quiet)
dc4945
 			poptPrintUsage(pc, stderr, 0);
dc4945
 		exit(10);
dc4945
@@ -915,7 +945,8 @@ int main(int argc, const char *argv[])
dc4945
     }
dc4945
 
dc4945
         if (askpass) {
dc4945
-		password = ask_password(krbctx);
dc4945
+		password = ask_password(krbctx, _("New Principal Password"),
dc4945
+               	                _("Verify Principal Password"), true);
dc4945
 		if (!password) {
dc4945
 			exit(2);
dc4945
 		}
dc4945
diff --git a/client/man/ipa-getkeytab.1 b/client/man/ipa-getkeytab.1
dc4945
index 39ff0d5da85b5a641328a512feeb06bc9c1ab9d7..6e7fdf39ee4e28772365edafd4c7e86d0c37d343 100644
dc4945
--- a/client/man/ipa-getkeytab.1
dc4945
+++ b/client/man/ipa-getkeytab.1
dc4945
@@ -21,7 +21,7 @@
dc4945
 .SH "NAME"
dc4945
 ipa\-getkeytab \- Get a keytab for a Kerberos principal
dc4945
 .SH "SYNOPSIS"
dc4945
-ipa\-getkeytab \fB\-p\fR \fIprincipal\-name\fR \fB\-k\fR \fIkeytab\-file\fR [ \fB\-e\fR \fIencryption\-types\fR ] [ \fB\-s\fR \fIipaserver\fR ] [ \fB\-q\fR ] [ \fB\-D\fR|\fB\-\-binddn\fR \fIBINDDN\fR ] [ \fB\-w|\-\-bindpw\fR ] [ \fB\-P\fR|\fB\-\-password\fR \fIPASSWORD\fR ] [ \fB\-\-cacert \fICACERT\fR ] [ \fB\-H|\-\-ldapuri \fIURI\fR ] [ \fB\-Y|\-\-mech \fIGSSAPI|EXTERNAL\fR ] [ \fB\-r\fR ]
dc4945
+ipa\-getkeytab \fB\-p\fR \fIprincipal\-name\fR \fB\-k\fR \fIkeytab\-file\fR [ \fB\-e\fR \fIencryption\-types\fR ] [ \fB\-s\fR \fIipaserver\fR ] [ \fB\-q\fR ] [ \fB\-D\fR|\fB\-\-binddn\fR \fIBINDDN\fR ] [ \fB\-w|\-\-bindpw\fR ] [ \fB-W\fR ] [ \fB\-P\fR|\fB\-\-password\fR \fIPASSWORD\fR ] [ \fB\-\-cacert \fICACERT\fR ] [ \fB\-H|\-\-ldapuri \fIURI\fR ] [ \fB\-Y|\-\-mech \fIGSSAPI|EXTERNAL\fR ] [ \fB\-r\fR ]
dc4945
 
dc4945
 .SH "DESCRIPTION"
dc4945
 Retrieves a Kerberos \fIkeytab\fR.
dc4945
@@ -44,7 +44,7 @@ provided, so the principal name is just the service
dc4945
 name and hostname (ldap/foo.example.com from the
dc4945
 example above).
dc4945
 
dc4945
-ipa-getkeytab is used during IPA client enrollment to retrieve a host service principal and store it in /etc/krb5.keytab. It is possible to retrieve the keytab without Kerberos credentials if the host was pre\-created with a one\-time password. The keytab can be retrieved by binding as the host and authenticating with this one\-time password. The \fB\-D|\-\-binddn\fR and \fB\-w|\-\-bindpw\fR options are used for this authentication.
dc4945
+ipa-getkeytab is used during IPA client enrollment to retrieve a host service principal and store it in /etc/krb5.keytab. It is possible to retrieve the keytab without Kerberos credentials if the host was pre\-created with a one\-time password. The keytab can be retrieved by binding as the host and authenticating with this one\-time password. The \fB\-D|\-\-binddn\fR \fB\-w|\-\-bindpw\fR options are used for this authentication. \fB-W\fR can be used instead of \fB\-w|\-\-bindpw\fR to interactively prompt for the bind password.
dc4945
 
dc4945
 \fBWARNING:\fR retrieving the keytab resets the secret for the Kerberos principal.
dc4945
 This renders all other keytabs for that principal invalid.
dc4945
@@ -98,11 +98,14 @@ DES cbc mode with RSA\-MD4
dc4945
 Use this password for the key instead of one randomly generated.
dc4945
 .TP
dc4945
 \fB\-D, \-\-binddn\fR
dc4945
-The LDAP DN to bind as when retrieving a keytab without Kerberos credentials. Generally used with the \fB\-w\fR option.
dc4945
+The LDAP DN to bind as when retrieving a keytab without Kerberos credentials. Generally used with the \fB\-w\fR or \fB\-W\fR options.
dc4945
 .TP
dc4945
 \fB\-w, \-\-bindpw\fR
dc4945
 The LDAP password to use when not binding with Kerberos. \fB\-D\fR and \fB\-w\fR can not be used together with \fB\-Y\fR.
dc4945
 .TP
dc4945
+\fB\-W\fR
dc4945
+Interactive prompt for the bind password. \fB\-D\fR and \fB\-W\fR can not be used together with \fB\-Y\fR
dc4945
+.TP
dc4945
 \fB\-\-cacert\fR
dc4945
 The path to the IPA CA certificate used to validate LDAPS/STARTTLS connections.
dc4945
 Defaults to /etc/ipa/ca.crt
dc4945
-- 
dc4945
2.25.2
dc4945