Changes from APPLIANCE_HEAD:
[ira/wip.git] / source3 / passdb / ldap.c
index 4ee53edbef483c0c70ec29fa11d796104af0f2f3..54566e454b93c256b476b8c57ef640e6f1eb78a0 100644 (file)
@@ -20,7 +20,7 @@
    
 */
 
-#ifdef USE_LDAP
+#ifdef WITH_LDAP
 
 #include "includes.h"
 
@@ -39,8 +39,8 @@ static BOOL ldap_open_connection(LDAP **ldap_struct)
 {
        if ( (*ldap_struct = ldap_open(lp_ldap_server(),lp_ldap_port()) ) == NULL)
        {
-               DEBUG(0,("%s: The LDAP server is not responding !\n",timestring()));
-               return(False);
+               DEBUG( 0, ( "The LDAP server is not responding !\n" ) );
+               return( False );
        }
        DEBUG(2,("ldap_open_connection: connection opened\n"));
        return (True);
@@ -55,7 +55,7 @@ static BOOL ldap_connect_anonymous(LDAP *ldap_struct)
 {
        if ( ldap_simple_bind_s(ldap_struct,lp_ldap_root(),lp_ldap_rootpasswd()) ! = LDAP_SUCCESS)
        {
-               DEBUG(0,("%s: Couldn't bind to the LDAP server !\n", timestring() ));
+               DEBUG( 0, ( "Couldn't bind to the LDAP server !\n" ) );
                return(False);
        }
        return (True);
@@ -69,7 +69,7 @@ static BOOL ldap_connect_system(LDAP *ldap_struct)
 {
        if ( ldap_simple_bind_s(ldap_struct,lp_ldap_root(),lp_ldap_rootpasswd()) ! = LDAP_SUCCESS)
        {
-               DEBUG(0,("%s: Couldn't bind to the LDAP server !\n", timestring() ));
+               DEBUG( 0, ( "Couldn't bind to the LDAP server!\n" ) );
                return(False);
        }
        DEBUG(2,("ldap_connect_system: succesful connection to the LDAP server\n"));
@@ -83,7 +83,7 @@ static BOOL ldap_connect_user(LDAP *ldap_struct, char *user, char *password)
 {
        if ( ldap_simple_bind_s(ldap_struct,lp_ldap_root(),lp_ldap_rootpasswd()) ! = LDAP_SUCCESS)
        {
-               DEBUG(0,("%s: Couldn't bind to the LDAP server !\n", timestring() ));
+               DEBUG( 0, ( "Couldn't bind to the LDAP server !\n" ) );
                return(False);
        }
        DEBUG(2,("ldap_connect_user: succesful connection to the LDAP server\n"));
@@ -104,7 +104,7 @@ static BOOL ldap_search_one_user(LDAP *ldap_struct, char *filter, LDAPMessage **
 
        if (rc ! = LDAP_SUCCESS )
        {
-               DEBUG(0,("%s: Problem during the LDAP search\n",timestring()));
+               DEBUG( 0, ( "Problem during the LDAP search\n" ) );
                return(False);
        }
        return (True);
@@ -121,7 +121,7 @@ static BOOL ldap_search_one_user_by_name(LDAP *ldap_struct, char *user, LDAPMess
           so in ldap filter, %u MUST exist :-)
        */      
        pstrcpy(filter,lp_ldap_filter());
-       string_sub(filter,"%u",user);
+       pstring_sub(filter,"%u",user);
        
        if ( !ldap_search_one_user(ldap_struct, filter, result) )
        {
@@ -137,7 +137,7 @@ static BOOL ldap_search_one_user_by_uid(LDAP *ldap_struct, int uid, LDAPMessage
 {      
        pstring filter;
        
-       snprintf(filter, sizeof(pstring), "uidAccount = %d", uid);
+       slprintf(filter, sizeof(pstring)-1, "uidAccount = %d", uid);
        
        if ( !ldap_search_one_user(ldap_struct, filter, result) )
        {       
@@ -225,8 +225,8 @@ static void ldap_get_smb_passwd(LDAP *ldap_struct,LDAPMessage *entry,
 
        pdb_init_smb(user);
 
-       bzero(smblmpwd, sizeof(smblmpwd));
-       bzero(smbntpwd, sizeof(smbntpwd));
+       memset((char *)smblmpwd, '\0', sizeof(smblmpwd));
+       memset((char *)smbntpwd, '\0', sizeof(smbntpwd));
 
        get_single_attribute(ldap_struct, entry, "cn", user_name);
        DEBUG(2,("ldap_get_smb_passwd: user: %s\n",user_name));
@@ -234,15 +234,15 @@ static void ldap_get_smb_passwd(LDAP *ldap_struct,LDAPMessage *entry,
 #ifdef LDAP_PLAINTEXT_PASSWORD
        get_single_attribute(ldap_struct, entry, "userPassword", temp);
        nt_lm_owf_gen(temp, user->smb_nt_passwd, user->smb_passwd);
-       bzero(temp, sizeof(temp)); /* destroy local copy of the password */
+       memset((char *)temp, '\0', sizeof(temp)); /* destroy local copy of the password */
 #else
        get_single_attribute(ldap_struct, entry, "unicodePwd", temp);
        pdb_gethexpwd(temp, smbntpwd);          
-       bzero(temp, sizeof(temp)); /* destroy local copy of the password */
+       memset((char *)temp, '\0', sizeof(temp)); /* destroy local copy of the password */
 
        get_single_attribute(ldap_struct, entry, "dBCSPwd", temp);
        pdb_gethexpwd(temp, smblmpwd);          
-       bzero(temp, sizeof(temp)); /* destroy local copy of the password */
+       memset((char *)temp, '\0', sizeof(temp)); /* destroy local copy of the password */
 #endif
        
        get_single_attribute(ldap_struct, entry, "userAccountControl", temp);
@@ -387,6 +387,11 @@ static void make_a_mod(LDAPMod ***modlist,int modop, char *attribute, char *valu
        if (mods == NULL)
        {
                mods = (LDAPMod **)malloc( sizeof(LDAPMod *) );
+               if (mods == NULL)
+               {
+                       DEBUG(0,("make_a_mod: out of memory!\n"));
+                       return;
+               }
                mods[0] = NULL;
        }
        
@@ -402,7 +407,17 @@ static void make_a_mod(LDAPMod ***modlist,int modop, char *attribute, char *valu
        if (mods[i] == NULL)
        {
                mods = (LDAPMod **)realloc( mods,  (i+2) * sizeof( LDAPMod * ) );       
+               if (mods == NULL)
+               {
+                       DEBUG(0,("make_a_mod: out of memory!\n"));
+                       return;
+               }
                mods[i] = (LDAPMod *)malloc( sizeof( LDAPMod ) );
+               if (mods[i] == NULL)
+               {
+                       DEBUG(0,("make_a_mod: out of memory!\n"));
+                       return;
+               }
                mods[i]->mod_op = modop;
                mods[i]->mod_values = NULL;
                mods[i]->mod_type = strdup( attribute );
@@ -418,6 +433,11 @@ static void make_a_mod(LDAPMod ***modlist,int modop, char *attribute, char *valu
                }
                mods[ i ]->mod_values = (char **)realloc(mods[ i ]->mod_values,
                                                          (j+2) * sizeof( char * ));
+               if ( mods[ i ]->mod_values == NULL)
+               {
+                       DEBUG(0, "make_a_mod: Memory allocation failure!\n");
+                       return;
+               }
                mods[ i ]->mod_values[ j ] = strdup(value);     
                mods[ i ]->mod_values[ j + 1 ] = NULL;          
        }
@@ -576,7 +596,7 @@ static BOOL modadd_ldappwd_entry(struct smb_passwd *newpwd, int flag)
        
        make_a_mod(&mods, ldap_state, "rid", rid);
        make_a_mod(&mods, ldap_state, "pwdLastSet", lst);
-       make_a_mod(&mods, ldap_state, "userAccountControl", pdb_encode_acct_ctrl(newpwd->acct_ctrl));
+       make_a_mod(&mods, ldap_state, "userAccountControl", pdb_encode_acct_ctrl(newpwd->acct_ctrl, NEW_PW_FORMAT_SPACE_PADDED_LEN));
        
        switch(flag)
        {
@@ -708,7 +728,7 @@ static BOOL modadd_ldap21pwd_entry(struct sam_passwd *newpwd, int flag)
        
        make_a_mod(&mods, ldap_state, "rid", rid);
        make_a_mod(&mods, ldap_state, "pwdLastSet", lst);
-       make_a_mod(&mods, ldap_state, "userAccountControl", pdb_encode_acct_ctrl(newpwd->acct_ctrl));
+       make_a_mod(&mods, ldap_state, "userAccountControl", pdb_encode_acct_ctrl(newpwd->acct_ctrl,NEW_PW_FORMAT_SPACE_PADDED_LEN));
        
        ldap_modify_s(ldap_struct, dn, mods);
        
@@ -893,25 +913,25 @@ static void endldappwent(void *vp)
 }
 
 /*************************************************************************
- Return the current position in the ldap passwd list as an unsigned long.
+ Return the current position in the ldap passwd list as an SMB_BIG_UINT.
  This must be treated as an opaque token.
 
  do not call this function directly.  use passdb.c instead.
 
 *************************************************************************/
-static unsigned long getldappwpos(void *vp)
+static SMB_BIG_UINT getldappwpos(void *vp)
 {
-       return 0;
+       return (SMB_BIG_UINT)0;
 }
 
 /*************************************************************************
- Set the current position in the ldap passwd list from unsigned long.
+ Set the current position in the ldap passwd list from SMB_BIG_UINT.
  This must be treated as an opaque token.
 
  do not call this function directly.  use passdb.c instead.
 
 *************************************************************************/
-static BOOL setldappwpos(void *vp, unsigned long tok)
+static BOOL setldappwpos(void *vp, SMB_BIG_UINT tok)
 {
        return False;
 }
@@ -930,6 +950,11 @@ static struct smb_passwd *getldappwuid(uid_t smb_userid)
   return pdb_sam_to_smb(iterate_getsam21pwuid(smb_userid));
 }
 
+static struct smb_passwd *getldappwrid(uint32 user_rid)
+{
+  return pdb_sam_to_smb(iterate_getsam21pwuid(pdb_user_rid_to_uid(user_rid)));
+}
+
 static struct smb_passwd *getldappwent(void *vp)
 {
   return pdb_sam_to_smb(getldap21pwent(vp));
@@ -945,6 +970,31 @@ static BOOL mod_ldappwd_entry(struct smb_passwd* pwd, BOOL override)
   return mod_ldap21pwd_entry(pdb_smb_to_sam(pwd), override);
 }
 
+static BOOL del_ldappwd_entry(const char *name)
+{
+  return False; /* Dummy... */
+}
+
+static struct sam_disp_info *getldapdispnam(char *name)
+{
+       return pdb_sam_to_dispinfo(getldap21pwnam(name));
+}
+
+static struct sam_disp_info *getldapdisprid(uint32 rid)
+{
+       return pdb_sam_to_dispinfo(getldap21pwrid(rid));
+}
+
+static struct sam_disp_info *getldapdispent(void *vp)
+{
+       return pdb_sam_to_dispinfo(getldap21pwent(vp));
+}
+
+static struct sam_passwd *getldap21pwuid(uid_t uid)
+{
+       return pdb_smb_to_sam(iterate_getsam21pwuid(pdb_uid_to_user_rid(uid)));
+}
+
 static struct passdb_ops ldap_ops =
 {
        startldappwent,
@@ -952,18 +1002,21 @@ static struct passdb_ops ldap_ops =
        getldappwpos,
        setldappwpos,
        getldappwnam,
-       NULL, /* getldappwuid, */
-       NULL, /* getldappwent, */
-       NULL, /* add_ldappwd_entry, */
-       NULL, /* mod_ldappwd_entry, */
+       getldappwuid,
+       getldappwrid,
+       getldappwent,
+       add_ldappwd_entry,
+       mod_ldappwd_entry,
+       del_ldappwd_entry,
        getldap21pwent,
        iterate_getsam21pwnam,       /* From passdb.c */
        iterate_getsam21pwuid,       /* From passdb.c */
        iterate_getsam21pwrid,       /* From passdb.c */
        add_ldap21pwd_entry,
        mod_ldap21pwd_entry,
-       NULL, /* getsamdisprid, */
-       NULL /* getsamdispent */
+       getldapdispnam,
+       getldapdisprid,
+       getldapdispent
 };
 
 struct passdb_ops *ldap_initialize_password_db(void)
@@ -972,5 +1025,6 @@ struct passdb_ops *ldap_initialize_password_db(void)
 }
 
 #else
+ void dummy_function(void);
  void dummy_function(void) { } /* stop some compilers complaining */
 #endif