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