Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/135.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 通过C++;使用ldap或pam编程_C++_Security_Authentication_Ldap_Pam - Fatal编程技术网

C++ 通过C++;使用ldap或pam编程

C++ 通过C++;使用ldap或pam编程,c++,security,authentication,ldap,pam,C++,Security,Authentication,Ldap,Pam,我的程序必须对人进行身份验证,它运行的当前计算机通过ldap服务器识别用户 我在一台简单的计算机上成功地测试了这段代码: #include <stdlib.h> #include <iostream> #include <fstream> #include <security/pam_appl.h> #include <unistd.h> // To build this: // g++ pam.cc -lpam -o test s

我的程序必须对人进行身份验证,它运行的当前计算机通过ldap服务器识别用户

我在一台简单的计算机上成功地测试了这段代码:

#include <stdlib.h>
#include <iostream>
#include <fstream>
#include <security/pam_appl.h>
#include <unistd.h>

// To build this:
// g++ pam.cc -lpam -o test

struct pam_response *reply;

//function used to get user input
int function_conversation(int num_msg, const struct pam_message **msg, struct pam_response **resp, void *appdata_ptr)
{
    *resp = reply;
    return PAM_SUCCESS;
}

int main(int argc, char** argv)
{
    if(argc != 2) {
        fprintf(stderr, "Usage: check_user <username>\n");
        exit(1);
    }
    const char *username;
    username = argv[1];

    const struct pam_conv local_conversation = { function_conversation, NULL };
  pam_handle_t *local_auth_handle = NULL; // this gets set by pam_start

  int retval;

  // local_auth_handle gets set based on the service
  retval = pam_start("common-auth", username, &local_conversation, &local_auth_handle);

  if (retval != PAM_SUCCESS)
  {
    std::cout << "pam_start returned " << retval << std::endl;
    exit(retval);
  }

  reply = (struct pam_response *)malloc(sizeof(struct pam_response));

  // *** Get the password by any method, or maybe it was passed into this function.
  reply[0].resp = getpass("Password: ");
  reply[0].resp_retcode = 0;

  retval = pam_authenticate(local_auth_handle, 0);

  if (retval != PAM_SUCCESS)
  {
    if (retval == PAM_AUTH_ERR)
    {
        std::cout << "Authentication failure." << std::endl;
    }
    else
    {
        std::cout << "pam_authenticate returned " << retval << std::endl;
    }
    exit(retval);
  }

  std::cout << "Authenticated." << std::endl;

  retval = pam_end(local_auth_handle, retval);

  if (retval != PAM_SUCCESS)
  {
    std::cout << "pam_end returned " << retval << std::endl;
    exit(retval);
  }

  return retval;
}
似乎找到了服务器,但无法对我进行身份验证


我的问题是:pam和ldap之间有什么区别,pam是一种更通用的身份验证方法吗?我可以用pam程序实现ldap的身份验证吗?如果没有,我的ldap测试代码有什么问题吗?

我希望您知道,除非您有SSL,否则简单的绑定会以明文传输密码。如果没有,至少求助于Digest MD5。我不知道,当我有一个基本的工作版本时,我可能会朝着哈希密码的方向发展。我猜哈希密码是不相关的,因为simple bind通过TCP传递一个明文密码。好吧,反正我还没有,但我想我必须使用pam或ldap中的其他函数。这些示例甚至都不起作用,所以我不需要为哈希或其他任何事情而烦恼。我有点不明白为什么您首先要使用PAM。如果用户已经在系统中,我只需要使用OpenLDAP libs。这就是你想要做的。你用谷歌搜索过一个例子吗?
#include <stdio.h>
#include <ldap.h>
/* LDAP Server settings */
#define LDAP_SERVER "ldap://annuaire.upmc.fr:389"

int
main( int argc, char **argv )
{
    LDAP        *ld;
    int        rc;
    char        bind_dn[100];

/* Get username and password */
    if( argc != 3 )
    {
        perror( "invalid args, required: username password" );
        return( 1 );
    }
    sprintf( bind_dn, "uid=%s,ou=people,dc=upmc,dc=fr", argv[1] );
    printf( "Connecting as %s...\n", bind_dn );

/* Open LDAP Connection */
    if( ldap_initialize( &ld, LDAP_SERVER ) )
    {
        perror( "ldap_initialize" );
        return( 1 );
    }


/* User authentication (bind) */
    rc = ldap_simple_bind_s( ld, bind_dn, argv[2] );
    if( rc != LDAP_SUCCESS )
    {
        fprintf(stderr, "ldap_simple_bind_s: %s\n", ldap_err2string(rc) );
        return( 1 );
    }
    printf( "Successful authentication\n" );
    ldap_unbind( ld );
    return( 0 );
}
ldap_simple_bind_s: Invalid credentials.