summaryrefslogtreecommitdiff
path: root/source3/nsswitch/winbindd_idmap.c
blob: 11f7b8aae7a76e3f9431433bcb54258e77d00be6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
/* 
   Unix SMB/Netbios implementation.
   Version 2.0

   Winbind daemon - user related function

   Copyright (C) Tim Potter 2000
   
   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.
   
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
   
   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/

#include "winbindd.h"

/* High water mark keys */

#define HWM_GROUP  "GROUP HWM"
#define HWM_USER   "USER HWM"

/* Globals */

static TDB_CONTEXT *idmap_tdb;

/* Allocate either a user or group id from the pool */

static BOOL allocate_id(int *id, BOOL isgroup)
{
    int hwm;

    /* Get current high water mark */

    if ((hwm = tdb_fetch_int(idmap_tdb, 
                             isgroup ? HWM_GROUP : HWM_USER)) == -1) {
        return False;
    }

    /* Return next available uid in list */

    if ((isgroup && (hwm > server_state.gid_high)) ||
        (!isgroup && (hwm > server_state.uid_high))) {
        DEBUG(0, ("winbind %sid range full!\n", isgroup ? "g" : "u"));
        return False;
    }

    if (id) {
        *id = hwm;
    }

    hwm++;

    /* Store new high water mark */

    tdb_store_int(idmap_tdb, isgroup ? HWM_GROUP : HWM_USER, hwm);

    return True;
}

/* Get an id from a rid */

static BOOL get_id_from_rid(char *domain_name, uint32 rid, int *id,
                            BOOL isgroup)
{
    TDB_DATA data, key;
    fstring keystr;
    BOOL result;

    /* Check if rid is present in database */

    slprintf(keystr, sizeof(keystr)-1, "%s/%d", domain_name, rid);
	dos_to_unix(keystr, True);             /* Convert key to unix-codepage */
    
    key.dptr = keystr;
    key.dsize = strlen(keystr) + 1;

    data = tdb_fetch(idmap_tdb, key);

    if (data.dptr) {
        fstring scanstr;
        int the_id;

        /* Parse and return existing uid */

        fstrcpy(scanstr, isgroup ? "GID" : "UID");
        fstrcat(scanstr, " %d");

        if (sscanf(data.dptr, scanstr, &the_id) == 1) {

            /* Store uid */

            if (id) {
                *id = the_id;
            }

            result = True;
        }

        free(data.dptr);

    } else {

        /* Allocate a new id for this rid */

        if (id && allocate_id(id, isgroup)) {
            fstring keystr2;

            /* Store new id */
            
            slprintf(keystr2, sizeof(keystr2)-1, "%s %d", isgroup ? "GID" :
                     "UID", *id);

            data.dptr = keystr2;
            data.dsize = strlen(keystr2) + 1;

            tdb_store(idmap_tdb, key, data, TDB_REPLACE);
            tdb_store(idmap_tdb, data, key, TDB_REPLACE);

            result = True;
        }
    }

    return result;
}

/* Get a uid from a user rid */

BOOL winbindd_idmap_get_uid_from_rid(char *domain_name, uint32 user_rid, 
                                     uid_t *uid)
{
    return get_id_from_rid(domain_name, user_rid, uid, False);
}

/* Get a gid from a group rid */

BOOL winbindd_idmap_get_gid_from_rid(char *domain_name, uint32 group_rid, 
                                     gid_t *gid)
{
    return get_id_from_rid(domain_name, group_rid, gid, True);
}

BOOL get_rid_from_id(int id, uint32 *rid, struct winbindd_domain **domain,
                     BOOL isgroup)
{
    TDB_DATA key, data;
    fstring keystr;
    BOOL result = False;

    slprintf(keystr, sizeof(keystr)-1, "%s %d", isgroup ? "GID" : "UID", id);

    key.dptr = keystr;
    key.dsize = strlen(keystr) + 1;

    data = tdb_fetch(idmap_tdb, key);

    if (data.dptr) {
        char *p = data.dptr;
        fstring domain_name;
        uint32 the_rid;

        if (next_token(&p, domain_name, "/", sizeof(fstring))) {

            the_rid = atoi(p);

            if (rid) {
                *rid = the_rid;
            }

            if (domain) {
                *domain = find_domain_from_name(domain_name);
            }

            result = True;
        }
            
        free(data.dptr);
    }

    return result;
}

/* Get a user rid from a uid */

BOOL winbindd_idmap_get_rid_from_uid(uid_t uid, uint32 *user_rid,
                                     struct winbindd_domain **domain)
{
    return get_rid_from_id((int)uid, user_rid, domain, False);
}

/* Get a group rid from a gid */

BOOL winbindd_idmap_get_rid_from_gid(gid_t gid, uint32 *group_rid, 
                                     struct winbindd_domain **domain)
{
    return get_rid_from_id((int)gid, group_rid, domain, True);
}

/* Initialise idmap database */

BOOL winbindd_idmap_init(void)
{
    /* Open tdb cache */

    if (!(idmap_tdb = tdb_open(lock_path("winbindd_idmap.tdb"), 0,
                               TDB_NOLOCK | TDB_NOMMAP, 
                               O_RDWR | O_CREAT, 0600))) {
        DEBUG(0, ("Unable to open idmap database\n"));
        return False;
    }

     /* Create high water marks for group and user id */

    if (tdb_fetch_int(idmap_tdb, HWM_USER) == -1) {
        if (tdb_store_int(idmap_tdb, HWM_USER, server_state.uid_low) == -1) {
            DEBUG(0, ("Unable to initialise user hwm in idmap database\n"));
            return False;
        }
    }

    if (tdb_fetch_int(idmap_tdb, HWM_GROUP) == -1) {
        if (tdb_store_int(idmap_tdb, HWM_GROUP, server_state.gid_low) == -1) {
            DEBUG(0, ("Unable to initialise group hwm in idmap database\n"));
            return False;
        }
    }

    return True;   
}