]>
Commit | Line | Data |
---|---|---|
8efc0c15 | 1 | /* |
5260325f | 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 | * Created: Thu Apr 6 00:52:24 1995 ylo | |
6 | * Adds an identity to the authentication server, or removes an identity. | |
7 | */ | |
8efc0c15 | 8 | |
9 | #include "includes.h" | |
10 | RCSID("$Id$"); | |
11 | ||
12 | #include "rsa.h" | |
13 | #include "ssh.h" | |
14 | #include "xmalloc.h" | |
15 | #include "authfd.h" | |
f095fcc7 | 16 | #include "fingerprint.h" |
8efc0c15 | 17 | |
f601d847 | 18 | #ifdef HAVE___PROGNAME |
19 | extern char *__progname; | |
20 | #else /* HAVE___PROGNAME */ | |
21 | const char *__progname = "ssh-add"; | |
22 | #endif /* HAVE___PROGNAME */ | |
23 | ||
8efc0c15 | 24 | void |
5068f573 | 25 | delete_file(AuthenticationConnection *ac, const char *filename) |
8efc0c15 | 26 | { |
5260325f | 27 | RSA *key; |
28 | char *comment; | |
29 | ||
30 | key = RSA_new(); | |
31 | if (!load_public_key(filename, key, &comment)) { | |
32 | printf("Bad key file %s: %s\n", filename, strerror(errno)); | |
33 | return; | |
34 | } | |
35 | if (ssh_remove_identity(ac, key)) | |
36 | fprintf(stderr, "Identity removed: %s (%s)\n", filename, comment); | |
37 | else | |
38 | fprintf(stderr, "Could not remove identity: %s\n", filename); | |
39 | RSA_free(key); | |
40 | xfree(comment); | |
8efc0c15 | 41 | } |
42 | ||
43 | void | |
5068f573 | 44 | delete_all(AuthenticationConnection *ac) |
8efc0c15 | 45 | { |
5260325f | 46 | /* Send a request to remove all identities. */ |
47 | if (ssh_remove_all_identities(ac)) | |
48 | fprintf(stderr, "All identities removed.\n"); | |
49 | else | |
50 | fprintf(stderr, "Failed to remove all identitities.\n"); | |
8efc0c15 | 51 | } |
52 | ||
aa3378df | 53 | char * |
54 | ssh_askpass(char *askpass, char *msg) | |
55 | { | |
56 | pid_t pid; | |
57 | size_t len; | |
58 | char *nl, *pass; | |
59 | int p[2], status; | |
60 | char buf[1024]; | |
61 | ||
62 | if (askpass == NULL) | |
63 | fatal("internal error: askpass undefined"); | |
64 | if (pipe(p) < 0) | |
65 | fatal("ssh_askpass: pipe: %s", strerror(errno)); | |
66 | if ((pid = fork()) < 0) | |
67 | fatal("ssh_askpass: fork: %s", strerror(errno)); | |
68 | if (pid == 0) { | |
69 | close(p[0]); | |
70 | if (dup2(p[1], STDOUT_FILENO) < 0) | |
71 | fatal("ssh_askpass: dup2: %s", strerror(errno)); | |
72 | execlp(askpass, askpass, msg, (char *) 0); | |
73 | fatal("ssh_askpass: exec(%s): %s", askpass, strerror(errno)); | |
74 | } | |
75 | close(p[1]); | |
76 | len = read(p[0], buf, sizeof buf); | |
77 | close(p[0]); | |
78 | while (waitpid(pid, &status, 0) < 0) | |
79 | if (errno != EINTR) | |
80 | break; | |
81 | if (len <= 1) | |
82 | return xstrdup(""); | |
83 | nl = strchr(buf, '\n'); | |
84 | if (nl) | |
85 | *nl = '\0'; | |
86 | pass = xstrdup(buf); | |
87 | memset(buf, 0, sizeof(buf)); | |
88 | return pass; | |
89 | } | |
90 | ||
8efc0c15 | 91 | void |
5068f573 | 92 | add_file(AuthenticationConnection *ac, const char *filename) |
8efc0c15 | 93 | { |
5260325f | 94 | RSA *key; |
95 | RSA *public_key; | |
aa3378df | 96 | char *saved_comment, *comment, *askpass = NULL; |
97 | char buf[1024], msg[1024]; | |
5260325f | 98 | int success; |
aa3378df | 99 | int interactive = isatty(STDIN_FILENO); |
5260325f | 100 | |
101 | key = RSA_new(); | |
102 | public_key = RSA_new(); | |
103 | if (!load_public_key(filename, public_key, &saved_comment)) { | |
104 | printf("Bad key file %s: %s\n", filename, strerror(errno)); | |
105 | return; | |
106 | } | |
107 | RSA_free(public_key); | |
dad9a31e | 108 | |
aa3378df | 109 | if (!interactive && getenv("DISPLAY")) |
110 | askpass = getenv("SSH_ASKPASS"); | |
111 | ||
5260325f | 112 | /* At first, try empty passphrase */ |
113 | success = load_private_key(filename, "", key, &comment); | |
114 | if (!success) { | |
aa3378df | 115 | printf("Need passphrase for %.200s\n", filename); |
116 | if (!interactive && askpass == NULL) { | |
117 | xfree(saved_comment); | |
118 | return; | |
5260325f | 119 | } |
aa3378df | 120 | snprintf(msg, sizeof msg, "Enter passphrase for %.200s", saved_comment); |
121 | for (;;) { | |
122 | char *pass; | |
123 | if (interactive) { | |
124 | snprintf(buf, sizeof buf, "%s: ", msg); | |
125 | pass = read_passphrase(buf, 1); | |
126 | } else { | |
127 | pass = ssh_askpass(askpass, msg); | |
128 | } | |
5260325f | 129 | if (strcmp(pass, "") == 0) { |
130 | xfree(pass); | |
131 | xfree(saved_comment); | |
132 | return; | |
133 | } | |
134 | success = load_private_key(filename, pass, key, &comment); | |
135 | memset(pass, 0, strlen(pass)); | |
136 | xfree(pass); | |
137 | if (success) | |
138 | break; | |
aa3378df | 139 | strlcpy(msg, "Bad passphrase, try again", sizeof msg); |
5260325f | 140 | } |
141 | } | |
142 | xfree(saved_comment); | |
143 | ||
144 | if (ssh_add_identity(ac, key, comment)) | |
145 | fprintf(stderr, "Identity added: %s (%s)\n", filename, comment); | |
146 | else | |
147 | fprintf(stderr, "Could not add identity: %s\n", filename); | |
148 | RSA_free(key); | |
149 | xfree(comment); | |
8efc0c15 | 150 | } |
151 | ||
152 | void | |
f095fcc7 | 153 | list_identities(AuthenticationConnection *ac, int fp) |
8efc0c15 | 154 | { |
5260325f | 155 | BIGNUM *e, *n; |
156 | int status; | |
157 | char *comment; | |
158 | int had_identities; | |
159 | ||
160 | e = BN_new(); | |
161 | n = BN_new(); | |
162 | had_identities = 0; | |
163 | for (status = ssh_get_first_identity(ac, e, n, &comment); | |
164 | status; | |
165 | status = ssh_get_next_identity(ac, e, n, &comment)) { | |
166 | unsigned int bits = BN_num_bits(n); | |
167 | had_identities = 1; | |
168 | if (fp) { | |
169 | printf("%d %s %s\n", bits, fingerprint(e, n), comment); | |
170 | } else { | |
171 | char *ebuf, *nbuf; | |
172 | ebuf = BN_bn2dec(e); | |
173 | if (ebuf == NULL) { | |
174 | error("list_identities: BN_bn2dec(e) failed."); | |
175 | } else { | |
176 | nbuf = BN_bn2dec(n); | |
177 | if (nbuf == NULL) { | |
178 | error("list_identities: BN_bn2dec(n) failed."); | |
179 | } else { | |
180 | printf("%d %s %s %s\n", bits, ebuf, nbuf, comment); | |
181 | free(nbuf); | |
182 | } | |
183 | free(ebuf); | |
184 | } | |
185 | } | |
186 | xfree(comment); | |
f095fcc7 | 187 | } |
5260325f | 188 | BN_clear_free(e); |
189 | BN_clear_free(n); | |
190 | if (!had_identities) | |
191 | printf("The agent has no identities.\n"); | |
8efc0c15 | 192 | } |
193 | ||
194 | int | |
5068f573 | 195 | main(int argc, char **argv) |
8efc0c15 | 196 | { |
5260325f | 197 | AuthenticationConnection *ac = NULL; |
198 | struct passwd *pw; | |
199 | char buf[1024]; | |
200 | int no_files = 1; | |
201 | int i; | |
202 | int deleting = 0; | |
203 | ||
204 | /* check if RSA support exists */ | |
205 | if (rsa_alive() == 0) { | |
206 | extern char *__progname; | |
207 | ||
208 | fprintf(stderr, | |
209 | "%s: no RSA support in libssl and libcrypto. See ssl(8).\n", | |
210 | __progname); | |
211 | exit(1); | |
8efc0c15 | 212 | } |
5260325f | 213 | /* At first, get a connection to the authentication agent. */ |
214 | ac = ssh_get_authentication_connection(); | |
215 | if (ac == NULL) { | |
216 | fprintf(stderr, "Could not open a connection to your authentication agent.\n"); | |
217 | exit(1); | |
8efc0c15 | 218 | } |
5260325f | 219 | for (i = 1; i < argc; i++) { |
220 | if ((strcmp(argv[i], "-l") == 0) || | |
221 | (strcmp(argv[i], "-L") == 0)) { | |
222 | list_identities(ac, argv[i][1] == 'l' ? 1 : 0); | |
223 | /* Don't default-add/delete if -l. */ | |
224 | no_files = 0; | |
225 | continue; | |
226 | } | |
227 | if (strcmp(argv[i], "-d") == 0) { | |
228 | deleting = 1; | |
229 | continue; | |
230 | } | |
231 | if (strcmp(argv[i], "-D") == 0) { | |
232 | delete_all(ac); | |
233 | no_files = 0; | |
234 | continue; | |
235 | } | |
236 | no_files = 0; | |
237 | if (deleting) | |
238 | delete_file(ac, argv[i]); | |
239 | else | |
240 | add_file(ac, argv[i]); | |
8efc0c15 | 241 | } |
5260325f | 242 | if (no_files) { |
243 | pw = getpwuid(getuid()); | |
244 | if (!pw) { | |
245 | fprintf(stderr, "No user found with uid %d\n", (int) getuid()); | |
246 | ssh_close_authentication_connection(ac); | |
247 | exit(1); | |
248 | } | |
249 | snprintf(buf, sizeof buf, "%s/%s", pw->pw_dir, SSH_CLIENT_IDENTITY); | |
250 | if (deleting) | |
251 | delete_file(ac, buf); | |
252 | else | |
253 | add_file(ac, buf); | |
8efc0c15 | 254 | } |
5260325f | 255 | ssh_close_authentication_connection(ac); |
256 | exit(0); | |
8efc0c15 | 257 | } |