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

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