[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.69

1.1       deraadt     1: /*
1.13      deraadt     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.
1.19      markus      6:  *
1.22      deraadt     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:  *
1.19      markus     13:  * SSH2 implementation,
1.41      markus     14:  * Copyright (c) 2000, 2001 Markus Friedl.  All rights reserved.
1.22      deraadt    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.
1.13      deraadt    35:  */
1.1       deraadt    36:
                     37: #include "includes.h"
1.69    ! djm        38: RCSID("$OpenBSD: ssh-add.c,v 1.68 2003/06/16 10:22:45 markus Exp $");
1.16      markus     39:
1.19      markus     40: #include <openssl/evp.h>
1.1       deraadt    41:
1.27      markus     42: #include "ssh.h"
1.1       deraadt    43: #include "rsa.h"
1.27      markus     44: #include "log.h"
1.1       deraadt    45: #include "xmalloc.h"
1.16      markus     46: #include "key.h"
1.18      markus     47: #include "authfd.h"
1.16      markus     48: #include "authfile.h"
1.25      markus     49: #include "pathnames.h"
1.27      markus     50: #include "readpass.h"
1.57      stevesk    51: #include "misc.h"
1.1       deraadt    52:
1.45      jakob      53: /* argv0 */
                     54: extern char *__progname;
                     55:
1.48      djm        56: /* Default files to add */
                     57: static char *default_files[] = {
                     58:        _PATH_SSH_CLIENT_ID_RSA,
                     59:        _PATH_SSH_CLIENT_ID_DSA,
1.51      markus     60:        _PATH_SSH_CLIENT_IDENTITY,
1.48      djm        61:        NULL
                     62: };
                     63:
1.56      markus     64: /* Default lifetime (0 == forever) */
1.57      stevesk    65: static int lifetime = 0;
1.48      djm        66:
1.65      markus     67: /* User has to confirm key use */
                     68: static int confirm = 0;
                     69:
1.33      markus     70: /* we keep a cache of one passphrases */
                     71: static char *pass = NULL;
1.39      itojun     72: static void
1.33      markus     73: clear_pass(void)
                     74: {
                     75:        if (pass) {
                     76:                memset(pass, 0, strlen(pass));
                     77:                xfree(pass);
                     78:                pass = NULL;
                     79:        }
                     80: }
                     81:
1.46      djm        82: static int
1.7       markus     83: delete_file(AuthenticationConnection *ac, const char *filename)
1.1       deraadt    84: {
1.16      markus     85:        Key *public;
1.32      markus     86:        char *comment = NULL;
1.46      djm        87:        int ret = -1;
1.1       deraadt    88:
1.31      markus     89:        public = key_load_public(filename, &comment);
                     90:        if (public == NULL) {
                     91:                printf("Bad key file %s\n", filename);
1.46      djm        92:                return -1;
1.12      markus     93:        }
1.46      djm        94:        if (ssh_remove_identity(ac, public)) {
1.12      markus     95:                fprintf(stderr, "Identity removed: %s (%s)\n", filename, comment);
1.46      djm        96:                ret = 0;
                     97:        } else
1.12      markus     98:                fprintf(stderr, "Could not remove identity: %s\n", filename);
1.46      djm        99:
1.16      markus    100:        key_free(public);
1.12      markus    101:        xfree(comment);
1.47      deraadt   102:
1.46      djm       103:        return ret;
1.1       deraadt   104: }
                    105:
1.19      markus    106: /* Send a request to remove all identities. */
1.46      djm       107: static int
1.7       markus    108: delete_all(AuthenticationConnection *ac)
1.1       deraadt   109: {
1.46      djm       110:        int ret = -1;
1.19      markus    111:
1.46      djm       112:        if (ssh_remove_all_identities(ac, 1))
                    113:                ret = 0;
1.19      markus    114:        /* ignore error-code for ssh2 */
                    115:        ssh_remove_all_identities(ac, 2);
                    116:
1.46      djm       117:        if (ret == 0)
1.12      markus    118:                fprintf(stderr, "All identities removed.\n");
                    119:        else
1.24      markus    120:                fprintf(stderr, "Failed to remove all identities.\n");
1.46      djm       121:
                    122:        return ret;
1.1       deraadt   123: }
                    124:
1.46      djm       125: static int
1.7       markus    126: add_file(AuthenticationConnection *ac, const char *filename)
1.1       deraadt   127: {
1.19      markus    128:        struct stat st;
1.16      markus    129:        Key *private;
1.36      markus    130:        char *comment = NULL;
                    131:        char msg[1024];
1.46      djm       132:        int ret = -1;
1.12      markus    133:
1.19      markus    134:        if (stat(filename, &st) < 0) {
                    135:                perror(filename);
1.46      djm       136:                return -1;
1.19      markus    137:        }
1.12      markus    138:        /* At first, try empty passphrase */
1.31      markus    139:        private = key_load_private(filename, "", &comment);
                    140:        if (comment == NULL)
                    141:                comment = xstrdup(filename);
1.33      markus    142:        /* try last */
                    143:        if (private == NULL && pass != NULL)
                    144:                private = key_load_private(filename, pass, NULL);
1.31      markus    145:        if (private == NULL) {
1.33      markus    146:                /* clear passphrase since it did not work */
                    147:                clear_pass();
1.37      markus    148:                snprintf(msg, sizeof msg, "Enter passphrase for %.200s: ",
1.36      markus    149:                   comment);
1.12      markus    150:                for (;;) {
1.40      markus    151:                        pass = read_passphrase(msg, RP_ALLOW_STDIN);
1.12      markus    152:                        if (strcmp(pass, "") == 0) {
1.35      markus    153:                                clear_pass();
1.31      markus    154:                                xfree(comment);
1.46      djm       155:                                return -1;
1.12      markus    156:                        }
1.31      markus    157:                        private = key_load_private(filename, pass, &comment);
                    158:                        if (private != NULL)
1.12      markus    159:                                break;
1.33      markus    160:                        clear_pass();
1.68      markus    161:                        snprintf(msg, sizeof msg,
                    162:                            "Bad passphrase, try again for %.200s: ", comment);
1.12      markus    163:                }
                    164:        }
1.60      markus    165:
1.69    ! djm       166:        if (ssh_add_identity_constrained(ac, private, comment, lifetime,
        !           167:            confirm)) {
1.60      markus    168:                fprintf(stderr, "Identity added: %s (%s)\n", filename, comment);
                    169:                ret = 0;
                    170:                if (lifetime != 0)
1.64      deraadt   171:                        fprintf(stderr,
1.60      markus    172:                            "Lifetime set to %d seconds\n", lifetime);
1.69    ! djm       173:                if (confirm != 0)
1.65      markus    174:                        fprintf(stderr,
                    175:                            "The user has to confirm each use of the key\n");
1.60      markus    176:        } else if (ssh_add_identity(ac, private, comment)) {
1.31      markus    177:                fprintf(stderr, "Identity added: %s (%s)\n", filename, comment);
1.46      djm       178:                ret = 0;
1.60      markus    179:        } else {
1.12      markus    180:                fprintf(stderr, "Could not add identity: %s\n", filename);
1.56      markus    181:        }
                    182:
1.31      markus    183:        xfree(comment);
1.16      markus    184:        key_free(private);
1.47      deraadt   185:
1.46      djm       186:        return ret;
1.1       deraadt   187: }
                    188:
1.46      djm       189: static int
1.44      markus    190: update_card(AuthenticationConnection *ac, int add, const char *id)
1.42      markus    191: {
1.53      rees      192:        char *pin;
1.66      markus    193:        int ret = -1;
1.53      rees      194:
                    195:        pin = read_passphrase("Enter passphrase for smartcard: ", RP_ALLOW_STDIN);
                    196:        if (pin == NULL)
                    197:                return -1;
                    198:
1.67      djm       199:        if (ssh_update_card(ac, add, id, pin, lifetime, confirm)) {
1.44      markus    200:                fprintf(stderr, "Card %s: %s\n",
1.47      deraadt   201:                    add ? "added" : "removed", id);
1.66      markus    202:                ret = 0;
1.46      djm       203:        } else {
1.44      markus    204:                fprintf(stderr, "Could not %s card: %s\n",
1.47      deraadt   205:                    add ? "add" : "remove", id);
1.66      markus    206:                ret = -1;
1.46      djm       207:        }
1.66      markus    208:        xfree(pin);
                    209:        return ret;
1.42      markus    210: }
                    211:
1.50      markus    212: static int
1.30      markus    213: list_identities(AuthenticationConnection *ac, int do_fp)
1.1       deraadt   214: {
1.19      markus    215:        Key *key;
1.30      markus    216:        char *comment, *fp;
1.19      markus    217:        int had_identities = 0;
                    218:        int version;
1.12      markus    219:
1.19      markus    220:        for (version = 1; version <= 2; version++) {
                    221:                for (key = ssh_get_first_identity(ac, &comment, version);
1.47      deraadt   222:                    key != NULL;
                    223:                    key = ssh_get_next_identity(ac, &comment, version)) {
1.19      markus    224:                        had_identities = 1;
1.30      markus    225:                        if (do_fp) {
                    226:                                fp = key_fingerprint(key, SSH_FP_MD5,
                    227:                                    SSH_FP_HEX);
1.23      markus    228:                                printf("%d %s %s (%s)\n",
1.30      markus    229:                                    key_size(key), fp, comment, key_type(key));
                    230:                                xfree(fp);
1.12      markus    231:                        } else {
1.19      markus    232:                                if (!key_write(key, stdout))
                    233:                                        fprintf(stderr, "key_write failed");
                    234:                                fprintf(stdout, " %s\n", comment);
1.12      markus    235:                        }
1.19      markus    236:                        key_free(key);
                    237:                        xfree(comment);
1.12      markus    238:                }
                    239:        }
1.50      markus    240:        if (!had_identities) {
1.12      markus    241:                printf("The agent has no identities.\n");
1.50      markus    242:                return -1;
                    243:        }
                    244:        return 0;
1.1       deraadt   245: }
                    246:
1.48      djm       247: static int
1.54      markus    248: lock_agent(AuthenticationConnection *ac, int lock)
                    249: {
                    250:        char prompt[100], *p1, *p2;
                    251:        int passok = 1, ret = -1;
1.61      deraadt   252:
1.54      markus    253:        strlcpy(prompt, "Enter lock password: ", sizeof(prompt));
                    254:        p1 = read_passphrase(prompt, RP_ALLOW_STDIN);
                    255:        if (lock) {
                    256:                strlcpy(prompt, "Again: ", sizeof prompt);
                    257:                p2 = read_passphrase(prompt, RP_ALLOW_STDIN);
                    258:                if (strcmp(p1, p2) != 0) {
                    259:                        fprintf(stderr, "Passwords do not match.\n");
                    260:                        passok = 0;
                    261:                }
                    262:                memset(p2, 0, strlen(p2));
                    263:                xfree(p2);
                    264:        }
                    265:        if (passok && ssh_lock_agent(ac, lock, p1)) {
                    266:                fprintf(stderr, "Agent %slocked.\n", lock ? "" : "un");
                    267:                ret = 0;
                    268:        } else
                    269:                fprintf(stderr, "Failed to %slock agent.\n", lock ? "" : "un");
                    270:        memset(p1, 0, strlen(p1));
                    271:        xfree(p1);
1.62      markus    272:        return (ret);
1.54      markus    273: }
                    274:
                    275: static int
1.48      djm       276: do_file(AuthenticationConnection *ac, int deleting, char *file)
                    277: {
                    278:        if (deleting) {
                    279:                if (delete_file(ac, file) == -1)
                    280:                        return -1;
                    281:        } else {
                    282:                if (add_file(ac, file) == -1)
                    283:                        return -1;
                    284:        }
                    285:        return 0;
                    286: }
                    287:
1.42      markus    288: static void
                    289: usage(void)
                    290: {
1.45      jakob     291:        fprintf(stderr, "Usage: %s [options]\n", __progname);
                    292:        fprintf(stderr, "Options:\n");
                    293:        fprintf(stderr, "  -l          List fingerprints of all identities.\n");
                    294:        fprintf(stderr, "  -L          List public key parameters of all identities.\n");
                    295:        fprintf(stderr, "  -d          Delete identity.\n");
                    296:        fprintf(stderr, "  -D          Delete all identities.\n");
1.55      markus    297:        fprintf(stderr, "  -x          Lock agent.\n");
1.63      markus    298:        fprintf(stderr, "  -X          Unlock agent.\n");
1.56      markus    299:        fprintf(stderr, "  -t life     Set lifetime (in seconds) when adding identities.\n");
1.65      markus    300:        fprintf(stderr, "  -c          Require confirmation to sign using identities\n");
1.45      jakob     301: #ifdef SMARTCARD
                    302:        fprintf(stderr, "  -s reader   Add key in smartcard reader.\n");
                    303:        fprintf(stderr, "  -e reader   Remove key in smartcard reader.\n");
                    304: #endif
1.42      markus    305: }
                    306:
1.2       provos    307: int
1.7       markus    308: main(int argc, char **argv)
1.1       deraadt   309: {
1.43      markus    310:        extern char *optarg;
                    311:        extern int optind;
1.12      markus    312:        AuthenticationConnection *ac = NULL;
1.44      markus    313:        char *sc_reader_id = NULL;
1.46      djm       314:        int i, ch, deleting = 0, ret = 0;
1.12      markus    315:
1.28      stevesk   316:        SSLeay_add_all_algorithms();
1.19      markus    317:
1.12      markus    318:        /* At first, get a connection to the authentication agent. */
                    319:        ac = ssh_get_authentication_connection();
                    320:        if (ac == NULL) {
                    321:                fprintf(stderr, "Could not open a connection to your authentication agent.\n");
1.50      markus    322:                exit(2);
1.12      markus    323:        }
1.65      markus    324:        while ((ch = getopt(argc, argv, "lLcdDxXe:s:t:")) != -1) {
1.43      markus    325:                switch (ch) {
                    326:                case 'l':
                    327:                case 'L':
1.50      markus    328:                        if (list_identities(ac, ch == 'l' ? 1 : 0) == -1)
1.54      markus    329:                                ret = 1;
                    330:                        goto done;
                    331:                        break;
                    332:                case 'x':
                    333:                case 'X':
                    334:                        if (lock_agent(ac, ch == 'x' ? 1 : 0) == -1)
1.50      markus    335:                                ret = 1;
1.43      markus    336:                        goto done;
1.65      markus    337:                        break;
                    338:                case 'c':
                    339:                        confirm = 1;
1.43      markus    340:                        break;
                    341:                case 'd':
1.12      markus    342:                        deleting = 1;
1.43      markus    343:                        break;
                    344:                case 'D':
1.46      djm       345:                        if (delete_all(ac) == -1)
                    346:                                ret = 1;
1.43      markus    347:                        goto done;
                    348:                        break;
                    349:                case 's':
1.44      markus    350:                        sc_reader_id = optarg;
1.43      markus    351:                        break;
                    352:                case 'e':
1.47      deraadt   353:                        deleting = 1;
1.44      markus    354:                        sc_reader_id = optarg;
1.56      markus    355:                        break;
                    356:                case 't':
1.57      stevesk   357:                        if ((lifetime = convtime(optarg)) == -1) {
                    358:                                fprintf(stderr, "Invalid lifetime\n");
                    359:                                ret = 1;
                    360:                                goto done;
                    361:                        }
1.43      markus    362:                        break;
                    363:                default:
                    364:                        usage();
1.46      djm       365:                        ret = 1;
                    366:                        goto done;
1.42      markus    367:                }
                    368:        }
1.43      markus    369:        argc -= optind;
                    370:        argv += optind;
1.44      markus    371:        if (sc_reader_id != NULL) {
1.46      djm       372:                if (update_card(ac, !deleting, sc_reader_id) == -1)
                    373:                        ret = 1;
1.43      markus    374:                goto done;
1.12      markus    375:        }
1.43      markus    376:        if (argc == 0) {
1.48      djm       377:                char buf[MAXPATHLEN];
                    378:                struct passwd *pw;
1.52      markus    379:                struct stat st;
                    380:                int count = 0;
1.48      djm       381:
                    382:                if ((pw = getpwuid(getuid())) == NULL) {
1.20      deraadt   383:                        fprintf(stderr, "No user found with uid %u\n",
                    384:                            (u_int)getuid());
1.46      djm       385:                        ret = 1;
                    386:                        goto done;
1.12      markus    387:                }
1.48      djm       388:
                    389:                for(i = 0; default_files[i]; i++) {
1.51      markus    390:                        snprintf(buf, sizeof(buf), "%s/%s", pw->pw_dir,
1.48      djm       391:                            default_files[i]);
1.52      markus    392:                        if (stat(buf, &st) < 0)
                    393:                                continue;
1.48      djm       394:                        if (do_file(ac, deleting, buf) == -1)
1.46      djm       395:                                ret = 1;
1.52      markus    396:                        else
                    397:                                count++;
1.46      djm       398:                }
1.52      markus    399:                if (count == 0)
                    400:                        ret = 1;
1.43      markus    401:        } else {
1.48      djm       402:                for(i = 0; i < argc; i++) {
1.49      deraadt   403:                        if (do_file(ac, deleting, argv[i]) == -1)
1.48      djm       404:                                ret = 1;
1.43      markus    405:                }
1.12      markus    406:        }
1.33      markus    407:        clear_pass();
1.43      markus    408:
                    409: done:
1.12      markus    410:        ssh_close_authentication_connection(ac);
1.46      djm       411:        return ret;
1.1       deraadt   412: }