summaryrefslogtreecommitdiff
authordwmw2 <dwmw2>2002-09-23 08:37:16 (UTC)
committer dwmw2 <dwmw2>2002-09-23 08:37:16 (UTC)
commit8b06ea9e4407628ebae54d63592e8b8c940a62ad (patch) (unidiff)
tree344af00df0e403c5b5d5475e641489213ace46f7
parentd7098ef25f7f4ebe5678061aa3a3c0bd1f077f7f (diff)
downloadopie-8b06ea9e4407628ebae54d63592e8b8c940a62ad.zip
opie-8b06ea9e4407628ebae54d63592e8b8c940a62ad.tar.gz
opie-8b06ea9e4407628ebae54d63592e8b8c940a62ad.tar.bz2
Log stderr more sanely, fix buffer abuse. It is really sane to use '+=' on
a QString? Do I care? It's C++, let it do what it does best -- hide implementation details like the fact that some stuff _really_ sucks and encourage the programmer to do those things :)
Diffstat (more/less context) (ignore whitespace changes)
-rw-r--r--noncore/settings/sshkeys/sshkeys.cpp58
-rw-r--r--noncore/settings/sshkeys/sshkeys.h5
2 files changed, 54 insertions, 9 deletions
diff --git a/noncore/settings/sshkeys/sshkeys.cpp b/noncore/settings/sshkeys/sshkeys.cpp
index c5e1170..43e02af 100644
--- a/noncore/settings/sshkeys/sshkeys.cpp
+++ b/noncore/settings/sshkeys/sshkeys.cpp
@@ -20,119 +20,120 @@
20 */ 20 */
21#include "sshkeys.h" 21#include "sshkeys.h"
22 22
23#include <qpe/qpeapplication.h> 23#include <qpe/qpeapplication.h>
24#include <opie/oprocess.h> 24#include <opie/oprocess.h>
25#include <qmultilineedit.h> 25#include <qmultilineedit.h>
26#include <qpushbutton.h> 26#include <qpushbutton.h>
27#include <qlistview.h> 27#include <qlistview.h>
28#include <qcombobox.h> 28#include <qcombobox.h>
29 29
30#include <sys/types.h> 30#include <sys/types.h>
31#include <sys/stat.h> 31#include <sys/stat.h>
32#include <stdlib.h> 32#include <stdlib.h>
33#include <unistd.h> 33#include <unistd.h>
34#include <stdio.h> 34#include <stdio.h>
35#include <ctype.h> 35#include <ctype.h>
36 36
37static char *keynames[] = { "identity", "id_rsa", "id_dsa" }; 37static char *keynames[] = { "identity", "id_rsa", "id_dsa" };
38 38
39SSHKeysApp::SSHKeysApp( QWidget* parent, const char* name, WFlags fl ) 39SSHKeysApp::SSHKeysApp( QWidget* parent, const char* name, WFlags fl )
40 : SSHKeysBase( parent, name, fl ) 40 : SSHKeysBase( parent, name, fl )
41{ 41{
42 char *home = getenv("HOME"); 42 char *home = getenv("HOME");
43 unsigned i; 43 unsigned i;
44 44
45 connect(AddButton, SIGNAL(clicked()), this, SLOT(doAddButton())); 45 connect(AddButton, SIGNAL(clicked()), this, SLOT(doAddButton()));
46 connect(RefreshListButton, SIGNAL(clicked()), this, SLOT(doRefreshListButton())); 46 connect(RefreshListButton, SIGNAL(clicked()), this, SLOT(doRefreshListButton()));
47 connect(RemoveAllButton, SIGNAL(clicked()), this, SLOT(doRemoveAllButton())); 47 connect(RemoveAllButton, SIGNAL(clicked()), this, SLOT(doRemoveAllButton()));
48 48
49 connect(&addprocess, SIGNAL(receivedStdout(OProcess*,char*,int)), 49 connect(&addprocess, SIGNAL(receivedStdout(OProcess*,char*,int)),
50 this, SLOT(log_sshadd_output(OProcess*,char*,int))); 50 this, SLOT(log_sshadd_output(OProcess*,char*,int)));
51 connect(&addprocess, SIGNAL(receivedStderr(OProcess*,char*,int)), 51 connect(&addprocess, SIGNAL(receivedStderr(OProcess*,char*,int)),
52 this, SLOT(log_sshadd_output(OProcess*,char*,int))); 52 this, SLOT(log_sshadd_stderr(OProcess*,char*,int)));
53 connect(&addprocess, SIGNAL(processExited(OProcess*)), 53 connect(&addprocess, SIGNAL(processExited(OProcess*)),
54 this, SLOT(ssh_add_exited(OProcess*))); 54 this, SLOT(ssh_add_exited(OProcess*)));
55 55
56 connect(KeyFileName, SIGNAL(textChanged(const QString&)), 56 connect(KeyFileName, SIGNAL(textChanged(const QString&)),
57 this, SLOT(add_text_changed(const QString&))); 57 this, SLOT(add_text_changed(const QString&)));
58 58
59 if (home) { 59 if (home) {
60 for (i = 0; i < sizeof(keynames)/sizeof(keynames[0]); i++) { 60 for (i = 0; i < sizeof(keynames)/sizeof(keynames[0]); i++) {
61 char thiskeyname[32]; 61 char thiskeyname[32];
62 62
63 thiskeyname[31] = 0; 63 thiskeyname[31] = 0;
64 snprintf(thiskeyname, 31, "%s/.ssh/%s", home, keynames[i]); 64 snprintf(thiskeyname, 31, "%s/.ssh/%s", home, keynames[i]);
65 if (!access(thiskeyname, R_OK)) { 65 if (!access(thiskeyname, R_OK)) {
66 KeyFileName->insertItem(thiskeyname); 66 KeyFileName->insertItem(thiskeyname);
67 } 67 }
68 } 68 }
69 } 69 }
70 70
71 doRefreshListButton(); 71 doRefreshListButton();
72} 72}
73 73
74SSHKeysApp::~SSHKeysApp() 74SSHKeysApp::~SSHKeysApp()
75{ 75{
76} 76}
77 77
78void SSHKeysApp::doRefreshListButton() 78void SSHKeysApp::doRefreshListButton()
79{ 79{
80 OProcess sshadd_process; 80 OProcess sshadd_process;
81 QListViewItem *t = KeyList->firstChild(); 81 QListViewItem *t = KeyList->firstChild();
82 82
83 while(t) { 83 while(t) {
84 QListViewItem *next = t->nextSibling(); 84 QListViewItem *next = t->nextSibling();
85 KeyList->takeItem(t); 85 KeyList->takeItem(t);
86 delete(t); 86 delete(t);
87 t = next; 87 t = next;
88 } 88 }
89 89
90 connect(&sshadd_process, SIGNAL(receivedStdout(OProcess*,char*,int)), 90 connect(&sshadd_process, SIGNAL(receivedStdout(OProcess*,char*,int)),
91 this, SLOT(get_list_keys_output(OProcess*,char*,int))); 91 this, SLOT(get_list_keys_output(OProcess*,char*,int)));
92 connect(&sshadd_process, SIGNAL(receivedStderr(OProcess*,char*,int)), 92 connect(&sshadd_process, SIGNAL(receivedStderr(OProcess*,char*,int)),
93 this, SLOT(log_sshadd_output(OProcess*,char*,int))); 93 this, SLOT(log_sshadd_stderr(OProcess*,char*,int)));
94 94
95 keystate = KeySize; 95 keystate = KeySize;
96 incoming_keyname=""; 96 incoming_keyname="";
97 incoming_keysize=""; 97 incoming_keysize="";
98 incoming_keyfingerprint=""; 98 incoming_keyfingerprint="";
99 99
100 //log_text("Running ssh-add -l"); 100 //log_text("Running ssh-add -l");
101 sshadd_process << "ssh-add" << "-l"; 101 sshadd_process << "ssh-add" << "-l";
102 bool ret = sshadd_process.start(OProcess::Block, OProcess::AllOutput); 102 bool ret = sshadd_process.start(OProcess::Block, OProcess::AllOutput);
103 if (!ret) { 103 if (!ret) {
104 log_text(tr("Error running ssh-add")); 104 log_text(tr("Error running ssh-add"));
105 return; 105 return;
106 } 106 }
107 flush_sshadd_output();
107 if (sshadd_process.exitStatus() == 2) { 108 if (sshadd_process.exitStatus() == 2) {
108 setEnabled(FALSE); 109 setEnabled(FALSE);
109 } 110 }
110} 111}
111 112
112void SSHKeysApp::get_list_keys_output(OProcess *proc, char *buffer, int buflen) 113void SSHKeysApp::get_list_keys_output(OProcess *proc, char *buffer, int buflen)
113{ 114{
114 int i; 115 int i;
115 (void) proc; 116 (void) proc;
116 117
117 for (i=0; i<buflen; i++) { 118 for (i=0; i<buflen; i++) {
118 switch(keystate) { 119 switch(keystate) {
119 case Noise: 120 case Noise:
120 noise: 121 noise:
121 if (buffer[i] == '\n') { 122 if (buffer[i] == '\n') {
122 log_text(incoming_noise.local8Bit()); 123 log_text(incoming_noise.local8Bit());
123 incoming_noise = ""; 124 incoming_noise = "";
124 keystate = KeySize; 125 keystate = KeySize;
125 } else { 126 } else {
126 incoming_noise += buffer[i]; 127 incoming_noise += buffer[i];
127 } 128 }
128 break; 129 break;
129 130
130 case KeySize: 131 case KeySize:
131 if (isdigit(buffer[i])) { 132 if (isdigit(buffer[i])) {
132 incoming_keysize += buffer[i]; 133 incoming_keysize += buffer[i];
133 } else if (buffer[i] == ' ') { 134 } else if (buffer[i] == ' ') {
134 keystate = KeyFingerprint; 135 keystate = KeyFingerprint;
135 } else { 136 } else {
136 incoming_keysize = ""; 137 incoming_keysize = "";
137 incoming_noise = ""; 138 incoming_noise = "";
138 keystate = Noise; 139 keystate = Noise;
@@ -148,107 +149,146 @@ void SSHKeysApp::get_list_keys_output(OProcess *proc, char *buffer, int buflen)
148 } else { 149 } else {
149 incoming_keysize = ""; 150 incoming_keysize = "";
150 incoming_keyfingerprint = ""; 151 incoming_keyfingerprint = "";
151 incoming_noise = ""; 152 incoming_noise = "";
152 keystate = Noise; 153 keystate = Noise;
153 goto noise; 154 goto noise;
154 } 155 }
155 break; 156 break;
156 157
157 case KeyName: 158 case KeyName:
158 if (buffer[i] == '\n') { 159 if (buffer[i] == '\n') {
159 /* Wheee. Got one. */ 160 /* Wheee. Got one. */
160 KeyList->insertItem(new 161 KeyList->insertItem(new
161 QListViewItem(KeyList, incoming_keyname, incoming_keysize, incoming_keyfingerprint)); 162 QListViewItem(KeyList, incoming_keyname, incoming_keysize, incoming_keyfingerprint));
162 incoming_keysize = ""; 163 incoming_keysize = "";
163 incoming_keyfingerprint = ""; 164 incoming_keyfingerprint = "";
164 incoming_keyname = ""; 165 incoming_keyname = "";
165 keystate = KeySize; 166 keystate = KeySize;
166 } else if (isprint(buffer[i])) { 167 } else if (isprint(buffer[i])) {
167 incoming_keyname += buffer[i]; 168 incoming_keyname += buffer[i];
168 } else { 169 } else {
169 incoming_keysize = ""; 170 incoming_keysize = "";
170 incoming_keyfingerprint = ""; 171 incoming_keyfingerprint = "";
171 incoming_noise = ""; 172 incoming_noise = "";
172 keystate = Noise; 173 keystate = Noise;
173 goto noise; 174 goto noise;
174 } 175 }
175 break; 176 break;
176 } 177 }
177 } 178 }
178} 179}
179 180
181void SSHKeysApp::flush_sshadd_output(void)
182{
183 if (pending_stdout.length()) {
184 log_text(pending_stdout.ascii());
185 }
186 pending_stdout = "";
187
188 if (pending_stderr.length()) {
189 log_text(pending_stderr.ascii());
190 }
191 pending_stderr = "";
192}
193
180void SSHKeysApp::log_sshadd_output(OProcess *proc, char *buffer, int buflen) 194void SSHKeysApp::log_sshadd_output(OProcess *proc, char *buffer, int buflen)
181{ 195{
182 (void)proc; 196 (void) proc;
183 (void)buflen;
184 197
185 log_text(buffer); 198 while (buflen) {
199 if (*buffer == '\n') {
200 log_text(pending_stdout);
201 pending_stdout = "";
202 } else {
203 pending_stdout += *buffer;
204 }
205 buffer++;
206 buflen--;
207 }
208}
209
210void SSHKeysApp::log_sshadd_stderr(OProcess *proc, char *buffer, int buflen)
211{
212 (void) proc;
213
214 while (buflen) {
215 if (*buffer == '\n') {
216 log_text(pending_stderr);
217 pending_stderr = "";
218 } else {
219 pending_stderr += *buffer;
220 }
221 buffer++;
222 buflen--;
223 }
186} 224}
187 225
188void SSHKeysApp::ssh_add_exited(OProcess *proc) 226void SSHKeysApp::ssh_add_exited(OProcess *proc)
189{ 227{
190 (void)proc; 228 (void)proc;
191 229
192 doRefreshListButton(); 230 doRefreshListButton();
193 setEnabled(TRUE); 231 setEnabled(TRUE);
194 if (proc->exitStatus() == 2) { 232 if (proc->exitStatus() == 2) {
195 setEnabled(FALSE); 233 setEnabled(FALSE);
196 } 234 }
197} 235}
198 236
199void SSHKeysApp::add_text_changed(const QString &text) 237void SSHKeysApp::add_text_changed(const QString &text)
200{ 238{
201 struct stat sbuf; 239 struct stat sbuf;
202 240
203 if (!text.length() || (!access(text.ascii(), R_OK) && 241 if (!text.length() || (!access(text.ascii(), R_OK) &&
204 !stat(text.ascii(), &sbuf) && 242 !stat(text.ascii(), &sbuf) &&
205 S_ISREG(sbuf.st_mode))) 243 S_ISREG(sbuf.st_mode)))
206 AddButton->setEnabled(TRUE); 244 AddButton->setEnabled(TRUE);
207 else 245 else
208 AddButton->setEnabled(FALSE); 246 AddButton->setEnabled(FALSE);
209} 247}
210 248
211void SSHKeysApp::doAddButton() 249void SSHKeysApp::doAddButton()
212{ 250{
213 addprocess.clearArguments(); 251 addprocess.clearArguments();
214 252
215 setEnabled(FALSE); 253 setEnabled(FALSE);
216 254
217 if (KeyFileName->currentText().length()) { 255 if (KeyFileName->currentText().length()) {
218 addprocess << "ssh-add" << "--" << KeyFileName->currentText(); 256 addprocess << "ssh-add" << "--" << KeyFileName->currentText();
219 log_text(QString(tr("Running ssh-add -- ")) + KeyFileName->currentText()); 257 // log_text(QString(tr("Running ssh-add -- ")) + KeyFileName->currentText());
220 } else { 258 } else {
221 addprocess << "ssh-add"; 259 addprocess << "ssh-add";
222 log_text(tr("Running ssh-add")); 260 // log_text(tr("Running ssh-add"));
223 } 261 }
224 bool ret = addprocess.start(OProcess::NotifyOnExit, OProcess::AllOutput); 262 bool ret = addprocess.start(OProcess::NotifyOnExit, OProcess::AllOutput);
225 if (!ret) { 263 if (!ret) {
226 log_text(tr("Error running ssh-add")); 264 log_text(tr("Error running ssh-add"));
227 doRefreshListButton(); 265 doRefreshListButton();
228 setEnabled(TRUE); 266 setEnabled(TRUE);
229 } 267 }
268 flush_sshadd_output();
230} 269}
231 270
232void SSHKeysApp::log_text(const char *text) 271void SSHKeysApp::log_text(const char *text)
233{ 272{
234 TextOutput->append(text); 273 TextOutput->append(text);
235 TextOutput->setCursorPosition(TextOutput->numLines()+1, 0, FALSE); 274 TextOutput->setCursorPosition(TextOutput->numLines()+1, 0, FALSE);
236} 275}
237 276
238void SSHKeysApp::doRemoveAllButton() 277void SSHKeysApp::doRemoveAllButton()
239{ 278{
240 OProcess sshadd_process; 279 OProcess sshadd_process;
241 280
242 connect(&sshadd_process, SIGNAL(receivedStdout(OProcess*,char*,int)), 281 connect(&sshadd_process, SIGNAL(receivedStdout(OProcess*,char*,int)),
243 this, SLOT(log_sshadd_output(OProcess*,char*,int))); 282 this, SLOT(log_sshadd_output(OProcess*,char*,int)));
244 connect(&sshadd_process, SIGNAL(receivedStderr(OProcess*,char*,int)), 283 connect(&sshadd_process, SIGNAL(receivedStderr(OProcess*,char*,int)),
245 this, SLOT(log_sshadd_output(OProcess*,char*,int))); 284 this, SLOT(log_sshadd_stderr(OProcess*,char*,int)));
246 285
247 log_text(tr("Running ssh-add -D")); 286 //log_text(tr("Running ssh-add -D"));
248 sshadd_process << "ssh-add" << "-D"; 287 sshadd_process << "ssh-add" << "-D";
249 bool ret = sshadd_process.start(OProcess::Block, OProcess::AllOutput); 288 bool ret = sshadd_process.start(OProcess::Block, OProcess::AllOutput);
250 if (!ret) { 289 if (!ret) {
251 log_text(tr("Error running ssh-add")); 290 log_text(tr("Error running ssh-add"));
252 } 291 }
292 flush_sshadd_output();
253 doRefreshListButton(); 293 doRefreshListButton();
254} 294}
diff --git a/noncore/settings/sshkeys/sshkeys.h b/noncore/settings/sshkeys/sshkeys.h
index 5e306d1..bbc6993 100644
--- a/noncore/settings/sshkeys/sshkeys.h
+++ b/noncore/settings/sshkeys/sshkeys.h
@@ -6,50 +6,55 @@
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 21
22#ifndef SSHKEYSAPP_H 22#ifndef SSHKEYSAPP_H
23#define SSHKEYSAPP_H 23#define SSHKEYSAPP_H
24 24
25#include "sshkeysbase.h" 25#include "sshkeysbase.h"
26#include <opie/oprocess.h> 26#include <opie/oprocess.h>
27 27
28class SSHKeysApp : public SSHKeysBase 28class SSHKeysApp : public SSHKeysBase
29{ 29{
30 Q_OBJECT 30 Q_OBJECT
31 31
32 public: 32 public:
33 SSHKeysApp( QWidget* parent = 0, const char* name = 0, WFlags fl = 0 ); 33 SSHKeysApp( QWidget* parent = 0, const char* name = 0, WFlags fl = 0 );
34 ~SSHKeysApp(); 34 ~SSHKeysApp();
35 35
36 private: 36 private:
37 void log_text(const char *text); 37 void log_text(const char *text);
38 void flush_sshadd_output(void);
39
38 enum { Noise, KeyName, KeySize, KeyFingerprint } keystate; 40 enum { Noise, KeyName, KeySize, KeyFingerprint } keystate;
39 QString incoming_keyname; 41 QString incoming_keyname;
40 QString incoming_keysize; 42 QString incoming_keysize;
41 QString incoming_keyfingerprint; 43 QString incoming_keyfingerprint;
42 QString incoming_noise; 44 QString incoming_noise;
43 OProcess addprocess; 45 OProcess addprocess;
46 QString pending_stdout;
47 QString pending_stderr;
44 48
45 private slots: 49 private slots:
46 void doAddButton(); 50 void doAddButton();
47 void doRefreshListButton(); 51 void doRefreshListButton();
48 void doRemoveAllButton(); 52 void doRemoveAllButton();
49 void get_list_keys_output(OProcess *proc, char *buffer, int buflen); 53 void get_list_keys_output(OProcess *proc, char *buffer, int buflen);
50 void log_sshadd_output(OProcess *proc, char *buffer, int buflen); 54 void log_sshadd_output(OProcess *proc, char *buffer, int buflen);
55 void log_sshadd_stderr(OProcess *proc, char *buffer, int buflen);
51 void ssh_add_exited(OProcess *proc); 56 void ssh_add_exited(OProcess *proc);
52 void add_text_changed(const QString &text); 57 void add_text_changed(const QString &text);
53}; 58};
54#endif 59#endif
55 60