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

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.16.2.1  jason       6:  *
1.16.2.2  jason       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.16.2.1  jason      13:  * SSH2 implementation,
1.16.2.4! jason      14:  * Copyright (c) 2000 Markus Friedl.  All rights reserved.
1.16.2.2  jason      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.16.2.4! jason      38: RCSID("$OpenBSD: ssh-add.c,v 1.30 2001/03/12 22:02:02 markus Exp $");
1.16      markus     39:
1.16.2.1  jason      40: #include <openssl/evp.h>
1.1       deraadt    41:
                     42: #include "ssh.h"
1.16.2.3  jason      43: #include "rsa.h"
                     44: #include "log.h"
1.1       deraadt    45: #include "xmalloc.h"
1.16      markus     46: #include "key.h"
1.16.2.1  jason      47: #include "authfd.h"
1.16      markus     48: #include "authfile.h"
1.16.2.3  jason      49: #include "pathnames.h"
                     50: #include "readpass.h"
1.1       deraadt    51:
1.2       provos     52: void
1.7       markus     53: delete_file(AuthenticationConnection *ac, const char *filename)
1.1       deraadt    54: {
1.16      markus     55:        Key *public;
1.12      markus     56:        char *comment;
1.1       deraadt    57:
1.16.2.3  jason      58:        public = key_new(KEY_RSA1);
1.16      markus     59:        if (!load_public_key(filename, public, &comment)) {
1.16.2.2  jason      60:                key_free(public);
1.16.2.3  jason      61:                public = key_new(KEY_UNSPEC);
1.16.2.2  jason      62:                if (!try_load_public_key(filename, public, &comment)) {
                     63:                        printf("Bad key file %s\n", filename);
                     64:                        return;
                     65:                }
1.12      markus     66:        }
1.16.2.1  jason      67:        if (ssh_remove_identity(ac, public))
1.12      markus     68:                fprintf(stderr, "Identity removed: %s (%s)\n", filename, comment);
                     69:        else
                     70:                fprintf(stderr, "Could not remove identity: %s\n", filename);
1.16      markus     71:        key_free(public);
1.12      markus     72:        xfree(comment);
1.1       deraadt    73: }
                     74:
1.16.2.1  jason      75: /* Send a request to remove all identities. */
1.2       provos     76: void
1.7       markus     77: delete_all(AuthenticationConnection *ac)
1.1       deraadt    78: {
1.16.2.1  jason      79:        int success = 1;
                     80:
                     81:        if (!ssh_remove_all_identities(ac, 1))
                     82:                success = 0;
                     83:        /* ignore error-code for ssh2 */
                     84:        ssh_remove_all_identities(ac, 2);
                     85:
                     86:        if (success)
1.12      markus     87:                fprintf(stderr, "All identities removed.\n");
                     88:        else
1.16.2.3  jason      89:                fprintf(stderr, "Failed to remove all identities.\n");
1.1       deraadt    90: }
                     91:
1.14      markus     92: char *
                     93: ssh_askpass(char *askpass, char *msg)
                     94: {
                     95:        pid_t pid;
                     96:        size_t len;
                     97:        char *nl, *pass;
                     98:        int p[2], status;
                     99:        char buf[1024];
                    100:
1.16.2.3  jason     101:        if (fflush(stdout) != 0)
                    102:                error("ssh_askpass: fflush: %s", strerror(errno));
1.14      markus    103:        if (askpass == NULL)
                    104:                fatal("internal error: askpass undefined");
                    105:        if (pipe(p) < 0)
                    106:                fatal("ssh_askpass: pipe: %s", strerror(errno));
                    107:        if ((pid = fork()) < 0)
                    108:                fatal("ssh_askpass: fork: %s", strerror(errno));
                    109:        if (pid == 0) {
                    110:                close(p[0]);
                    111:                if (dup2(p[1], STDOUT_FILENO) < 0)
                    112:                        fatal("ssh_askpass: dup2: %s", strerror(errno));
                    113:                execlp(askpass, askpass, msg, (char *) 0);
                    114:                fatal("ssh_askpass: exec(%s): %s", askpass, strerror(errno));
                    115:        }
                    116:        close(p[1]);
                    117:        len = read(p[0], buf, sizeof buf);
                    118:        close(p[0]);
                    119:        while (waitpid(pid, &status, 0) < 0)
                    120:                if (errno != EINTR)
                    121:                        break;
                    122:        if (len <= 1)
                    123:                return xstrdup("");
                    124:        nl = strchr(buf, '\n');
                    125:        if (nl)
                    126:                *nl = '\0';
                    127:        pass = xstrdup(buf);
                    128:        memset(buf, 0, sizeof(buf));
                    129:        return pass;
                    130: }
                    131:
