~ubuntu-branches/ubuntu/saucy/sudo/saucy

« back to all changes in this revision

Viewing changes to .pc/keep_home_by_default.patch/plugins/sudoers/env.c

  • Committer: Package Import Robot
  • Author(s): Stéphane Graber
  • Date: 2012-11-16 09:31:32 UTC
  • mfrom: (1.4.13)
  • Revision ID: package-import@ubuntu.com-20121116093132-ptext55adlzbrq6y
Tags: 1.8.6p3-0ubuntu1
* New upstream release (1.8.6p3).
* Add patch to fix building with sssd when ldap is disabled.
* Drop sudo.manpages and sudo-ldap.manpages as the upstream build system
  now does the right thing here.
* Build the main sudo package with support for sssd, this doesn't add any
  additional build time or runtime dependency. sudo will dynamically load
  the sssd library if 'sss' is listed for the 'sudoers' nss service.

Show diffs side-by-side

added added

removed removed

Lines of Context:
42
42
#ifdef HAVE_UNISTD_H
43
43
# include <unistd.h>
44
44
#endif /* HAVE_UNISTD_H */
 
45
#ifdef HAVE_INTTYPES_H
 
46
# include <inttypes.h>
 
47
#endif
45
48
#ifdef HAVE_LOGIN_CAP_H
46
49
# include <login_cap.h>
47
50
# ifndef LOGIN_SETENV
50
53
#endif /* HAVE_LOGIN_CAP_H */
51
54
#include <ctype.h>
52
55
#include <errno.h>
 
56
#include <limits.h>
53
57
#include <pwd.h>
54
58
 
55
59
#include "sudoers.h"
56
60
 
57
61
/*
 
62
 * If there is no SIZE_MAX or SIZE_T_MAX we have to assume that size_t
 
63
 * could be signed (as it is on SunOS 4.x).  This just means that
 
64
 * emalloc2() and erealloc3() cannot allocate huge amounts on such a
 
65
 * platform but that is OK since sudo doesn't need to do so anyway.
 
66
 */
 
67
#ifndef SIZE_MAX
 
68
# ifdef SIZE_T_MAX
 
69
#  define SIZE_MAX      SIZE_T_MAX
 
70
# else
 
71
#  define SIZE_MAX      INT_MAX
 
72
# endif /* SIZE_T_MAX */
 
73
#endif /* SIZE_MAX */
 
74
        
 
75
/*
58
76
 * Flags used in rebuild_env()
59
77
 */
60
78
#undef DID_TERM
229
247
        memset(env.envp, 0, env.env_size * sizeof(char *));
230
248
#endif
231
249
        memcpy(env.envp, envp, len * sizeof(char *));
232
 
        env.envp[len] = '\0';
 
250
        env.envp[len] = NULL;
233
251
 
234
252
        /* Free the old envp we allocated, if any. */
235
253
        if (env.old_envp != NULL)
263
281
    bool found = false;
264
282
 
265
283
    /* Make sure there is room for the new entry plus a NULL. */
