-rw-r--r-- | noncore/settings/sshkeys/sshkeys.cpp | 5 |
1 files changed, 4 insertions, 1 deletions
diff --git a/noncore/settings/sshkeys/sshkeys.cpp b/noncore/settings/sshkeys/sshkeys.cpp index 701356a..3b4dce6 100644 --- a/noncore/settings/sshkeys/sshkeys.cpp +++ b/noncore/settings/sshkeys/sshkeys.cpp | |||
@@ -1,227 +1,230 @@ | |||
1 | #include "sshkeys.h" | 1 | #include "sshkeys.h" |
2 | 2 | ||
3 | #include <qpe/qpeapplication.h> | 3 | #include <qpe/qpeapplication.h> |
4 | #include <opie/oprocess.h> | 4 | #include <opie/oprocess.h> |
5 | #include <qmultilineedit.h> | 5 | #include <qmultilineedit.h> |
6 | #include <qpushbutton.h> | 6 | #include <qpushbutton.h> |
7 | #include <qlistview.h> | 7 | #include <qlistview.h> |
8 | #include <qcombobox.h> | 8 | #include <qcombobox.h> |
9 | 9 | ||
10 | #include <sys/types.h> | 10 | #include <sys/types.h> |
11 | #include <sys/stat.h> | 11 | #include <sys/stat.h> |
12 | #include <stdlib.h> | 12 | #include <stdlib.h> |
13 | #include <unistd.h> | 13 | #include <unistd.h> |
14 | #include <stdio.h> | 14 | #include <stdio.h> |
15 | #include <ctype.h> | 15 | #include <ctype.h> |
16 | 16 | ||
17 | static char *keynames[] = { "identity", "id_rsa", "id_dsa" }; | 17 | static char *keynames[] = { "identity", "id_rsa", "id_dsa" }; |
18 | 18 | ||
19 | SSHKeysApp::SSHKeysApp( QWidget* parent, const char* name, WFlags fl ) | 19 | SSHKeysApp::SSHKeysApp( QWidget* parent, const char* name, WFlags fl ) |
20 | : SSHKeysBase( parent, name, fl ) | 20 | : SSHKeysBase( parent, name, fl ) |
21 | { | 21 | { |
22 | char *home = getenv("HOME"); | 22 | char *home = getenv("HOME"); |
23 | unsigned i; | 23 | unsigned i; |
24 | 24 | ||
25 | connect(AddButton, SIGNAL(clicked()), this, SLOT(doAddButton())); | 25 | connect(AddButton, SIGNAL(clicked()), this, SLOT(doAddButton())); |
26 | connect(RefreshListButton, SIGNAL(clicked()), this, SLOT(doRefreshListButton())); | 26 | connect(RefreshListButton, SIGNAL(clicked()), this, SLOT(doRefreshListButton())); |
27 | connect(RemoveAllButton, SIGNAL(clicked()), this, SLOT(doRemoveAllButton())); | 27 | connect(RemoveAllButton, SIGNAL(clicked()), this, SLOT(doRemoveAllButton())); |
28 | 28 | ||
29 | connect(&addprocess, SIGNAL(receivedStdout(OProcess*,char*,int)), | 29 | connect(&addprocess, SIGNAL(receivedStdout(OProcess*,char*,int)), |
30 | this, SLOT(log_sshadd_output(OProcess*,char*,int))); | 30 | this, SLOT(log_sshadd_output(OProcess*,char*,int))); |
31 | connect(&addprocess, SIGNAL(processExited(OProcess*)), | 31 | connect(&addprocess, SIGNAL(processExited(OProcess*)), |
32 | this, SLOT(ssh_add_exited(OProcess*))); | 32 | this, SLOT(ssh_add_exited(OProcess*))); |
33 | 33 | ||
34 | connect(KeyFileName, SIGNAL(textChanged(const QString&)), | 34 | connect(KeyFileName, SIGNAL(textChanged(const QString&)), |
35 | this, SLOT(add_text_changed(const QString&))); | 35 | this, SLOT(add_text_changed(const QString&))); |
36 | 36 | ||
37 | if (home) { | 37 | if (home) { |
38 | for (i = 0; i < sizeof(keynames)/sizeof(keynames[0]); i++) { | 38 | for (i = 0; i < sizeof(keynames)/sizeof(keynames[0]); i++) { |
39 | char thiskeyname[32]; | 39 | char thiskeyname[32]; |
40 | 40 | ||
41 | thiskeyname[31] = 0; | 41 | thiskeyname[31] = 0; |
42 | snprintf(thiskeyname, 31, "%s/.ssh/%s", home, keynames[i]); | 42 | snprintf(thiskeyname, 31, "%s/.ssh/%s", home, keynames[i]); |
43 | if (!access(thiskeyname, R_OK)) { | 43 | if (!access(thiskeyname, R_OK)) { |
44 | KeyFileName->insertItem(thiskeyname); | 44 | KeyFileName->insertItem(thiskeyname); |
45 | } | 45 | } |
46 | } | 46 | } |
47 | } | 47 | } |
48 | 48 | ||
49 | doRefreshListButton(); | 49 | doRefreshListButton(); |
50 | } | 50 | } |
51 | 51 | ||
52 | SSHKeysApp::~SSHKeysApp() | 52 | SSHKeysApp::~SSHKeysApp() |
53 | { | 53 | { |
54 | } | 54 | } |
55 | 55 | ||
56 | void SSHKeysApp::doRefreshListButton() | 56 | void SSHKeysApp::doRefreshListButton() |
57 | { | 57 | { |
58 | OProcess sshadd_process; | 58 | OProcess sshadd_process; |
59 | QListViewItem *t = KeyList->firstChild(); | 59 | QListViewItem *t = KeyList->firstChild(); |
60 | 60 | ||
61 | while(t) { | 61 | while(t) { |
62 | QListViewItem *next = t->nextSibling(); | 62 | QListViewItem *next = t->nextSibling(); |
63 | KeyList->takeItem(t); | 63 | KeyList->takeItem(t); |
64 | delete(t); | 64 | delete(t); |
65 | t = next; | 65 | t = next; |
66 | } | 66 | } |
67 | 67 | ||
68 | connect(&sshadd_process, SIGNAL(receivedStdout(OProcess*,char*,int)), | 68 | connect(&sshadd_process, SIGNAL(receivedStdout(OProcess*,char*,int)), |
69 | this, SLOT(get_list_keys_output(OProcess*,char*,int))); | 69 | this, SLOT(get_list_keys_output(OProcess*,char*,int))); |
70 | 70 | ||
71 | keystate = KeySize; | 71 | keystate = KeySize; |
72 | incoming_keyname=""; | 72 | incoming_keyname=""; |
73 | incoming_keysize=""; | 73 | incoming_keysize=""; |
74 | incoming_keyfingerprint=""; | 74 | incoming_keyfingerprint=""; |
75 | 75 | ||
76 | //log_text("Running ssh-add -l"); | 76 | //log_text("Running ssh-add -l"); |
77 | sshadd_process << "ssh-add" << "-l"; | 77 | sshadd_process << "ssh-add" << "-l"; |
78 | bool ret = sshadd_process.start(OProcess::Block, OProcess::AllOutput); | 78 | bool ret = sshadd_process.start(OProcess::Block, OProcess::AllOutput); |
79 | if (!ret) { | 79 | if (!ret) { |
80 | log_text(tr("Error running ssh-add")); | 80 | log_text(tr("Error running ssh-add")); |
81 | return; | 81 | return; |
82 | } | 82 | } |
83 | 83 | if (sshadd_process.exitStatus() == 2) { | |
84 | log_text(tr("Connection to ssh-agent failed")); | ||
85 | setEnabled(FALSE); | ||
86 | } | ||
84 | } | 87 | } |
85 | 88 | ||
86 | void SSHKeysApp::get_list_keys_output(OProcess *proc, char *buffer, int buflen) | 89 | void SSHKeysApp::get_list_keys_output(OProcess *proc, char *buffer, int buflen) |
87 | { | 90 | { |
88 | int i; | 91 | int i; |
89 | (void) proc; | 92 | (void) proc; |
90 | 93 | ||
91 | for (i=0; i<buflen; i++) { | 94 | for (i=0; i<buflen; i++) { |
92 | switch(keystate) { | 95 | switch(keystate) { |
93 | case Noise: | 96 | case Noise: |
94 | noise: | 97 | noise: |
95 | if (buffer[i] == '\n') { | 98 | if (buffer[i] == '\n') { |
96 | log_text(incoming_noise.local8Bit()); | 99 | log_text(incoming_noise.local8Bit()); |
97 | incoming_noise = ""; | 100 | incoming_noise = ""; |
98 | keystate = KeySize; | 101 | keystate = KeySize; |
99 | } else { | 102 | } else { |
100 | incoming_noise += buffer[i]; | 103 | incoming_noise += buffer[i]; |
101 | } | 104 | } |
102 | break; | 105 | break; |
103 | 106 | ||
104 | case KeySize: | 107 | case KeySize: |
105 | if (isdigit(buffer[i])) { | 108 | if (isdigit(buffer[i])) { |
106 | incoming_keysize += buffer[i]; | 109 | incoming_keysize += buffer[i]; |
107 | } else if (buffer[i] == ' ') { | 110 | } else if (buffer[i] == ' ') { |
108 | keystate = KeyFingerprint; | 111 | keystate = KeyFingerprint; |
109 | } else { | 112 | } else { |
110 | incoming_keysize = ""; | 113 | incoming_keysize = ""; |
111 | incoming_noise = ""; | 114 | incoming_noise = ""; |
112 | keystate = Noise; | 115 | keystate = Noise; |
113 | goto noise; | 116 | goto noise; |
114 | } | 117 | } |
115 | break; | 118 | break; |
116 | 119 | ||
117 | case KeyFingerprint: | 120 | case KeyFingerprint: |
118 | if (isxdigit(buffer[i]) || buffer[i] == ':') { | 121 | if (isxdigit(buffer[i]) || buffer[i] == ':') { |
119 | incoming_keyfingerprint += buffer[i]; | 122 | incoming_keyfingerprint += buffer[i]; |
120 | } else if (buffer[i] == ' ') { | 123 | } else if (buffer[i] == ' ') { |
121 | keystate = KeyName; | 124 | keystate = KeyName; |
122 | } else { | 125 | } else { |
123 | incoming_keysize = ""; | 126 | incoming_keysize = ""; |
124 | incoming_keyfingerprint = ""; | 127 | incoming_keyfingerprint = ""; |
125 | incoming_noise = ""; | 128 | incoming_noise = ""; |
126 | keystate = Noise; | 129 | keystate = Noise; |
127 | goto noise; | 130 | goto noise; |
128 | } | 131 | } |
129 | break; | 132 | break; |
130 | 133 | ||
131 | case KeyName: | 134 | case KeyName: |
132 | if (buffer[i] == '\n') { | 135 | if (buffer[i] == '\n') { |
133 | /* Wheee. Got one. */ | 136 | /* Wheee. Got one. */ |
134 | KeyList->insertItem(new | 137 | KeyList->insertItem(new |
135 | QListViewItem(KeyList, incoming_keyname, incoming_keysize, incoming_keyfingerprint)); | 138 | QListViewItem(KeyList, incoming_keyname, incoming_keysize, incoming_keyfingerprint)); |
136 | incoming_keysize = ""; | 139 | incoming_keysize = ""; |
137 | incoming_keyfingerprint = ""; | 140 | incoming_keyfingerprint = ""; |
138 | incoming_keyname = ""; | 141 | incoming_keyname = ""; |
139 | keystate = KeySize; | 142 | keystate = KeySize; |
140 | } else if (isprint(buffer[i])) { | 143 | } else if (isprint(buffer[i])) { |
141 | incoming_keyname += buffer[i]; | 144 | incoming_keyname += buffer[i]; |
142 | } else { | 145 | } else { |
143 | incoming_keysize = ""; | 146 | incoming_keysize = ""; |
144 | incoming_keyfingerprint = ""; | 147 | incoming_keyfingerprint = ""; |
145 | incoming_noise = ""; | 148 | incoming_noise = ""; |
146 | keystate = Noise; | 149 | keystate = Noise; |
147 | goto noise; | 150 | goto noise; |
148 | } | 151 | } |
149 | break; | 152 | break; |
150 | } | 153 | } |
151 | } | 154 | } |
152 | } | 155 | } |
153 | 156 | ||
154 | void SSHKeysApp::log_sshadd_output(OProcess *proc, char *buffer, int buflen) | 157 | void SSHKeysApp::log_sshadd_output(OProcess *proc, char *buffer, int buflen) |
155 | { | 158 | { |
156 | (void)proc; | 159 | (void)proc; |
157 | (void)buflen; | 160 | (void)buflen; |
158 | 161 | ||
159 | log_text(buffer); | 162 | log_text(buffer); |
160 | } | 163 | } |
161 | 164 | ||
162 | void SSHKeysApp::ssh_add_exited(OProcess *proc) | 165 | void SSHKeysApp::ssh_add_exited(OProcess *proc) |
163 | { | 166 | { |
164 | (void)proc; | 167 | (void)proc; |
165 | 168 | ||
166 | doRefreshListButton(); | 169 | doRefreshListButton(); |
167 | setEnabled(TRUE); | 170 | setEnabled(TRUE); |
168 | if (proc->exitStatus()) { | 171 | if (proc->exitStatus()) { |
169 | 172 | ||
170 | log_text(QString(tr("ssh-add failed"))); | 173 | log_text(QString(tr("ssh-add failed"))); |
171 | } | 174 | } |
172 | } | 175 | } |
173 | 176 | ||
174 | void SSHKeysApp::add_text_changed(const QString &text) | 177 | void SSHKeysApp::add_text_changed(const QString &text) |
175 | { | 178 | { |
176 | struct stat sbuf; | 179 | struct stat sbuf; |
177 | 180 | ||
178 | if (!text.length() || (!access(text.ascii(), R_OK) && | 181 | if (!text.length() || (!access(text.ascii(), R_OK) && |
179 | !stat(text.ascii(), &sbuf) && | 182 | !stat(text.ascii(), &sbuf) && |
180 | S_ISREG(sbuf.st_mode))) | 183 | S_ISREG(sbuf.st_mode))) |
181 | AddButton->setEnabled(TRUE); | 184 | AddButton->setEnabled(TRUE); |
182 | else | 185 | else |
183 | AddButton->setEnabled(FALSE); | 186 | AddButton->setEnabled(FALSE); |
184 | } | 187 | } |
185 | 188 | ||
186 | void SSHKeysApp::doAddButton() | 189 | void SSHKeysApp::doAddButton() |
187 | { | 190 | { |
188 | addprocess.clearArguments(); | 191 | addprocess.clearArguments(); |
189 | 192 | ||
190 | setEnabled(FALSE); | 193 | setEnabled(FALSE); |
191 | 194 | ||
192 | if (KeyFileName->currentText().length()) { | 195 | if (KeyFileName->currentText().length()) { |
193 | addprocess << "ssh-add" << "--" << KeyFileName->currentText(); | 196 | addprocess << "ssh-add" << "--" << KeyFileName->currentText(); |
194 | log_text(QString(tr("Running ssh-add -- ")) + KeyFileName->currentText()); | 197 | log_text(QString(tr("Running ssh-add -- ")) + KeyFileName->currentText()); |
195 | } else { | 198 | } else { |
196 | addprocess << "ssh-add"; | 199 | addprocess << "ssh-add"; |
197 | log_text(tr("Running ssh-add")); | 200 | log_text(tr("Running ssh-add")); |
198 | } | 201 | } |
199 | bool ret = addprocess.start(OProcess::NotifyOnExit, OProcess::AllOutput); | 202 | bool ret = addprocess.start(OProcess::NotifyOnExit, OProcess::AllOutput); |
200 | if (!ret) { | 203 | if (!ret) { |
201 | log_text(tr("Error running ssh-add")); | 204 | log_text(tr("Error running ssh-add")); |
202 | doRefreshListButton(); | 205 | doRefreshListButton(); |
203 | setEnabled(TRUE); | 206 | setEnabled(TRUE); |
204 | } | 207 | } |
205 | } | 208 | } |
206 | 209 | ||
207 | void SSHKeysApp::log_text(const char *text) | 210 | void SSHKeysApp::log_text(const char *text) |
208 | { | 211 | { |
209 | TextOutput->append(text); | 212 | TextOutput->append(text); |
210 | TextOutput->setCursorPosition(TextOutput->numLines()+1, 0, FALSE); | 213 | TextOutput->setCursorPosition(TextOutput->numLines()+1, 0, FALSE); |
211 | } | 214 | } |
212 | 215 | ||
213 | void SSHKeysApp::doRemoveAllButton() | 216 | void SSHKeysApp::doRemoveAllButton() |
214 | { | 217 | { |
215 | OProcess sshadd_process; | 218 | OProcess sshadd_process; |
216 | 219 | ||
217 | connect(&sshadd_process, SIGNAL(receivedStdout(OProcess*,char*,int)), | 220 | connect(&sshadd_process, SIGNAL(receivedStdout(OProcess*,char*,int)), |
218 | this, SLOT(log_sshadd_output(OProcess*,char*,int))); | 221 | this, SLOT(log_sshadd_output(OProcess*,char*,int))); |
219 | 222 | ||
220 | log_text(tr("Running ssh-add -D")); | 223 | log_text(tr("Running ssh-add -D")); |
221 | sshadd_process << "ssh-add" << "-D"; | 224 | sshadd_process << "ssh-add" << "-D"; |
222 | bool ret = sshadd_process.start(OProcess::Block, OProcess::AllOutput); | 225 | bool ret = sshadd_process.start(OProcess::Block, OProcess::AllOutput); |
223 | if (!ret) { | 226 | if (!ret) { |
224 | log_text(tr("Error running ssh-add")); | 227 | log_text(tr("Error running ssh-add")); |
225 | } | 228 | } |
226 | doRefreshListButton(); | 229 | doRefreshListButton(); |
227 | } | 230 | } |