summaryrefslogtreecommitdiff
Unidiff
Diffstat (more/less context) (ignore whitespace changes)
-rw-r--r--noncore/settings/sshkeys/sshkeys.cpp15
1 files changed, 11 insertions, 4 deletions
diff --git a/noncore/settings/sshkeys/sshkeys.cpp b/noncore/settings/sshkeys/sshkeys.cpp
index d8b1014..57118c9 100644
--- a/noncore/settings/sshkeys/sshkeys.cpp
+++ b/noncore/settings/sshkeys/sshkeys.cpp
@@ -1,306 +1,313 @@
1/* 1/*
2 * ssh-agent key manipulation utility 2 * ssh-agent key manipulation utility
3 * 3 *
4 * (C) 2002 David Woodhouse <dwmw2@infradead.org> 4 * (C) 2002 David Woodhouse <dwmw2@infradead.org>
5 * 5 *
6 * This program is free software; you can redistribute it and/or modify 6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by 7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or 8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version. 9 * (at your option) any later version.
10 * 10 *
11 * This program is distributed in the hope that it will be useful, 11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details. 14 * GNU General Public License for more details.
15 * 15 *
16 * You should have received a copy of the GNU General Public License 16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software 17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 * 19 *
20 */ 20 */
21#include "sshkeys.h" 21#include "sshkeys.h"
22 22
23#include <qpe/qpeapplication.h> 23#include <qpe/qpeapplication.h>
24#include <qdir.h> 24#include <qdir.h>
25#include <qfile.h> 25#include <qfile.h>
26#include <qmultilineedit.h> 26#include <qmultilineedit.h>
27#include <qpushbutton.h> 27#include <qpushbutton.h>
28#include <qlistview.h> 28#include <qlistview.h>
29#include <qcombobox.h> 29#include <qcombobox.h>
30 30
31#include <sys/types.h> 31#include <sys/types.h>
32#include <sys/stat.h> 32#include <sys/stat.h>
33#include <fcntl.h> 33#include <fcntl.h>
34#include <sys/ioctl.h> 34#include <sys/ioctl.h>
35#include <stdlib.h> 35#include <stdlib.h>
36#include <unistd.h> 36#include <unistd.h>
37#include <stdio.h> 37#include <stdio.h>
38#include <ctype.h> 38#include <ctype.h>
39 39
40using namespace Opie::Core; 40using namespace Opie::Core;
41static char *keynames[] = { "identity", "id_rsa", "id_dsa" }; 41static const char *keynames[] = { "identity", "id_rsa", "id_dsa" };
42 42
43SSHKeysApp::SSHKeysApp( QWidget* parent, const char* name, WFlags fl ) 43
44 : SSHKeysBase( parent, name, fl ) 44static void detachTerminal() {
45{
46 /* If we had a controlling TTY, detach from it. 45 /* If we had a controlling TTY, detach from it.
47 This is to ensure that SSH uses ssh-askpass */ 46 This is to ensure that SSH uses ssh-askpass */
48 int fd = open("/dev/tty", O_RDONLY); 47 int fd = open("/dev/tty", O_RDONLY);
49 if (fd != -1) { 48 if (fd != -1) {
50 ioctl(fd, TIOCNOTTY, NULL); 49 ioctl(fd, TIOCNOTTY, NULL);
51 close(fd); 50 close(fd);
52 } 51 }
52}
53
54
55SSHKeysApp::SSHKeysApp( QWidget* parent, const char* name, WFlags fl )
56 : SSHKeysBase( parent, name, fl )
57{
58 detachTerminal();
59
53 60
54 QCString home = QFile::encodeName( QDir::homeDirPath() ); 61 QCString home = QFile::encodeName( QDir::homeDirPath() );
55 unsigned i; 62 unsigned i;
56 63
57 connect(AddButton, SIGNAL(clicked()), this, SLOT(doAddButton())); 64 connect(AddButton, SIGNAL(clicked()), this, SLOT(doAddButton()));
58 connect(RefreshListButton, SIGNAL(clicked()), this, SLOT(doRefreshListButton())); 65 connect(RefreshListButton, SIGNAL(clicked()), this, SLOT(doRefreshListButton()));
59 connect(RemoveAllButton, SIGNAL(clicked()), this, SLOT(doRemoveAllButton())); 66 connect(RemoveAllButton, SIGNAL(clicked()), this, SLOT(doRemoveAllButton()));
60 67
61 connect(&addprocess, SIGNAL(receivedStdout(Opie::Core::OProcess*,char*,int)), 68 connect(&addprocess, SIGNAL(receivedStdout(Opie::Core::OProcess*,char*,int)),
62 this, SLOT(log_sshadd_output(Opie::Core::OProcess*,char*,int))); 69 this, SLOT(log_sshadd_output(Opie::Core::OProcess*,char*,int)));
63 connect(&addprocess, SIGNAL(receivedStderr(Opie::Core::OProcess*,char*,int)), 70 connect(&addprocess, SIGNAL(receivedStderr(Opie::Core::OProcess*,char*,int)),
64 this, SLOT(log_sshadd_stderr(Opie::Core::OProcess*,char*,int))); 71 this, SLOT(log_sshadd_stderr(Opie::Core::OProcess*,char*,int)));
65 connect(&addprocess, SIGNAL(processExited(Opie::Core::OProcess*)), 72 connect(&addprocess, SIGNAL(processExited(Opie::Core::OProcess*)),
66 this, SLOT(ssh_add_exited(Opie::Core::OProcess*))); 73 this, SLOT(ssh_add_exited(Opie::Core::OProcess*)));
67 74
68 connect(KeyFileName, SIGNAL(textChanged(const QString&)), 75 connect(KeyFileName, SIGNAL(textChanged(const QString&)),
69 this, SLOT(add_text_changed(const QString&))); 76 this, SLOT(add_text_changed(const QString&)));
70 77
71 if (home) { 78 if (home) {
72 for (i = 0; i < sizeof(keynames)/sizeof(keynames[0]); i++) { 79 for (i = 0; i < sizeof(keynames)/sizeof(keynames[0]); i++) {
73 char thiskeyname[32]; 80 char thiskeyname[32];
74 81
75 thiskeyname[31] = 0; 82 thiskeyname[31] = 0;
76 snprintf(thiskeyname, 31, "%s/.ssh/%s", home.data(), keynames[i]); 83 snprintf(thiskeyname, 31, "%s/.ssh/%s", home.data(), keynames[i]);
77 if (!access(thiskeyname, R_OK)) { 84 if (!access(thiskeyname, R_OK)) {
78 KeyFileName->insertItem(thiskeyname); 85 KeyFileName->insertItem(thiskeyname);
79 } 86 }
80 } 87 }
81 } 88 }
82 89
83 doRefreshListButton(); 90 doRefreshListButton();
84} 91}
85 92
86SSHKeysApp::~SSHKeysApp() 93SSHKeysApp::~SSHKeysApp()
87{ 94{
88} 95}
89 96
90void SSHKeysApp::doRefreshListButton() 97void SSHKeysApp::doRefreshListButton()
91{ 98{
92 OProcess sshadd_process; 99 OProcess sshadd_process;
93 QListViewItem *t = KeyList->firstChild(); 100 QListViewItem *t = KeyList->firstChild();
94 101
95 while(t) { 102 while(t) {
96 QListViewItem *next = t->nextSibling(); 103 QListViewItem *next = t->nextSibling();
97 KeyList->takeItem(t); 104 KeyList->takeItem(t);
98 delete(t); 105 delete(t);
99 t = next; 106 t = next;
100 } 107 }
101 108
102 connect(&sshadd_process, SIGNAL(receivedStdout(Opie::Core::OProcess*,char*,int)), 109 connect(&sshadd_process, SIGNAL(receivedStdout(Opie::Core::OProcess*,char*,int)),
103 this, SLOT(get_list_keys_output(Opie::Core::OProcess*,char*,int))); 110 this, SLOT(get_list_keys_output(Opie::Core::OProcess*,char*,int)));
104 connect(&sshadd_process, SIGNAL(receivedStderr(Opie::Core::OProcess*,char*,int)), 111 connect(&sshadd_process, SIGNAL(receivedStderr(Opie::Core::OProcess*,char*,int)),
105 this, SLOT(log_sshadd_stderr(Opie::Core::OProcess*,char*,int))); 112 this, SLOT(log_sshadd_stderr(Opie::Core::OProcess*,char*,int)));
106 113
107 keystate = KeySize; 114 keystate = KeySize;
108 incoming_keyname=""; 115 incoming_keyname="";
109 incoming_keysize=""; 116 incoming_keysize="";
110 incoming_keyfingerprint=""; 117 incoming_keyfingerprint="";
111 118
112// log_text("Running ssh-add -l"); 119// log_text("Running ssh-add -l");
113 sshadd_process << "ssh-add" << "-l"; 120 sshadd_process << "ssh-add" << "-l";
114 bool ret = sshadd_process.start(OProcess::Block, OProcess::AllOutput); 121 bool ret = sshadd_process.start(OProcess::Block, OProcess::AllOutput);
115 if (!ret) { 122 if (!ret) {
116 log_text(tr("Error running ssh-add")); 123 log_text(tr("Error running ssh-add"));
117 return; 124 return;
118 } 125 }
119 flush_sshadd_output(); 126 flush_sshadd_output();
120 if (sshadd_process.exitStatus() == 2) { 127 if (sshadd_process.exitStatus() == 2) {
121 setEnabled(FALSE); 128 setEnabled(FALSE);
122 } 129 }
123} 130}
124 131
125void SSHKeysApp::get_list_keys_output(OProcess *proc, char *buffer, int buflen) 132void SSHKeysApp::get_list_keys_output(OProcess *proc, char *buffer, int buflen)
126{ 133{
127 int i; 134 int i;
128 (void) proc; 135 (void) proc;
129 136
130 for (i=0; i<buflen; i++) { 137 for (i=0; i<buflen; i++) {
131 switch(keystate) { 138 switch(keystate) {
132 case Noise: 139 case Noise:
133 noise: 140 noise:
134 if (buffer[i] == '\n') { 141 if (buffer[i] == '\n') {
135 log_text(incoming_noise.local8Bit()); 142 log_text(incoming_noise.local8Bit());
136 incoming_noise = ""; 143 incoming_noise = "";
137 keystate = KeySize; 144 keystate = KeySize;
138 } else { 145 } else {
139 incoming_noise += buffer[i]; 146 incoming_noise += buffer[i];
140 } 147 }
141 break; 148 break;
142 149
143 case KeySize: 150 case KeySize:
144 if (isdigit(buffer[i])) { 151 if (isdigit(buffer[i])) {
145 incoming_keysize += buffer[i]; 152 incoming_keysize += buffer[i];
146 } else if (buffer[i] == ' ') { 153 } else if (buffer[i] == ' ') {
147 keystate = KeyFingerprint; 154 keystate = KeyFingerprint;
148 } else { 155 } else {
149 incoming_keysize = ""; 156 incoming_keysize = "";
150 incoming_noise = ""; 157 incoming_noise = "";
151 keystate = Noise; 158 keystate = Noise;
152 goto noise; 159 goto noise;
153 } 160 }
154 break; 161 break;
155 162
156 case KeyFingerprint: 163 case KeyFingerprint:
157 if (isxdigit(buffer[i]) || buffer[i] == ':') { 164 if (isxdigit(buffer[i]) || buffer[i] == ':') {
158 incoming_keyfingerprint += buffer[i]; 165 incoming_keyfingerprint += buffer[i];
159 } else if (buffer[i] == ' ') { 166 } else if (buffer[i] == ' ') {
160 keystate = KeyName; 167 keystate = KeyName;
161 } else { 168 } else {
162 incoming_keysize = ""; 169 incoming_keysize = "";
163 incoming_keyfingerprint = ""; 170 incoming_keyfingerprint = "";
164 incoming_noise = ""; 171 incoming_noise = "";
165 keystate = Noise; 172 keystate = Noise;
166 goto noise; 173 goto noise;
167 } 174 }
168 break; 175 break;
169 176
170 case KeyName: 177 case KeyName:
171 if (buffer[i] == '\n') { 178 if (buffer[i] == '\n') {
172 /* Wheee. Got one. */ 179 /* Wheee. Got one. */
173 KeyList->insertItem(new 180 KeyList->insertItem(new
174 QListViewItem(KeyList, incoming_keyname, incoming_keysize, incoming_keyfingerprint)); 181 QListViewItem(KeyList, incoming_keyname, incoming_keysize, incoming_keyfingerprint));
175 incoming_keysize = ""; 182 incoming_keysize = "";
176 incoming_keyfingerprint = ""; 183 incoming_keyfingerprint = "";
177 incoming_keyname = ""; 184 incoming_keyname = "";
178 keystate = KeySize; 185 keystate = KeySize;
179 } else if (isprint(buffer[i])) { 186 } else if (isprint(buffer[i])) {
180 incoming_keyname += buffer[i]; 187 incoming_keyname += buffer[i];
181 } else { 188 } else {
182 incoming_keysize = ""; 189 incoming_keysize = "";
183 incoming_keyfingerprint = ""; 190 incoming_keyfingerprint = "";
184 incoming_noise = ""; 191 incoming_noise = "";
185 keystate = Noise; 192 keystate = Noise;
186 goto noise; 193 goto noise;
187 } 194 }
188 break; 195 break;
189 } 196 }
190 } 197 }
191} 198}
192 199
193void SSHKeysApp::flush_sshadd_output(void) 200void SSHKeysApp::flush_sshadd_output(void)
194{ 201{
195 if (pending_stdout.length()) { 202 if (pending_stdout.length()) {
196 log_text(pending_stdout.ascii()); 203 log_text(pending_stdout.ascii());
197 } 204 }
198 pending_stdout = ""; 205 pending_stdout = "";
199 206
200 if (pending_stderr.length()) { 207 if (pending_stderr.length()) {
201 log_text(pending_stderr.ascii()); 208 log_text(pending_stderr.ascii());
202 } 209 }
203 pending_stderr = ""; 210 pending_stderr = "";
204} 211}
205 212
206void SSHKeysApp::log_sshadd_output(OProcess *proc, char *buffer, int buflen) 213void SSHKeysApp::log_sshadd_output(OProcess *proc, char *buffer, int buflen)
207{ 214{
208 (void) proc; 215 (void) proc;
209 216
210 while (buflen) { 217 while (buflen) {
211 if (*buffer == '\n') { 218 if (*buffer == '\n') {
212 log_text(pending_stdout); 219 log_text(pending_stdout);
213 pending_stdout = ""; 220 pending_stdout = "";
214 } else { 221 } else {
215 pending_stdout += *buffer; 222 pending_stdout += *buffer;
216 } 223 }
217 buffer++; 224 buffer++;
218 buflen--; 225 buflen--;
219 } 226 }
220} 227}
221 228
222void SSHKeysApp::log_sshadd_stderr(OProcess *proc, char *buffer, int buflen) 229void SSHKeysApp::log_sshadd_stderr(OProcess *proc, char *buffer, int buflen)
223{ 230{
224 (void) proc; 231 (void) proc;
225 232
226 while (buflen) { 233 while (buflen) {
227 if (*buffer == '\n') { 234 if (*buffer == '\n') {
228 log_text(pending_stderr); 235 log_text(pending_stderr);
229 pending_stderr = ""; 236 pending_stderr = "";
230 } else { 237 } else {
231 pending_stderr += *buffer; 238 pending_stderr += *buffer;
232 } 239 }
233 buffer++; 240 buffer++;
234 buflen--; 241 buflen--;
235 } 242 }
236} 243}
237 244
238void SSHKeysApp::ssh_add_exited(OProcess *proc) 245void SSHKeysApp::ssh_add_exited(OProcess *proc)
239{ 246{
240 (void)proc; 247 (void)proc;
241 248
242 doRefreshListButton(); 249 doRefreshListButton();
243 setEnabled(TRUE); 250 setEnabled(TRUE);
244 if (proc->exitStatus() == 2) { 251 if (proc->exitStatus() == 2) {
245 setEnabled(FALSE); 252 setEnabled(FALSE);
246 } 253 }
247} 254}
248 255
249void SSHKeysApp::add_text_changed(const QString &text) 256void SSHKeysApp::add_text_changed(const QString &text)
250{ 257{
251 struct stat sbuf; 258 struct stat sbuf;
252 259
253 if (!text.length() || (!access(text.ascii(), R_OK) && 260 if (!text.length() || (!access(text.ascii(), R_OK) &&
254 !stat(text.ascii(), &sbuf) && 261 !stat(text.ascii(), &sbuf) &&
255 S_ISREG(sbuf.st_mode))) 262 S_ISREG(sbuf.st_mode)))
256 AddButton->setEnabled(TRUE); 263 AddButton->setEnabled(TRUE);
257 else 264 else
258 AddButton->setEnabled(FALSE); 265 AddButton->setEnabled(FALSE);
259} 266}
260 267
261void SSHKeysApp::doAddButton() 268void SSHKeysApp::doAddButton()
262{ 269{
263 addprocess.clearArguments(); 270 addprocess.clearArguments();
264 271
265 setEnabled(FALSE); 272 setEnabled(FALSE);
266 273
267 if (KeyFileName->currentText().length()) { 274 if (KeyFileName->currentText().length()) {
268 addprocess << "ssh-add" << "--" << KeyFileName->currentText(); 275 addprocess << "ssh-add" << "--" << KeyFileName->currentText();
269// log_text(QString(tr("Running ssh-add -- ")) + KeyFileName->currentText()); 276// log_text(QString(tr("Running ssh-add -- ")) + KeyFileName->currentText());
270 } else { 277 } else {
271 addprocess << "ssh-add"; 278 addprocess << "ssh-add";
272// log_text(tr("Running ssh-add")); 279// log_text(tr("Running ssh-add"));
273 } 280 }
274 bool ret = addprocess.start(OProcess::NotifyOnExit, OProcess::AllOutput); 281 bool ret = addprocess.start(OProcess::NotifyOnExit, OProcess::AllOutput);
275 if (!ret) { 282 if (!ret) {
276 log_text(tr("Error running ssh-add")); 283 log_text(tr("Error running ssh-add"));
277 doRefreshListButton(); 284 doRefreshListButton();
278 setEnabled(TRUE); 285 setEnabled(TRUE);
279 } 286 }
280 flush_sshadd_output(); 287 flush_sshadd_output();
281} 288}
282 289
283void SSHKeysApp::log_text(const char *text) 290void SSHKeysApp::log_text(const char *text)
284{ 291{
285 TextOutput->append(text); 292 TextOutput->append(text);
286 TextOutput->setCursorPosition(TextOutput->numLines()+1, 0, FALSE); 293 TextOutput->setCursorPosition(TextOutput->numLines()+1, 0, FALSE);
287} 294}
288 295
289void SSHKeysApp::doRemoveAllButton() 296void SSHKeysApp::doRemoveAllButton()
290{ 297{
291 OProcess sshadd_process; 298 OProcess sshadd_process;
292 299
293 connect(&sshadd_process, SIGNAL(receivedStdout(Opie::Core::OProcess*,char*,int)), 300 connect(&sshadd_process, SIGNAL(receivedStdout(Opie::Core::OProcess*,char*,int)),
294 this, SLOT(log_sshadd_output(Opie::Core::OProcess*,char*,int))); 301 this, SLOT(log_sshadd_output(Opie::Core::OProcess*,char*,int)));
295 connect(&sshadd_process, SIGNAL(receivedStderr(Opie::Core::OProcess*,char*,int)), 302 connect(&sshadd_process, SIGNAL(receivedStderr(Opie::Core::OProcess*,char*,int)),
296 this, SLOT(log_sshadd_stderr(Opie::Core::OProcess*,char*,int))); 303 this, SLOT(log_sshadd_stderr(Opie::Core::OProcess*,char*,int)));
297 304
298// log_text(tr("Running ssh-add -D")); 305// log_text(tr("Running ssh-add -D"));
299 sshadd_process << "ssh-add" << "-D"; 306 sshadd_process << "ssh-add" << "-D";
300 bool ret = sshadd_process.start(OProcess::Block, OProcess::AllOutput); 307 bool ret = sshadd_process.start(OProcess::Block, OProcess::AllOutput);
301 if (!ret) { 308 if (!ret) {
302 log_text(tr("Error running ssh-add")); 309 log_text(tr("Error running ssh-add"));
303 } 310 }
304 flush_sshadd_output(); 311 flush_sshadd_output();
305 doRefreshListButton(); 312 doRefreshListButton();
306} 313}