~ubuntu-branches/ubuntu/gutsy/samba/gutsy-updates

« back to all changes in this revision

Viewing changes to source/smbd/sec_ctx.c

  • Committer: Bazaar Package Importer
  • Author(s): Andrew Mitchell
  • Date: 2006-11-28 20:14:37 UTC
  • mfrom: (0.10.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20061128201437-a6x4lzlhempazocp
Tags: 3.0.23d-1ubuntu1
* Merge from debian unstable.
* Drop python2.4-samba, replace with python-samba. Added Conflicts/Replaces
  on python2.4-samba
* Drop track-connection-dos.patch, ubuntu-winbind-panic.patch, 
  ubuntu-fix-ldap.patch, ubuntu-setlocale.patch, ubuntu-setlocale-fixes.patch
* Remaining Ubuntu changes:
  - Revert Debian's installation of mount.cifs and umount.cifs as suid
  - Comment out the default [homes] shares and add more verbose comments to
    explain what they do and how they work (closes: launchpad.net/27608)
  - Add a "valid users = %S" stanza to the commented-out [homes] section, to
    show users how to restrict access to \\server\username to only username.
  - Change the (commented-out) "printer admin" example to use "@lpadmin"
    instead of "@ntadmin", since the lpadmin group is used for spool admin.
  - Alter the panic-action script to encourage users to report their
    bugs in Ubuntu packages to Ubuntu, rather than reporting to Debian.
    Modify text to more closely match the Debian script
  - Munge our init script to deal with the fact that our implementation
    (or lack thereof) of log_daemon_msg and log_progress_msg differs
    from Debian's implementation of the same (Ubuntu #19691)
  - Kept ubuntu-auxsrc.patch: some auxilliary sources (undocumented in 
    previous changelogs)
  - Set default workgroup to MSHOME

Show diffs side-by-side

added added

removed removed

Lines of Context:
23
23
extern struct current_user current_user;
24
24
 
25
25
struct sec_ctx {
26
 
        uid_t uid;
27
 
        uid_t gid;
28
 
        int ngroups;
29
 
        gid_t *groups;
 
26
        UNIX_USER_TOKEN ut;
30
27
        NT_USER_TOKEN *token;
31
28
};
32
29
 
132
129
 Get the list of current groups.
133
130
****************************************************************************/
134
131
 
135
 
int get_current_groups(gid_t gid, int *p_ngroups, gid_t **p_groups)
 
132
static int get_current_groups(gid_t gid, int *p_ngroups, gid_t **p_groups)
136
133
{
137
134
        int i;
138
135
        gid_t grp;
183
180
}
184
181
 
185
182
/****************************************************************************
186
 
 Initialize the groups a user belongs to.
187
 
****************************************************************************/
188
 
 
189
 
BOOL initialise_groups(char *user, uid_t uid, gid_t gid)
190
 
{
191
 
        struct sec_ctx *prev_ctx_p;
192
 
        BOOL result = True;
193
 
 
194
 
        if (non_root_mode()) {
195
 
                return True;
196
 
        }
197
 
 
198
 
        become_root();
199
 
 
200
 
        /* Call initgroups() to get user groups */
201
 
 
202
 
        if (winbind_initgroups(user,gid) == -1) {
203
 
                DEBUG(0,("Unable to initgroups. Error was %s\n", strerror(errno) ));
204
 
                if (getuid() == 0) {
205
 
                        if (gid < 0 || gid > 32767 || uid < 0 || uid > 32767) {
206
 
                                DEBUG(0,("This is probably a problem with the account %s\n", user));
207
 
                        }
208
 
                }
209
 
                result = False;
210
 
                goto done;
211
 
        }
212
 
 
213
 
        /* Store groups in previous user's security context.  This will
214
 
           always work as the become_root() call increments the stack
215
 
           pointer. */
216
 
 
217
 
        prev_ctx_p = &sec_ctx_stack[sec_ctx_stack_ndx - 1];
218
 
 
219
 
        SAFE_FREE(prev_ctx_p->groups);
220
 
        prev_ctx_p->ngroups = 0;
221
 
 
222
 
        get_current_groups(gid, &prev_ctx_p->ngroups, &prev_ctx_p->groups);
223
 
 
224
 
 done:
225
 
        unbecome_root();
226
 
 
227
 
        return result;
228
 
}
229
 
 
230
 
/****************************************************************************
231
183
 Create a new security context on the stack.  It is the same as the old
232
184
 one.  User changes are done using the set_sec_ctx() function.
233
185
****************************************************************************/
249
201
 
250
202
        ctx_p = &sec_ctx_stack[sec_ctx_stack_ndx];
251
203
 
252
 
        ctx_p->uid = geteuid();
253
 
        ctx_p->gid = getegid();
 
204
        ctx_p->ut.uid = geteuid();
 
205
        ctx_p->ut.gid = getegid();
254
206
 
255
207
        DEBUG(3, ("push_sec_ctx(%u, %u) : sec_ctx_stack_ndx = %d\n", 
256
 
                  (unsigned int)ctx_p->uid, (unsigned int)ctx_p->gid, sec_ctx_stack_ndx ));
257
 
 
258
 
        ctx_p->token = dup_nt_token(sec_ctx_stack[sec_ctx_stack_ndx-1].token);
259
 
 
260
 
        ctx_p->ngroups = sys_getgroups(0, NULL);
261
 
 
262
 
        if (ctx_p->ngroups != 0) {
263
 
                if (!(ctx_p->groups = SMB_MALLOC_ARRAY(gid_t, ctx_p->ngroups))) {
 
208
                  (unsigned int)ctx_p->ut.uid, (unsigned int)ctx_p->ut.gid, sec_ctx_stack_ndx ));
 
209
 
 
210
        ctx_p->token = dup_nt_token(NULL,
 
211
                                    sec_ctx_stack[sec_ctx_stack_ndx-1].token);
 
212
 
 
213
        ctx_p->ut.ngroups = sys_getgroups(0, NULL);
 
214
 
 
215
        if (ctx_p->ut.ngroups != 0) {
 
216
                if (!(ctx_p->ut.groups = SMB_MALLOC_ARRAY(gid_t, ctx_p->ut.ngroups))) {
264
217
                        DEBUG(0, ("Out of memory in push_sec_ctx()\n"));
265
 
                        delete_nt_token(&ctx_p->token);
 
218
                        TALLOC_FREE(ctx_p->token);
266
219
                        return False;
267
220
                }
268
221
 
269
 
                sys_getgroups(ctx_p->ngroups, ctx_p->groups);
 
222
                sys_getgroups(ctx_p->ut.ngroups, ctx_p->ut.groups);
270
223
        } else {
271
 
                ctx_p->groups = NULL;
 
224
                ctx_p->ut.groups = NULL;
272
225
        }
273
226
 
274
227
        return True;
296
249
        sys_setgroups(ngroups, groups);
297
250
#endif
298
251
 
299
 
        ctx_p->ngroups = ngroups;
 
252
        ctx_p->ut.ngroups = ngroups;
300
253
 
301
 
        SAFE_FREE(ctx_p->groups);
 
254
        SAFE_FREE(ctx_p->ut.groups);
302
255
        if (token && (token == ctx_p->token))
303
256
                smb_panic("DUPLICATE_TOKEN");
304
257
 
305
 
        delete_nt_token(&ctx_p->token);
 
258
        TALLOC_FREE(ctx_p->token);
306
259
        
307
 
        ctx_p->groups = memdup(groups, sizeof(gid_t) * ngroups);
308
 
        ctx_p->token = dup_nt_token(token);
 
260
        ctx_p->ut.groups = memdup(groups, sizeof(gid_t) * ngroups);
 
261
        ctx_p->token = dup_nt_token(NULL, token);
309
262
 
310
263
        become_id(uid, gid);
311
264
 
312
 
        ctx_p->uid = uid;
313
 
        ctx_p->gid = gid;
 
265
        ctx_p->ut.uid = uid;
 
266
        ctx_p->ut.gid = gid;
314
267
 
315
268
        /* Update current_user stuff */
316
269
 
317
 
        current_user.uid = uid;
318
 
        current_user.gid = gid;
319
 
        current_user.ngroups = ngroups;
320
 
        current_user.groups = groups;
 
270
        current_user.ut.uid = uid;
 
271
        current_user.ut.gid = gid;
 
272
        current_user.ut.ngroups = ngroups;
 
273
        current_user.ut.groups = groups;
321
274
        current_user.nt_user_token = ctx_p->token;
322
275
}
323
276
 
