]> andersk Git - openssh.git/blob - ssh-add.c
- (dtucker) [configure.ac ssh-keygen.c openbsd-compat/bsd-openpty.c
[openssh.git] / ssh-add.c
1 /* $OpenBSD: ssh-add.c,v 1.83 2006/07/09 15:27:59 stevesk Exp $ */
2 /*
3  * Author: Tatu Ylonen <ylo@cs.hut.fi>
4  * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
5  *                    All rights reserved
6  * Adds an identity to the authentication server, or removes an identity.
7  *
8  * As far as I am concerned, the code I have written for this software
9  * can be used freely for any purpose.  Any derived versions of this
10  * software must be clearly marked as such, and if the derived work is
11  * incompatible with the protocol description in the RFC file, it must be
12  * called by a name other than "ssh" or "Secure Shell".
13  *
14  * SSH2 implementation,
15  * Copyright (c) 2000, 2001 Markus Friedl.  All rights reserved.
16  *
17  * Redistribution and use in source and binary forms, with or without
18  * modification, are permitted provided that the following conditions
19  * are met:
20  * 1. Redistributions of source code must retain the above copyright
21  *    notice, this list of conditions and the following disclaimer.
22  * 2. Redistributions in binary form must reproduce the above copyright
23  *    notice, this list of conditions and the following disclaimer in the
24  *    documentation and/or other materials provided with the distribution.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
27  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
28  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
29  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
30  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
31  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
35  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36  */
37
38 #include "includes.h"
39
40 #include <sys/types.h>
41 #include <sys/stat.h>
42
43 #include <openssl/evp.h>
44
45 #include <fcntl.h>
46 #include <pwd.h>
47
48 #include "ssh.h"
49 #include "rsa.h"
50 #include "log.h"
51 #include "xmalloc.h"
52 #include "key.h"
53 #include "authfd.h"
54 #include "authfile.h"
55 #include "pathnames.h"
56 #include "misc.h"
57
58 /* argv0 */
59 extern char *__progname;
60
61 /* Default files to add */
62 static char *default_files[] = {
63         _PATH_SSH_CLIENT_ID_RSA,
64         _PATH_SSH_CLIENT_ID_DSA,
65         _PATH_SSH_CLIENT_IDENTITY,
66         NULL
67 };
68
69 /* Default lifetime (0 == forever) */
70 static int lifetime = 0;
71
72 /* User has to confirm key use */
73 static int confirm = 0;
74
75 /* we keep a cache of one passphrases */
76 static char *pass = NULL;
77 static void
78 clear_pass(void)
79 {
80         if (pass) {
81                 memset(pass, 0, strlen(pass));
82                 xfree(pass);
83                 pass = NULL;
84         }
85 }
86
87 static int
88 delete_file(AuthenticationConnection *ac, const char *filename)
89 {
90         Key *public;
91         char *comment = NULL;
92         int ret = -1;
93
94         public = key_load_public(filename, &comment);
95         if (public == NULL) {
96                 printf("Bad key file %s\n", filename);
97                 return -1;
98         }
99         if (ssh_remove_identity(ac, public)) {
100                 fprintf(stderr, "Identity removed: %s (%s)\n", filename, comment);
101                 ret = 0;
102         } else
103                 fprintf(stderr, "Could not remove identity: %s\n", filename);
104
105         key_free(public);
106         xfree(comment);
107
108         return ret;
109 }
110
111 /* Send a request to remove all identities. */
112 static int
113 delete_all(AuthenticationConnection *ac)
114 {
115         int ret = -1;
116
117         if (ssh_remove_all_identities(ac, 1))
118                 ret = 0;
119         /* ignore error-code for ssh2 */
120         ssh_remove_all_identities(ac, 2);
121
122         if (ret == 0)
123                 fprintf(stderr, "All identities removed.\n");
124         else
125                 fprintf(stderr, "Failed to remove all identities.\n");
126
127         return ret;
128 }
129
130 static int
131 add_file(AuthenticationConnection *ac, const char *filename)
132 {
133         Key *private;
134         char *comment = NULL;
135         char msg[1024];
136         int fd, perms_ok, ret = -1;
137
138         if ((fd = open(filename, O_RDONLY)) < 0) {
139                 perror(filename);
140                 return -1;
141         }
142
143         /*
144          * Since we'll try to load a keyfile multiple times, permission errors
145          * will occur multiple times, so check perms first and bail if wrong.
146          */
147         perms_ok = key_perm_ok(fd, filename);
148         close(fd);
149         if (!perms_ok)
150                 return -1;
151
152         /* At first, try empty passphrase */
153         private = key_load_private(filename, "", &comment);
154         if (comment == NULL)
155                 comment = xstrdup(filename);
156         /* try last */
157         if (private == NULL && pass != NULL)
158                 private = key_load_private(filename, pass, NULL);
159         if (private == NULL) {
160                 /* clear passphrase since it did not work */
161                 clear_pass();
162                 snprintf(msg, sizeof msg, "Enter passphrase for %.200s: ",
163                     comment);
164                 for (;;) {
165                         pass = read_passphrase(msg, RP_ALLOW_STDIN);
166                         if (strcmp(pass, "") == 0) {
167                                 clear_pass();
168                                 xfree(comment);
169                                 return -1;
170                         }
171                         private = key_load_private(filename, pass, &comment);
172                         if (private != NULL)
173                                 break;
174                         clear_pass();
175                         snprintf(msg, sizeof msg,
176                             "Bad passphrase, try again for %.200s: ", comment);
177                 }
178         }
179
180         if (ssh_add_identity_constrained(ac, private, comment, lifetime,
181             confirm)) {
182                 fprintf(stderr, "Identity added: %s (%s)\n", filename, comment);
183                 ret = 0;
184                 if (lifetime != 0)
185                         fprintf(stderr,
186                             "Lifetime set to %d seconds\n", lifetime);
187                 if (confirm != 0)
188                         fprintf(stderr,
189                             "The user has to confirm each use of the key\n");
190         } else if (ssh_add_identity(ac, private, comment)) {
191                 fprintf(stderr, "Identity added: %s (%s)\n", filename, comment);
192                 ret = 0;
193         } else {
194                 fprintf(stderr, "Could not add identity: %s\n", filename);
195         }
196
197         xfree(comment);
198         key_free(private);
199
200         return ret;
201 }
202
203 static int
204 update_card(AuthenticationConnection *ac, int add, const char *id)
205 {
206         char *pin;
207         int ret = -1;
208
209         pin = read_passphrase("Enter passphrase for smartcard: ", RP_ALLOW_STDIN);
210         if (pin == NULL)
211                 return -1;
212
213         if (ssh_update_card(ac, add, id, pin, lifetime, confirm)) {
214                 fprintf(stderr, "Card %s: %s\n",
215                     add ? "added" : "removed", id);
216                 ret = 0;
217         } else {
218                 fprintf(stderr, "Could not %s card: %s\n",
219                     add ? "add" : "remove", id);
220                 ret = -1;
221         }
222         xfree(pin);
223         return ret;
224 }
225
226 static int
227 list_identities(AuthenticationConnection *ac, int do_fp)
228 {
229         Key *key;
230         char *comment, *fp;
231         int had_identities = 0;
232         int version;
233
234         for (version = 1; version <= 2; version++) {
235                 for (key = ssh_get_first_identity(ac, &comment, version);
236                     key != NULL;
237                     key = ssh_get_next_identity(ac, &comment, version)) {
238                         had_identities = 1;
239                         if (do_fp) {
240                                 fp = key_fingerprint(key, SSH_FP_MD5,
241                                     SSH_FP_HEX);
242                                 printf("%d %s %s (%s)\n",
243                                     key_size(key), fp, comment, key_type(key));
244                                 xfree(fp);
245                         } else {
246                                 if (!key_write(key, stdout))
247                                         fprintf(stderr, "key_write failed");
248                                 fprintf(stdout, " %s\n", comment);
249                         }
250                         key_free(key);
251                         xfree(comment);
252                 }
253         }
254         if (!had_identities) {
255                 printf("The agent has no identities.\n");
256                 return -1;
257         }
258         return 0;
259 }
260
261 static int
262 lock_agent(AuthenticationConnection *ac, int lock)
263 {
264         char prompt[100], *p1, *p2;
265         int passok = 1, ret = -1;
266
267         strlcpy(prompt, "Enter lock password: ", sizeof(prompt));
268         p1 = read_passphrase(prompt, RP_ALLOW_STDIN);
269         if (lock) {
270                 strlcpy(prompt, "Again: ", sizeof prompt);
271                 p2 = read_passphrase(prompt, RP_ALLOW_STDIN);
272                 if (strcmp(p1, p2) != 0) {
273                         fprintf(stderr, "Passwords do not match.\n");
274                         passok = 0;
275                 }
276                 memset(p2, 0, strlen(p2));
277                 xfree(p2);
278         }
279         if (passok && ssh_lock_agent(ac, lock, p1)) {
280                 fprintf(stderr, "Agent %slocked.\n", lock ? "" : "un");
281                 ret = 0;
282         } else
283                 fprintf(stderr, "Failed to %slock agent.\n", lock ? "" : "un");
284         memset(p1, 0, strlen(p1));
285         xfree(p1);
286         return (ret);
287 }
288
289 static int
290 do_file(AuthenticationConnection *ac, int deleting, char *file)
291 {
292         if (deleting) {
293                 if (delete_file(ac, file) == -1)
294                         return -1;
295         } else {
296                 if (add_file(ac, file) == -1)
297                         return -1;
298         }
299         return 0;
300 }
301
302 static void
303 usage(void)
304 {
305         fprintf(stderr, "Usage: %s [options] [file ...]\n", __progname);
306         fprintf(stderr, "Options:\n");
307         fprintf(stderr, "  -l          List fingerprints of all identities.\n");
308         fprintf(stderr, "  -L          List public key parameters of all identities.\n");
309         fprintf(stderr, "  -d          Delete identity.\n");
310         fprintf(stderr, "  -D          Delete all identities.\n");
311         fprintf(stderr, "  -x          Lock agent.\n");
312         fprintf(stderr, "  -X          Unlock agent.\n");
313         fprintf(stderr, "  -t life     Set lifetime (in seconds) when adding identities.\n");
314         fprintf(stderr, "  -c          Require confirmation to sign using identities\n");
315 #ifdef SMARTCARD
316         fprintf(stderr, "  -s reader   Add key in smartcard reader.\n");
317         fprintf(stderr, "  -e reader   Remove key in smartcard reader.\n");
318 #endif
319 }
320
321 int
322 main(int argc, char **argv)
323 {
324         extern char *optarg;
325         extern int optind;
326         AuthenticationConnection *ac = NULL;
327         char *sc_reader_id = NULL;
328         int i, ch, deleting = 0, ret = 0;
329
330         /* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */
331         sanitise_stdfd();
332
333         __progname = ssh_get_progname(argv[0]);
334         init_rng();
335         seed_rng();
336
337         SSLeay_add_all_algorithms();
338
339         /* At first, get a connection to the authentication agent. */
340         ac = ssh_get_authentication_connection();
341         if (ac == NULL) {
342                 fprintf(stderr,
343                     "Could not open a connection to your authentication agent.\n");
344                 exit(2);
345         }
346         while ((ch = getopt(argc, argv, "lLcdDxXe:s:t:")) != -1) {
347                 switch (ch) {
348                 case 'l':
349                 case 'L':
350                         if (list_identities(ac, ch == 'l' ? 1 : 0) == -1)
351                                 ret = 1;
352                         goto done;
353                 case 'x':
354                 case 'X':
355                         if (lock_agent(ac, ch == 'x' ? 1 : 0) == -1)
356                                 ret = 1;
357                         goto done;
358                 case 'c':
359                         confirm = 1;
360                         break;
361                 case 'd':
362                         deleting = 1;
363                         break;
364                 case 'D':
365                         if (delete_all(ac) == -1)
366                                 ret = 1;
367                         goto done;
368                 case 's':
369                         sc_reader_id = optarg;
370                         break;
371                 case 'e':
372                         deleting = 1;
373                         sc_reader_id = optarg;
374                         break;
375                 case 't':
376                         if ((lifetime = convtime(optarg)) == -1) {
377                                 fprintf(stderr, "Invalid lifetime\n");
378                                 ret = 1;
379                                 goto done;
380                         }
381                         break;
382                 default:
383                         usage();
384                         ret = 1;
385                         goto done;
386                 }
387         }
388         argc -= optind;
389         argv += optind;
390         if (sc_reader_id != NULL) {
391                 if (update_card(ac, !deleting, sc_reader_id) == -1)
392                         ret = 1;
393                 goto done;
394         }
395         if (argc == 0) {
396                 char buf[MAXPATHLEN];
397                 struct passwd *pw;
398                 struct stat st;
399                 int count = 0;
400
401                 if ((pw = getpwuid(getuid())) == NULL) {
402                         fprintf(stderr, "No user found with uid %u\n",
403                             (u_int)getuid());
404                         ret = 1;
405                         goto done;
406                 }
407
408                 for (i = 0; default_files[i]; i++) {
409                         snprintf(buf, sizeof(buf), "%s/%s", pw->pw_dir,
410                             default_files[i]);
411                         if (stat(buf, &st) < 0)
412                                 continue;
413                         if (do_file(ac, deleting, buf) == -1)
414                                 ret = 1;
415                         else
416                                 count++;
417                 }
418                 if (count == 0)
419                         ret = 1;
420         } else {
421                 for (i = 0; i < argc; i++) {
422                         if (do_file(ac, deleting, argv[i]) == -1)
423                                 ret = 1;
424                 }
425         }
426         clear_pass();
427
428 done:
429         ssh_close_authentication_connection(ac);
430         return ret;
431 }
This page took 0.071073 seconds and 5 git commands to generate.