~ci-train-bot/libertine/libertine-ubuntu-zesty-2086

« back to all changes in this revision

Viewing changes to libertine/PasswordHelper.cpp

  • Committer: Chris Townsend
  • Date: 2016-07-15 18:39:58 UTC
  • mto: This revision was merged to the branch mainline in revision 272.
  • Revision ID: christopher.townsend@canonical.com-20160715183958-l95grj44p272lfz4
Rework the whole way we handle updating the necessary system files for unprivileged LXC's:
- Add new script that does the updates.
- Add sudoers file for this script.
- Remove the password dialog in the UI.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/**
2
 
 * @file PasswordHelper.cpp
3
 
 * @brief Helper class for managing password entry
4
 
 */
5
 
/*
6
 
 * Copyright 2015 Canonical Ltd
7
 
 *
8
 
 * Libertine is free software: you can redistribute it and/or modify it under
9
 
 * the terms of the GNU General Public License, version 3, as published by the
10
 
 * Free Software Foundation.
11
 
 *
12
 
 * Libertine is distributed in the hope that it will be useful, but WITHOUT ANY
13
 
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14
 
 * A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
15
 
 *
16
 
 * You should have received a copy of the GNU General Public License
17
 
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18
 
 */
19
 
#include "libertine/PasswordHelper.h"
20
 
 
21
 
#include <iostream>
22
 
#include <termios.h>
23
 
#include <unistd.h>
24
 
 
25
 
using namespace std;
26
 
 
27
 
namespace
28
 
{
29
 
 
30
 
struct pam_response *reply;
31
 
 
32
 
int function_conversation(int, const struct pam_message**, struct pam_response **resp, void*)
33
 
{
34
 
  *resp = reply;
35
 
  return PAM_SUCCESS;
36
 
}
37
 
 
38
 
void set_stdin_echo(bool enable = true)
39
 
{
40
 
  struct termios tty;
41
 
 
42
 
  tcgetattr(STDIN_FILENO, &tty);
43
 
  if (!enable)
44
 
      tty.c_lflag &= ~ECHO;
45
 
  else
46
 
      tty.c_lflag |= ECHO;
47
 
 
48
 
  (void) tcsetattr(STDIN_FILENO, TCSANOW, &tty);
49
 
}
50
 
 
51
 
}  // anonymous namespace
52
 
 
53
 
 
54
 
PasswordHelper::
55
 
PasswordHelper()
56
 
{
57
 
}
58
 
 
59
 
 
60
 
PasswordHelper::
61
 
~PasswordHelper()
62
 
{
63
 
}
64
 
 
65
 
 
66
 
QString PasswordHelper::
67
 
GetPassword()
68
 
{
69
 
  string password;
70
 
 
71
 
  cout << "Please enter your password:" << endl;
72
 
 
73
 
  set_stdin_echo(false);
74
 
  getline(cin, password);
75
 
  set_stdin_echo(true);
76
 
 
77
 
  if (cin.fail() || cin.eof())
78
 
  {
79
 
    return nullptr;
80
 
  }
81
 
  else
82
 
  {
83
 
    return QString::fromStdString(password);
84
 
  }
85
 
}
86
 
 
87
 
 
88
 
bool PasswordHelper::
89
 
VerifyUserPassword(QString const& password)
90
 
{
91
 
  char *username = getenv("USER");
92
 
  int retval;
93
 
 
94
 
  const struct pam_conv local_conversation = { function_conversation, NULL };
95
 
  pam_handle_t *local_auth_handle = NULL;
96
 
 
97
 
  retval = pam_start("common_auth", username, &local_conversation, &local_auth_handle);
98
 
 
99
 
  if (retval != PAM_SUCCESS)
100
 
  {
101
 
    return false;
102
 
  }
103
 
 
104
 
  reply = (struct pam_response *)malloc(sizeof(struct pam_response));
105
 
 
106
 
  reply[0].resp = strdup(password.toStdString().c_str());
107
 
  reply[0].resp_retcode = 0;
108
 
 
109
 
  retval = pam_authenticate(local_auth_handle, 0);
110
 
 
111
 
  if (retval != PAM_SUCCESS)
112
 
  {
113
 
    return false;
114
 
  }
115
 
 
116
 
  retval = pam_end(local_auth_handle, retval);
117
 
 
118
 
  if (retval != PAM_SUCCESS)
119
 
  {
120
 
    return false;
121
 
  }
122
 
  return true;
123
 
}