1.2       provos    132: void
1.7       markus    133: add_file(AuthenticationConnection *ac, const char *filename)
1.1       deraadt   134: {
1.16.2.1  jason     135:        struct stat st;
1.16      markus    136:        Key *public;
                    137:        Key *private;
1.14      markus    138:        char *saved_comment, *comment, *askpass = NULL;
                    139:        char buf[1024], msg[1024];
1.12      markus    140:        int success;
1.14      markus    141:        int interactive = isatty(STDIN_FILENO);
1.16.2.3  jason     142:        int type = KEY_RSA1;
1.12      markus    143:
1.16.2.1  jason     144:        if (stat(filename, &st) < 0) {
                    145:                perror(filename);
                    146:                exit(1);
                    147:        }
                    148:        /*
                    149:         * try to load the public key. right now this only works for RSA,
                    150:         * since DSA keys are fully encrypted
                    151:         */
1.16.2.3  jason     152:        public = key_new(KEY_RSA1);
1.16      markus    153:        if (!load_public_key(filename, public, &saved_comment)) {
1.16.2.3  jason     154:                /* ok, so we will assume this is 'some' key */
                    155:                type = KEY_UNSPEC;
1.16.2.1  jason     156:                saved_comment = xstrdup(filename);
1.12      markus    157:        }
1.16      markus    158:        key_free(public);
1.12      markus    159:
1.15      markus    160:        if (!interactive && getenv("DISPLAY")) {
                    161:                if (getenv(SSH_ASKPASS_ENV))
                    162:                        askpass = getenv(SSH_ASKPASS_ENV);
                    163:                else
1.16.2.3  jason     164:                        askpass = _PATH_SSH_ASKPASS_DEFAULT;
1.15      markus    165:        }
1.14      markus    166:
1.12      markus    167:        /* At first, try empty passphrase */
1.16.2.1  jason     168:        private = key_new(type);
1.16      markus    169:        success = load_private_key(filename, "", private, &comment);
1.12      markus    170:        if (!success) {
1.14      markus    171:                printf("Need passphrase for %.200s\n", filename);
                    172:                if (!interactive && askpass == NULL) {
1.12      markus    173:                        xfree(saved_comment);
                    174:                        return;
                    175:                }
1.14      markus    176:                snprintf(msg, sizeof msg, "Enter passphrase for %.200s", saved_comment);
1.12      markus    177:                for (;;) {
1.14      markus    178:                        char *pass;
                    179:                        if (interactive) {
                    180:                                snprintf(buf, sizeof buf, "%s: ", msg);
                    181:                                pass = read_passphrase(buf, 1);
                    182:                        } else {
                    183:                                pass = ssh_askpass(askpass, msg);
                    184:                        }
1.12      markus    185:                        if (strcmp(pass, "") == 0) {
                    186:                                xfree(pass);
                    187:                                xfree(saved_comment);
                    188:                                return;
                    189:                        }
1.16      markus    190:                        success = load_private_key(filename, pass, private, &comment);
1.12      markus    191:                        memset(pass, 0, strlen(pass));
                    192:                        xfree(pass);
                    193:                        if (success)
                    194:                                break;
1.14      markus    195:                        strlcpy(msg, "Bad passphrase, try again", sizeof msg);
1.12      markus    196:                }
                    197:        }
1.16.2.1  jason     198:        xfree(comment);
                    199:        if (ssh_add_identity(ac, private, saved_comment))
                    200:                fprintf(stderr, "Identity added: %s (%s)\n", filename, saved_comment);
1.12      markus    201:        else
                    202:                fprintf(stderr, "Could not add identity: %s\n", filename);
1.16      markus    203:        key_free(private);
1.16.2.1  jason     204:        xfree(saved_comment);
1.1       deraadt   205: }
                    206:
