summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBen Lindstrom <mouring@eviladmin.org>2001-10-03 17:43:01 +0000
committerBen Lindstrom <mouring@eviladmin.org>2001-10-03 17:43:01 +0000
commit569f88de0440506347e3c14f954d0aa0e9f18651 (patch)
treed026070a9c62ce87ce2c94a6565b7eb17eb74d04
parent3cecc9a41f32681b8729a7e4b32dbe8fe80a3f8a (diff)
- djm@cvs.openbsd.org 2001/10/02 08:38:50
[ssh-add.c] return non-zero exit code on error; ok markus@
-rw-r--r--ChangeLog5
-rw-r--r--ssh-add.c91
2 files changed, 61 insertions, 35 deletions
diff --git a/ChangeLog b/ChangeLog
index 65655e587..8a8332a4d 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -31,6 +31,9 @@
31 [readconf.c readconf.h ssh.1 sshconnect.c] 31 [readconf.c readconf.h ssh.1 sshconnect.c]
32 add NoHostAuthenticationForLocalhost; note that the hostkey is 32 add NoHostAuthenticationForLocalhost; note that the hostkey is
33 now check for localhost, too. 33 now check for localhost, too.
34 - djm@cvs.openbsd.org 2001/10/02 08:38:50
35 [ssh-add.c]
36 return non-zero exit code on error; ok markus@
34 37
3520011001 3820011001
36 - (stevesk) loginrec.c: fix type conversion problems exposed when using 39 - (stevesk) loginrec.c: fix type conversion problems exposed when using
@@ -6623,4 +6626,4 @@
6623 - Wrote replacements for strlcpy and mkdtemp 6626 - Wrote replacements for strlcpy and mkdtemp
6624 - Released 1.0pre1 6627 - Released 1.0pre1
6625 6628
6626$Id: ChangeLog,v 1.1578 2001/10/03 17:39:38 mouring Exp $ 6629$Id: ChangeLog,v 1.1579 2001/10/03 17:43:01 mouring Exp $
diff --git a/ssh-add.c b/ssh-add.c
index 979164cae..585b71487 100644
--- a/ssh-add.c
+++ b/ssh-add.c
@@ -35,7 +35,7 @@
35 */ 35 */
36 36
37#include "includes.h" 37#include "includes.h"
38RCSID("$OpenBSD: ssh-add.c,v 1.45 2001/08/03 10:31:30 jakob Exp $"); 38RCSID("$OpenBSD: ssh-add.c,v 1.46 2001/10/02 08:38:50 djm Exp $");
39 39
40#include <openssl/evp.h> 40#include <openssl/evp.h>
41 41
@@ -70,53 +70,61 @@ clear_pass(void)
70 } 70 }
71} 71}
72 72
73static void 73static int
74delete_file(AuthenticationConnection *ac, const char *filename) 74delete_file(AuthenticationConnection *ac, const char *filename)
75{ 75{
76 Key *public; 76 Key *public;
77 char *comment = NULL; 77 char *comment = NULL;
78 int ret = -1;
78 79
79 public = key_load_public(filename, &comment); 80 public = key_load_public(filename, &comment);
80 if (public == NULL) { 81 if (public == NULL) {
81 printf("Bad key file %s\n", filename); 82 printf("Bad key file %s\n", filename);
82 return; 83 return -1;
83 } 84 }
84 if (ssh_remove_identity(ac, public)) 85 if (ssh_remove_identity(ac, public)) {
85 fprintf(stderr, "Identity removed: %s (%s)\n", filename, comment); 86 fprintf(stderr, "Identity removed: %s (%s)\n", filename, comment);
86 else 87 ret = 0;
88 } else
87 fprintf(stderr, "Could not remove identity: %s\n", filename); 89 fprintf(stderr, "Could not remove identity: %s\n", filename);
90
88 key_free(public); 91 key_free(public);
89 xfree(comment); 92 xfree(comment);
93
94 return ret;
90} 95}
91 96
92/* Send a request to remove all identities. */ 97/* Send a request to remove all identities. */
93static void 98static int
94delete_all(AuthenticationConnection *ac) 99delete_all(AuthenticationConnection *ac)
95{ 100{
96 int success = 1; 101 int ret = -1;
97 102
98 if (!ssh_remove_all_identities(ac, 1)) 103 if (ssh_remove_all_identities(ac, 1))
99 success = 0; 104 ret = 0;
100 /* ignore error-code for ssh2 */ 105 /* ignore error-code for ssh2 */
101 ssh_remove_all_identities(ac, 2); 106 ssh_remove_all_identities(ac, 2);
102 107
103 if (success) 108 if (ret == 0)
104 fprintf(stderr, "All identities removed.\n"); 109 fprintf(stderr, "All identities removed.\n");
105 else 110 else
106 fprintf(stderr, "Failed to remove all identities.\n"); 111 fprintf(stderr, "Failed to remove all identities.\n");
112
113 return ret;
107} 114}
108 115
109static void 116static int
110add_file(AuthenticationConnection *ac, const char *filename) 117add_file(AuthenticationConnection *ac, const char *filename)
111{ 118{
112 struct stat st; 119 struct stat st;
113 Key *private; 120 Key *private;
114 char *comment = NULL; 121 char *comment = NULL;
115 char msg[1024]; 122 char msg[1024];
123 int ret = -1;
116 124
117 if (stat(filename, &st) < 0) { 125 if (stat(filename, &st) < 0) {
118 perror(filename); 126 perror(filename);
119 exit(1); 127 return -1;
120 } 128 }
121 /* At first, try empty passphrase */ 129 /* At first, try empty passphrase */
122 private = key_load_private(filename, "", &comment); 130 private = key_load_private(filename, "", &comment);
@@ -135,7 +143,7 @@ add_file(AuthenticationConnection *ac, const char *filename)
135 if (strcmp(pass, "") == 0) { 143 if (strcmp(pass, "") == 0) {
136 clear_pass(); 144 clear_pass();
137 xfree(comment); 145 xfree(comment);
138 return; 146 return -1;
139 } 147 }
140 private = key_load_private(filename, pass, &comment); 148 private = key_load_private(filename, pass, &comment);
141 if (private != NULL) 149 if (private != NULL)
@@ -144,23 +152,30 @@ add_file(AuthenticationConnection *ac, const char *filename)
144 strlcpy(msg, "Bad passphrase, try again: ", sizeof msg); 152 strlcpy(msg, "Bad passphrase, try again: ", sizeof msg);
145 } 153 }
146 } 154 }
147 if (ssh_add_identity(ac, private, comment)) 155 if (ssh_add_identity(ac, private, comment)) {
148 fprintf(stderr, "Identity added: %s (%s)\n", filename, comment); 156 fprintf(stderr, "Identity added: %s (%s)\n", filename, comment);
149 else 157 ret = 0;
158 } else
150 fprintf(stderr, "Could not add identity: %s\n", filename); 159 fprintf(stderr, "Could not add identity: %s\n", filename);
160
151 xfree(comment); 161 xfree(comment);
152 key_free(private); 162 key_free(private);
163
164 return ret;
153} 165}
154 166
155static void 167static int
156update_card(AuthenticationConnection *ac, int add, const char *id) 168update_card(AuthenticationConnection *ac, int add, const char *id)
157{ 169{
158 if (ssh_update_card(ac, add, id)) 170 if (ssh_update_card(ac, add, id)) {
159 fprintf(stderr, "Card %s: %s\n", 171 fprintf(stderr, "Card %s: %s\n",
160 add ? "added" : "removed", id); 172 add ? "added" : "removed", id);
161 else 173 return 0;
174 } else {
162 fprintf(stderr, "Could not %s card: %s\n", 175 fprintf(stderr, "Could not %s card: %s\n",
163 add ? "add" : "remove", id); 176 add ? "add" : "remove", id);
177 return -1;
178 }
164} 179}
165 180
166static void 181static void
@@ -219,7 +234,7 @@ main(int argc, char **argv)
219 struct passwd *pw; 234 struct passwd *pw;
220 char buf[1024]; 235 char buf[1024];
221 char *sc_reader_id = NULL; 236 char *sc_reader_id = NULL;
222 int i, ch, deleting = 0; 237 int i, ch, deleting = 0, ret = 0;
223 238
224 __progname = get_progname(argv[0]); 239 __progname = get_progname(argv[0]);
225 init_rng(); 240 init_rng();
@@ -244,7 +259,8 @@ main(int argc, char **argv)
244 deleting = 1; 259 deleting = 1;
245 break; 260 break;
246 case 'D': 261 case 'D':
247 delete_all(ac); 262 if (delete_all(ac) == -1)
263 ret = 1;
248 goto done; 264 goto done;
249 break; 265 break;
250 case 's': 266 case 's':
@@ -256,14 +272,15 @@ main(int argc, char **argv)
256 break; 272 break;
257 default: 273 default:
258 usage(); 274 usage();
259 exit(1); 275 ret = 1;
260 break; 276 goto done;
261 } 277 }
262 } 278 }
263 argc -= optind; 279 argc -= optind;
264 argv += optind; 280 argv += optind;
265 if (sc_reader_id != NULL) { 281 if (sc_reader_id != NULL) {
266 update_card(ac, !deleting, sc_reader_id); 282 if (update_card(ac, !deleting, sc_reader_id) == -1)
283 ret = 1;
267 goto done; 284 goto done;
268 } 285 }
269 if (argc == 0) { 286 if (argc == 0) {
@@ -271,25 +288,31 @@ main(int argc, char **argv)
271 if (!pw) { 288 if (!pw) {
272 fprintf(stderr, "No user found with uid %u\n", 289 fprintf(stderr, "No user found with uid %u\n",
273 (u_int)getuid()); 290 (u_int)getuid());
274 ssh_close_authentication_connection(ac); 291 ret = 1;
275 exit(1); 292 goto done;
276 } 293 }
277 snprintf(buf, sizeof buf, "%s/%s", pw->pw_dir, _PATH_SSH_CLIENT_IDENTITY); 294 snprintf(buf, sizeof buf, "%s/%s", pw->pw_dir, _PATH_SSH_CLIENT_IDENTITY);
278 if (deleting) 295 if (deleting) {
279 delete_file(ac, buf); 296 if (delete_file(ac, buf) == -1)
280 else 297 ret = 1;
281 add_file(ac, buf); 298 } else {
299 if (add_file(ac, buf) == -1)
300 ret = 1;
301 }
282 } else { 302 } else {
283 for (i = 0; i < argc; i++) { 303 for (i = 0; i < argc; i++) {
284 if (deleting) 304 if (deleting) {
285 delete_file(ac, argv[i]); 305 if (delete_file(ac, argv[i]) == -1)
286 else 306 ret = 1;
287 add_file(ac, argv[i]); 307 } else {
308 if (add_file(ac, argv[i]) == -1)
309 ret = 1;
310 }
288 } 311 }
289 } 312 }
290 clear_pass(); 313 clear_pass();
291 314
292done: 315done:
293 ssh_close_authentication_connection(ac); 316 ssh_close_authentication_connection(ac);
294 exit(0); 317 return ret;
295} 318}