[BACK]Return to ssh-add.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / ssh

Annotation of src/usr.bin/ssh/ssh-add.c, Revision 1.11

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"
1.11    ! markus     17: RCSID("$Id: ssh-add.c,v 1.10 1999/11/15 20:53:24 markus Exp $");
1.1       deraadt    18:
                     19: #include "rsa.h"
                     20: #include "ssh.h"
                     21: #include "xmalloc.h"
                     22: #include "authfd.h"
1.11    ! markus     23: #include "fingerprint.h"
1.1       deraadt    24:
1.2       provos     25: void
1.7       markus     26: delete_file(AuthenticationConnection *ac, const char *filename)
1.1       deraadt    27: {
1.2       provos     28:   RSA *key;
1.1       deraadt    29:   char *comment;
                     30:
1.2       provos     31:   key = RSA_new();
                     32:   if (!load_public_key(filename, key, &comment))
1.1       deraadt    33:     {
                     34:       printf("Bad key file %s: %s\n", filename, strerror(errno));
                     35:       return;
                     36:     }
                     37:
1.2       provos     38:   if (ssh_remove_identity(ac, key))
1.1       deraadt    39:     fprintf(stderr, "Identity removed: %s (%s)\n", filename, comment);
                     40:   else
                     41:     fprintf(stderr, "Could not remove identity: %s\n", filename);
1.2       provos     42:   RSA_free(key);
1.1       deraadt    43:   xfree(comment);
                     44: }
                     45:
1.2       provos     46: void
1.7       markus     47: delete_all(AuthenticationConnection *ac)
1.1       deraadt    48: {
                     49:   /* Send a request to remove all identities. */
                     50:   if (ssh_remove_all_identities(ac))
                     51:     fprintf(stderr, "All identities removed.\n");
                     52:   else
                     53:     fprintf(stderr, "Failed to remove all identitities.\n");
                     54: }
                     55:
1.2       provos     56: void
1.7       markus     57: add_file(AuthenticationConnection *ac, const char *filename)
1.1       deraadt    58: {
1.2       provos     59:   RSA *key;
                     60:   RSA *public_key;
1.9       markus     61:   char *saved_comment, *comment;
                     62:   int success;
1.1       deraadt    63:
1.2       provos     64:   key = RSA_new();
                     65:   public_key = RSA_new();
                     66:   if (!load_public_key(filename, public_key, &saved_comment))
1.1       deraadt    67:     {
                     68:       printf("Bad key file %s: %s\n", filename, strerror(errno));
                     69:       return;
                     70:     }
1.2       provos     71:   RSA_free(public_key);
1.9       markus     72:
                     73:   /* At first, try empty passphrase */
                     74:   success = load_private_key(filename, "", key, &comment);
                     75:   if (!success) {
                     76:     printf("Need passphrase for %s (%s).\n", filename, saved_comment);
                     77:     if (!isatty(STDIN_FILENO)){
                     78:       xfree(saved_comment);
                     79:       return;
                     80:     }
                     81:     for (;;) {
                     82:       char *pass = read_passphrase("Enter passphrase: ", 1);
                     83:       if (strcmp(pass, "") == 0){
                     84:         xfree(pass);
                     85:         xfree(saved_comment);
                     86:         return;
                     87:       }
                     88:       success = load_private_key(filename, pass, key, &comment);
1.1       deraadt    89:       memset(pass, 0, strlen(pass));
                     90:       xfree(pass);
1.9       markus     91:       if (success)
                     92:        break;
                     93:       printf("Bad passphrase.\n");
                     94:     }
                     95:   }
1.1       deraadt    96:   xfree(saved_comment);
                     97:
1.2       provos     98:   if (ssh_add_identity(ac, key, comment))
1.1       deraadt    99:     fprintf(stderr, "Identity added: %s (%s)\n", filename, comment);
                    100:   else
                    101:     fprintf(stderr, "Could not add identity: %s\n", filename);
1.2       provos    102:   RSA_free(key);
1.1       deraadt   103:   xfree(comment);
                    104: }
                    105:
