]> andersk Git - openssh.git/blob - ssh-add.c
- markus@cvs.openbsd.org 2001/06/26 04:59:59
[openssh.git] / 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.42 2001/06/26 04:59:59 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
52 #ifdef HAVE___PROGNAME
53 extern char *__progname;
54 #else
55 char *__progname;
56 #endif
57
58 /* we keep a cache of one passphrases */
59 static char *pass = NULL;
60 static void
61 clear_pass(void)
62 {
63         if (pass) {
64                 memset(pass, 0, strlen(pass));
65                 xfree(pass);
66                 pass = NULL;
67         }
68 }
69
70 static void
71 delete_file(AuthenticationConnection *ac, const char *filename)
72 {
73         Key *public;
74         char *comment = NULL;
75
76         public = key_load_public(filename, &comment);
77         if (public == NULL) {
78                 printf("Bad key file %s\n", filename);
79                 return;
80         }
81         if (ssh_remove_identity(ac, public))
82                 fprintf(stderr, "Identity removed: %s (%s)\n", filename, comment);
83         else
84                 fprintf(stderr, "Could not remove identity: %s\n", filename);
85         key_free(public);
86         xfree(comment);
87 }
88
89 /* Send a request to remove all identities. */
90 static void
91 delete_all(AuthenticationConnection *ac)
92 {
93         int success = 1;
94
95         if (!ssh_remove_all_identities(ac, 1))
96                 success = 0;
97         /* ignore error-code for ssh2 */
98         ssh_remove_all_identities(ac, 2);
99
100         if (success)
101                 fprintf(stderr, "All identities removed.\n");
102         else
103                 fprintf(stderr, "Failed to remove all identities.\n");
104 }
105
106 static void
107 add_file(AuthenticationConnection *ac, const char *filename)
108 {
109         struct stat st;
110         Key *private;
111         char *comment = NULL;
112         char msg[1024];
113
114         if (stat(filename, &st) < 0) {
115                 perror(filename);
116                 exit(1);
117         }
118         /* At first, try empty passphrase */
119         private = key_load_private(filename, "", &comment);
120         if (comment == NULL)
121                 comment = xstrdup(filename);
122         /* try last */
123         if (private == NULL && pass != NULL)
124                 private = key_load_private(filename, pass, NULL);
125         if (private == NULL) {
126                 /* clear passphrase since it did not work */
127                 clear_pass();
128                 snprintf(msg, sizeof msg, "Enter passphrase for %.200s: ",
129                    comment);
130                 for (;;) {
131                         pass = read_passphrase(msg, RP_ALLOW_STDIN);
132                         if (strcmp(pass, "") == 0) {
133                                 clear_pass();
134                                 xfree(comment);
135                                 return;
136                         }
137                         private = key_load_private(filename, pass, &comment);
138                         if (private != NULL)
139                                 break;
140                         clear_pass();
141                         strlcpy(msg, "Bad passphrase, try again: ", sizeof msg);
142                 }
143         }
144         if (ssh_add_identity(ac, private, comment))
145                 fprintf(stderr, "Identity added: %s (%s)\n", filename, comment);
146         else
147                 fprintf(stderr, "Could not add identity: %s\n", filename);
148         xfree(comment);
149         key_free(private);
150 }
151
152 static void
153 update_card(AuthenticationConnection *ac, int add, int id)
154 {
155         if (ssh_update_card(ac, add, id))
156                 fprintf(stderr, "Card %s: %d\n",
157                      add ? "added" : "removed", id);
158         else
159                 fprintf(stderr, "Could not %s card: %d\n",
160                      add ? "add" : "remove", id);
161 }
162
163 static void
164 list_identities(AuthenticationConnection *ac, int do_fp)
165 {
166         Key *key;
167         char *comment, *fp;
168         int had_identities = 0;
169         int version;
170
171         for (version = 1; version <= 2; version++) {
172                 for (key = ssh_get_first_identity(ac, &comment, version);
173                      key != NULL;
174                      key = ssh_get_next_identity(ac, &comment, version)) {
175                         had_identities = 1;
176                         if (do_fp) {
177                                 fp = key_fingerprint(key, SSH_FP_MD5,
178                                     SSH_FP_HEX);
179                                 printf("%d %s %s (%s)\n",
180                                     key_size(key), fp, comment, key_type(key));
181                                 xfree(fp);
182                         } else {
183                                 if (!key_write(key, stdout))
184                                         fprintf(stderr, "key_write failed");
185                                 fprintf(stdout, " %s\n", comment);
186                         }
187                         key_free(key);
188                         xfree(comment);
189                 }
190         }
191         if (!had_identities)
192                 printf("The agent has no identities.\n");
193 }
194
195 static void
196 usage(void)
197 {
198         printf("Usage: ssh-add [options]\n");
199         printf("    -l, -L        : list identities\n");
200         printf("    -d            : delete identity\n");
201         printf("    -D            : delete all identities\n");
202         printf("    -s reader_num : add key in the smartcard in reader_num.\n");
203         printf("    -e reader_num : remove key in the smartcard in reader_num.\n");
204         exit (1);
205 }
206
207 int
208 main(int argc, char **argv)
209 {
210         AuthenticationConnection *ac = NULL;
211         struct passwd *pw;
212         char buf[1024];
213         int no_files = 1;
214         int i;
215         int deleting = 0;
216         int sc_mode = 0;
217         int sc_reader_num = 0;
218
219         __progname = get_progname(argv[0]);
220         init_rng();
221         seed_rng();
222
223         SSLeay_add_all_algorithms();
224
225         /* At first, get a connection to the authentication agent. */
226         ac = ssh_get_authentication_connection();
227         if (ac == NULL) {
228                 fprintf(stderr, "Could not open a connection to your authentication agent.\n");
229                 exit(1);
230         }
231         for (i = 1; i < argc; i++) {
232                 if ((strcmp(argv[i], "-l") == 0) ||
233                     (strcmp(argv[i], "-L") == 0)) {
234                         list_identities(ac, argv[i][1] == 'l' ? 1 : 0);
235                         /* Don't default-add/delete if -l. */
236                         no_files = 0;
237                         continue;
238                 }
239                 if (strcmp(argv[i], "-d") == 0) {
240                         deleting = 1;
241                         continue;
242                 }
243                 if (strcmp(argv[i], "-D") == 0) {
244                         delete_all(ac);
245                         no_files = 0;
246                         continue;
247                 }
248                 if (strcmp(argv[i], "-s") == 0) {
249                         sc_mode = 1;
250                         deleting = 0; 
251                         i++;
252                         if (i >= argc)
253                                 usage();
254                         sc_reader_num = atoi(argv[i]);
255                         continue; 
256                 }
257                 if (strcmp(argv[i], "-e") == 0) {
258                         sc_mode = 1;
259                         deleting = 1; 
260                         i++;
261                         if (i >= argc)
262                                 usage();
263                         sc_reader_num = atoi(argv[i]);
264                         continue; 
265                 }
266                 if (sc_mode == 1)
267                         update_card(ac, !deleting, sc_reader_num);
268                 no_files = 0;
269                 if (deleting)
270                         delete_file(ac, argv[i]);
271                 else
272                         add_file(ac, argv[i]);
273         }
274         if (sc_mode == 1) {
275                 update_card(ac, !deleting, sc_reader_num);
276                 ssh_close_authentication_connection(ac);
277                 exit(0);
278         }
279         if (no_files) {
280                 pw = getpwuid(getuid());
281                 if (!pw) {
282                         fprintf(stderr, "No user found with uid %u\n",
283                             (u_int)getuid());
284                         ssh_close_authentication_connection(ac);
285                         exit(1);
286                 }
287                 snprintf(buf, sizeof buf, "%s/%s", pw->pw_dir, _PATH_SSH_CLIENT_IDENTITY);
288                 if (deleting)
289                         delete_file(ac, buf);
290                 else
291                         add_file(ac, buf);
292         }
293         clear_pass();
294         ssh_close_authentication_connection(ac);
295         exit(0);
296 }
This page took 0.058384 seconds and 5 git commands to generate.