| 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 "sshagent.h" |
| 22 | #include "../debug.h" |
| 23 | |
| 24 | #include <kprocess.h> |
| 25 | #include <qregexp.h> |
| 26 | |
| 27 | #include <csignal> |
| 28 | |
| 29 | // initialize static member variables |
| 30 | bool SshAgent::m_isRunning = false; |
| 31 | bool SshAgent::m_isOurAgent = false; |
| 32 | QString SshAgent::m_authSock; |
| 33 | QString SshAgent::m_pid; |
| 34 | |
| 35 | SshAgent::SshAgent(QObject *parent) |
| 36 | : QObject(parent) |
| 37 | , m_agentProcess(0) |
| 38 | { |
| 39 | } |
| 40 | |
| 41 | SshAgent::~SshAgent() = default; |
| 42 | |
| 43 | bool SshAgent::querySshAgent() |
| 44 | { |
| 45 | qCDebug(log_cervisia) << "ENTER"; |
| 46 | |
| 47 | if (m_isRunning) |
| 48 | return true; |
| 49 | |
| 50 | // Did the user already start a ssh-agent process? |
| 51 | const QByteArray pid = qgetenv("SSH_AGENT_PID"); |
| 52 | if (!pid.isEmpty()) { |
| 53 | qCDebug(log_cervisia) << "ssh-agent already exists"; |
| 54 | |
| 55 | m_pid = QString::fromLocal8Bit(pid); |
| 56 | |
| 57 | const QByteArray sock = qgetenv("SSH_AUTH_SOCK"); |
| 58 | if (!sock.isEmpty()) |
| 59 | m_authSock = QString::fromLocal8Bit(sock); |
| 60 | |
| 61 | m_isOurAgent = false; |
| 62 | m_isRunning = true; |
| 63 | } |
| 64 | // We have to start a new ssh-agent process |
| 65 | else { |
| 66 | qCDebug(log_cervisia) << "start ssh-agent"; |
| 67 | |
| 68 | m_isOurAgent = true; |
| 69 | m_isRunning = startSshAgent(); |
| 70 | } |
| 71 | |
| 72 | return m_isRunning; |
| 73 | } |
| 74 | |
| 75 | bool SshAgent::addSshIdentities() |
| 76 | { |
| 77 | qCDebug(log_cervisia) << "ENTER"; |
| 78 | |
| 79 | if (!m_isRunning || !m_isOurAgent) |
| 80 | return false; |
| 81 | |
| 82 | // add identities to ssh-agent |
| 83 | KProcess proc; |
| 84 | |
| 85 | proc.setEnv("SSH_AGENT_PID", m_pid); |
| 86 | proc.setEnv("SSH_AUTH_SOCK", m_authSock); |
| 87 | proc.setEnv("SSH_ASKPASS", "cvsaskpass"); |
| 88 | |
| 89 | proc << "ssh-add"; |
| 90 | |
| 91 | proc.start(); |
| 92 | |
| 93 | // wait for process to finish |
| 94 | // TODO CL use timeout? |
| 95 | proc.waitForFinished(); |
| 96 | |
| 97 | qCDebug(log_cervisia) << "added identities"; |
| 98 | |
| 99 | return (proc.exitStatus() == QProcess::NormalExit && proc.exitCode() == 0); |
| 100 | } |
| 101 | |
| 102 | void SshAgent::killSshAgent() |
| 103 | { |
| 104 | qCDebug(log_cervisia) << "ENTER"; |
| 105 | |
| 106 | if (!m_isRunning || !m_isOurAgent) |
| 107 | return; |
| 108 | |
| 109 | kill(m_pid.toInt(), SIGTERM); |
| 110 | |
| 111 | qCDebug(log_cervisia) << "killed pid=" << m_pid; |
| 112 | } |
| 113 | |
| 114 | void SshAgent::slotProcessFinished() |
| 115 | { |
| 116 | qCDebug(log_cervisia) << "ENTER"; |
| 117 | |
| 118 | QRegExp cshPidRx("setenv SSH_AGENT_PID (\\d*);"); |
| 119 | QRegExp cshSockRx("setenv SSH_AUTH_SOCK (.*);"); |
| 120 | |
| 121 | QRegExp bashPidRx("SSH_AGENT_PID=(\\d*).*"); |
| 122 | QRegExp bashSockRx("SSH_AUTH_SOCK=(.*\\.\\d*);.*"); |
| 123 | |
| 124 | foreach (const QString &line, m_outputLines) { |
| 125 | if (m_pid.isEmpty()) { |
| 126 | if (line.contains(cshPidRx)) { |
| 127 | m_pid = cshPidRx.cap(1); |
| 128 | continue; |
| 129 | } |
| 130 | |
| 131 | if (line.contains(bashPidRx)) { |
| 132 | m_pid = bashPidRx.cap(1); |
| 133 | continue; |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | if (m_authSock.isEmpty()) { |
| 138 | if (line.contains(cshSockRx)) { |
| 139 | m_authSock = cshSockRx.cap(1); |
| 140 | continue; |
| 141 | } |
| 142 | |
| 143 | if (line.contains(bashSockRx)) { |
| 144 | m_authSock = bashSockRx.cap(1); |
| 145 | continue; |
| 146 | } |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | qCDebug(log_cervisia) << "pid=" << m_pid << ", socket=" << m_authSock; |
| 151 | } |
| 152 | |
| 153 | void SshAgent::slotReceivedOutput() |
| 154 | { |
| 155 | const QString output(QString::fromLocal8Bit(m_agentProcess->readAllStandardOutput())); |
| 156 | |
| 157 | m_outputLines += output.split('\n'); |
| 158 | |
| 159 | qCDebug(log_cervisia) << "output=" << output; |
| 160 | } |
| 161 | |
| 162 | bool SshAgent::startSshAgent() |
| 163 | { |
| 164 | qCDebug(log_cervisia) << "ENTER"; |
| 165 | |
| 166 | m_agentProcess = new KProcess(this); |
| 167 | |
| 168 | connect(m_agentProcess, SIGNAL(finished(int, QProcess::ExitStatus)), SLOT(slotProcessFinished())); |
| 169 | connect(m_agentProcess, SIGNAL(readyReadStandardOutput()), SLOT(slotReceivedOutput())); |
| 170 | |
| 171 | m_agentProcess->setOutputChannelMode(KProcess::MergedChannels); |
| 172 | m_agentProcess->setProgram(QLatin1String("ssh-agent")); |
| 173 | m_agentProcess->start(); |
| 174 | |
| 175 | // wait for process to finish |
| 176 | // TODO CL use timeout? |
| 177 | m_agentProcess->waitForFinished(); |
| 178 | |
| 179 | return (m_agentProcess->exitStatus() == QProcess::NormalExit && m_agentProcess->exitCode() == 0); |
| 180 | } |