1.2       provos    106: void
1.11    ! markus    107: list_identities(AuthenticationConnection *ac, int fp)
1.1       deraadt   108: {
1.2       provos    109:   BIGNUM *e, *n;
1.10      markus    110:   int status;
1.1       deraadt   111:   char *comment;
                    112:   int had_identities;
                    113:
1.2       provos    114:   e = BN_new();
                    115:   n = BN_new();
1.1       deraadt   116:   had_identities = 0;
1.10      markus    117:   for (status = ssh_get_first_identity(ac, e, n, &comment);
1.1       deraadt   118:        status;
1.10      markus    119:        status = ssh_get_next_identity(ac, e, n, &comment))
1.1       deraadt   120:     {
1.11    ! markus    121:       unsigned int bits = BN_num_bits(n);
1.1       deraadt   122:       had_identities = 1;
1.11    ! markus    123:       if (fp) {
        !           124:        printf("%d %s %s\n", bits, fingerprint(e, n), comment);
        !           125:       } else {
        !           126:        char *ebuf, *nbuf;
        !           127:        ebuf = BN_bn2dec(e);
        !           128:        if (ebuf == NULL) {
        !           129:          error("list_identities: BN_bn2dec(e) failed.");
        !           130:        }else{
        !           131:          nbuf = BN_bn2dec(n);
        !           132:          if (nbuf == NULL) {
        !           133:            error("list_identities: BN_bn2dec(n) failed.");
        !           134:          }else{
        !           135:            printf("%d %s %s %s\n", bits, ebuf, nbuf, comment);
        !           136:            free(nbuf);
        !           137:          }
        !           138:          free(ebuf);
        !           139:        }
1.8       markus    140:       }
1.1       deraadt   141:       xfree(comment);
                    142:     }
1.2       provos    143:   BN_clear_free(e);
                    144:   BN_clear_free(n);
1.1       deraadt   145:   if (!had_identities)
                    146:     printf("The agent has no identities.\n");
                    147: }
                    148:
1.2       provos    149: int
1.7       markus    150: main(int argc, char **argv)
1.1       deraadt   151: {
1.7       markus    152:   AuthenticationConnection *ac = NULL;
1.1       deraadt   153:   struct passwd *pw;
                    154:   char buf[1024];
                    155:   int no_files = 1;
                    156:   int i;
                    157:   int deleting = 0;
1.3       deraadt   158:
                    159:   /* check if RSA support exists */
                    160:   if (rsa_alive() == 0) {
                    161:     extern char *__progname;
                    162:
                    163:     fprintf(stderr,
                    164:       "%s: no RSA support in libssl and libcrypto.  See ssl(8).\n",
                    165:       __progname);
                    166:     exit(1);
                    167:   }
1.1       deraadt   168:
1.7       markus    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++)
1.1       deraadt   177:     {
1.11    ! markus    178:       if ((strcmp(argv[i], "-l") == 0) ||
        !           179:           (strcmp(argv[i], "-L") == 0))
1.1       deraadt   180:        {
1.11    ! markus    181:          list_identities(ac, argv[i][1] == 'l' ? 1 : 0);
1.1       deraadt   182:          no_files = 0; /* Don't default-add/delete if -l. */
                    183:          continue;
                    184:        }
1.7       markus    185:       if (strcmp(argv[i], "-d") == 0)
1.1       deraadt   186:        {
                    187:          deleting = 1;
                    188:          continue;
                    189:        }
1.7       markus    190:       if (strcmp(argv[i], "-D") == 0)
1.1       deraadt   191:        {
1.7       markus    192:          delete_all(ac);
1.1       deraadt   193:          no_files = 0;
                    194:          continue;
                    195:        }
                    196:       no_files = 0;
                    197:       if (deleting)
1.7       markus    198:        delete_file(ac, argv[i]);
1.1       deraadt   199:       else
1.7       markus    200:        add_file(ac, argv[i]);
1.1       deraadt   201:     }
                    202:   if (no_files)
                    203:     {
                    204:       pw = getpwuid(getuid());
                    205:       if (!pw)
                    206:        {
                    207:          fprintf(stderr, "No user found with uid %d\n", (int)getuid());
1.7       markus    208:          ssh_close_authentication_connection(ac);
1.1       deraadt   209:          exit(1);
                    210:        }
1.4       deraadt   211:       snprintf(buf, sizeof buf, "%s/%s", pw->pw_dir, SSH_CLIENT_IDENTITY);
1.1       deraadt   212:       if (deleting)
1.7       markus    213:        delete_file(ac, buf);
1.1       deraadt   214:       else
1.7       markus    215:        add_file(ac, buf);
1.1       deraadt   216:     }
1.7       markus    217:   ssh_close_authentication_connection(ac);
1.1       deraadt   218:   exit(0);
                    219: }