summaryrefslogtreecommitdiff
path: root/ssh-add.c
diff options
context:
space:
mode:
Diffstat (limited to 'ssh-add.c')
-rw-r--r--ssh-add.c88
1 files changed, 26 insertions, 62 deletions
diff --git a/ssh-add.c b/ssh-add.c
index 5ac3c303a..2b4966d73 100644
--- a/ssh-add.c
+++ b/ssh-add.c
@@ -14,7 +14,7 @@ Adds an identity to the authentication server, or removes an identity.
14*/ 14*/
15 15
16#include "includes.h" 16#include "includes.h"
17RCSID("$Id: ssh-add.c,v 1.1 1999/10/27 03:42:45 damien Exp $"); 17RCSID("$Id: ssh-add.c,v 1.2 1999/10/28 05:23:30 damien Exp $");
18 18
19#include "rsa.h" 19#include "rsa.h"
20#include "ssh.h" 20#include "ssh.h"
@@ -22,11 +22,10 @@ RCSID("$Id: ssh-add.c,v 1.1 1999/10/27 03:42:45 damien Exp $");
22#include "authfd.h" 22#include "authfd.h"
23 23
24void 24void
25delete_file(const char *filename) 25delete_file(AuthenticationConnection *ac, const char *filename)
26{ 26{
27 RSA *key; 27 RSA *key;
28 char *comment; 28 char *comment;
29 AuthenticationConnection *ac;
30 29
31 key = RSA_new(); 30 key = RSA_new();
32 if (!load_public_key(filename, key, &comment)) 31 if (!load_public_key(filename, key, &comment))
@@ -35,55 +34,29 @@ delete_file(const char *filename)
35 return; 34 return;
36 } 35 }
37 36
38 /* Send the request to the authentication agent. */
39 ac = ssh_get_authentication_connection();
40 if (!ac)
41 {
42 fprintf(stderr,
43 "Could not open a connection to your authentication agent.\n");
44 RSA_free(key);
45 xfree(comment);
46 return;
47 }
48 if (ssh_remove_identity(ac, key)) 37 if (ssh_remove_identity(ac, key))
49 fprintf(stderr, "Identity removed: %s (%s)\n", filename, comment); 38 fprintf(stderr, "Identity removed: %s (%s)\n", filename, comment);
50 else 39 else
51 fprintf(stderr, "Could not remove identity: %s\n", filename); 40 fprintf(stderr, "Could not remove identity: %s\n", filename);
52 RSA_free(key); 41 RSA_free(key);
53 xfree(comment); 42 xfree(comment);
54 ssh_close_authentication_connection(ac);
55} 43}
56 44
57void 45void
58delete_all() 46delete_all(AuthenticationConnection *ac)
59{ 47{
60 AuthenticationConnection *ac;
61
62 /* Get a connection to the agent. */
63 ac = ssh_get_authentication_connection();
64 if (!ac)
65 {
66 fprintf(stderr,
67 "Could not open a connection to your authentication agent.\n");
68 return;
69 }
70
71 /* Send a request to remove all identities. */ 48 /* Send a request to remove all identities. */
72 if (ssh_remove_all_identities(ac)) 49 if (ssh_remove_all_identities(ac))
73 fprintf(stderr, "All identities removed.\n"); 50 fprintf(stderr, "All identities removed.\n");
74 else 51 else
75 fprintf(stderr, "Failed to remove all identitities.\n"); 52 fprintf(stderr, "Failed to remove all identitities.\n");
76
77 /* Close the connection to the agent. */
78 ssh_close_authentication_connection(ac);
79} 53}
80 54
81void 55void
82add_file(const char *filename) 56add_file(AuthenticationConnection *ac, const char *filename)
83{ 57{
84 RSA *key; 58 RSA *key;
85 RSA *public_key; 59 RSA *public_key;
86 AuthenticationConnection *ac;
87 char *saved_comment, *comment, *pass; 60 char *saved_comment, *comment, *pass;
88 int first; 61 int first;
89 62
@@ -131,40 +104,22 @@ add_file(const char *filename)
131 104
132 xfree(saved_comment); 105 xfree(saved_comment);
133 106
134 /* Send the key to the authentication agent. */
135 ac = ssh_get_authentication_connection();
136 if (!ac)
137 {
138 fprintf(stderr,
139 "Could not open a connection to your authentication agent.\n");
140 RSA_free(key);
141 xfree(comment);
142 return;
143 }
144 if (ssh_add_identity(ac, key, comment)) 107 if (ssh_add_identity(ac, key, comment))
145 fprintf(stderr, "Identity added: %s (%s)\n", filename, comment); 108 fprintf(stderr, "Identity added: %s (%s)\n", filename, comment);
146 else 109 else
147 fprintf(stderr, "Could not add identity: %s\n", filename); 110 fprintf(stderr, "Could not add identity: %s\n", filename);
148 RSA_free(key); 111 RSA_free(key);
149 xfree(comment); 112 xfree(comment);
150 ssh_close_authentication_connection(ac);
151} 113}
152 114
153void 115void
154list_identities() 116list_identities(AuthenticationConnection *ac)
155{ 117{
156 AuthenticationConnection *ac;
157 BIGNUM *e, *n; 118 BIGNUM *e, *n;
158 int bits, status; 119 int bits, status;
159 char *comment; 120 char *comment;
160 int had_identities; 121 int had_identities;
161 122
162 ac = ssh_get_authentication_connection();
163 if (!ac)
164 {
165 fprintf(stderr, "Could not connect to authentication server.\n");
166 return;
167 }
168 e = BN_new(); 123 e = BN_new();
169 n = BN_new(); 124 n = BN_new();
170 had_identities = 0; 125 had_identities = 0;
@@ -189,12 +144,12 @@ list_identities()
189 BN_clear_free(n); 144 BN_clear_free(n);
190 if (!had_identities) 145 if (!had_identities)
191 printf("The agent has no identities.\n"); 146 printf("The agent has no identities.\n");
192 ssh_close_authentication_connection(ac);
193} 147}
194 148
195int 149int
196main(int ac, char **av) 150main(int argc, char **argv)
197{ 151{
152 AuthenticationConnection *ac = NULL;
198 struct passwd *pw; 153 struct passwd *pw;
199 char buf[1024]; 154 char buf[1024];
200 int no_files = 1; 155 int no_files = 1;
@@ -211,30 +166,37 @@ main(int ac, char **av)
211 exit(1); 166 exit(1);
212 } 167 }
213 168
214 for (i = 1; i < ac; i++) 169 /* At first, get a connection to the authentication agent. */
170 ac = ssh_get_authentication_connection();
171 if (ac == NULL) {
172 fprintf(stderr, "Could not open a connection to your authentication agent.\n");
173 exit(1);
174 }
175
176 for (i = 1; i < argc; i++)
215 { 177 {
216 if (strcmp(av[i], "-l") == 0) 178 if (strcmp(argv[i], "-l") == 0)
217 { 179 {
218 list_identities(); 180 list_identities(ac);
219 no_files = 0; /* Don't default-add/delete if -l. */ 181 no_files = 0; /* Don't default-add/delete if -l. */
220 continue; 182 continue;
221 } 183 }
222 if (strcmp(av[i], "-d") == 0) 184 if (strcmp(argv[i], "-d") == 0)
223 { 185 {
224 deleting = 1; 186 deleting = 1;
225 continue; 187 continue;
226 } 188 }
227 if (strcmp(av[i], "-D") == 0) 189 if (strcmp(argv[i], "-D") == 0)
228 { 190 {
229 delete_all(); 191 delete_all(ac);
230 no_files = 0; 192 no_files = 0;
231 continue; 193 continue;
232 } 194 }
233 no_files = 0; 195 no_files = 0;
234 if (deleting) 196 if (deleting)
235 delete_file(av[i]); 197 delete_file(ac, argv[i]);
236 else 198 else
237 add_file(av[i]); 199 add_file(ac, argv[i]);
238 } 200 }
239 if (no_files) 201 if (no_files)
240 { 202 {
@@ -242,13 +204,15 @@ main(int ac, char **av)
242 if (!pw) 204 if (!pw)
243 { 205 {
244 fprintf(stderr, "No user found with uid %d\n", (int)getuid()); 206 fprintf(stderr, "No user found with uid %d\n", (int)getuid());
207 ssh_close_authentication_connection(ac);
245 exit(1); 208 exit(1);
246 } 209 }
247 snprintf(buf, sizeof buf, "%s/%s", pw->pw_dir, SSH_CLIENT_IDENTITY); 210 snprintf(buf, sizeof buf, "%s/%s", pw->pw_dir, SSH_CLIENT_IDENTITY);
248 if (deleting) 211 if (deleting)
249 delete_file(buf); 212 delete_file(ac, buf);
250 else 213 else
251 add_file(buf); 214 add_file(ac, buf);
252 } 215 }
216 ssh_close_authentication_connection(ac);
253 exit(0); 217 exit(0);
254} 218}