switchuser.cpp 1.6 KB

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