352
305
 
353
306
        /* Clear previous user info */
354
307
 
355
 
        ctx_p->uid = (uid_t)-1;
356
 
        ctx_p->gid = (gid_t)-1;
357
 
 
358
 
        SAFE_FREE(ctx_p->groups);
359
 
        ctx_p->ngroups = 0;
360
 
 
361
 
        delete_nt_token(&ctx_p->token);
 
308
        ctx_p->ut.uid = (uid_t)-1;
 
309
        ctx_p->ut.gid = (gid_t)-1;
 
310
 
 
311
        SAFE_FREE(ctx_p->ut.groups);
 
312
        ctx_p->ut.ngroups = 0;
 
313
 
 
314
        TALLOC_FREE(ctx_p->token);
362
315
 
363
316
        /* Pop back previous user */
364
317
 
369
322
        prev_ctx_p = &sec_ctx_stack[sec_ctx_stack_ndx];
370
323
 
371
324
#ifdef HAVE_SETGROUPS
372
 
        sys_setgroups(prev_ctx_p->ngroups, prev_ctx_p->groups);
 
325
        sys_setgroups(prev_ctx_p->ut.ngroups, prev_ctx_p->ut.groups);
373
326
#endif
374
327
 
375
 
        become_id(prev_ctx_p->uid, prev_ctx_p->gid);
 