1.2       provos    207: void
1.16.2.4! jason     208: list_identities(AuthenticationConnection *ac, int do_fp)
1.1       deraadt   209: {
1.16.2.1  jason     210:        Key *key;
1.16.2.4! jason     211:        char *comment, *fp;
1.16.2.1  jason     212:        int had_identities = 0;
                    213:        int version;
1.12      markus    214:
1.16.2.1  jason     215:        for (version = 1; version <= 2; version++) {
                    216:                for (key = ssh_get_first_identity(ac, &comment, version);
                    217:                     key != NULL;
                    218:                     key = ssh_get_next_identity(ac, &comment, version)) {
                    219:                        had_identities = 1;
1.16.2.4! jason     220:                        if (do_fp) {
        !           221:                                fp = key_fingerprint(key, SSH_FP_MD5,
        !           222:                                    SSH_FP_HEX);
1.16.2.3  jason     223:                                printf("%d %s %s (%s)\n",
1.16.2.4! jason     224:                                    key_size(key), fp, comment, key_type(key));
        !           225:                                xfree(fp);
1.12      markus    226:                        } else {
1.16.2.1  jason     227:                                if (!key_write(key, stdout))
                    228:                                        fprintf(stderr, "key_write failed");
                    229:                                fprintf(stdout, " %s\n", comment);
1.12      markus    230:                        }
1.16.2.1  jason     231:                        key_free(key);
                    232:                        xfree(comment);
1.12      markus    233:                }
                    234:        }
                    235:        if (!had_identities)
                    236:                printf("The agent has no identities.\n");
1.1       deraadt   237: }
                    238:
1.2       provos    239: int
1.7       markus    240: main(int argc, char **argv)
1.1       deraadt   241: {
1.12      markus    242:        AuthenticationConnection *ac = NULL;
                    243:        struct passwd *pw;
                    244:        char buf[1024];
                    245:        int no_files = 1;
                    246:        int i;
                    247:        int deleting = 0;
                    248:
1.16.2.3  jason     249:        SSLeay_add_all_algorithms();
1.16.2.1  jason     250:
1.12      markus    251:        /* At first, get a connection to the authentication agent. */
                    252:        ac = ssh_get_authentication_connection();
                    253:        if (ac == NULL) {
                    254:                fprintf(stderr, "Could not open a connection to your authentication agent.\n");
                    255:                exit(1);
                    256:        }
                    257:        for (i = 1; i < argc; i++) {
                    258:                if ((strcmp(argv[i], "-l") == 0) ||
                    259:                    (strcmp(argv[i], "-L") == 0)) {
                    260:                        list_identities(ac, argv[i][1] == 'l' ? 1 : 0);
                    261:                        /* Don't default-add/delete if -l. */
                    262:                        no_files = 0;
                    263:                        continue;
                    264:                }
                    265:                if (strcmp(argv[i], "-d") == 0) {
                    266:                        deleting = 1;
                    267:                        continue;
                    268:                }
                    269:                if (strcmp(argv[i], "-D") == 0) {
                    270:                        delete_all(ac);
                    271:                        no_files = 0;
                    272:                        continue;
                    273:                }
                    274:                no_files = 0;
                    275:                if (deleting)
                    276:                        delete_file(ac, argv[i]);
                    277:                else
                    278:                        add_file(ac, argv[i]);
                    279:        }
                    280:        if (no_files) {
                    281:                pw = getpwuid(getuid());
                    282:                if (!pw) {
1.16.2.1  jason     283:                        fprintf(stderr, "No user found with uid %u\n",
                    284:                            (u_int)getuid());
1.12      markus    285:                        ssh_close_authentication_connection(ac);
                    286:                        exit(1);
                    287:                }
1.16.2.3  jason     288:                snprintf(buf, sizeof buf, "%s/%s", pw->pw_dir, _PATH_SSH_CLIENT_IDENTITY);
1.12      markus    289:                if (deleting)
                    290:                        delete_file(ac, buf);
                    291:                else
                    292:                        add_file(ac, buf);
                    293:        }
                    294:        ssh_close_authentication_connection(ac);
                    295:        exit(0);
1.1       deraadt   296: }