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