switchuser.cpp 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. /* SLiM - Simple Login Manager
  2. Copyright (C) 1997, 1998 Per Liden
  3. Copyright (C) 2004-06 Simone Rota <sip@varlock.com>
  4. Copyright (C) 2004-06 Johannes Winkelmann <jw@tks6.net>
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. */
  10. #include <cstdio>
  11. #include "switchuser.h"
  12. using namespace std;
  13. SwitchUser::SwitchUser(struct passwd *pw, Cfg *c, const string& display,
  14. char** _env)
  15. : cfg(c),
  16. Pw(pw),
  17. displayName(display),
  18. env(_env)
  19. {
  20. }
  21. SwitchUser::~SwitchUser() {
  22. // Never called
  23. }
  24. void SwitchUser::Login(const char* cmd, const char* mcookie) {
  25. SetUserId();
  26. SetClientAuth(mcookie);
  27. Execute(cmd);
  28. }
  29. void SwitchUser::SetUserId() {
  30. if( (Pw == 0) ||
  31. (initgroups(Pw->pw_name, Pw->pw_gid) != 0) ||
  32. (setgid(Pw->pw_gid) != 0) ||
  33. (setuid(Pw->pw_uid) != 0) ) {
  34. cerr << APPNAME << ": could not switch user id" << endl;
  35. exit(ERR_EXIT);
  36. }
  37. }
  38. void SwitchUser::Execute(const char* cmd) {
  39. chdir(Pw->pw_dir);
  40. execle(Pw->pw_shell, Pw->pw_shell, "-c", cmd, NULL, env);
  41. cerr << APPNAME << ": could not execute login command" << endl;
  42. }
  43. void SwitchUser::SetClientAuth(const char* mcookie) {
  44. int r;
  45. string home = string(Pw->pw_dir);
  46. string authfile = home + "/.Xauthority";
  47. remove(authfile.c_str());
  48. string cmd = cfg->getOption("xauth_path") + " -q -f " + authfile + " add :0 . " + mcookie;
  49. r = system(cmd.c_str());
  50. }