From 6d7b23de9d58c02e1843b4277ed84f18f04e7183 Mon Sep 17 00:00:00 2001 From: Gerald Carter Date: Thu, 27 Dec 2001 17:48:34 +0000 Subject: [PATCH] few ldap fixes; introduced init_flag to mark which fields have been initialized (without using NULL pointers); removed dependency for preexisting posixAccount entry in the directory to add a new user/machine. --- source/include/smb.h | 19 ++++++++++++++++++ source/passdb/passdb.c | 10 +++++++++- source/passdb/pdb_ldap.c | 51 ++++++++++++++++++++++++------------------------ 3 files changed, 53 insertions(+), 27 deletions(-) diff --git a/source/include/smb.h b/source/include/smb.h index b3cfeb3b4b9..a88a613e451 100644 --- a/source/include/smb.h +++ b/source/include/smb.h @@ -570,8 +570,27 @@ typedef struct { #define SHAREMODE_FN(fn) \ void (*fn)(share_mode_entry *, char*) +/* + * bit flags representing initialized fields in SAM_ACCOUNT + */ +#define FLAG_SAM_UNINIT 0x00000000 +#define FLAG_SAM_UID 0x00000001 +#define FLAG_SAM_GID 0x00000002 +#define FLAG_SAM_SMBHOME 0x00000004 +#define FLAG_SAM_PROFILE 0x00000008 +#define FLAG_SAM_LOGONSCRIPT 0x00000010 +#define FLAG_SAM_DRIVE 0x00000020 + +#define IS_SAM_ACCT_UNIX_USER(x) \ + (((x)->init_flag & SAM_ACCT_UNIX_UID) \ + && ((x)->init_flag & SAM_ACCT_UNIX_GID)) + + typedef struct sam_passwd { + /* initiailization flags */ + uint32 init_flag; + time_t logon_time; /* logon time */ time_t logoff_time; /* logoff time */ time_t kickoff_time; /* kickoff time */ diff --git a/source/passdb/passdb.c b/source/passdb/passdb.c index 00b62455578..b626d4684c1 100644 --- a/source/passdb/passdb.c +++ b/source/passdb/passdb.c @@ -76,6 +76,9 @@ static BOOL pdb_fill_default_sam(SAM_ACCOUNT *user) } ZERO_STRUCTP(user); + + user->init_flag = FLAG_SAM_UNINIT; + user->uid = user->gid = -1; user->logon_time = (time_t)0; user->pass_last_set_time = (time_t)0; user->pass_can_change_time = (time_t)0; @@ -136,6 +139,7 @@ BOOL pdb_init_sam_pw(SAM_ACCOUNT **new_sam_acct, struct passwd *pwd) return False; } + pdb_set_username(*new_sam_acct, pwd->pw_name); pdb_set_fullname(*new_sam_acct, pwd->pw_gecos); pdb_set_uid(*new_sam_acct, pwd->pw_uid); @@ -1327,6 +1331,8 @@ BOOL pdb_set_uid (SAM_ACCOUNT *sampass, uid_t uid) return False; sampass->uid = uid; + sampass->init_flag |= FLAG_SAM_UID; + return True; } @@ -1335,7 +1341,9 @@ BOOL pdb_set_gid (SAM_ACCOUNT *sampass, gid_t gid) if (!sampass) return False; - sampass->gid = gid; + sampass->gid = gid; + sampass->init_flag |= FLAG_SAM_GID; + return True; } diff --git a/source/passdb/pdb_ldap.c b/source/passdb/pdb_ldap.c index 26b75ea2f93..4cb61fc387d 100644 --- a/source/passdb/pdb_ldap.c +++ b/source/passdb/pdb_ldap.c @@ -150,6 +150,8 @@ static BOOL ldap_connect_system(LDAP * ldap_struct) /* removed the sasl_bind_s "EXTERNAL" stuff, as my testsuite (OpenLDAP) doesnt' seem to support it */ + DEBUG(10,("ldap_connect_system: Binding to ldap server as \"%s\"\n", + lp_ldap_admin_dn())); if ((rc = ldap_simple_bind_s(ldap_struct, lp_ldap_admin_dn(), ldap_secret)) != LDAP_SUCCESS) { @@ -539,9 +541,6 @@ static BOOL init_ldap_from_sam (LDAPMod *** mods, int ldap_state, SAM_ACCOUNT * make_a_mod(mods, ldap_state, "uid", pdb_get_username(sampass)); DEBUG(2, ("Setting entry for user: %s\n", pdb_get_username(sampass))); - /* not sure about using this for the nt_username */ - make_a_mod(mods, ldap_state, "sambaDomain", pdb_get_domain(sampass)); - slprintf(temp, sizeof(temp) - 1, "%i", pdb_get_uid(sampass)); make_a_mod(mods, ldap_state, "uidNumber", temp); @@ -940,13 +939,14 @@ Add SAM_ACCOUNT to LDAP *********************************************************************/ BOOL pdb_add_sam_account(SAM_ACCOUNT * newpwd) { - int rc; - pstring filter; - LDAP *ldap_struct; - LDAPMessage *result; - pstring dn; - LDAPMod **mods; - int ldap_op = LDAP_MOD_ADD; + int rc; + pstring filter; + LDAP *ldap_struct; + LDAPMessage *result; + pstring dn; + LDAPMod **mods; + int ldap_op; + uint32 num_result; if (!ldap_open_connection(&ldap_struct)) /* open a connection to the server */ { @@ -959,16 +959,6 @@ BOOL pdb_add_sam_account(SAM_ACCOUNT * newpwd) return False; } - if (pdb_get_username(newpwd) != NULL) { - slprintf (dn, sizeof (dn) - 1, "uid=%s,%s", - pdb_get_username(newpwd), lp_ldap_suffix ()); - } - else - { - return False; - } - - rc = ldap_search_one_user_by_name (ldap_struct, pdb_get_username(newpwd), &result); if (ldap_count_entries(ldap_struct, result) != 0) @@ -982,10 +972,18 @@ BOOL pdb_add_sam_account(SAM_ACCOUNT * newpwd) slprintf (filter, sizeof (filter) - 1, "uid=%s", pdb_get_username(newpwd)); rc = ldap_search_one_user(ldap_struct, filter, &result); - if (ldap_count_entries(ldap_struct, result) == 1) - { + num_result = ldap_count_entries(ldap_struct, result); + + if (num_result > 1) { + DEBUG (0, ("More than one user with that uid exists: bailing out!\n")); + return False; + } + + /* Check if we need to update an existing entry */ + if (num_result == 1) { char *tmp; LDAPMessage *entry; + DEBUG(3,("User exists without samba properties: adding them\n")); ldap_op = LDAP_MOD_REPLACE; entry = ldap_first_entry (ldap_struct, result); @@ -993,10 +991,11 @@ BOOL pdb_add_sam_account(SAM_ACCOUNT * newpwd) slprintf (dn, sizeof (dn) - 1, "%s", tmp); ldap_memfree (tmp); } - else - { - DEBUG (3, ("More than one user with that uid exists: bailing out!\n")); - return False; + else { + /* Check if we need to add an entry */ + DEBUG(3,("Adding new user\n")); + ldap_op = LDAP_MOD_ADD; + slprintf (dn, sizeof (dn) - 1, "uid=%s,%s", pdb_get_username(newpwd), lp_ldap_suffix ()); } ldap_msgfree(result); -- 2.11.4.GIT