266
 
    if (env.env_len + 2 > env.env_size) {
 
284
    if (env.env_size > 2 && env.env_len > env.env_size - 2) {
267
285
        char **nenvp;
268
 
        size_t nsize = env.env_size + 128;
269
 
        nenvp = env.envp ? realloc(env.envp, nsize * sizeof(char *)) :
270
 
            malloc(nsize * sizeof(char *));
 
286
        size_t nsize;
 
287
 
 
288
        if (env.env_size > SIZE_MAX - 128) {
 
289
            errorx2(1, _("internal error, %s overflow"),
 
290
                "sudo_putenv_nodebug()");
 
291
        }
 
292
        nsize = env.env_size + 128;
 
293
        if (nsize > SIZE_MAX / sizeof(char *)) {
 
294
            errorx2(1, _("internal error, %s overflow"),
 
295
                "sudo_putenv_nodebug()");
 
296
        }
 
297
        nenvp = realloc(env.envp, nsize * sizeof(char *));
271
298
        if (nenvp == NULL) {
272
299
            errno = ENOMEM;
273
300
            return -1;
289
316
 
290
317
    if (dupcheck) {
291
318
        len = (strchr(str, '=') - str) + 1;
292
 
        for (ep = env.envp; !found && *ep != NULL; ep++) {
 
319
        for (ep = env.envp; *ep != NULL; ep++) {
293
320
            if (strncmp(str, *ep, len) == 0) {
294
321
                if (overwrite)
295
322
                    *ep = str;
296
323
                found = true;
 
324
                break;
297
325
            }
298
326
        }
299
 
        /* Prune out duplicate variables. */
 
327
        /* Prune out extra instances of the variable we just overwrote. */
300
328
        if (found && overwrite) {
301
 
            while (*ep != NULL) {
 
329
            while (*++ep != NULL) {
302
330
                if (strncmp(str, *ep, len) == 0) {
303
331
                    char **cur = ep;
304
332
                    while ((*cur = *(cur + 1)) != NULL)
305
333
                        cur++;
306
 
                } else {
307
 
                    ep++;
 
334
                    ep--;
308
335
                }
309
336
            }
310
337
            env.env_len = ep - env.envp;
332
359
    int rval;
333
360
    debug_decl(sudo_putenv, SUDO_DEBUG_ENV)
334
361
 
 
362
    sudo_debug_printf(SUDO_DEBUG_INFO, "sudo_putenv: %s", str);
 
363
 
335
364
    rval = sudo_putenv_nodebug(str, dupcheck, overwrite);
336
365
    if (rval == -1) {
337
366
#ifdef ENV_DEBUG
353
382
{
354
383
    char *estring;
355
384
    size_t esize;
 
385
    int rval;
356
386
    debug_decl(sudo_setenv2, SUDO_DEBUG_ENV)
357
387
 
358
388
    esize = strlen(var) + 1 + strlen(val) + 1;
363
393
        strlcat(estring, "=", esize) >= esize ||
364
394
        strlcat(estring, val, esize) >= esize) {
365
395
 
366
 
        errorx(1, _("internal error, sudo_setenv2() overflow"));
 
396
        errorx(1, _("internal error, %s overflow"), "sudo_setenv2()");
367
397
    }
368
 
    debug_return_int(sudo_putenv(estring, dupcheck, overwrite));
 
398
    rval = sudo_putenv(estring, dupcheck, overwrite);
 
399
    if (rval == -1)
 
400
        efree(estring);
 
401
    debug_return_int(rval);
369
402
}
370
403
 
371
404
/*
377
410
{
378
411
    char *estring;
379
412
    size_t esize;
 
413
    int rval = -1;
380
414
 
381
415
    esize = strlen(var) + 1 + strlen(val) + 1;
382
416
    if ((estring = malloc(esize)) == NULL) {
383
417
        errno = ENOMEM;
384
 
        return -1;
 
418
        goto done;
385
419
    }
386
420
 
387
421
    /* Build environment string and insert it. */
390
424
        strlcat(estring, val, esize) >= esize) {
391
425
 
392
426
        errno = EINVAL;
393
 
        return -1;
 
427
        goto done;
394
428
    }
395
 
    return sudo_putenv_nodebug(estring, true, overwrite);
 
429
    rval = sudo_putenv_nodebug(estring, true, overwrite);
 
430
done:
 
431
    if (rval == -1)
 
432
        efree(estring);
 
433
    return rval;
396
434
}
397
435
 
398
436
/*
407
445
    rval = sudo_setenv_nodebug(var, val, overwrite);
408
446
    if (rval == -1) {
409
447
        if (errno == EINVAL)
410
 
            errorx(1, _("internal error, sudo_setenv() overflow"));
 
448
            errorx(1, _("internal error, %s overflow"), "sudo_setenv()");
411
449
        errorx(1, _("unable to allocate memory"));
412
450
    }
413
451
    debug_return_int(rval);
452
490
    int rval;
453
491
    debug_decl(sudo_unsetenv, SUDO_DEBUG_ENV)
454
492
 
 
493
    sudo_debug_printf(SUDO_DEBUG_INFO, "sudo_unsetenv: %s", name);
 
494
 
455
495
    rval = sudo_unsetenv_nodebug(name);
456
496
 
457
497
    debug_return_int(rval);
490
530
    char *val;
491
531
    debug_decl(sudo_getenv, SUDO_DEBUG_ENV)
492
532
 
 
533
    sudo_debug_printf(SUDO_DEBUG_INFO, "sudo_getenv: %s", name);
 
534
 
493
535
    val = sudo_getenv_nodebug(name);
494
536
 
495
537
    debug_return_str(val);
621
663
    delete_it = matches_env_delete(var);
622
664
    if (!delete_it)
623
665
        delete_it = matches_env_check(var) == false;
 
666
 
 
667
    sudo_debug_printf(SUDO_DEBUG_INFO, "delete %s: %s",
 
668
        var, delete_it ? "YES" : "NO");
624
669
    debug_return_bool(delete_it);
625
670
}
626
671
 
638
683
    if (keepit == -1)
639
684
        keepit = matches_env_keep(var);
640
685
 
 
686
    sudo_debug_printf(SUDO_DEBUG_INFO, "keep %s: %s",
 
687
        var, keepit ? "YES" : "NO");
641
688
    debug_return_bool(keepit == true);
642
689
}
643
690
 
687
734
rebuild_env(void)
688
735
{
689
736
    char **old_envp, **ep, *cp, *ps1;
690
 
    char idbuf[MAX_UID_T_LEN];
 
737
    char idbuf[MAX_UID_T_LEN + 1];
691
738
    unsigned int didvar;
692
739
    bool reset_home = false;
693
740
 
788
835
        } else {
789
836
            if (!ISSET(didvar, DID_SHELL))
790
837
                sudo_setenv2("SHELL", sudo_user.pw->pw_shell, false, true);
791
 
            if (!ISSET(didvar, DID_LOGNAME))
792
 
                sudo_setenv2("LOGNAME", user_name, false, true);
793
 
            if (!ISSET(didvar, DID_USER))
794
 
                sudo_setenv2("USER", user_name, false, true);
795
 
            if (!ISSET(didvar, DID_USERNAME))
796
 
                sudo_setenv2("USERNAME", user_name, false, true);
 
838
            /* We will set LOGNAME later in the !def_set_logname case. */
 
839
            if (!def_set_logname) {
 
840
                if (!ISSET(didvar, DID_LOGNAME))
 
841
                    sudo_setenv2("LOGNAME", user_name, false, true);
 
842
                if (!ISSET(didvar, DID_USER))
 
843
                    sudo_setenv2("USER", user_name, false, true);
 
844
                if (!ISSET(didvar, DID_USERNAME))
 
845
                    sudo_setenv2("USERNAME", user_name, false, true);
 
846
            }
797
847
        }
798
848
 
799
849
        /* If we didn't keep HOME, reset it based on target user. */
845
895
    /*
846
896
     * Set $USER, $LOGNAME and $USERNAME to target if "set_logname" is not
847
897
     * disabled.  We skip this if we are running a login shell (because
848
 
     * they have already been set them) or sudoedit (because we want the
849
 
     * editor to find the user's startup files).
 
898
     * they have already been set) or sudoedit (because we want the editor
 
899
     * to find the invoking user's startup files).
850
900
     */
851
901
    if (def_set_logname && !ISSET(sudo_mode, MODE_LOGIN_SHELL|MODE_EDIT)) {
852
902
        if (!ISSET(didvar, KEPT_LOGNAME))