| 1 | /* |
| 2 | * Copyright (c) 2003 Christian Loose <[email protected]> |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU Library General Public |
| 6 | * License as published by the Free Software Foundation; either |
| 7 | * version 2 of the License, or (at your option) any later version. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 12 | * Library General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU Library General Public License |
| 15 | * along with this program; see the file COPYING. If not, write to |
| 16 | * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
| 17 | * Boston, MA 02110-1301, USA. |
| 18 | * |
| 19 | */ |
| 20 | |
| 21 | #include "cvsloginjob.h" |
| 22 | #include "../debug.h" |
| 23 | |
| 24 | #include <KLocalizedString> |
| 25 | #include <kpassworddialog.h> |
| 26 | |
| 27 | #include <csignal> |
| 28 | #include <cvsloginjobadaptor.h> |
| 29 | #include <sys/types.h> |
| 30 | |
| 31 | static const char LOGIN_PHRASE[] = "Logging in to"; |
| 32 | static const char FAILURE_PHRASE[] = "authorization failed:"; |
| 33 | static const char PASS_PHRASE[] = "CVS PASSWORD: "; |
| 34 | |
| 35 | CvsLoginJob::CvsLoginJob(unsigned jobNum) |
| 36 | : m_Proc(0) |
| 37 | { |
| 38 | new CvsloginjobAdaptor(this); |
| 39 | m_dbusObjectPath = "/CvsLoginJob" + QString::number(jobNum); |
| 40 | QDBusConnection::sessionBus().registerObject(m_dbusObjectPath, this); |
| 41 | |
| 42 | m_Proc = new KDESu::PtyProcess; |
| 43 | } |
| 44 | |
| 45 | CvsLoginJob::~CvsLoginJob() |
| 46 | { |
| 47 | delete m_Proc; |
| 48 | } |
| 49 | |
| 50 | QString CvsLoginJob::dbusObjectPath() const |
| 51 | { |
| 52 | return m_dbusObjectPath; |
| 53 | } |
| 54 | |
| 55 | void CvsLoginJob::setServer(const QString &server) |
| 56 | { |
| 57 | m_Server = server; |
| 58 | } |
| 59 | |
| 60 | void CvsLoginJob::setCvsClient(const QByteArray &cvsClient) |
| 61 | { |
| 62 | m_CvsClient = cvsClient; |
| 63 | |
| 64 | m_Arguments.clear(); |
| 65 | m_Arguments += "-f"; |
| 66 | } |
| 67 | |
| 68 | void CvsLoginJob::setRepository(const QByteArray &repository) |
| 69 | { |
| 70 | m_Arguments += "-d"; |
| 71 | m_Arguments += repository; |
| 72 | m_Arguments += "login"; |
| 73 | } |
| 74 | |
| 75 | bool CvsLoginJob::execute() |
| 76 | { |
| 77 | static QByteArray repository; |
| 78 | |
| 79 | int res = m_Proc->exec(m_CvsClient, m_Arguments); |
| 80 | if (res < 0) { |
| 81 | qCDebug(log_cervisia) << "Couldn't start 'cvs login' process!"; |
| 82 | return false; |
| 83 | } |
| 84 | |
| 85 | bool result = false; |
| 86 | while (true) { |
| 87 | QByteArray line = m_Proc->readLine(); |
| 88 | if (line.isNull()) { |
| 89 | return result; |
| 90 | } |
| 91 | |
| 92 | // add line to output list |
| 93 | m_output << line; |
| 94 | qCDebug(log_cervisia) << "process output = " << line; |
| 95 | |
| 96 | // retrieve repository from 'Logging in to'-line |
| 97 | if (line.contains(LOGIN_PHRASE)) { |
| 98 | repository = line.remove(0, line.indexOf(":pserver:")); |
| 99 | continue; |
| 100 | } |
| 101 | |
| 102 | // process asks for the password |
| 103 | // search case insensitive as cvs and cvsnt use different capitalization |
| 104 | if (line.toUpper().contains(PASS_PHRASE)) { |
| 105 | // show password dialog |
| 106 | QString password; |
| 107 | KPasswordDialog dlg; |
| 108 | dlg.setPrompt(i18n("Enter password for repository %1.").arg(QString::fromLocal8Bit(repository))); |
| 109 | if (dlg.exec()) { |
| 110 | password = dlg.password(); |
| 111 | // send password to process |
| 112 | m_Proc->waitSlave(); |
| 113 | m_Proc->writeLine(password.toLocal8Bit()); |
| 114 | |
| 115 | // wait for the result |
| 116 | while (!line.contains(FAILURE_PHRASE)) { |
| 117 | line = m_Proc->readLine(); |
| 118 | if (line.isNull()) |
| 119 | return true; |
| 120 | |
| 121 | // add line to output list |
| 122 | m_output << line; |
| 123 | qCDebug(log_cervisia) << "process output = " << line; |
| 124 | } |
| 125 | |
| 126 | result = false; |
| 127 | } else { |
| 128 | // user pressed cancel so kill the process |
| 129 | kill(m_Proc->pid(), SIGKILL); |
| 130 | m_Proc->waitForChild(); |
| 131 | result = false; |
| 132 | } |
| 133 | } |
| 134 | } |
| 135 | return false; |
| 136 | } |
| 137 | |
| 138 | QStringList CvsLoginJob::output() |
| 139 | { |
| 140 | return m_output; |
| 141 | } |