Annotation of src/usr.bin/ssh/ssh-add.c, Revision 1.1
1.1 ! deraadt 1: /*
! 2:
! 3: ssh-add.c
! 4:
! 5: Author: Tatu Ylonen <ylo@cs.hut.fi>
! 6:
! 7: Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
! 8: All rights reserved
! 9:
! 10: Created: Thu Apr 6 00:52:24 1995 ylo
! 11:
! 12: Adds an identity to the authentication server, or removes an identity.
! 13:
! 14: */
! 15:
! 16: #include "includes.h"
! 17: RCSID("$Id: ssh-add.c,v 1.2 1999/05/04 11:59:12 bg Exp $");
! 18:
! 19: #include "randoms.h"
! 20: #include "rsa.h"
! 21: #include "ssh.h"
! 22: #include "xmalloc.h"
! 23: #include "authfd.h"
! 24:
! 25: void delete_file(const char *filename)
! 26: {
! 27: RSAPublicKey key;
! 28: char *comment;
! 29: AuthenticationConnection *ac;
! 30:
! 31: if (!load_public_key(filename, &key, &comment))
! 32: {
! 33: printf("Bad key file %s: %s\n", filename, strerror(errno));
! 34: return;
! 35: }
! 36:
! 37: /* Send the request to the authentication agent. */
! 38: ac = ssh_get_authentication_connection();
! 39: if (!ac)
! 40: {
! 41: fprintf(stderr,
! 42: "Could not open a connection to your authentication agent.\n");
! 43: rsa_clear_public_key(&key);
! 44: xfree(comment);
! 45: return;
! 46: }
! 47: if (ssh_remove_identity(ac, &key))
! 48: fprintf(stderr, "Identity removed: %s (%s)\n", filename, comment);
! 49: else
! 50: fprintf(stderr, "Could not remove identity: %s\n", filename);
! 51: rsa_clear_public_key(&key);
! 52: xfree(comment);
! 53: ssh_close_authentication_connection(ac);
! 54: }
! 55:
! 56: void delete_all()
! 57: {
! 58: AuthenticationConnection *ac;
! 59:
! 60: /* Get a connection to the agent. */
! 61: ac = ssh_get_authentication_connection();
! 62: if (!ac)
! 63: {
! 64: fprintf(stderr,
! 65: "Could not open a connection to your authentication agent.\n");
! 66: return;
! 67: }
! 68:
! 69: /* Send a request to remove all identities. */
! 70: if (ssh_remove_all_identities(ac))
! 71: fprintf(stderr, "All identities removed.\n");
! 72: else
! 73: fprintf(stderr, "Failed to remove all identitities.\n");
! 74:
! 75: /* Close the connection to the agent. */
! 76: ssh_close_authentication_connection(ac);
! 77: }
! 78:
! 79: void add_file(const char *filename)
! 80: {
! 81: RSAPrivateKey key;
! 82: RSAPublicKey public_key;
! 83: AuthenticationConnection *ac;
! 84: char *saved_comment, *comment, *pass;
! 85: int first;
! 86:
! 87: if (!load_public_key(filename, &public_key, &saved_comment))
! 88: {
! 89: printf("Bad key file %s: %s\n", filename, strerror(errno));
! 90: return;
! 91: }
! 92: rsa_clear_public_key(&public_key);
! 93:
! 94: pass = xstrdup("");
! 95: first = 1;
! 96: while (!load_private_key(filename, pass, &key, &comment))
! 97: {
! 98: char buf[1024];
! 99: FILE *f;
! 100:
! 101: /* Free the old passphrase. */
! 102: memset(pass, 0, strlen(pass));
! 103: xfree(pass);
! 104:
! 105: /* Ask for a passphrase. */
! 106: if (getenv("DISPLAY") && !isatty(fileno(stdin)))
! 107: {
! 108: sprintf(buf, "ssh-askpass '%sEnter passphrase for %.100s'",
! 109: first ? "" : "You entered wrong passphrase. ",
! 110: saved_comment);
! 111: f = popen(buf, "r");
! 112: if (!fgets(buf, sizeof(buf), f))
! 113: {
! 114: pclose(f);
! 115: xfree(saved_comment);
! 116: return;
! 117: }
! 118: pclose(f);
! 119: if (strchr(buf, '\n'))
! 120: *strchr(buf, '\n') = 0;
! 121: pass = xstrdup(buf);
! 122: }
! 123: else
! 124: {
! 125: if (first)
! 126: printf("Need passphrase for %s (%s).\n", filename, saved_comment);
! 127: else
! 128: printf("Bad passphrase.\n");
! 129: pass = read_passphrase("Enter passphrase: ", 1);
! 130: if (strcmp(pass, "") == 0)
! 131: {
! 132: xfree(saved_comment);
! 133: xfree(pass);
! 134: return;
! 135: }
! 136: }
! 137: first = 0;
! 138: }
! 139: memset(pass, 0, strlen(pass));
! 140: xfree(pass);
! 141:
! 142: xfree(saved_comment);
! 143:
! 144: /* Send the key to the authentication agent. */
! 145: ac = ssh_get_authentication_connection();
! 146: if (!ac)
! 147: {
! 148: fprintf(stderr,
! 149: "Could not open a connection to your authentication agent.\n");
! 150: rsa_clear_private_key(&key);
! 151: xfree(comment);
! 152: return;
! 153: }
! 154: if (ssh_add_identity(ac, &key, comment))
! 155: fprintf(stderr, "Identity added: %s (%s)\n", filename, comment);
! 156: else
! 157: fprintf(stderr, "Could not add identity: %s\n", filename);
! 158: rsa_clear_private_key(&key);
! 159: xfree(comment);
! 160: ssh_close_authentication_connection(ac);
! 161: }
! 162:
! 163: void list_identities()
! 164: {
! 165: AuthenticationConnection *ac;
! 166: MP_INT e, n;
! 167: int bits, status;
! 168: char *comment;
! 169: int had_identities;
! 170:
! 171: ac = ssh_get_authentication_connection();
! 172: if (!ac)
! 173: {
! 174: fprintf(stderr, "Could not connect to authentication server.\n");
! 175: return;
! 176: }
! 177: mpz_init(&e);
! 178: mpz_init(&n);
! 179: had_identities = 0;
! 180: for (status = ssh_get_first_identity(ac, &bits, &e, &n, &comment);
! 181: status;
! 182: status = ssh_get_next_identity(ac, &bits, &e, &n, &comment))
! 183: {
! 184: had_identities = 1;
! 185: printf("%d ", bits);
! 186: mpz_out_str(stdout, 10, &e);
! 187: printf(" ");
! 188: mpz_out_str(stdout, 10, &n);
! 189: printf(" %s\n", comment);
! 190: xfree(comment);
! 191: }
! 192: mpz_clear(&e);
! 193: mpz_clear(&n);
! 194: if (!had_identities)
! 195: printf("The agent has no identities.\n");
! 196: ssh_close_authentication_connection(ac);
! 197: }
! 198:
! 199: int main(int ac, char **av)
! 200: {
! 201: struct passwd *pw;
! 202: char buf[1024];
! 203: int no_files = 1;
! 204: int i;
! 205: int deleting = 0;
! 206:
! 207: for (i = 1; i < ac; i++)
! 208: {
! 209: if (strcmp(av[i], "-l") == 0)
! 210: {
! 211: list_identities();
! 212: no_files = 0; /* Don't default-add/delete if -l. */
! 213: continue;
! 214: }
! 215: if (strcmp(av[i], "-d") == 0)
! 216: {
! 217: deleting = 1;
! 218: continue;
! 219: }
! 220: if (strcmp(av[i], "-D") == 0)
! 221: {
! 222: delete_all();
! 223: no_files = 0;
! 224: continue;
! 225: }
! 226: no_files = 0;
! 227: if (deleting)
! 228: delete_file(av[i]);
! 229: else
! 230: add_file(av[i]);
! 231: }
! 232: if (no_files)
! 233: {
! 234: pw = getpwuid(getuid());
! 235: if (!pw)
! 236: {
! 237: fprintf(stderr, "No user found with uid %d\n", (int)getuid());
! 238: exit(1);
! 239: }
! 240: sprintf(buf, "%s/%s", pw->pw_dir, SSH_CLIENT_IDENTITY);
! 241: if (deleting)
! 242: delete_file(buf);
! 243: else
! 244: add_file(buf);
! 245: }
! 246: exit(0);
! 247: }