Revert "smbd: add an effective connection_struct->user_ev_ctx that holds the event...
[samba.git] / source3 / passdb / pdb_tdb.c
index 43e561bd93bcb87926a6b48fb84d7b830d625eb4..91735ff7084a098eee2ac834c8ee955b920d6577 100644 (file)
@@ -59,6 +59,7 @@ static int tdbsam_debug_level = DBGC_ALL;
 
 static struct db_context *db_sam;
 static char *tdbsam_filename;
+static bool map_builtin;
 
 struct tdbsam_convert_state {
        int32_t from;
@@ -100,27 +101,27 @@ static int tdbsam_convert_one(struct db_record *rec, void *priv)
        switch (state->from) {
        case 0:
                ret = init_samu_from_buffer(user, SAMU_BUFFER_V0,
-                                           (uint8 *)value.dptr,
+                                           (uint8_t *)value.dptr,
                                            value.dsize);
                break;
        case 1:
                ret = init_samu_from_buffer(user, SAMU_BUFFER_V1,
-                                           (uint8 *)value.dptr,
+                                           (uint8_t *)value.dptr,
                                            value.dsize);
                break;
        case 2:
                ret = init_samu_from_buffer(user, SAMU_BUFFER_V2,
-                                           (uint8 *)value.dptr,
+                                           (uint8_t *)value.dptr,
                                            value.dsize);
                break;
        case 3:
                ret = init_samu_from_buffer(user, SAMU_BUFFER_V3,
-                                           (uint8 *)value.dptr,
+                                           (uint8_t *)value.dptr,
                                            value.dsize);
                break;
        case 4:
                ret = init_samu_from_buffer(user, SAMU_BUFFER_V4,
-                                           (uint8 *)value.dptr,
+                                           (uint8_t *)value.dptr,
                                            value.dsize);
                break;
        default:
@@ -225,7 +226,7 @@ static bool tdbsam_convert_backup(const char *dbname, struct db_context **pp_db)
 
        tmp_db = db_open(NULL, tmp_fname, 0,
                         TDB_DEFAULT, O_CREAT|O_RDWR, 0600,
-                        DBWRAP_LOCK_ORDER_1);
+                        DBWRAP_LOCK_ORDER_1, DBWRAP_FLAG_NONE);
        if (tmp_db == NULL) {
                DEBUG(0, ("tdbsam_convert_backup: Failed to create backup TDB passwd "
                          "[%s]\n", tmp_fname));
@@ -292,7 +293,7 @@ static bool tdbsam_convert_backup(const char *dbname, struct db_context **pp_db)
 
        orig_db = db_open(NULL, dbname, 0,
                          TDB_DEFAULT, O_CREAT|O_RDWR, 0600,
-                         DBWRAP_LOCK_ORDER_1);
+                         DBWRAP_LOCK_ORDER_1, DBWRAP_FLAG_NONE);
        if (orig_db == NULL) {
                DEBUG(0, ("tdbsam_convert_backup: Failed to re-open "
                          "converted passdb TDB [%s]\n", dbname));
@@ -325,18 +326,24 @@ static bool tdbsam_convert_backup(const char *dbname, struct db_context **pp_db)
 static bool tdbsam_upgrade_next_rid(struct db_context *db)
 {
        TDB_CONTEXT *tdb;
-       uint32 rid;
+       uint32_t rid;
        bool ok = false;
        NTSTATUS status;
+       char *db_path;
 
        status = dbwrap_fetch_uint32_bystring(db, NEXT_RID_STRING, &rid);
        if (NT_STATUS_IS_OK(status)) {
                return true;
        }
 
-       tdb = tdb_open_log(state_path("winbindd_idmap.tdb"), 0,
-                          TDB_DEFAULT, O_RDONLY, 0644);
+       db_path = state_path(talloc_tos(), "winbindd_idmap.tdb");
+       if (db_path == NULL) {
+               return false;
+       }
 
+       tdb = tdb_open_log(db_path, 0,
+                          TDB_DEFAULT, O_RDONLY, 0644);
+       TALLOC_FREE(db_path);
        if (tdb) {
                ok = tdb_fetch_uint32(tdb, "RID_COUNTER", &rid);
                if (!ok) {
@@ -347,7 +354,7 @@ static bool tdbsam_upgrade_next_rid(struct db_context *db)
                rid = BASE_RID;
        }
 
-       status = dbwrap_store_uint32(db, NEXT_RID_STRING, rid);
+       status = dbwrap_store_uint32_bystring(db, NEXT_RID_STRING, rid);
        if (!NT_STATUS_IS_OK(status)) {
                return false;
        }
@@ -355,7 +362,7 @@ static bool tdbsam_upgrade_next_rid(struct db_context *db)
        return true;
 }
 
-static bool tdbsam_convert(struct db_context **pp_db, const char *name, int32 from)
+static bool tdbsam_convert(struct db_context **pp_db, const char *name, int32_t from)
 {
        struct tdbsam_convert_state state;
        struct db_context *db = NULL;
@@ -430,8 +437,8 @@ static bool tdbsam_convert(struct db_context **pp_db, const char *name, int32 fr
 
 static bool tdbsam_open( const char *name )
 {
-       int32   version;
-       int32   minor_version;
+       int32_t version;
+       int32_t minor_version;
        NTSTATUS status;
 
        /* check if we are already open */
@@ -443,7 +450,7 @@ static bool tdbsam_open( const char *name )
        /* Try to open tdb passwd.  Create a new one if necessary */
 
        db_sam = db_open(NULL, name, 0, TDB_DEFAULT, O_CREAT|O_RDWR, 0600,
-                        DBWRAP_LOCK_ORDER_1);
+                        DBWRAP_LOCK_ORDER_1, DBWRAP_FLAG_NONE);
        if (db_sam == NULL) {
                DEBUG(0, ("tdbsam_open: Failed to open/create TDB passwd "
                          "[%s]\n", name));
@@ -572,10 +579,12 @@ static NTSTATUS tdbsam_getsampwnam (struct pdb_methods *my_methods,
 
        /* Data is stored in all lower-case */
        fstrcpy(name, sname);
-       strlower_m(name);
+       if (!strlower_m(name)) {
+               return NT_STATUS_INVALID_PARAMETER;
+       }
 
        /* set search key */
-       slprintf(keystr, sizeof(keystr)-1, "%s%s", USERPREFIX, name);
+       fstr_sprintf(keystr, "%s%s", USERPREFIX, name);
 
        /* open the database */
 
@@ -593,11 +602,17 @@ static NTSTATUS tdbsam_getsampwnam (struct pdb_methods *my_methods,
                return NT_STATUS_NO_SUCH_USER;
        }
 
+       if (data.dsize == 0) {
+               DEBUG(5, ("%s: Got 0-sized record for key %s\n", __func__,
+                         keystr));
+               return NT_STATUS_NO_SUCH_USER;
+       }
+
        /* unpack the buffer */
 
        if (!init_samu_from_buffer(user, SAMU_BUFFER_LATEST, data.dptr, data.dsize)) {
                DEBUG(0,("pdb_getsampwent: Bad struct samu entry returned from TDB!\n"));
-               SAFE_FREE(data.dptr);
+               TALLOC_FREE(data.dptr);
                return NT_STATUS_NO_MEMORY;
        }
 
@@ -613,7 +628,7 @@ static NTSTATUS tdbsam_getsampwnam (struct pdb_methods *my_methods,
  **************************************************************************/
 
 static NTSTATUS tdbsam_getsampwrid (struct pdb_methods *my_methods,
-                                   struct samu *user, uint32 rid)
+                                   struct samu *user, uint32_t rid)
 {
        NTSTATUS                nt_status = NT_STATUS_UNSUCCESSFUL;
        TDB_DATA                data;
@@ -627,7 +642,7 @@ static NTSTATUS tdbsam_getsampwrid (struct pdb_methods *my_methods,
 
        /* set search key */
 
-       slprintf(keystr, sizeof(keystr)-1, "%s%.8x", RIDPREFIX, rid);
+       fstr_sprintf(keystr, "%s%.8x", RIDPREFIX, rid);
 
        /* open the database */
 
@@ -653,7 +668,7 @@ static NTSTATUS tdbsam_getsampwrid (struct pdb_methods *my_methods,
 static NTSTATUS tdbsam_getsampwsid(struct pdb_methods *my_methods,
                                   struct samu * user, const struct dom_sid *sid)
 {
-       uint32 rid;
+       uint32_t rid;
 
        if ( !sid_peek_check_rid(get_global_sam_sid(), sid, &rid) )
                return NT_STATUS_UNSUCCESSFUL;
@@ -668,11 +683,13 @@ static bool tdb_delete_samacct_only( struct samu *sam_pass )
        NTSTATUS status;
 
        fstrcpy(name, pdb_get_username(sam_pass));
-       strlower_m(name);
+       if (!strlower_m(name)) {
+               return false;
+       }
 
        /* set the search key */
 
-       slprintf(keystr, sizeof(keystr)-1, "%s%s", USERPREFIX, name);
+       fstr_sprintf(keystr, "%s%s", USERPREFIX, name);
 
        /* it's outaa here!  8^) */
        if ( !tdbsam_open( tdbsam_filename ) ) {
@@ -700,7 +717,7 @@ static NTSTATUS tdbsam_delete_sam_account(struct pdb_methods *my_methods,
 {
        NTSTATUS        nt_status = NT_STATUS_UNSUCCESSFUL;
        fstring         keystr;
-       uint32          rid;
+       uint32_t        rid;
        fstring         name;
 
        /* open the database */
@@ -712,11 +729,13 @@ static NTSTATUS tdbsam_delete_sam_account(struct pdb_methods *my_methods,
        }
 
        fstrcpy(name, pdb_get_username(sam_pass));
-       strlower_m(name);
+       if (!strlower_m(name)) {
+               return NT_STATUS_INVALID_PARAMETER;
+       }
 
        /* set the search key */
 
-       slprintf(keystr, sizeof(keystr)-1, "%s%s", USERPREFIX, name);
+       fstr_sprintf(keystr, "%s%s", USERPREFIX, name);
 
        rid = pdb_get_user_rid(sam_pass);
 
@@ -736,7 +755,7 @@ static NTSTATUS tdbsam_delete_sam_account(struct pdb_methods *my_methods,
 
        /* set the search key */
 
-       slprintf(keystr, sizeof(keystr)-1, "%s%.8x", RIDPREFIX, rid);
+       fstr_sprintf(keystr, "%s%.8x", RIDPREFIX, rid);
 
        /* it's outaa here!  8^) */
 
@@ -770,7 +789,7 @@ static NTSTATUS tdbsam_delete_sam_account(struct pdb_methods *my_methods,
 static bool tdb_update_samacct_only( struct samu* newpwd, int flag )
 {
        TDB_DATA        data;
-       uint8           *buf = NULL;
+       uint8_t         *buf = NULL;
        fstring         keystr;
        fstring         name;
        bool            ret = false;
@@ -785,14 +804,16 @@ static bool tdb_update_samacct_only( struct samu* newpwd, int flag )
        data.dptr = buf;
 
        fstrcpy(name, pdb_get_username(newpwd));
-       strlower_m(name);
+       if (!strlower_m(name)) {
+               goto done;
+       }
 
        DEBUG(5, ("Storing %saccount %s with RID %d\n",
                  flag == TDB_INSERT ? "(new) " : "", name,
                  pdb_get_user_rid(newpwd)));
 
        /* setup the USER index key */
-       slprintf(keystr, sizeof(keystr)-1, "%s%s", USERPREFIX, name);
+       fstr_sprintf(keystr, "%s%s", USERPREFIX, name);
 
        /* add the account */
 
@@ -823,14 +844,15 @@ static bool tdb_update_ridrec_only( struct samu* newpwd, int flag )
        NTSTATUS status;
 
        fstrcpy(name, pdb_get_username(newpwd));
-       strlower_m(name);
+       if (!strlower_m(name)) {
+               return false;
+       }
 
        /* setup RID data */
        data = string_term_tdb_data(name);
 
        /* setup the RID index key */
-       slprintf(keystr, sizeof(keystr)-1, "%s%.8x", RIDPREFIX,
-                pdb_get_user_rid(newpwd));
+       fstr_sprintf(keystr, "%s%.8x", RIDPREFIX, pdb_get_user_rid(newpwd));
 
        /* add the reference */
        status = dbwrap_store_bystring(db_sam, keystr, data, flag);
@@ -910,7 +932,7 @@ static bool tdb_update_sam(struct pdb_methods *my_methods, struct samu* newpwd,
 
                /* Delete old RID key */
                DEBUG(10, ("tdb_update_sam: Deleting key for RID %u\n", oldrid));
-               slprintf(keystr, sizeof(keystr) - 1, "%s%.8x", RIDPREFIX, oldrid);
+               fstr_sprintf(keystr, "%s%.8x", RIDPREFIX, oldrid);
                if (!NT_STATUS_IS_OK(dbwrap_delete_bystring(db_sam, keystr))) {
                        DEBUG(0, ("tdb_update_sam: Can't delete %s\n", keystr));
                        goto cancel;
@@ -991,7 +1013,7 @@ static NTSTATUS tdbsam_rename_sam_account(struct pdb_methods *my_methods,
                return NT_STATUS_NO_MEMORY;
        }
 
-       rename_script = talloc_strdup(new_acct, lp_renameuser_script());
+       rename_script = lp_rename_user_script(new_acct);
        if (!rename_script) {
                TALLOC_FREE(new_acct);
                return NT_STATUS_NO_MEMORY;
@@ -1032,10 +1054,14 @@ static NTSTATUS tdbsam_rename_sam_account(struct pdb_methods *my_methods,
           so that we lower case the posix name but preserve the case in passdb */
 
        fstrcpy( oldname_lower, pdb_get_username(old_acct) );
-       strlower_m( oldname_lower );
+       if (!strlower_m( oldname_lower )) {
+               goto cancel;
+       }
 
        fstrcpy( newname_lower, newname );
-       strlower_m( newname_lower );
+       if (!strlower_m( newname_lower )) {
+               goto cancel;
+       }
 
        rename_script = talloc_string_sub2(new_acct,
                                rename_script,
@@ -1057,7 +1083,7 @@ static NTSTATUS tdbsam_rename_sam_account(struct pdb_methods *my_methods,
        if (!rename_script) {
                goto cancel;
        }
-       rename_ret = smbrun(rename_script, NULL);
+       rename_ret = smbrun(rename_script, NULL, NULL);
 
        DEBUG(rename_ret ? 0 : 3,("Running the command `%s' gave %d\n",
                                rename_script, rename_ret));
@@ -1105,9 +1131,9 @@ static uint32_t tdbsam_capabilities(struct pdb_methods *methods)
        return PDB_CAP_STORE_RIDS;
 }
 
-static bool tdbsam_new_rid(struct pdb_methods *methods, uint32 *prid)
+static bool tdbsam_new_rid(struct pdb_methods *methods, uint32_t *prid)
 {
-       uint32 rid;
+       uint32_t rid;
        NTSTATUS status;
 
        rid = BASE_RID;         /* Default if not set */
@@ -1118,8 +1144,8 @@ static bool tdbsam_new_rid(struct pdb_methods *methods, uint32 *prid)
                return false;
        }
 
-       status = dbwrap_trans_change_uint32_atomic(db_sam, NEXT_RID_STRING,
-                                                  &rid, 1);
+       status = dbwrap_trans_change_uint32_atomic_bystring(
+               db_sam, NEXT_RID_STRING, &rid, 1);
        if (!NT_STATUS_IS_OK(status)) {
                DEBUG(3, ("tdbsam_new_rid: Failed to increase %s: %s\n",
                        NEXT_RID_STRING, nt_errstr(status)));
@@ -1146,7 +1172,7 @@ static int tdbsam_collect_rids(struct db_record *rec, void *private_data)
        struct tdbsam_search_state *state = talloc_get_type_abort(
                private_data, struct tdbsam_search_state);
        size_t prefixlen = strlen(RIDPREFIX);
-       uint32 rid;
+       uint32_t rid;
        TDB_DATA key;
 
        key = dbwrap_record_get_key(rec);
@@ -1158,7 +1184,7 @@ static int tdbsam_collect_rids(struct db_record *rec, void *private_data)
 
        rid = strtoul((char *)key.dptr+prefixlen, NULL, 16);
 
-       ADD_TO_LARGE_ARRAY(state, uint32, rid, &state->rids, &state->num_rids,
+       ADD_TO_LARGE_ARRAY(state, uint32_t, rid, &state->rids, &state->num_rids,
                           &state->array_size);
 
        return 0;
@@ -1189,6 +1215,7 @@ static bool tdbsam_search_next_entry(struct pdb_search *search,
        }
 
        if (state->current == state->num_rids) {
+               TALLOC_FREE(user);
                return false;
        }
 
@@ -1234,7 +1261,7 @@ static bool tdbsam_search_next_entry(struct pdb_search *search,
 
 static bool tdbsam_search_users(struct pdb_methods *methods,
                                struct pdb_search *search,
-                               uint32 acct_flags)
+                               uint32_t acct_flags)
 {
        struct tdbsam_search_state *state;
 
@@ -1261,6 +1288,11 @@ static bool tdbsam_search_users(struct pdb_methods *methods,
        return true;
 }
 
+static bool tdbsam_is_responsible_for_builtin(struct pdb_methods *m)
+{
+       return map_builtin;
+}
+
 /*********************************************************************
  Initialize the tdb sam backend.  Setup the dispath table of methods,
  open the tdb, etc...
@@ -1289,6 +1321,10 @@ static NTSTATUS pdb_init_tdbsam(struct pdb_methods **pdb_method, const char *loc
        (*pdb_method)->capabilities = tdbsam_capabilities;
        (*pdb_method)->new_rid = tdbsam_new_rid;
 
+       (*pdb_method)->is_responsible_for_builtin =
+                                       tdbsam_is_responsible_for_builtin;
+       map_builtin = lp_parm_bool(-1, "tdbsam", "map builtin", true);
+
        /* save the path for later */
 
        if (!location) {
@@ -1312,7 +1348,7 @@ static NTSTATUS pdb_init_tdbsam(struct pdb_methods **pdb_method, const char *loc
        return NT_STATUS_OK;
 }
 
-NTSTATUS pdb_tdbsam_init(void)
+NTSTATUS pdb_tdbsam_init(TALLOC_CTX *ctx)
 {
        return smb_register_passdb(PASSDB_INTERFACE_VERSION, "tdbsam", pdb_init_tdbsam);
 }