switchuser.cpp 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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. logStream << 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. logStream << APPNAME << ": could not execute login command" << endl;
  43. }
  44. void SwitchUser::SetClientAuth(const char* mcookie) {
  45. string home = string(Pw->pw_dir);
  46. string authfile = home + "/.Xauthority";
  47. remove(authfile.c_str());
  48. Util::add_mcookie(mcookie, ":0", cfg->getOption("xauth_path"),
  49. authfile);
  50. }