Subversion Repositories public

Rev

Rev 100 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 100 Rev 102
Line 9... Line 9...
9
 *
9
 *
10
 * This program is free software; you can use it, redistribute
10
 * This program is free software; you can use it, redistribute
11
 * it and/or modify it under the terms of the GNU General Public
11
 * it and/or modify it under the terms of the GNU General Public
12
 * License, version 2 (see LICENSE-GPL).
12
 * License, version 2 (see LICENSE-GPL).
13
 *  
13
 *
-
 
14
 * Credits:
-
 
15
 *   kolter (fix for segfaulting on --user)
-
 
16
 *
14
 */
17
 */
15
 
18
16
19
17
#include <argp.h>
20
#include <argp.h>
18
#include <pwd.h>
21
#include <pwd.h>
Line 452... Line 455...
452
  if (uid != 0 && chuser) {
455
  if (uid != 0 && chuser) {
453
    fprintf(stderr, "cannot override user to '%s': insufficient privileges\n", user.c_str());
456
    fprintf(stderr, "cannot override user to '%s': insufficient privileges\n", user.c_str());
454
    return 1;
457
    return 1;
455
  }
458
  }
456
 
459
457
  struct passwd pwd;
460
  struct passwd* ppwd = NULL;
458
 
461
459
  if (!chuser) {
462
  if (chuser) {
-
 
463
        if ((ppwd = getpwnam(user.c_str())) != NULL) {
-
 
464
      if (    setenv("LOGNAME",   ppwd->pw_name,   1) != 0
-
 
465
                  ||  setenv("USER",      ppwd->pw_name,   1) != 0
-
 
466
                  ||  setenv("USERNAME",  ppwd->pw_name,   1) != 0
-
 
467
                  ||  setenv("HOME",      ppwd->pw_dir,    1) != 0
460
    struct passwd* ppwd = getpwuid(uid);
468
                  ||  setenv("SHELL",     ppwd->pw_shell,  1) != 0)
461
    if (ppwd == NULL) {
469
      {
462
      fprintf(stderr, "cannot determine current user\n");
470
                perror("cannot set environment variables");
463
      return 1;
471
                return 1;
464
    }
472
          }
465
    memcpy(&pwd, ppwd, sizeof(pwd));
-
 
466
    user = pwd.pw_name;
-
 
467
  }
473
        } else {
468
  else if (getpwnam(user.c_str()) == NULL) {
-
 
469
    fprintf(stderr, "user '%s' not found\n", user.c_str());
474
          fprintf(stderr, "user '%s' not found\n", user.c_str());
470
    return 1;
475
          return 1;
471
  }
476
        }
472
  else if ( setenv("LOGNAME",   pwd.pw_name,   1) != 0
-
 
473
        ||  setenv("USER",      pwd.pw_name,   1) != 0
477
  } else {
474
        ||  setenv("USERNAME",  pwd.pw_name,   1) != 0
478
    ppwd = getpwuid(uid);
475
        ||  setenv("HOME",      pwd.pw_dir,    1) != 0
479
    if (ppwd == NULL) {
476
        ||  setenv("SHELL",     pwd.pw_shell,  1) != 0)
-
 
477
  {
-
 
478
    perror("cannot set environment variables");
480
      fprintf(stderr, "cannot determine current user\n");
479
    return 1;
481
      return 1;
480
  }
482
    }
-
 
483
    user = ppwd->pw_name;
-
 
484
  }
481
 
485
482
  try {
486
  try {
483
 
487
484
    IncronCfg::Init();
488
    IncronCfg::Init();
485
   
489