From dae9380ac7ebb8c490c1e85da93e85fed368ba02 Mon Sep 17 00:00:00 2001 From: Meno Abels Date: Tue, 28 Oct 2014 16:27:28 +0100 Subject: [PATCH 1/3] added a better ldap handling, and to allow query active directory --- pam_yubico.c | 79 +++++++++++++++++++++++++++++++++-------------- tests/util_test.c | 20 ++++++++++++ util.c | 46 +++++++++++++++++++++++++++ util.h | 3 ++ 4 files changed, 125 insertions(+), 23 deletions(-) diff --git a/pam_yubico.c b/pam_yubico.c index 48308bd..db3cbcd 100644 --- a/pam_yubico.c +++ b/pam_yubico.c @@ -113,6 +113,10 @@ struct cfg const char *urllist; const char *ldapserver; const char *ldap_uri; + int ldap_bind_no_anonymous; + const char *ldap_bind_user; + const char *ldap_bind_password; + const char *ldap_filter; const char *ldapdn; const char *user_attr; const char *yubi_attr; @@ -206,6 +210,7 @@ free_out: static int authorize_user_token_ldap (struct cfg *cfg, const char *user, + const char *password, const char *token_id) { int retval = 0; @@ -221,15 +226,12 @@ authorize_user_token_ldap (struct cfg *cfg, struct berval **vals; int i, rc; + const char *filter = NULL; char *find = NULL; + int scope = LDAP_SCOPE_BASE; #endif DBG(("called")); #ifdef HAVE_LIBLDAP - - if (cfg->user_attr == NULL) { - DBG (("Trying to look up user to YubiKey mapping in LDAP, but user_attr not set!")); - return 0; - } if (cfg->yubi_attr == NULL) { DBG (("Trying to look up user to YubiKey mapping in LDAP, but yubi_attr not set!")); return 0; @@ -265,7 +267,16 @@ authorize_user_token_ldap (struct cfg *cfg, ldap_set_option (ld, LDAP_OPT_PROTOCOL_VERSION, &protocol); /* Bind anonymously to the LDAP server. */ - rc = ldap_simple_bind_s (ld, NULL, NULL); + if (cfg->ldap_bind_user && cfg->ldap_bind_password) { + DBG (("try bind with: %s:%s", cfg->ldap_bind_user, cfg->ldap_bind_password)); + rc = ldap_simple_bind_s (ld, cfg->ldap_bind_user, cfg->ldap_bind_password); + } else if (cfg->ldap_bind_no_anonymous) { + DBG (("try bind with: %s:%s", user, password)); + rc = ldap_simple_bind_s (ld, user, password); + } else { + DBG (("try bind anonymous")); + rc = ldap_simple_bind_s (ld, NULL, NULL); + } if (rc != LDAP_SUCCESS) { DBG (("ldap_simple_bind_s: %s", ldap_err2string (rc))); @@ -274,22 +285,29 @@ authorize_user_token_ldap (struct cfg *cfg, } /* Allocation of memory for search strings depending on input size */ - i = (strlen(cfg->user_attr) + strlen(cfg->ldapdn) + strlen(user) + 3) * sizeof(char); - if ((find = malloc(i)) == NULL) { - DBG (("Failed allocating %i bytes", i)); - retval = 0; - goto done; + find = strdup(cfg->ldapdn); // allow free later-:) + if (cfg->user_attr && cfg->yubi_attr) { + i = (strlen(cfg->user_attr) + strlen(cfg->ldapdn) + strlen(user) + 3) * sizeof(char); + if ((find = malloc(i)) == NULL) { + DBG (("Failed allocating %i bytes", i)); + retval = 0; + goto done; + } + sprintf (find, "%s=%s,%s", cfg->user_attr, user, cfg->ldapdn); + filter = NULL; + } + if (cfg->ldap_filter) { + filter = filter_printf(cfg->ldap_filter, user); + scope = LDAP_SCOPE_SUBTREE; } - - sprintf (find, "%s=%s,%s", cfg->user_attr, user, cfg->ldapdn); - attrs[0] = (char *) cfg->yubi_attr; - DBG(("LDAP : look up object '%s', ask for attribute '%s'", find, cfg->yubi_attr)); + DBG(("LDAP : look up object base='%s' filter='%s', ask for attribute '%s'", find, + filter ? filter:"(null)", cfg->yubi_attr)); /* Search for the entry. */ - if ((rc = ldap_search_ext_s (ld, find, LDAP_SCOPE_BASE, - NULL, attrs, 0, NULL, NULL, LDAP_NO_LIMIT, + if ((rc = ldap_search_ext_s (ld, find, scope, + filter, attrs, 0, NULL, NULL, LDAP_NO_LIMIT, LDAP_NO_LIMIT, &result)) != LDAP_SUCCESS) { DBG (("ldap_search_ext_s: %s", ldap_err2string (rc))); @@ -313,10 +331,10 @@ authorize_user_token_ldap (struct cfg *cfg, { if ((vals = ldap_get_values_len (ld, e, a)) != NULL) { - DBG(("LDAP : Found %i values - checking if any of them match '%s%s'", - ldap_count_values_len(vals), - cfg->yubi_attr_prefix ? cfg->yubi_attr_prefix : "", - token_id)); + DBG(("LDAP : Found %i values - checking if any of them match '%s:%s:%s'", + ldap_count_values_len(vals), + vals[i]->bv_val, + cfg->yubi_attr_prefix ? cfg->yubi_attr_prefix : "", token_id)); yubi_attr_prefix_len = cfg->yubi_attr_prefix ? strlen(cfg->yubi_attr_prefix) : 0; @@ -350,6 +368,8 @@ authorize_user_token_ldap (struct cfg *cfg, /* free memory allocated for search strings */ if (find != NULL) free(find); + if (filter != NULL) + free(filter); #else DBG (("Trying to use LDAP, but this function is not compiled in pam_yubico!!")); @@ -666,6 +686,14 @@ parse_cfg (int flags, int argc, const char **argv, struct cfg *cfg) cfg->ldapserver = argv[i] + 11; if (strncmp (argv[i], "ldap_uri=", 9) == 0) cfg->ldap_uri = argv[i] + 9; + if (strncmp (argv[i], "ldap_bind_no_anonymous", sizeof("ldap_bind_no_anonymous")-1) == 0) + cfg->ldap_bind_no_anonymous = 1; + if (strncmp (argv[i], "ldap_bind_user=", sizeof("ldap_bind_user=")-1) == 0) + cfg->ldap_bind_user = argv[i] + sizeof("ldap_bind_user=")-1; + if (strncmp (argv[i], "ldap_bind_password=", sizeof("ldap_bind_password=")-1) == 0) + cfg->ldap_bind_password = argv[i] + sizeof("ldap_bind_password=")-1; + if (strncmp (argv[i], "ldap_filter=", sizeof("ldap_filter=")-1) == 0) + cfg->ldap_filter = argv[i] + sizeof("ldap_filter=")-1; if (strncmp (argv[i], "ldapdn=", 7) == 0) cfg->ldapdn = argv[i] + 7; if (strncmp (argv[i], "user_attr=", 10) == 0) @@ -700,6 +728,10 @@ parse_cfg (int flags, int argc, const char **argv, struct cfg *cfg) D (("authfile=%s", cfg->auth_file ? cfg->auth_file : "(null)")); D (("ldapserver=%s", cfg->ldapserver ? cfg->ldapserver : "(null)")); D (("ldap_uri=%s", cfg->ldap_uri ? cfg->ldap_uri : "(null)")); + D (("ldap_bind_no_anonymous=%d", cfg->ldap_bind_no_anonymous)); + D (("ldap_bind_user=%s", cfg->ldap_bind_user ? cfg->ldap_bind_user : "(null)")); + D (("ldap_bind_password=%s", cfg->ldap_bind_password ? cfg->ldap_bind_password : "(null)")); + D (("ldap_filter=%s", cfg->ldap_filter ? cfg->ldap_filter : "(null)")); D (("ldapdn=%s", cfg->ldapdn ? cfg->ldapdn : "(null)")); D (("user_attr=%s", cfg->user_attr ? cfg->user_attr : "(null)")); D (("yubi_attr=%s", cfg->yubi_attr ? cfg->yubi_attr : "(null)")); @@ -922,9 +954,10 @@ pam_sm_authenticate (pam_handle_t * pamh, DBG (("OTP: %s ID: %s ", otp, otp_id)); /* user entered their system password followed by generated OTP? */ + char *onlypasswd = NULL; if (password_len > TOKEN_OTP_LEN + cfg->token_id_length) { - char *onlypasswd = strdup (password); + onlypasswd = strdup (password); if (! onlypasswd) { retval = PAM_BUF_ERR; @@ -969,7 +1002,7 @@ pam_sm_authenticate (pam_handle_t * pamh, /* authorize the user with supplied token id */ if (cfg->ldapserver != NULL || cfg->ldap_uri != NULL) - valid_token = authorize_user_token_ldap (cfg, user, otp_id); + valid_token = authorize_user_token_ldap (cfg, user, onlypasswd, otp_id); else valid_token = authorize_user_token (cfg, user, otp_id, pamh); diff --git a/tests/util_test.c b/tests/util_test.c index e044e6a..8ae3649 100644 --- a/tests/util_test.c +++ b/tests/util_test.c @@ -125,7 +125,27 @@ static void test_load_chalresp_state(void) { #endif /* HAVE_CR */ +static void test_filter_printf(void) { + assert(filter_result_len("meno %u", "doof", NULL) == 9); + assert(filter_result_len("meno %u %u", "doof", NULL) == 14); + assert(filter_result_len("%u meno %u", "doof", NULL) == 14); + assert(filter_result_len("%u me %u no %u", "doof", NULL) == 20); + assert(filter_result_len("meno %w %%u", "doof", NULL) == 13); + assert(filter_result_len("meno %w %%u meno", "doof", NULL) == 18); + assert(filter_result_len("meno ", "doof", NULL) == 5); + + assert(!strcmp(filter_printf("meno %u", "doof"), "meno doof")); + assert(!strcmp(filter_printf("meno %u %u", "doof"), "meno doof doof")); + assert(!strcmp(filter_printf("%u meno %u", "doof"), "doof meno doof")); + assert(!strcmp(filter_printf("%u me %u no %u", "doof"), "doof me doof no doof")); + assert(!strcmp(filter_printf("meno %w %%u", "doof"), "meno %w %doof")); + assert(!strcmp(filter_printf("meno %w %%u meno", "doof"), "meno %w %doof meno")); + assert(!strcmp(filter_printf("meno ", "doof"), "meno ")); + printf("test_filter_printf OK\n"); +} + int main (void) { + test_filter_printf(); test_get_user_cfgfile_path(); test_check_user_token(); #if HAVE_CR diff --git a/util.c b/util.c index ecfacf6..45bcf55 100644 --- a/util.c +++ b/util.c @@ -35,6 +35,9 @@ #include #include #include +#include +#include +#include #include #include @@ -460,3 +463,46 @@ write_chalresp_state(FILE *f, CR_STATE *state) return 0; } #endif /* HAVE_CR */ + + +int filter_result_len(const char *filter, const char *user, char *output) { + int user_len = strlen(user); + int filter_len = strlen(filter); + const char *result; + int result_len = 0; + const char *percent_sign; + for (result = filter ; (percent_sign = strchr(result, '%')) ; result = percent_sign) { + if ((percent_sign + 1 - filter) > filter_len) { + break; + } + if (output) { + memcpy(output, result, percent_sign - result); + output += percent_sign - result; + } + if (*(percent_sign+1) == 'u') { + if (output) { + memcpy(output, user, user_len); + output += user_len; + } + result_len += (percent_sign - result) + user_len; + ++percent_sign; // skip u + } else { + if (output) { + *output++ = '%'; + } + result_len += percent_sign + 1 - result; + } + ++percent_sign; + } + if (output) { + memcpy(output, result, ((filter+filter_len)-result) + 1); + } + return result_len + (filter+filter_len-result); +} + +const char *filter_printf(const char *filter, const char *user) { + char *result = malloc(filter_result_len(filter, user, NULL) + 1); + filter_result_len(filter, user, result); + return result; +} + diff --git a/util.h b/util.h index edb81ac..0d19e98 100644 --- a/util.h +++ b/util.h @@ -96,4 +96,7 @@ int challenge_response(YK_KEY *yk, int slot, #endif /* HAVE_CR */ +int filter_result_len(const char *filter, const char *user, char *output); +const char *filter_printf(const char *filter, const char *user); + #endif /* __PAM_YUBICO_UTIL_H_INCLUDED__ */ From 37553c41ce33c0e2555f407e225033d7f6949b8f Mon Sep 17 00:00:00 2001 From: Meno Abels Date: Wed, 29 Oct 2014 13:25:29 +0100 Subject: [PATCH 2/3] enable that openvpn can now run without any local user --- README | 21 ++++++++++++- pam_yubico.c | 88 ++++++++++++++++++++++++++++++++++++++++++---------- util.c | 2 +- util.h | 2 +- 4 files changed, 94 insertions(+), 19 deletions(-) diff --git a/README b/README index 10abff2..0696387 100644 --- a/README +++ b/README @@ -345,5 +345,24 @@ Examples If you want to use the Yubikey to authenticate you on linux console logins, add the following to the top of `/etc/pam.d/login`: - auth sufficient pam_yubico.so id=16 debug + auth sufficient pam_yubico.so id=16 debug + +OpenVPN and ActiveDirectory + +create file '/etc/pam.d/openvpn': +auth required pam_yubico.so ldap_uri=ldap://ldap-srv debug id=19 yubi_attr=pager + ldapdn=dc=ad,dc=next-audience,dc=net + ldap_filter=(&(sAMAccountName=%u)(memberOf=CN=mygroup,OU=DefaultUser,DC=adivser,DC=net)) + ldap_bind_no_anonymous ldap_bind_user_filter=%u@adviser.com try_first_pass +account required pam_yubico.so + +create file 'openvpn.conf' +plugin openvpn-plugin-auth-pam.so openvpn + + +Feedback +-------- + +If you want to discuss anything related to the Yubico PAM module, +please e-mail the mailing list yubico-devel@googlegroups.com. diff --git a/pam_yubico.c b/pam_yubico.c index db3cbcd..e137ad6 100644 --- a/pam_yubico.c +++ b/pam_yubico.c @@ -114,6 +114,7 @@ struct cfg const char *ldapserver; const char *ldap_uri; int ldap_bind_no_anonymous; + const char *ldap_bind_user_filter; const char *ldap_bind_user; const char *ldap_bind_password; const char *ldap_filter; @@ -226,7 +227,7 @@ authorize_user_token_ldap (struct cfg *cfg, struct berval **vals; int i, rc; - const char *filter = NULL; + char *filter = NULL; char *find = NULL; int scope = LDAP_SCOPE_BASE; #endif @@ -263,16 +264,24 @@ authorize_user_token_ldap (struct cfg *cfg, } /* LDAPv2 is historical -- RFC3494. */ + ldap_set_option(ld, LDAP_OPT_REFERRALS, LDAP_OPT_OFF); protocol = LDAP_VERSION3; ldap_set_option (ld, LDAP_OPT_PROTOCOL_VERSION, &protocol); /* Bind anonymously to the LDAP server. */ if (cfg->ldap_bind_user && cfg->ldap_bind_password) { - DBG (("try bind with: %s:%s", cfg->ldap_bind_user, cfg->ldap_bind_password)); + DBG (("try bind with: %s:[%s]", cfg->ldap_bind_user, cfg->ldap_bind_password)); rc = ldap_simple_bind_s (ld, cfg->ldap_bind_user, cfg->ldap_bind_password); } else if (cfg->ldap_bind_no_anonymous) { - DBG (("try bind with: %s:%s", user, password)); - rc = ldap_simple_bind_s (ld, user, password); + char *tmp_user; + if (cfg->ldap_bind_user_filter) { + tmp_user = filter_printf(cfg->ldap_bind_user_filter, user); + } else { + tmp_user = strdup(user); + } + DBG (("try bind with: %s:[XXXXX]", tmp_user, password)); + rc = ldap_simple_bind_s (ld, tmp_user, password); + free(tmp_user); } else { DBG (("try bind anonymous")); rc = ldap_simple_bind_s (ld, NULL, NULL); @@ -331,16 +340,16 @@ authorize_user_token_ldap (struct cfg *cfg, { if ((vals = ldap_get_values_len (ld, e, a)) != NULL) { - DBG(("LDAP : Found %i values - checking if any of them match '%s:%s:%s'", - ldap_count_values_len(vals), - vals[i]->bv_val, - cfg->yubi_attr_prefix ? cfg->yubi_attr_prefix : "", token_id)); - yubi_attr_prefix_len = cfg->yubi_attr_prefix ? strlen(cfg->yubi_attr_prefix) : 0; /* Compare each value for the attribute against the token id. */ for (i = 0; vals[i] != NULL; i++) { + DBG(("LDAP : Found %i values - checking if any of them match '%s:%s:%s'", + ldap_count_values_len(vals), + vals[i]->bv_val, + cfg->yubi_attr_prefix ? cfg->yubi_attr_prefix : "", token_id)); + /* Only values containing this prefix are considered. */ if ((!cfg->yubi_attr_prefix || !strncmp (cfg->yubi_attr_prefix, vals[i]->bv_val, yubi_attr_prefix_len))) { @@ -690,6 +699,8 @@ parse_cfg (int flags, int argc, const char **argv, struct cfg *cfg) cfg->ldap_bind_no_anonymous = 1; if (strncmp (argv[i], "ldap_bind_user=", sizeof("ldap_bind_user=")-1) == 0) cfg->ldap_bind_user = argv[i] + sizeof("ldap_bind_user=")-1; + if (strncmp (argv[i], "ldap_bind_user_filter=", sizeof("ldap_bind_user_filter=")-1) == 0) + cfg->ldap_bind_user_filter = argv[i] + sizeof("ldap_bind_user_filter=")-1; if (strncmp (argv[i], "ldap_bind_password=", sizeof("ldap_bind_password=")-1) == 0) cfg->ldap_bind_password = argv[i] + sizeof("ldap_bind_password=")-1; if (strncmp (argv[i], "ldap_filter=", sizeof("ldap_filter=")-1) == 0) @@ -768,6 +779,7 @@ pam_sm_authenticate (pam_handle_t * pamh, size_t templates = 0; char *urls[10]; char *tmpurl = NULL; + char *onlypasswd = NULL; parse_cfg (flags, argc, argv, cfg); @@ -954,7 +966,6 @@ pam_sm_authenticate (pam_handle_t * pamh, DBG (("OTP: %s ID: %s ", otp, otp_id)); /* user entered their system password followed by generated OTP? */ - char *onlypasswd = NULL; if (password_len > TOKEN_OTP_LEN + cfg->token_id_length) { onlypasswd = strdup (password); @@ -970,7 +981,6 @@ pam_sm_authenticate (pam_handle_t * pamh, "setting item PAM_AUTHTOK")); retval = pam_set_item (pamh, PAM_AUTHTOK, onlypasswd); - free (onlypasswd); if (retval != PAM_SUCCESS) { DBG (("set_item returned error: %s", pam_strerror (pamh, retval))); @@ -1029,6 +1039,8 @@ pam_sm_authenticate (pam_handle_t * pamh, } done: + if (onlypasswd) + free(onlypasswd); if (templates > 0) { size_t i; @@ -1047,7 +1059,8 @@ done: retval = PAM_SUCCESS; } DBG (("done. [%s]", pam_strerror (pamh, retval))); - pam_set_data (pamh, "yubico_setcred_return", (void*) (intptr_t) retval, NULL); + pam_set_data (pamh, "yubico_setcred_return", (void*)(intptr_t)retval, NULL); + pam_set_data (pamh, "yubico_used_ldap", (void*)(intptr_t)cfg->ldap_bind_no_anonymous, NULL); return retval; } @@ -1058,16 +1071,59 @@ pam_sm_setcred (pam_handle_t * pamh, int flags, int argc, const char **argv) return PAM_SUCCESS; } +PAM_EXTERN int +pam_sm_acct_mgmt(pam_handle_t *pamh, int flags, int argc, const char **argv) +{ + int use_ldap = -1; + int rc = pam_get_data(pamh, "yubico_used_ldap", (const void**)&use_ldap); + if (rc == PAM_SUCCESS && use_ldap) { + int retval; + rc = pam_get_data(pamh, "yubico_setcred_return", (const void**)&retval); + if (rc == PAM_SUCCESS && retval == PAM_SUCCESS) { + D (("pam_sm_acct_mgmt returing PAM_SUCCESS")); + return PAM_SUCCESS; + } + } + D (("pam_sm_acct_mgmt returing PAM_AUTH_ERR:%d", use_ldap)); + return PAM_AUTH_ERR; +} + +PAM_EXTERN int +pam_sm_open_session(pam_handle_t *pamh, int flags, + int argc, const char *argv[]) +{ + + D(("pam_sm_open_session")); + return (PAM_SUCCESS); +} + +PAM_EXTERN int +pam_sm_close_session(pam_handle_t *pamh, int flags, + int argc, const char *argv[]) +{ + D(("pam_sm_close_session")); + return (PAM_SUCCESS); +} + +PAM_EXTERN int +pam_sm_chauthtok(pam_handle_t *pamh, int flags, + int argc, const char *argv[]) +{ + D(("pam_sm_chauthtok")); + return (PAM_SERVICE_ERR); +} + + #ifdef PAM_STATIC struct pam_module _pam_yubico_modstruct = { "pam_yubico", pam_sm_authenticate, pam_sm_setcred, - NULL, - NULL, - NULL, - NULL + pam_sm_acct_mgmt, + pam_sm_open_session, + pam_sm_close_session, + pam_sm_chauthtok }; #endif diff --git a/util.c b/util.c index 45bcf55..0b017dd 100644 --- a/util.c +++ b/util.c @@ -500,7 +500,7 @@ int filter_result_len(const char *filter, const char *user, char *output) { return result_len + (filter+filter_len-result); } -const char *filter_printf(const char *filter, const char *user) { +char *filter_printf(const char *filter, const char *user) { char *result = malloc(filter_result_len(filter, user, NULL) + 1); filter_result_len(filter, user, result); return result; diff --git a/util.h b/util.h index 0d19e98..04c23b9 100644 --- a/util.h +++ b/util.h @@ -97,6 +97,6 @@ int challenge_response(YK_KEY *yk, int slot, #endif /* HAVE_CR */ int filter_result_len(const char *filter, const char *user, char *output); -const char *filter_printf(const char *filter, const char *user); +char *filter_printf(const char *filter, const char *user); #endif /* __PAM_YUBICO_UTIL_H_INCLUDED__ */ From aa87979eb84adb3adef170dac6ff2285ba43cd26 Mon Sep 17 00:00:00 2001 From: Meno Abels Date: Thu, 20 Nov 2014 22:40:55 +0100 Subject: [PATCH 3/3] integrate https://github.com/Yubico/yubico-pam/pull/39/files --- README | 17 +++++++++-------- pam_yubico.c | 46 +++++++++++++++++++++++++++++++++------------- 2 files changed, 42 insertions(+), 21 deletions(-) diff --git a/README b/README index 0696387..3d7dd19 100644 --- a/README +++ b/README @@ -20,7 +20,7 @@ later. This has introduced a dependency of libykpers-1 from the ykpersonalize package. Pass `--without-cr` to `configure` to avoid this dependency. -The development community is co-ordinated via +The development community is co-ordinated via https://github.com/Yubico/yubico-pam[the GitHub project page]. The project is licensed under a BSD license. See the file COPYING for @@ -67,7 +67,7 @@ It in turn requires Curl, which you need to have installed, and https://developers.yubico.com/yubico-c[libyubikey]. The new Challenge-Response offline authentication requires libykpers-1 -from the +from the https://developers.yubico.com/yubikey-personalization[yubikey-personalization] project : The build system uses Autoconf, to set up the build system run: @@ -152,7 +152,7 @@ service, go https://upgrade.yubico.com/getapikey[here]. debug:: to enable debug output to stdout. -alwaysok:: +alwaysok:: to enable all authentication attempts to succeed (aka presentation mode). @@ -319,10 +319,10 @@ information including the OTP and ID of your token to the shell - copy the ID into your config file and you should be up and going. ------ -Yubikey for `youruser': +Yubikey for `youruser': [pam_yubico.c:pam_sm_authenticate(867)] conv returned 44 bytes [pam_yubico.c:pam_sm_authenticate(885)] Skipping first 0 bytes. Length is 44, token_id set to 12 and token OTP always 32. -[pam_yubico.c:pam_sm_authenticate(892)] OTP: ccccccclabcabkhbdncicglfltnukadfoifadfhhhhfe ID: cccccclabcab +[pam_yubico.c:pam_sm_authenticate(892)] OTP: ccccccclabcabkhbdncicglfltnukadfoifadfhhhhfe ID: cccccclabcab ------ @@ -348,11 +348,12 @@ logins, add the following to the top of `/etc/pam.d/login`: auth sufficient pam_yubico.so id=16 debug OpenVPN and ActiveDirectory +--------------------------- create file '/etc/pam.d/openvpn': -auth required pam_yubico.so ldap_uri=ldap://ldap-srv debug id=19 yubi_attr=pager - ldapdn=dc=ad,dc=next-audience,dc=net - ldap_filter=(&(sAMAccountName=%u)(memberOf=CN=mygroup,OU=DefaultUser,DC=adivser,DC=net)) +auth required pam_yubico.so ldap_uri=ldap://ldap-srv debug id=19 yubi_attr=pager + ldapdn=dc=ad,dc=next-audience,dc=net + ldap_filter=(&(sAMAccountName=%u)(memberOf=CN=mygroup,OU=DefaultUser,DC=adivser,DC=net)) ldap_bind_no_anonymous ldap_bind_user_filter=%u@adviser.com try_first_pass account required pam_yubico.so diff --git a/pam_yubico.c b/pam_yubico.c index e137ad6..bfa3c4c 100644 --- a/pam_yubico.c +++ b/pam_yubico.c @@ -118,6 +118,7 @@ struct cfg const char *ldap_bind_user; const char *ldap_bind_password; const char *ldap_filter; + const char *ldap_cacertfile; const char *ldapdn; const char *user_attr; const char *yubi_attr; @@ -198,8 +199,10 @@ free_out: * This function will look in ldap id the token correspond to the * requested user. It will returns 0 for failure and 1 for success. * - * For the moment ldaps is not supported. ldap serve can be on a - * remote host. + * ldaps is only supported for ldap_uri based connections. + * ldap_cacertfile usually needs to be set for this to work. + * + * ldap serve can be on a remote host. * * You need the following parameters in you pam config: * ldapserver= OR ldap_uri= @@ -207,6 +210,9 @@ free_out: * user_attr= * yubi_attr= * + * If using ldap_uri, you can specify multiple failover hosts + * eg. + * ldap_uri=ldaps://host1.fqdn.example.com,ldaps://host2.fqdn.example.com */ static int authorize_user_token_ldap (struct cfg *cfg, @@ -248,7 +254,7 @@ authorize_user_token_ldap (struct cfg *cfg, rc = ldap_initialize (&ld, cfg->ldap_uri); if (rc != LDAP_SUCCESS) { - DBG (("ldap_init: %s", ldap_err2string (rc))); + DBG (("ldap_initialize: %s", ldap_err2string (rc))); retval = 0; goto done; } @@ -268,18 +274,22 @@ authorize_user_token_ldap (struct cfg *cfg, protocol = LDAP_VERSION3; ldap_set_option (ld, LDAP_OPT_PROTOCOL_VERSION, &protocol); + if (cfg->ldap_uri && cfg->ldap_cacertfile) { + /* Set CA CERTFILE. This makes ldaps work when using ldap_uri */ + ldap_set_option (0, LDAP_OPT_X_TLS_CACERTFILE, cfg->ldap_cacertfile); + } /* Bind anonymously to the LDAP server. */ if (cfg->ldap_bind_user && cfg->ldap_bind_password) { - DBG (("try bind with: %s:[%s]", cfg->ldap_bind_user, cfg->ldap_bind_password)); + DBG (("try bind with: %s:[%s]", cfg->ldap_bind_user, cfg->ldap_bind_password)); rc = ldap_simple_bind_s (ld, cfg->ldap_bind_user, cfg->ldap_bind_password); } else if (cfg->ldap_bind_no_anonymous) { char *tmp_user; if (cfg->ldap_bind_user_filter) { tmp_user = filter_printf(cfg->ldap_bind_user_filter, user); } else { - tmp_user = strdup(user); + tmp_user = strdup(user); } - DBG (("try bind with: %s:[XXXXX]", tmp_user, password)); + DBG (("try bind with: %s:[XXXXX]", tmp_user, password)); rc = ldap_simple_bind_s (ld, tmp_user, password); free(tmp_user); } else { @@ -294,7 +304,6 @@ authorize_user_token_ldap (struct cfg *cfg, } /* Allocation of memory for search strings depending on input size */ - find = strdup(cfg->ldapdn); // allow free later-:) if (cfg->user_attr && cfg->yubi_attr) { i = (strlen(cfg->user_attr) + strlen(cfg->ldapdn) + strlen(user) + 3) * sizeof(char); if ((find = malloc(i)) == NULL) { @@ -304,14 +313,16 @@ authorize_user_token_ldap (struct cfg *cfg, } sprintf (find, "%s=%s,%s", cfg->user_attr, user, cfg->ldapdn); filter = NULL; - } + } else { + find = strdup(cfg->ldapdn); // allow free later-:) + } if (cfg->ldap_filter) { filter = filter_printf(cfg->ldap_filter, user); scope = LDAP_SCOPE_SUBTREE; } attrs[0] = (char *) cfg->yubi_attr; - DBG(("LDAP : look up object base='%s' filter='%s', ask for attribute '%s'", find, + DBG(("LDAP : look up object base='%s' filter='%s', ask for attribute '%s'", find, filter ? filter:"(null)", cfg->yubi_attr)); /* Search for the entry. */ @@ -346,7 +357,7 @@ authorize_user_token_ldap (struct cfg *cfg, for (i = 0; vals[i] != NULL; i++) { DBG(("LDAP : Found %i values - checking if any of them match '%s:%s:%s'", - ldap_count_values_len(vals), + ldap_count_values_len(vals), vals[i]->bv_val, cfg->yubi_attr_prefix ? cfg->yubi_attr_prefix : "", token_id)); @@ -705,6 +716,14 @@ parse_cfg (int flags, int argc, const char **argv, struct cfg *cfg) cfg->ldap_bind_password = argv[i] + sizeof("ldap_bind_password=")-1; if (strncmp (argv[i], "ldap_filter=", sizeof("ldap_filter=")-1) == 0) cfg->ldap_filter = argv[i] + sizeof("ldap_filter=")-1; + if (strncmp (argv[i], "ldap_cacertfile=", sizeof("ldap_cacertfile=")-1) == 0) + cfg->ldap_cacertfile = (char *) argv[i] + sizeof("ldap_cacertfile=")-1; + /* compatible with https://github.com/Yubico/yubico-pam/pull/39/files */ + if (strncmp (argv[i], "binddn=", sizeof("binddn=")-1) == 0) + cfg->ldap_bind_user = (char *) argv[i] + sizeof("binddn=")-1; + if (strncmp (argv[i], "bindpw=", sizeof("bindpw=")-1) == 0) + cfg->ldap_bind_password = (char *) argv[i] + sizeof("bindpw=")-1; + /* compatible with https://github.com/Yubico/yubico-pam/pull/39/files */ if (strncmp (argv[i], "ldapdn=", 7) == 0) cfg->ldapdn = argv[i] + 7; if (strncmp (argv[i], "user_attr=", 10) == 0) @@ -743,6 +762,7 @@ parse_cfg (int flags, int argc, const char **argv, struct cfg *cfg) D (("ldap_bind_user=%s", cfg->ldap_bind_user ? cfg->ldap_bind_user : "(null)")); D (("ldap_bind_password=%s", cfg->ldap_bind_password ? cfg->ldap_bind_password : "(null)")); D (("ldap_filter=%s", cfg->ldap_filter ? cfg->ldap_filter : "(null)")); + D (("ldap_cacertfile=%s", cfg->ldap_cacertfile ? cfg->ldap_cacertfile : "(null)")); D (("ldapdn=%s", cfg->ldapdn ? cfg->ldapdn : "(null)")); D (("user_attr=%s", cfg->user_attr ? cfg->user_attr : "(null)")); D (("yubi_attr=%s", cfg->yubi_attr ? cfg->yubi_attr : "(null)")); @@ -1059,7 +1079,7 @@ done: retval = PAM_SUCCESS; } DBG (("done. [%s]", pam_strerror (pamh, retval))); - pam_set_data (pamh, "yubico_setcred_return", (void*)(intptr_t)retval, NULL); + pam_set_data (pamh, "yubico_setcred_return", (void*)(intptr_t)retval, NULL); pam_set_data (pamh, "yubico_used_ldap", (void*)(intptr_t)cfg->ldap_bind_no_anonymous, NULL); return retval; @@ -1072,7 +1092,7 @@ pam_sm_setcred (pam_handle_t * pamh, int flags, int argc, const char **argv) } PAM_EXTERN int -pam_sm_acct_mgmt(pam_handle_t *pamh, int flags, int argc, const char **argv) +pam_sm_acct_mgmt(pam_handle_t *pamh, int flags, int argc, const char **argv) { int use_ldap = -1; int rc = pam_get_data(pamh, "yubico_used_ldap", (const void**)&use_ldap); @@ -1082,7 +1102,7 @@ pam_sm_acct_mgmt(pam_handle_t *pamh, int flags, int argc, const char **argv) if (rc == PAM_SUCCESS && retval == PAM_SUCCESS) { D (("pam_sm_acct_mgmt returing PAM_SUCCESS")); return PAM_SUCCESS; - } + } } D (("pam_sm_acct_mgmt returing PAM_AUTH_ERR:%d", use_ldap)); return PAM_AUTH_ERR;