]> andersk Git - openssh.git/blob - ssh-add.c
- stevesk@cvs.openbsd.org 2006/07/08 23:30:06
[openssh.git] / ssh-add.c
1 /* $OpenBSD: ssh-add.c,v 1.81 2006/07/06 16:03:53 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 <pwd.h>
46
47 #include "ssh.h"
48 #include "rsa.h"
49 #include "log.h"
50 #include "xmalloc.h"
51 #include "key.h"
52 #include "authfd.h"
53 #include "authfile.h"
54 #include "pathnames.h"
55 #include "misc.h"
56
57 /* argv0 */
58 extern char *__progname;
59
60 /* Default files to add */
61 static char *default_files[] = {
62         _PATH_SSH_CLIENT_ID_RSA,
63         _PATH_SSH_CLIENT_ID_DSA,
64         _PATH_SSH_CLIENT_IDENTITY,
65         NULL
66 };
67
68 /* Default lifetime (0 == forever) */
69 static int lifetime = 0;
70
71 /* User has to confirm key use */
72 static int confirm = 0;
73
74 /* we keep a cache of one passphrases */
75 static char *pass = NULL;
76 static void
77 clear_pass(void)
78 {
79         if (pass) {
80                 memset(pass, 0, strlen(pass));
81                 xfree(pass);
82                 pass = NULL;
83         }
84 }
85
86 static int
87 delete_file(AuthenticationConnection *ac, const char *filename)
88 {
89         Key *public;
90         char *comment = NULL;
91         int ret = -1;
92
93         public = key_load_public(filename, &comment);
94         if (public == NULL) {
95                 printf("Bad key file %s\n", filename);
96                 return -1;
97         }
98         if (ssh_remove_identity(ac, public)) {
99                 fprintf(stderr, "Identity removed: %s (%s)\n", filename, comment);
100                 ret = 0;
101         } else
102                 fprintf(stderr, "Could not remove identity: %s\n", filename);
103
104         key_free(public);
105         xfree(comment);
106
107         return ret;
108 }
109
110 /* Send a request to remove all identities. */
111 static int
112 delete_all(AuthenticationConnection *ac)
113 {
114         int ret = -1;
115
116         if (ssh_remove_all_identities(ac, 1))
117                 ret = 0;
118         /* ignore error-code for ssh2 */
119         ssh_remove_all_identities(ac, 2);
120
121         if (ret == 0)
122                 fprintf(stderr, "All identities removed.\n");
123         else
124                 fprintf(stderr, "Failed to remove all identities.\n");
125
126         return ret;
127 }
128
129 static int
130 add_file(AuthenticationConnection *ac, const char *filename)
131 {
132         Key *private;
133         char *comment = NULL;
134         char msg[1024];
135         int fd, perms_ok, ret = -1;
136
137         if ((fd = open(filename, 0)) < 0) {
138                 perror(filename);
139                 return -1;
140         }
141
142         /*
143          * Since we'll try to load a keyfile multiple times, permission errors
144          * will occur multiple times, so check perms first and bail if wrong.
145          */
146         perms_ok = key_perm_ok(fd, filename);
147         close(fd);
148         if (!perms_ok)
149                 return -1;
150
151         /* At first, try empty passphrase */
152         private = key_load_private(filename, "", &comment);
153         if (comment == NULL)
154                 comment = xstrdup(filename);
155         /* try last */
156         if (private == NULL && pass != NULL)
157                 private = key_load_private(filename, pass, NULL);
158         if (private == NULL) {
159                 /* clear passphrase since it did not work */
160                 clear_pass();
161                 snprintf(msg, sizeof msg, "Enter passphrase for %.200s: ",
162                     comment);
163                 for (;;) {
164                         pass = read_passphrase(msg, RP_ALLOW_STDIN);
165                         if (strcmp(pass, "") == 0) {
166                                 clear_pass();
167                                 xfree(comment);
168                                 return -1;
169                         }
170                         private = key_load_private(filename, pass, &comment);
171                         if (private != NULL)
172                                 break;
173                         clear_pass();
174                         snprintf(msg, sizeof msg,
175                             "Bad passphrase, try again for %.200s: ", comment);
176                 }
177         }
178
179         if (ssh_add_identity_constrained(ac, private, comment, lifetime,
180             confirm)) {
181                 fprintf(stderr, "Identity added: %s (%s)\n", filename, comment);
182                 ret = 0;
183                 if (lifetime != 0)
184                         fprintf(stderr,
185                             "Lifetime set to %d seconds\n", lifetime);
186                 if (confirm != 0)
187                         fprintf(stderr,
188                             "The user has to confirm each use of the key\n");
189         } else if (ssh_add_identity(ac, private, comment)) {
190                 fprintf(stderr, "Identity added: %s (%s)\n", filename, comment);
191                 ret = 0;
192         } else {
193                 fprintf(stderr, "Could not add identity: %s\n", filename);
194         }
195
196         xfree(comment);
197         key_free(private);
198
199         return ret;
200 }
201
202 static int
203 update_card(AuthenticationConnection *ac, int add, const char *id)
204 {
205         char *pin;
206         int ret = -1;
207
208         pin = read_passphrase("Enter passphrase for smartcard: ", RP_ALLOW_STDIN);
209         if (pin == NULL)
210                 return -1;
211
212         if (ssh_update_card(ac, add, id, pin, lifetime, confirm)) {
213                 fprintf(stderr, "Card %s: %s\n",
214                     add ? "added" : "removed", id);
215                 ret = 0;
216         } else {
217                 fprintf(stderr, "Could not %s card: %s\n",
218                     add ? "add" : "remove", id);
219                 ret = -1;
220         }
221         xfree(pin);
222         return ret;
223 }
224
225 static int
226 list_identities(AuthenticationConnection *ac, int do_fp)
227 {
228         Key *key;
229         char *comment, *fp;
230         int had_identities = 0;
231         int version;
232
233         for (version = 1; version <= 2; version++) {
234                 for (key = ssh_get_first_identity(ac, &comment, version);
235                     key != NULL;
236                     key = ssh_get_next_identity(ac, &comment, version)) {
237                         had_identities = 1;
238                         if (do_fp) {
239                                 fp = key_fingerprint(key, SSH_FP_MD5,
240                                     SSH_FP_HEX);
241                                 printf("%d %s %s (%s)\n",
242                                     key_size(key), fp, comment, key_type(key));
243                                 xfree(fp);
244                         } else {
245                                 if (!key_write(key, stdout))
246                                         fprintf(stderr, "key_write failed");
247                                 fprintf(stdout, " %s\n", comment);
248                         }
249                         key_free(key);
250                         xfree(comment);
251                 }
252         }
253         if (!had_identities) {
254                 printf("The agent has no identities.\n");
255                 return -1;
256         }
257         return 0;
258 }
259
260 static int
261 lock_agent(AuthenticationConnection *ac, int lock)
262 {
263         char prompt[100], *p1, *p2;
264         int passok = 1, ret = -1;
265
266         strlcpy(prompt, "Enter lock password: ", sizeof(prompt));
267         p1 = read_passphrase(prompt, RP_ALLOW_STDIN);
268         if (lock) {
269                 strlcpy(prompt, "Again: ", sizeof prompt);
270                 p2 = read_passphrase(prompt, RP_ALLOW_STDIN);
271                 if (strcmp(p1, p2) != 0) {
272                         fprintf(stderr, "Passwords do not match.\n");
273                         passok = 0;
274                 }
275                 memset(p2, 0, strlen(p2));
276                 xfree(p2);
277         }
278         if (passok && ssh_lock_agent(ac, lock, p1)) {
279                 fprintf(stderr, "Agent %slocked.\n", lock ? "" : "un");
280                 ret = 0;
281         } else
282                 fprintf(stderr, "Failed to %slock agent.\n", lock ? "" : "un");
283         memset(p1, 0, strlen(p1));
284         xfree(p1);
285         return (ret);
286 }
287
288 static int
289 do_file(AuthenticationConnection *ac, int deleting, char *file)
290 {
291         if (deleting) {
292                 if (delete_file(ac, file) == -1)
293                         return -1;
294         } else {
295                 if (add_file(ac, file) == -1)
296                         return -1;
297         }
298         return 0;
299 }
300
301 static void
302 usage(void)
303 {
304         fprintf(stderr, "Usage: %s [options] [file ...]\n", __progname);
305         fprintf(stderr, "Options:\n");
306         fprintf(stderr, "  -l          List fingerprints of all identities.\n");
307         fprintf(stderr, "  -L          List public key parameters of all identities.\n");
308         fprintf(stderr, "  -d          Delete identity.\n");
309         fprintf(stderr, "  -D          Delete all identities.\n");
310         fprintf(stderr, "  -x          Lock agent.\n");
311         fprintf(stderr, "  -X          Unlock agent.\n");
312         fprintf(stderr, "  -t life     Set lifetime (in seconds) when adding identities.\n");
313         fprintf(stderr, "  -c          Require confirmation to sign using identities\n");
314 #ifdef SMARTCARD
315         fprintf(stderr, "  -s reader   Add key in smartcard reader.\n");
316         fprintf(stderr, "  -e reader   Remove key in smartcard reader.\n");
317 #endif
318 }
319
320 int
321 main(int argc, char **argv)
322 {
323         extern char *optarg;
324         extern int optind;
325         AuthenticationConnection *ac = NULL;
326         char *sc_reader_id = NULL;
327         int i, ch, deleting = 0, ret = 0;
328
329         /* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */
330         sanitise_stdfd();
331
332         __progname = ssh_get_progname(argv[0]);
333         init_rng();
334         seed_rng();
335
336         SSLeay_add_all_algorithms();
337
338         /* At first, get a connection to the authentication agent. */
339         ac = ssh_get_authentication_connection();
340         if (ac == NULL) {
341                 fprintf(stderr,
342                     "Could not open a connection to your authentication agent.\n");
343                 exit(2);
344         }
345         while ((ch = getopt(argc, argv, "lLcdDxXe:s:t:")) != -1) {
346                 switch (ch) {
347                 case 'l':
348                 case 'L':
349                         if (list_identities(ac, ch == 'l' ? 1 : 0) == -1)
350                                 ret = 1;
351                         goto done;
352                 case 'x':
353                 case 'X':
354                         if (lock_agent(ac, ch == 'x' ? 1 : 0) == -1)
355                                 ret = 1;
356                         goto done;
357                 case 'c':
358                         confirm = 1;
359                         break;
360                 case 'd':
361                         deleting = 1;
362                         break;
363                 case 'D':
364                         if (delete_all(ac) == -1)
365                                 ret = 1;
366                         goto done;
367                 case 's':
368                         sc_reader_id = optarg;
369                         break;
370                 case 'e':
371                         deleting = 1;
372                         sc_reader_id = optarg;
373                         break;
374                 case 't':
375                         if ((lifetime = convtime(optarg)) == -1) {
376                                 fprintf(stderr, "Invalid lifetime\n");
377                                 ret = 1;
378                                 goto done;
379                         }
380                         break;
381                 default:
382                         usage();
383                         ret = 1;
384                         goto done;
385                 }
386         }
387         argc -= optind;
388         argv += optind;
389         if (sc_reader_id != NULL) {
390                 if (update_card(ac, !deleting, sc_reader_id) == -1)
391                         ret = 1;
392                 goto done;
393         }
394         if (argc == 0) {
395                 char buf[MAXPATHLEN];
396                 struct passwd *pw;
397                 struct stat st;
398                 int count = 0;
399
400                 if ((pw = getpwuid(getuid())) == NULL) {
401                         fprintf(stderr, "No user found with uid %u\n",
402                             (u_int)getuid());
403                         ret = 1;
404                         goto done;
405                 }
406
407                 for (i = 0; default_files[i]; i++) {
408                         snprintf(buf, sizeof(buf), "%s/%s", pw->pw_dir,
409                             default_files[i]);
410                         if (stat(buf, &st) < 0)
411                                 continue;
412                         if (do_file(ac, deleting, buf) == -1)
413                                 ret = 1;
414                         else
415                                 count++;
416                 }
417                 if (count == 0)
418                         ret = 1;
419         } else {
420                 for (i = 0; i < argc; i++) {
421                         if (do_file(ac, deleting, argv[i]) == -1)
422                                 ret = 1;
423                 }
424         }
425         clear_pass();
426
427 done:
428         ssh_close_authentication_connection(ac);
429         return ret;
430 }
This page took 0.073511 seconds and 5 git commands to generate.