328
        become_id(prev_ctx_p->ut.uid, prev_ctx_p->ut.gid);
376
329
 
377
330
        /* Update current_user stuff */
378
331
 
379
 
        current_user.uid = prev_ctx_p->uid;
380
 
        current_user.gid = prev_ctx_p->gid;
381
 
        current_user.ngroups = prev_ctx_p->ngroups;
382
 
        current_user.groups = prev_ctx_p->groups;
 
332
        current_user.ut.uid = prev_ctx_p->ut.uid;
 
333
        current_user.ut.gid = prev_ctx_p->ut.gid;
 
334
        current_user.ut.ngroups = prev_ctx_p->ut.ngroups;
 
335
        current_user.ut.groups = prev_ctx_p->ut.groups;
383
336
        current_user.nt_user_token = prev_ctx_p->token;
384
337
 
385
338
        DEBUG(3, ("pop_sec_ctx (%u, %u) - sec_ctx_stack_ndx = %d\n", 
400
353
        memset(sec_ctx_stack, 0, sizeof(struct sec_ctx) * MAX_SEC_CTX_DEPTH);
401
354
 
402
355
        for (i = 0; i < MAX_SEC_CTX_DEPTH; i++) {
403
 
                sec_ctx_stack[i].uid = (uid_t)-1;
404
 
                sec_ctx_stack[i].gid = (gid_t)-1;
 
356
                sec_ctx_stack[i].ut.uid = (uid_t)-1;
 
357
                sec_ctx_stack[i].ut.gid = (gid_t)-1;
405
358
        }
406
359
 
407
360
        /* Initialise first level of stack.  It is the current context */
408
361
        ctx_p = &sec_ctx_stack[0];
409
362
 
410
 
        ctx_p->uid = geteuid();
411
 
        ctx_p->gid = getegid();
 
363
        ctx_p->ut.uid = geteuid();
 
364
        ctx_p->ut.gid = getegid();
412
365
 
413
 
        get_current_groups(ctx_p->gid, &ctx_p->ngroups, &ctx_p->groups);
 
366
        get_current_groups(ctx_p->ut.gid, &ctx_p->ut.ngroups, &ctx_p->ut.groups);
414
367
 
415
368
        ctx_p->token = NULL; /* Maps to guest user. */
416
369
 
417
370
        /* Initialise current_user global */
418
371
 
419
 
        current_user.uid = ctx_p->uid;
420
 
        current_user.gid = ctx_p->gid;
421
 
        current_user.ngroups = ctx_p->ngroups;
422
 
        current_user.groups = ctx_p->groups;
 
372
        current_user.ut.uid = ctx_p->ut.uid;
 
373
        current_user.ut.gid = ctx_p->ut.gid;
 
374
        current_user.ut.ngroups = ctx_p->ut.ngroups;
 
375
        current_user.ut.groups = ctx_p->ut.groups;
423
376
 
424
377
        /* The conn and vuid are usually taken care of by other modules.
425
378
           We initialise them here. */