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

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