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