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