Ignore:
Timestamp:
Nov 27, 2012, 4:43:17 PM (13 years ago)
Author:
Silvan Scherrer
Message:

Samba Server: updated trunk to 3.6.0

Location:
trunk/server
Files:
1 deleted
4 edited
1 copied

Legend:

Unmodified
Added
Removed
  • trunk/server

  • trunk/server/source3/groupdb/mapping.c

    r414 r745  
    2222
    2323#include "includes.h"
     24#include "system/passwd.h"
     25#include "passdb.h"
    2426#include "groupdb/mapping.h"
     27#include "../libcli/security/security.h"
     28#include "lib/winbind_util.h"
     29#include <tdb.h>
    2530
    2631static const struct mapping_backend *backend;
     
    3136static bool init_group_mapping(void)
    3237{
    33         const char *backend_string;
    34 
    3538        if (backend != NULL) {
    3639                /* already initialised */
     
    3841        }
    3942
    40         /*
    41          * default to using the ldb backend. This parameter should
    42          * disappear in future versions of Samba3.
    43          *
    44          * But it's needed for cluster setups, because it's
    45          * not yet possible to distribute a ldb inside a cluster.
    46          */
    47         backend_string = lp_parm_const_string(-1, "groupdb", "backend", "ldb");
    48 
    49         if (strcmp(backend_string, "ldb") == 0) {
    50                 backend = groupdb_ldb_init();
    51         } else if (strcmp(backend_string, "tdb") == 0) {
    52                 backend = groupdb_tdb_init();
    53         } else {
    54                 DEBUG(0,("Unknown groupdb backend '%s'\n", backend_string));
    55                 smb_panic("Unknown groupdb backend");
    56         }
     43        backend = groupdb_tdb_init();
    5744
    5845        return backend != NULL;
     
    7057                return NT_STATUS_UNSUCCESSFUL;
    7158        }
    72        
     59
    7360        map.gid=gid;
    7461        if (!string_to_sid(&map.sid, sid)) {
     
    7663                return NT_STATUS_UNSUCCESSFUL;
    7764        }
    78        
     65
    7966        map.sid_name_use=sid_name_use;
    8067        fstrcpy(map.nt_name, nt_name);
     
    8471}
    8572
    86 static NTSTATUS alias_memberships(const DOM_SID *members, size_t num_members,
    87                                   DOM_SID **sids, size_t *num)
     73static NTSTATUS alias_memberships(const struct dom_sid *members, size_t num_members,
     74                                  struct dom_sid **sids, size_t *num)
    8875{
    8976        size_t i;
     
    10188
    10289struct aliasmem_closure {
    103         const DOM_SID *alias;
    104         DOM_SID **sids;
     90        const struct dom_sid *alias;
     91        struct dom_sid **sids;
    10592        size_t *num;
    10693};
     
    120107/* get a domain group from it's SID */
    121108
    122 bool get_domain_group_from_sid(DOM_SID sid, GROUP_MAP *map)
     109bool get_domain_group_from_sid(struct dom_sid sid, GROUP_MAP *map)
    123110{
    124111        struct group *grp;
    125112        bool ret;
    126        
     113
    127114        if(!init_group_mapping()) {
    128115                DEBUG(0,("failed to initialize group mapping\n"));
     
    133120
    134121        /* if the group is NOT in the database, it CAN NOT be a domain group */
    135        
     122
    136123        become_root();
    137124        ret = pdb_getgrsid(map, sid);
    138125        unbecome_root();
    139        
     126
    140127        /* special case check for rid 513 */
    141        
     128
    142129        if ( !ret ) {
    143130                uint32 rid;
    144                
     131
    145132                sid_peek_rid( &sid, &rid );
    146                
    147                 if ( rid == DOMAIN_GROUP_RID_USERS ) {
     133
     134                if ( rid == DOMAIN_RID_USERS ) {
    148135                        fstrcpy( map->nt_name, "None" );
    149136                        fstrcpy( map->comment, "Ordinary Users" );
     
    151138                        map->sid_name_use = SID_NAME_DOM_GRP;
    152139                        map->gid = (gid_t)-1;
    153                        
    154140                        return True;
    155141                }
    156                
    157142                return False;
    158143        }
    159144
    160         DEBUG(10, ("get_domain_group_from_sid: SID found in the TDB\n"));
     145        DEBUG(10, ("get_domain_group_from_sid: SID found in passdb\n"));
    161146
    162147        /* if it's not a domain group, continue */
     
    166151
    167152        DEBUG(10, ("get_domain_group_from_sid: SID is a domain group\n"));
    168        
     153
    169154        if (map->gid==-1) {
    170155                return False;
     
    172157
    173158        DEBUG(10, ("get_domain_group_from_sid: SID is mapped to gid:%lu\n",(unsigned long)map->gid));
    174        
     159
    175160        grp = getgrgid(map->gid);
    176161        if ( !grp ) {
     
    344329                        return -1;
    345330                }
    346                 add_script = talloc_string_sub(ctx,
    347                                 add_script, "%u", unix_user);
     331                add_script = talloc_string_sub2(ctx,
     332                                add_script, "%u", unix_user, true, false, true);
    348333                if (!add_script) {
    349334                        return -1;
     
    384369                        return -1;
    385370                }
    386                 del_script = talloc_string_sub(ctx,
    387                                 del_script, "%u", unix_user);
     371                del_script = talloc_string_sub2(ctx,
     372                                del_script, "%u", unix_user, true, false, true);
    388373                if (!del_script) {
    389374                        return -1;
     
    402387
    403388NTSTATUS pdb_default_getgrsid(struct pdb_methods *methods, GROUP_MAP *map,
    404                                  DOM_SID sid)
     389                                 struct dom_sid sid)
    405390{
    406391        if (!init_group_mapping()) {
     
    457442
    458443NTSTATUS pdb_default_delete_group_mapping_entry(struct pdb_methods *methods,
    459                                                    DOM_SID sid)
     444                                                   struct dom_sid sid)
    460445{
    461446        if (!init_group_mapping()) {
     
    468453
    469454NTSTATUS pdb_default_enum_group_mapping(struct pdb_methods *methods,
    470                                            const DOM_SID *sid, enum lsa_SidType sid_name_use,
     455                                           const struct dom_sid *sid, enum lsa_SidType sid_name_use,
    471456                                           GROUP_MAP **pp_rmap, size_t *p_num_entries,
    472457                                           bool unix_only)
     
    483468                                  const char *name, uint32 *rid)
    484469{
    485         DOM_SID sid;
     470        struct dom_sid sid;
    486471        enum lsa_SidType type;
    487472        uint32 new_rid;
     
    507492        }
    508493
     494        if (!pdb_new_rid(&new_rid)) {
     495                DEBUG(0, ("Could not allocate a RID.\n"));
     496                return NT_STATUS_ACCESS_DENIED;
     497        }
     498
     499        sid_compose(&sid, get_global_sam_sid(), new_rid);
     500
    509501        if (!winbind_allocate_gid(&gid)) {
    510                 DEBUG(3, ("Could not get a gid out of winbind\n"));
    511                 return NT_STATUS_ACCESS_DENIED;
    512         }
    513 
    514         if (!pdb_new_rid(&new_rid)) {
    515                 DEBUG(0, ("Could not allocate a RID -- wasted a gid :-(\n"));
     502                DEBUG(3, ("Could not get a gid out of winbind - "
     503                          "wasted a rid :-(\n"));
    516504                return NT_STATUS_ACCESS_DENIED;
    517505        }
     
    519507        DEBUG(10, ("Creating alias %s with gid %u and rid %u\n",
    520508                   name, (unsigned int)gid, (unsigned int)new_rid));
    521 
    522         sid_copy(&sid, get_global_sam_sid());
    523         sid_append_rid(&sid, new_rid);
    524509
    525510        map.gid = gid;
     
    543528
    544529NTSTATUS pdb_default_delete_alias(struct pdb_methods *methods,
    545                                   const DOM_SID *sid)
     530                                  const struct dom_sid *sid)
    546531{
    547532        return pdb_delete_group_mapping_entry(*sid);
     
    549534
    550535NTSTATUS pdb_default_get_aliasinfo(struct pdb_methods *methods,
    551                                    const DOM_SID *sid,
     536                                   const struct dom_sid *sid,
    552537                                   struct acct_info *info)
    553538{
     
    572557
    573558NTSTATUS pdb_default_set_aliasinfo(struct pdb_methods *methods,
    574                                    const DOM_SID *sid,
     559                                   const struct dom_sid *sid,
    575560                                   struct acct_info *info)
    576561{
     
    587572
    588573NTSTATUS pdb_default_add_aliasmem(struct pdb_methods *methods,
    589                                   const DOM_SID *alias, const DOM_SID *member)
     574                                  const struct dom_sid *alias, const struct dom_sid *member)
    590575{
    591576        if (!init_group_mapping()) {
     
    597582
    598583NTSTATUS pdb_default_del_aliasmem(struct pdb_methods *methods,
    599                                   const DOM_SID *alias, const DOM_SID *member)
     584                                  const struct dom_sid *alias, const struct dom_sid *member)
    600585{
    601586        if (!init_group_mapping()) {
     
    607592
    608593NTSTATUS pdb_default_enum_aliasmem(struct pdb_methods *methods,
    609                                    const DOM_SID *alias, TALLOC_CTX *mem_ctx,
    610                                    DOM_SID **pp_members, size_t *p_num_members)
     594                                   const struct dom_sid *alias, TALLOC_CTX *mem_ctx,
     595                                   struct dom_sid **pp_members, size_t *p_num_members)
    611596{
    612597        if (!init_group_mapping()) {
     
    620605NTSTATUS pdb_default_alias_memberships(struct pdb_methods *methods,
    621606                                       TALLOC_CTX *mem_ctx,
    622                                        const DOM_SID *domain_sid,
    623                                        const DOM_SID *members,
     607                                       const struct dom_sid *domain_sid,
     608                                       const struct dom_sid *members,
    624609                                       size_t num_members,
    625610                                       uint32 **pp_alias_rids,
    626611                                       size_t *p_num_alias_rids)
    627612{
    628         DOM_SID *alias_sids;
     613        struct dom_sid *alias_sids;
    629614        size_t i, num_alias_sids;
    630615        NTSTATUS result;
     
    672657
    673658NTSTATUS pdb_nop_getgrsid(struct pdb_methods *methods, GROUP_MAP *map,
    674                                  DOM_SID sid)
     659                                 struct dom_sid sid)
    675660{
    676661        return NT_STATUS_UNSUCCESSFUL;
     
    702687
    703688NTSTATUS pdb_nop_delete_group_mapping_entry(struct pdb_methods *methods,
    704                                                    DOM_SID sid)
     689                                                   struct dom_sid sid)
    705690{
    706691        return NT_STATUS_UNSUCCESSFUL;
     
    718703 These need to be redirected through pdb_interface.c
    719704****************************************************************************/
    720 bool pdb_get_dom_grp_info(const DOM_SID *sid, struct acct_info *info)
     705bool pdb_get_dom_grp_info(const struct dom_sid *sid, struct acct_info *info)
    721706{
    722707        GROUP_MAP map;
     
    736721}
    737722
    738 bool pdb_set_dom_grp_info(const DOM_SID *sid, const struct acct_info *info)
     723bool pdb_set_dom_grp_info(const struct dom_sid *sid, const struct acct_info *info)
    739724{
    740725        GROUP_MAP map;
     
    755740NTSTATUS pdb_create_builtin_alias(uint32 rid)
    756741{
    757         DOM_SID sid;
     742        struct dom_sid sid;
    758743        enum lsa_SidType type;
    759744        gid_t gid;
     
    765750
    766751        DEBUG(10, ("Trying to create builtin alias %d\n", rid));
    767        
     752
    768753        if ( !sid_compose( &sid, &global_sid_Builtin, rid ) ) {
    769754                return NT_STATUS_NO_SUCH_ALIAS;
    770755        }
    771        
     756
    772757        if ( (mem_ctx = talloc_new(NULL)) == NULL ) {
    773758                return NT_STATUS_NO_MEMORY;
    774759        }
    775        
     760
    776761        if ( !lookup_sid(mem_ctx, &sid, NULL, &name, &type) ) {
    777762                TALLOC_FREE( mem_ctx );
    778763                return NT_STATUS_NO_SUCH_ALIAS;
    779764        }
    780        
     765
    781766        /* validate RID so copy the name and move on */
    782                
     767
    783768        fstrcpy( groupname, name );
    784769        TALLOC_FREE( mem_ctx );
  • trunk/server/source3/groupdb/mapping.h

    r414 r745  
     1/*
     2 *  Unix SMB/CIFS implementation.
     3 *  RPC Pipe client / server routines
     4 *  Copyright (C) Andrew Tridgell              1992-2000,
     5 *  Copyright (C) Jean François Micouleau      1998-2001.
     6 *  Copyright (C) Volker Lendecke              2006.
     7 *  Copyright (C) Gerald Carter                2006.
     8 *
     9 *  This program is free software; you can redistribute it and/or modify
     10 *  it under the terms of the GNU General Public License as published by
     11 *  the Free Software Foundation; either version 3 of the License, or
     12 *  (at your option) any later version.
     13 *
     14 *  This program is distributed in the hope that it will be useful,
     15 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
     16 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     17 *  GNU General Public License for more details.
     18 *
     19 *  You should have received a copy of the GNU General Public License
     20 *  along with this program; if not, see <http://www.gnu.org/licenses/>.
     21 */
     22
    123#define DATABASE_VERSION_V1 1 /* native byte format. */
    224#define DATABASE_VERSION_V2 2 /* le format. */
     
    1941        bool (*init_group_mapping)(void);
    2042        bool (*add_mapping_entry)(GROUP_MAP *map, int flag);
    21         bool (*get_group_map_from_sid)(DOM_SID sid, GROUP_MAP *map);
     43        bool (*get_group_map_from_sid)(struct dom_sid sid, GROUP_MAP *map);
    2244        bool (*get_group_map_from_gid)(gid_t gid, GROUP_MAP *map);
    2345        bool (*get_group_map_from_ntname)(const char *name, GROUP_MAP *map);
    24         bool (*group_map_remove)(const DOM_SID *sid);
    25         bool (*enum_group_mapping)(const DOM_SID *domsid, enum lsa_SidType sid_name_use,
     46        bool (*group_map_remove)(const struct dom_sid *sid);
     47        bool (*enum_group_mapping)(const struct dom_sid *domsid, enum lsa_SidType sid_name_use,
    2648                                   GROUP_MAP **pp_rmap,
    2749                                   size_t *p_num_entries, bool unix_only);
    28         NTSTATUS (*one_alias_membership)(const DOM_SID *member,
    29                                          DOM_SID **sids, size_t *num);
    30         NTSTATUS (*add_aliasmem)(const DOM_SID *alias, const DOM_SID *member);
    31         NTSTATUS (*del_aliasmem)(const DOM_SID *alias, const DOM_SID *member);
    32         NTSTATUS (*enum_aliasmem)(const DOM_SID *alias, TALLOC_CTX *mem_ctx,
    33                                   DOM_SID **sids, size_t *num);
     50        NTSTATUS (*one_alias_membership)(const struct dom_sid *member,
     51                                         struct dom_sid **sids, size_t *num);
     52        NTSTATUS (*add_aliasmem)(const struct dom_sid *alias, const struct dom_sid *member);
     53        NTSTATUS (*del_aliasmem)(const struct dom_sid *alias, const struct dom_sid *member);
     54        NTSTATUS (*enum_aliasmem)(const struct dom_sid *alias, TALLOC_CTX *mem_ctx,
     55                                  struct dom_sid **sids, size_t *num);
    3456};
  • trunk/server/source3/groupdb/mapping_tdb.c

    r414 r745  
    2222
    2323#include "includes.h"
     24#include "system/filesys.h"
     25#include "passdb.h"
    2426#include "groupdb/mapping.h"
     27#include "dbwrap.h"
     28#include "util_tdb.h"
     29#include "../libcli/security/security.h"
    2530
    2631static struct db_context *db; /* used for driver files */
    2732
    28 static bool enum_group_mapping(const DOM_SID *domsid, enum lsa_SidType sid_name_use, GROUP_MAP **pp_rmap,
    29                                size_t *p_num_entries, bool unix_only);
    30 static bool group_map_remove(const DOM_SID *sid);
    31        
     33static bool enum_group_mapping(const struct dom_sid *domsid,
     34                               enum lsa_SidType sid_name_use,
     35                               GROUP_MAP **pp_rmap,
     36                               size_t *p_num_entries,
     37                               bool unix_only);
     38static bool group_map_remove(const struct dom_sid *sid);
     39
     40static bool mapping_switch(const char *ldb_path);
     41
    3242/****************************************************************************
    3343 Open the group mapping tdb.
     
    3545static bool init_group_mapping(void)
    3646{
     47        const char *ldb_path;
     48
    3749        if (db != NULL) {
    3850                return true;
     
    4759        }
    4860
    49 #if 0
    50         /*
    51          * This code was designed to handle a group mapping version
    52          * upgrade. mapping_tdb is not active by default anymore, so ignore
    53          * this here.
    54          */
    55         {
     61        ldb_path = state_path("group_mapping.ldb");
     62        if (file_exist(ldb_path) && !mapping_switch(ldb_path)) {
     63                unlink(state_path("group_mapping.tdb"));
     64                return false;
     65
     66        } else {
     67                /* handle upgrade from old versions of the database */
     68#if 0 /* -- Needs conversion to dbwrap -- */
    5669                const char *vstring = "INFO/version";
    5770                int32 vers_id;
     
    97110                        SAFE_FREE( map_table );
    98111                }
    99         }
    100112#endif
    101 
     113        }
    102114        return true;
    103115}
    104116
    105 static char *group_mapping_key(TALLOC_CTX *mem_ctx, const DOM_SID *sid)
     117static char *group_mapping_key(TALLOC_CTX *mem_ctx, const struct dom_sid *sid)
    106118{
    107119        char *sidstr, *result;
     
    156168****************************************************************************/
    157169
    158 static bool get_group_map_from_sid(DOM_SID sid, GROUP_MAP *map)
     170static bool get_group_map_from_sid(struct dom_sid sid, GROUP_MAP *map)
    159171{
    160172        TDB_DATA dbuf;
     
    280292****************************************************************************/
    281293
    282 static bool group_map_remove(const DOM_SID *sid)
     294static bool group_map_remove(const struct dom_sid *sid)
    283295{
    284296        char *key;
     
    301313
    302314struct enum_map_state {
    303         const DOM_SID *domsid;
     315        const struct dom_sid *domsid;
    304316        enum lsa_SidType sid_name_use;
    305317        bool unix_only;
     
    333345
    334346        if ((state->domsid != NULL) &&
    335             (sid_compare_domain(state->domsid, &map.sid) != 0)) {
     347            (dom_sid_compare_domain(state->domsid, &map.sid) != 0)) {
    336348                DEBUG(11,("enum_group_mapping: group %s is not in domain\n",
    337349                          sid_string_dbg(&map.sid)));
     
    352364}
    353365
    354 static bool enum_group_mapping(const DOM_SID *domsid,
     366static bool enum_group_mapping(const struct dom_sid *domsid,
    355367                               enum lsa_SidType sid_name_use,
    356368                               GROUP_MAP **pp_rmap,
     
    378390 * store a list of aliases a SID is member of hanging off MEMBEROF/SID. */
    379391
    380 static NTSTATUS one_alias_membership(const DOM_SID *member,
    381                                DOM_SID **sids, size_t *num)
     392static NTSTATUS one_alias_membership(const struct dom_sid *member,
     393                               struct dom_sid **sids, size_t *num)
    382394{
    383395        fstring tmp;
     
    401413
    402414        while (next_token_talloc(frame, &p, &string_sid, " ")) {
    403                 DOM_SID alias;
     415                struct dom_sid alias;
     416                uint32_t num_sids;
    404417
    405418                if (!string_to_sid(&alias, string_sid))
    406419                        continue;
    407420
    408                 status= add_sid_to_array_unique(NULL, &alias, sids, num);
     421                num_sids = *num;
     422                status= add_sid_to_array_unique(NULL, &alias, sids, &num_sids);
    409423                if (!NT_STATUS_IS_OK(status)) {
    410424                        goto done;
    411425                }
     426                *num = num_sids;
    412427        }
    413428
     
    417432}
    418433
    419 static NTSTATUS alias_memberships(const DOM_SID *members, size_t num_members,
    420                                   DOM_SID **sids, size_t *num)
     434static NTSTATUS alias_memberships(const struct dom_sid *members, size_t num_members,
     435                                  struct dom_sid **sids, size_t *num)
    421436{
    422437        size_t i;
     
    433448}
    434449
    435 static bool is_aliasmem(const DOM_SID *alias, const DOM_SID *member)
    436 {
    437         DOM_SID *sids;
    438         size_t i, num;
     450static bool is_aliasmem(const struct dom_sid *alias, const struct dom_sid *member)
     451{
     452        struct dom_sid *sids;
     453        size_t i;
     454        size_t num;
    439455
    440456        /* This feels the wrong way round, but the on-disk data structure
     
    444460
    445461        for (i=0; i<num; i++) {
    446                 if (sid_compare(alias, &sids[i]) == 0) {
     462                if (dom_sid_compare(alias, &sids[i]) == 0) {
    447463                        TALLOC_FREE(sids);
    448464                        return True;
     
    454470
    455471
    456 static NTSTATUS add_aliasmem(const DOM_SID *alias, const DOM_SID *member)
     472static NTSTATUS add_aliasmem(const struct dom_sid *alias, const struct dom_sid *member)
    457473{
    458474        GROUP_MAP map;
     
    537553struct aliasmem_state {
    538554        TALLOC_CTX *mem_ctx;
    539         const DOM_SID *alias;
    540         DOM_SID **sids;
     555        const struct dom_sid *alias;
     556        struct dom_sid **sids;
    541557        size_t *num;
    542558};
     
    558574
    559575        while (next_token_talloc(frame, &p, &alias_string, " ")) {
    560                 DOM_SID alias, member;
     576                struct dom_sid alias, member;
    561577                const char *member_string;
     578                uint32_t num_sids;
    562579
    563580                if (!string_to_sid(&alias, alias_string))
    564581                        continue;
    565582
    566                 if (sid_compare(state->alias, &alias) != 0)
     583                if (dom_sid_compare(state->alias, &alias) != 0)
    567584                        continue;
    568585
     
    582599                        continue;
    583600
     601                num_sids = *state->num;
    584602                if (!NT_STATUS_IS_OK(add_sid_to_array(state->mem_ctx, &member,
    585603                                                      state->sids,
    586                                                       state->num)))
     604                                                      &num_sids)))
    587605                {
    588606                        /* talloc fail. */
    589607                        break;
    590608                }
     609                *state->num = num_sids;
    591610        }
    592611
     
    595614}
    596615
    597 static NTSTATUS enum_aliasmem(const DOM_SID *alias, TALLOC_CTX *mem_ctx,
    598                               DOM_SID **sids, size_t *num)
     616static NTSTATUS enum_aliasmem(const struct dom_sid *alias, TALLOC_CTX *mem_ctx,
     617                              struct dom_sid **sids, size_t *num)
    599618{
    600619        GROUP_MAP map;
     
    620639}
    621640
    622 static NTSTATUS del_aliasmem(const DOM_SID *alias, const DOM_SID *member)
     641static NTSTATUS del_aliasmem(const struct dom_sid *alias, const struct dom_sid *member)
    623642{
    624643        NTSTATUS status;
    625         DOM_SID *sids;
     644        struct dom_sid *sids;
    626645        size_t i, num;
    627646        bool found = False;
     
    642661
    643662        for (i=0; i<num; i++) {
    644                 if (sid_compare(&sids[i], alias) == 0) {
     663                if (dom_sid_compare(&sids[i], alias) == 0) {
    645664                        found = True;
    646665                        break;
     
    718737        }
    719738        return status;
     739}
     740
     741
     742/* -- ldb->tdb switching code -------------------------------------------- */
     743
     744/* change this if the data format ever changes */
     745#define LTDB_PACKING_FORMAT 0x26011967
     746
     747/* old packing formats (not supported for now,
     748 * it was never used for group mapping AFAIK) */
     749#define LTDB_PACKING_FORMAT_NODN 0x26011966
     750
     751static unsigned int pull_uint32(uint8_t *p, int ofs)
     752{
     753        p += ofs;
     754        return p[0] | (p[1]<<8) | (p[2]<<16) | (p[3]<<24);
     755}
     756
     757/*
     758  unpack a ldb message from a linear buffer in TDB_DATA
     759*/
     760static int convert_ldb_record(TDB_CONTEXT *ltdb, TDB_DATA key,
     761                              TDB_DATA data, void *ptr)
     762{
     763        TALLOC_CTX *tmp_ctx = talloc_tos();
     764        GROUP_MAP map;
     765        uint8_t *p;
     766        uint32_t format;
     767        uint32_t num_el;
     768        unsigned int remaining;
     769        unsigned int i, j;
     770        size_t len;
     771        char *name;
     772        char *val;
     773        char *q;
     774        uint32_t num_mem = 0;
     775        struct dom_sid *members = NULL;
     776
     777        p = (uint8_t *)data.dptr;
     778        if (data.dsize < 8) {
     779                errno = EIO;
     780                goto failed;
     781        }
     782
     783        format = pull_uint32(p, 0);
     784        num_el = pull_uint32(p, 4);
     785        p += 8;
     786
     787        remaining = data.dsize - 8;
     788
     789        switch (format) {
     790        case LTDB_PACKING_FORMAT:
     791                len = strnlen((char *)p, remaining);
     792                if (len == remaining) {
     793                        errno = EIO;
     794                        goto failed;
     795                }
     796
     797                if (*p == '@') {
     798                        /* ignore special LDB attributes */
     799                        return 0;
     800                }
     801
     802                if (strncmp((char *)p, "rid=", 4)) {
     803                        /* unknown entry, ignore */
     804                        DEBUG(3, ("Found unknown entry in group mapping "
     805                                  "database named [%s]\n", (char *)p));
     806                        return 0;
     807                }
     808
     809                remaining -= len + 1;
     810                p += len + 1;
     811                break;
     812
     813        case LTDB_PACKING_FORMAT_NODN:
     814        default:
     815                errno = EIO;
     816                goto failed;
     817        }
     818
     819        if (num_el == 0) {
     820                /* bad entry, ignore */
     821                return 0;
     822        }
     823
     824        if (num_el > remaining / 6) {
     825                errno = EIO;
     826                goto failed;
     827        }
     828
     829        ZERO_STRUCT(map);
     830
     831        for (i = 0; i < num_el; i++) {
     832                uint32_t num_vals;
     833
     834                if (remaining < 10) {
     835                        errno = EIO;
     836                        goto failed;
     837                }
     838                len = strnlen((char *)p, remaining - 6);
     839                if (len == remaining - 6) {
     840                        errno = EIO;
     841                        goto failed;
     842                }
     843                name = talloc_strndup(tmp_ctx, (char *)p, len);
     844                if (name == NULL) {
     845                        errno = ENOMEM;
     846                        goto failed;
     847                }
     848                remaining -= len + 1;
     849                p += len + 1;
     850
     851                num_vals = pull_uint32(p, 0);
     852                if (StrCaseCmp(name, "member") == 0) {
     853                        num_mem = num_vals;
     854                        members = talloc_array(tmp_ctx, struct dom_sid, num_mem);
     855                        if (members == NULL) {
     856                                errno = ENOMEM;
     857                                goto failed;
     858                        }
     859                } else if (num_vals != 1) {
     860                        errno = EIO;
     861                        goto failed;
     862                }
     863
     864                p += 4;
     865                remaining -= 4;
     866
     867                for (j = 0; j < num_vals; j++) {
     868                        len = pull_uint32(p, 0);
     869                        if (len > remaining-5) {
     870                                errno = EIO;
     871                                goto failed;
     872                        }
     873
     874                        val = talloc_strndup(tmp_ctx, (char *)(p + 4), len);
     875                        if (val == NULL) {
     876                                errno = ENOMEM;
     877                                goto failed;
     878                        }
     879
     880                        remaining -= len+4+1;
     881                        p += len+4+1;
     882
     883                        /* we ignore unknown or uninteresting attributes
     884                         * (objectclass, etc.) */
     885                        if (StrCaseCmp(name, "gidNumber") == 0) {
     886                                map.gid = strtoul(val, &q, 10);
     887                                if (*q) {
     888                                        errno = EIO;
     889                                        goto failed;
     890                                }
     891                        } else if (StrCaseCmp(name, "sid") == 0) {
     892                                if (!string_to_sid(&map.sid, val)) {
     893                                        errno = EIO;
     894                                        goto failed;
     895                                }
     896                        } else if (StrCaseCmp(name, "sidNameUse") == 0) {
     897                                map.sid_name_use = strtoul(val, &q, 10);
     898                                if (*q) {
     899                                        errno = EIO;
     900                                        goto failed;
     901                                }
     902                        } else if (StrCaseCmp(name, "ntname") == 0) {
     903                                strlcpy(map.nt_name, val,
     904                                        sizeof(map.nt_name));
     905                        } else if (StrCaseCmp(name, "comment") == 0) {
     906                                strlcpy(map.comment, val,
     907                                        sizeof(map.comment));
     908                        } else if (StrCaseCmp(name, "member") == 0) {
     909                                if (!string_to_sid(&members[j], val)) {
     910                                        errno = EIO;
     911                                        goto failed;
     912                                }
     913                        }
     914
     915                        TALLOC_FREE(val);
     916                }
     917
     918                TALLOC_FREE(name);
     919        }
     920
     921        if (!add_mapping_entry(&map, 0)) {
     922                errno = EIO;
     923                goto failed;
     924        }
     925
     926        if (num_mem) {
     927                for (j = 0; j < num_mem; j++) {
     928                        NTSTATUS status;
     929                        status = add_aliasmem(&map.sid, &members[j]);
     930                        if (!NT_STATUS_IS_OK(status)) {
     931                                errno = EIO;
     932                                goto failed;
     933                        }
     934                }
     935        }
     936
     937        if (remaining != 0) {
     938                DEBUG(0, ("Errror: %d bytes unread in ltdb_unpack_data\n",
     939                          remaining));
     940        }
     941
     942        return 0;
     943
     944failed:
     945        return -1;
     946}
     947
     948static bool mapping_switch(const char *ldb_path)
     949{
     950        TDB_CONTEXT *ltdb;
     951        TALLOC_CTX *frame;
     952        char *new_path;
     953        int ret;
     954
     955        frame = talloc_stackframe();
     956
     957        ltdb = tdb_open_log(ldb_path, 0, TDB_DEFAULT, O_RDONLY, 0600);
     958        if (ltdb == NULL) goto failed;
     959
     960        /* ldb is just a very fancy tdb, read out raw data and perform
     961         * conversion */
     962        ret = tdb_traverse(ltdb, convert_ldb_record, NULL);
     963        if (ret == -1) goto failed;
     964
     965        if (ltdb) {
     966                tdb_close(ltdb);
     967                ltdb = NULL;
     968        }
     969
     970        /* now rename the old db out of the way */
     971        new_path = state_path("group_mapping.ldb.replaced");
     972        if (!new_path) {
     973                goto failed;
     974        }
     975        if (rename(ldb_path, new_path) != 0) {
     976                DEBUG(0,("Failed to rename old group mapping database\n"));
     977                goto failed;
     978        }
     979        TALLOC_FREE(frame);
     980        return True;
     981
     982failed:
     983        DEBUG(0, ("Failed to switch to tdb group mapping database\n"));
     984        if (ltdb) tdb_close(ltdb);
     985        TALLOC_FREE(frame);
     986        return False;
    720987}
    721988
Note: See TracChangeset for help on using the changeset viewer.