switchuser.cpp 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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. #include "util.h"
  13. using namespace std;
  14. SwitchUser::SwitchUser(struct passwd *pw, Cfg *c, const string& display,
  15. char** _env)
  16. : cfg(c),
  17. Pw(pw),
  18. displayName(display),
  19. env(_env)
  20. {
  21. }
  22. SwitchUser::~SwitchUser() {
  23. // Never called
  24. }
  25. void SwitchUser::Login(const char* cmd, const char* mcookie) {
  26. SetUserId();
  27. SetClientAuth(mcookie);
  28. Execute(cmd);
  29. }
  30. void SwitchUser::SetUserId() {
  31. if( (Pw == 0) ||
  32. (initgroups(Pw->pw_name, Pw->pw_gid) != 0) ||
  33. (setgid(Pw->pw_gid) != 0) ||
  34. (setuid(Pw->pw_uid) != 0) ) {
  35. cerr << APPNAME << ": could not switch user id" << endl;
  36. exit(ERR_EXIT);
  37. }
  38. }
  39. void SwitchUser::Execute(const char* cmd) {
  40. chdir(Pw->pw_dir);
  41. execle(Pw->pw_shell, Pw->pw_shell, "-c", cmd, NULL, env);
  42. cerr << APPNAME << ": could not execute login command" << endl;
  43. }
  44. void SwitchUser::SetClientAuth(const char* mcookie) {
  45. bool r;
  46. string home = string(Pw->pw_dir);
  47. string authfile = home + "/.Xauthority";
  48. remove(authfile.c_str());
  49. r = Util::add_mcookie(mcookie, ":0", cfg->getOption("xauth_path"),
  50. authfile);
  51. }