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

Annotation of src/usr.bin/ssh/ssh-agent.c, Revision 1.17

1.17    ! markus      1: /*     $OpenBSD: ssh-agent.c,v 1.16 1999/10/28 20:41:23 markus Exp $   */
1.15      markus      2:
1.1       deraadt     3: /*
                      4:
                      5: ssh-agent.c
                      6:
                      7: Author: Tatu Ylonen <ylo@cs.hut.fi>
                      8:
                      9: Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
                     10:                    All rights reserved
                     11:
                     12: Created: Wed Mar 29 03:46:59 1995 ylo
                     13:
                     14: The authentication agent program.
                     15:
                     16: */
                     17:
                     18: #include "includes.h"
1.17    ! markus     19: RCSID("$OpenBSD: ssh-agent.c,v 1.16 1999/10/28 20:41:23 markus Exp $");
1.1       deraadt    20:
                     21: #include "ssh.h"
                     22: #include "rsa.h"
                     23: #include "authfd.h"
                     24: #include "buffer.h"
                     25: #include "bufaux.h"
                     26: #include "xmalloc.h"
                     27: #include "packet.h"
                     28: #include "getput.h"
                     29: #include "mpaux.h"
                     30:
1.13      deraadt    31: #include <ssl/md5.h>
1.7       deraadt    32:
1.1       deraadt    33: typedef struct
                     34: {
                     35:   int fd;
1.12      markus     36:   enum { AUTH_UNUSED, AUTH_SOCKET, AUTH_CONNECTION } type;
1.1       deraadt    37:   Buffer input;
                     38:   Buffer output;
                     39: } SocketEntry;
                     40:
                     41: unsigned int sockets_alloc = 0;
                     42: SocketEntry *sockets = NULL;
                     43:
                     44: typedef struct
                     45: {
1.2       provos     46:   RSA *key;
1.1       deraadt    47:   char *comment;
                     48: } Identity;
                     49:
                     50: unsigned int num_identities = 0;
                     51: Identity *identities = NULL;
                     52:
                     53: int max_fd = 0;
                     54:
1.11      markus     55: /* pid of shell == parent of agent */
1.10      markus     56: int parent_pid = -1;
                     57:
                     58: /* pathname and directory for AUTH_SOCKET */
                     59: char socket_name[1024];
                     60: char socket_dir[1024];
                     61:
1.2       provos     62: void
                     63: process_request_identity(SocketEntry *e)
1.1       deraadt    64: {
                     65:   Buffer msg;
                     66:   int i;
                     67:
                     68:   buffer_init(&msg);
                     69:   buffer_put_char(&msg, SSH_AGENT_RSA_IDENTITIES_ANSWER);
                     70:   buffer_put_int(&msg, num_identities);
                     71:   for (i = 0; i < num_identities; i++)
                     72:     {
1.2       provos     73:       buffer_put_int(&msg, BN_num_bits(identities[i].key->n));
                     74:       buffer_put_bignum(&msg, identities[i].key->e);
                     75:       buffer_put_bignum(&msg, identities[i].key->n);
1.1       deraadt    76:       buffer_put_string(&msg, identities[i].comment,
                     77:                        strlen(identities[i].comment));
                     78:     }
                     79:   buffer_put_int(&e->output, buffer_len(&msg));
                     80:   buffer_append(&e->output, buffer_ptr(&msg), buffer_len(&msg));
                     81:   buffer_free(&msg);
                     82: }
                     83:
1.2       provos     84: void
                     85: process_authentication_challenge(SocketEntry *e)
1.1       deraadt    86: {
1.2       provos     87:   int i, pub_bits, len;
                     88:   BIGNUM *pub_e, *pub_n, *challenge;
1.1       deraadt    89:   Buffer msg;
1.7       deraadt    90:   MD5_CTX md;
1.1       deraadt    91:   unsigned char buf[32], mdbuf[16], session_id[16];
                     92:   unsigned int response_type;
                     93:
                     94:   buffer_init(&msg);
1.2       provos     95:   pub_e = BN_new();
                     96:   pub_n = BN_new();
                     97:   challenge = BN_new();
1.1       deraadt    98:   pub_bits = buffer_get_int(&e->input);
1.2       provos     99:   buffer_get_bignum(&e->input, pub_e);
                    100:   buffer_get_bignum(&e->input, pub_n);
                    101:   buffer_get_bignum(&e->input, challenge);
1.1       deraadt   102:   if (buffer_len(&e->input) == 0)
                    103:     {
                    104:       /* Compatibility code for old servers. */
                    105:       memset(session_id, 0, 16);
                    106:       response_type = 0;
                    107:     }
                    108:   else
                    109:     {
                    110:       /* New code. */
                    111:       buffer_get(&e->input, (char *)session_id, 16);
                    112:       response_type = buffer_get_int(&e->input);
                    113:     }
                    114:   for (i = 0; i < num_identities; i++)
1.2       provos    115:     if (pub_bits == BN_num_bits(identities[i].key->n) &&
                    116:        BN_cmp(pub_e, identities[i].key->e) == 0 &&
                    117:        BN_cmp(pub_n, identities[i].key->n) == 0)
1.1       deraadt   118:       {
                    119:        /* Decrypt the challenge using the private key. */
1.2       provos    120:        rsa_private_decrypt(challenge, challenge, identities[i].key);
1.1       deraadt   121:
                    122:        /* Compute the desired response. */
                    123:        switch (response_type)
                    124:          {
                    125:          case 0: /* As of protocol 1.0 */
                    126:            /* This response type is no longer supported. */
                    127:            log("Compatibility with ssh protocol 1.0 no longer supported.");
                    128:            buffer_put_char(&msg, SSH_AGENT_FAILURE);
                    129:            goto send;
                    130:
                    131:          case 1: /* As of protocol 1.1 */
                    132:            /* The response is MD5 of decrypted challenge plus session id. */
1.2       provos    133:            len = BN_num_bytes(challenge);
1.17    ! markus    134:
        !           135:            if (len <= 0 || len > 32) {
        !           136:              fatal("process_authentication_challenge: "
        !           137:                    "bad challenge length %d", len);
        !           138:            }
        !           139:
1.2       provos    140:            memset(buf, 0, 32);
                    141:            BN_bn2bin(challenge, buf + 32 - len);
1.13      deraadt   142:            MD5_Init(&md);
                    143:            MD5_Update(&md, buf, 32);
                    144:            MD5_Update(&md, session_id, 16);
                    145:            MD5_Final(mdbuf, &md);
1.1       deraadt   146:            break;
                    147:
                    148:          default:
                    149:            fatal("process_authentication_challenge: bad response_type %d",
                    150:                  response_type);
                    151:            break;
                    152:          }
                    153:
                    154:        /* Send the response. */
                    155:        buffer_put_char(&msg, SSH_AGENT_RSA_RESPONSE);
                    156:        for (i = 0; i < 16; i++)
                    157:          buffer_put_char(&msg, mdbuf[i]);
                    158:
                    159:        goto send;
                    160:       }
                    161:   /* Unknown identity.  Send failure. */
                    162:   buffer_put_char(&msg, SSH_AGENT_FAILURE);
                    163:  send:
                    164:   buffer_put_int(&e->output, buffer_len(&msg));
                    165:   buffer_append(&e->output, buffer_ptr(&msg),
                    166:                buffer_len(&msg));
                    167:   buffer_free(&msg);
1.2       provos    168:   BN_clear_free(pub_e);
                    169:   BN_clear_free(pub_n);
                    170:   BN_clear_free(challenge);
1.1       deraadt   171: }
                    172:
1.2       provos    173: void
                    174: process_remove_identity(SocketEntry *e)
1.1       deraadt   175: {
                    176:   unsigned int bits;
                    177:   unsigned int i;
1.2       provos    178:   BIGNUM *dummy, *n;
1.1       deraadt   179:
1.2       provos    180:   dummy = BN_new();
                    181:   n = BN_new();
1.1       deraadt   182:
                    183:   /* Get the key from the packet. */
                    184:   bits = buffer_get_int(&e->input);
1.2       provos    185:   buffer_get_bignum(&e->input, dummy);
                    186:   buffer_get_bignum(&e->input, n);
1.1       deraadt   187:
                    188:   /* Check if we have the key. */
                    189:   for (i = 0; i < num_identities; i++)
1.2       provos    190:     if (BN_cmp(identities[i].key->n, n) == 0)
1.1       deraadt   191:       {
                    192:        /* We have this key.  Free the old key.  Since we don\'t want to leave
                    193:           empty slots in the middle of the array, we actually free the
                    194:           key there and copy data from the last entry. */
1.2       provos    195:        RSA_free(identities[i].key);
1.1       deraadt   196:        xfree(identities[i].comment);
                    197:        if (i < num_identities - 1)
                    198:          identities[i] = identities[num_identities - 1];
                    199:        num_identities--;
1.2       provos    200:        BN_clear_free(dummy);
                    201:        BN_clear_free(n);
1.1       deraadt   202:
                    203:        /* Send success. */
                    204:        buffer_put_int(&e->output, 1);
                    205:        buffer_put_char(&e->output, SSH_AGENT_SUCCESS);
                    206:        return;
                    207:       }
                    208:   /* We did not have the key. */
1.2       provos    209:   BN_clear(dummy);
                    210:   BN_clear(n);
1.1       deraadt   211:
                    212:   /* Send failure. */
                    213:   buffer_put_int(&e->output, 1);
                    214:   buffer_put_char(&e->output, SSH_AGENT_FAILURE);
                    215: }
                    216:
                    217: /* Removes all identities from the agent. */
                    218:
1.2       provos    219: void
                    220: process_remove_all_identities(SocketEntry *e)
1.1       deraadt   221: {
                    222:   unsigned int i;
                    223:
                    224:   /* Loop over all identities and clear the keys. */
                    225:   for (i = 0; i < num_identities; i++)
                    226:     {
1.2       provos    227:       RSA_free(identities[i].key);
1.1       deraadt   228:       xfree(identities[i].comment);
                    229:     }
                    230:
                    231:   /* Mark that there are no identities. */
                    232:   num_identities = 0;
                    233:
                    234:   /* Send success. */
                    235:   buffer_put_int(&e->output, 1);
                    236:   buffer_put_char(&e->output, SSH_AGENT_SUCCESS);
                    237:   return;
                    238: }
                    239:
                    240: /* Adds an identity to the agent. */
                    241:
1.2       provos    242: void
                    243: process_add_identity(SocketEntry *e)
1.1       deraadt   244: {
1.2       provos    245:   RSA *k;
1.1       deraadt   246:   int i;
1.2       provos    247:   BIGNUM *aux;
                    248:   BN_CTX *ctx;
                    249:
1.1       deraadt   250:   if (num_identities == 0)
                    251:     identities = xmalloc(sizeof(Identity));
                    252:   else
                    253:     identities = xrealloc(identities, (num_identities + 1) * sizeof(Identity));
1.2       provos    254:
                    255:   identities[num_identities].key = RSA_new();
                    256:   k = identities[num_identities].key;
                    257:   buffer_get_int(&e->input); /* bits */
                    258:   k->n = BN_new();
                    259:   buffer_get_bignum(&e->input, k->n);
                    260:   k->e = BN_new();
                    261:   buffer_get_bignum(&e->input, k->e);
                    262:   k->d = BN_new();
                    263:   buffer_get_bignum(&e->input, k->d);
                    264:   k->iqmp = BN_new();
                    265:   buffer_get_bignum(&e->input, k->iqmp);
                    266:   /* SSH and SSL have p and q swapped */
                    267:   k->q = BN_new();
                    268:   buffer_get_bignum(&e->input, k->q); /* p */
                    269:   k->p = BN_new();
                    270:   buffer_get_bignum(&e->input, k->p); /* q */
                    271:
                    272:   /* Generate additional parameters */
                    273:   aux = BN_new();
                    274:   ctx = BN_CTX_new();
                    275:
                    276:   BN_sub(aux, k->q, BN_value_one());
                    277:   k->dmq1 = BN_new();
                    278:   BN_mod(k->dmq1, k->d, aux, ctx);
                    279:
                    280:   BN_sub(aux, k->p, BN_value_one());
                    281:   k->dmp1 = BN_new();
                    282:   BN_mod(k->dmp1, k->d, aux, ctx);
                    283:
                    284:   BN_clear_free(aux);
                    285:   BN_CTX_free(ctx);
                    286:
1.1       deraadt   287:   identities[num_identities].comment = buffer_get_string(&e->input, NULL);
                    288:
                    289:   /* Check if we already have the key. */
                    290:   for (i = 0; i < num_identities; i++)
1.2       provos    291:     if (BN_cmp(identities[i].key->n, k->n) == 0)
1.1       deraadt   292:       {
                    293:        /* We already have this key.  Clear and free the new data and
                    294:           return success. */
1.2       provos    295:        RSA_free(k);
1.1       deraadt   296:        xfree(identities[num_identities].comment);
                    297:
                    298:        /* Send success. */
                    299:        buffer_put_int(&e->output, 1);
                    300:        buffer_put_char(&e->output, SSH_AGENT_SUCCESS);
                    301:        return;
                    302:       }
                    303:
                    304:   /* Increment the number of identities. */
                    305:   num_identities++;
                    306:
                    307:   /* Send a success message. */
                    308:   buffer_put_int(&e->output, 1);
                    309:   buffer_put_char(&e->output, SSH_AGENT_SUCCESS);
                    310: }
                    311:
1.2       provos    312: void
                    313: process_message(SocketEntry *e)
1.1       deraadt   314: {
                    315:   unsigned int msg_len;
                    316:   unsigned int type;
                    317:   unsigned char *cp;
                    318:   if (buffer_len(&e->input) < 5)
                    319:     return; /* Incomplete message. */
                    320:   cp = (unsigned char *)buffer_ptr(&e->input);
                    321:   msg_len = GET_32BIT(cp);
                    322:   if (msg_len > 256 * 1024)
                    323:     {
1.8       deraadt   324:       shutdown(e->fd, SHUT_RDWR);
1.1       deraadt   325:       close(e->fd);
                    326:       e->type = AUTH_UNUSED;
                    327:       return;
                    328:     }
                    329:   if (buffer_len(&e->input) < msg_len + 4)
                    330:     return;
                    331:   buffer_consume(&e->input, 4);
                    332:   type = buffer_get_char(&e->input);
1.12      markus    333:
1.1       deraadt   334:   switch (type)
                    335:     {
                    336:     case SSH_AGENTC_REQUEST_RSA_IDENTITIES:
                    337:       process_request_identity(e);
                    338:       break;
                    339:     case SSH_AGENTC_RSA_CHALLENGE:
                    340:       process_authentication_challenge(e);
                    341:       break;
                    342:     case SSH_AGENTC_ADD_RSA_IDENTITY:
                    343:       process_add_identity(e);
                    344:       break;
                    345:     case SSH_AGENTC_REMOVE_RSA_IDENTITY:
                    346:       process_remove_identity(e);
                    347:       break;
                    348:     case SSH_AGENTC_REMOVE_ALL_RSA_IDENTITIES:
                    349:       process_remove_all_identities(e);
                    350:       break;
                    351:     default:
                    352:       /* Unknown message.  Respond with failure. */
                    353:       error("Unknown message %d", type);
                    354:       buffer_clear(&e->input);
                    355:       buffer_put_int(&e->output, 1);
                    356:       buffer_put_char(&e->output, SSH_AGENT_FAILURE);
                    357:       break;
                    358:     }
                    359: }
                    360:
1.2       provos    361: void
                    362: new_socket(int type, int fd)
1.1       deraadt   363: {
                    364:   unsigned int i, old_alloc;
                    365:   if (fcntl(fd, F_SETFL, O_NONBLOCK) < 0)
                    366:     error("fcntl O_NONBLOCK: %s", strerror(errno));
                    367:
                    368:   if (fd > max_fd)
                    369:     max_fd = fd;
                    370:
                    371:   for (i = 0; i < sockets_alloc; i++)
                    372:     if (sockets[i].type == AUTH_UNUSED)
                    373:       {
                    374:        sockets[i].fd = fd;
                    375:        sockets[i].type = type;
                    376:        buffer_init(&sockets[i].input);
                    377:        buffer_init(&sockets[i].output);
                    378:        return;
                    379:       }
                    380:   old_alloc = sockets_alloc;
                    381:   sockets_alloc += 10;
                    382:   if (sockets)
                    383:     sockets = xrealloc(sockets, sockets_alloc * sizeof(sockets[0]));
                    384:   else
                    385:     sockets = xmalloc(sockets_alloc * sizeof(sockets[0]));
                    386:   for (i = old_alloc; i < sockets_alloc; i++)
                    387:     sockets[i].type = AUTH_UNUSED;
                    388:   sockets[old_alloc].type = type;
                    389:   sockets[old_alloc].fd = fd;
                    390:   buffer_init(&sockets[old_alloc].input);
                    391:   buffer_init(&sockets[old_alloc].output);
                    392: }
                    393:
1.2       provos    394: void
                    395: prepare_select(fd_set *readset, fd_set *writeset)
1.1       deraadt   396: {
                    397:   unsigned int i;
                    398:   for (i = 0; i < sockets_alloc; i++)
                    399:     switch (sockets[i].type)
                    400:       {
1.12      markus    401:       case AUTH_SOCKET:
1.1       deraadt   402:       case AUTH_CONNECTION:
                    403:        FD_SET(sockets[i].fd, readset);
                    404:        if (buffer_len(&sockets[i].output) > 0)
                    405:          FD_SET(sockets[i].fd, writeset);
                    406:        break;
                    407:       case AUTH_UNUSED:
                    408:        break;
                    409:       default:
                    410:        fatal("Unknown socket type %d", sockets[i].type);
                    411:        break;
                    412:       }
                    413: }
                    414:
                    415: void after_select(fd_set *readset, fd_set *writeset)
                    416: {
                    417:   unsigned int i;
1.12      markus    418:   int len, sock;
1.1       deraadt   419:   char buf[1024];
                    420:   struct sockaddr_un sunaddr;
                    421:
                    422:   for (i = 0; i < sockets_alloc; i++)
                    423:     switch (sockets[i].type)
                    424:       {
                    425:       case AUTH_UNUSED:
                    426:        break;
                    427:       case AUTH_SOCKET:
                    428:        if (FD_ISSET(sockets[i].fd, readset))
                    429:          {
                    430:            len = sizeof(sunaddr);
                    431:            sock = accept(sockets[i].fd, (struct sockaddr *)&sunaddr, &len);
                    432:            if (sock < 0)
                    433:              {
                    434:                perror("accept from AUTH_SOCKET");
                    435:                break;
                    436:              }
1.12      markus    437:            new_socket(AUTH_CONNECTION, sock);
1.1       deraadt   438:          }
                    439:        break;
                    440:       case AUTH_CONNECTION:
                    441:        if (buffer_len(&sockets[i].output) > 0 &&
                    442:            FD_ISSET(sockets[i].fd, writeset))
                    443:          {
                    444:            len = write(sockets[i].fd, buffer_ptr(&sockets[i].output),
                    445:                        buffer_len(&sockets[i].output));
                    446:            if (len <= 0)
                    447:              {
1.8       deraadt   448:                shutdown(sockets[i].fd, SHUT_RDWR);
1.1       deraadt   449:                close(sockets[i].fd);
                    450:                sockets[i].type = AUTH_UNUSED;
                    451:                break;
                    452:              }
                    453:            buffer_consume(&sockets[i].output, len);
                    454:          }
                    455:        if (FD_ISSET(sockets[i].fd, readset))
                    456:          {
                    457:            len = read(sockets[i].fd, buf, sizeof(buf));
                    458:            if (len <= 0)
                    459:              {
1.8       deraadt   460:                shutdown(sockets[i].fd, SHUT_RDWR);
1.1       deraadt   461:                close(sockets[i].fd);
                    462:                sockets[i].type = AUTH_UNUSED;
                    463:                break;
                    464:              }
                    465:            buffer_append(&sockets[i].input, buf, len);
                    466:            process_message(&sockets[i]);
                    467:          }
                    468:        break;
                    469:       default:
                    470:        fatal("Unknown type %d", sockets[i].type);
                    471:       }
                    472: }
                    473:
1.6       deraadt   474: void
1.2       provos    475: check_parent_exists(int sig)
1.1       deraadt   476: {
                    477:   if (kill(parent_pid, 0) < 0)
                    478:     {
                    479:       /* printf("Parent has died - Authentication agent exiting.\n"); */
                    480:       exit(1);
                    481:     }
                    482:   signal(SIGALRM, check_parent_exists);
                    483:   alarm(10);
                    484: }
                    485:
1.15      markus    486: void
                    487: cleanup_socket(void)
                    488: {
1.10      markus    489:   remove(socket_name);
                    490:   rmdir(socket_dir);
                    491: }
                    492:
1.15      markus    493: void
                    494: cleanup_exit(int i)
                    495: {
                    496:   cleanup_socket();
                    497:   exit(i);
                    498: }
                    499:
                    500: void
                    501: usage()
                    502: {
                    503:   extern char *__progname;
                    504:
                    505:   fprintf(stderr, "ssh-agent version %s\n", SSH_VERSION);
                    506:   fprintf(stderr, "Usage: %s [-c | -s] [-k] [command {args...]]\n",
                    507:          __progname);
                    508:   exit(1);
                    509: }
                    510:
1.2       provos    511: int
                    512: main(int ac, char **av)
1.1       deraadt   513: {
                    514:   fd_set readset, writeset;
1.15      markus    515:   int sock, c_flag = 0, k_flag = 0, s_flag = 0, ch;
1.1       deraadt   516:   struct sockaddr_un sunaddr;
1.15      markus    517:   pid_t pid;
                    518:   char *shell, *format, *pidstr, pidstrbuf[1 + 3 * sizeof pid];
1.1       deraadt   519:
1.3       deraadt   520:   /* check if RSA support exists */
                    521:   if (rsa_alive() == 0) {
                    522:     extern char *__progname;
                    523:     fprintf(stderr,
                    524:       "%s: no RSA support in libssl and libcrypto.  See ssl(8).\n",
                    525:       __progname);
                    526:     exit(1);
                    527:   }
1.1       deraadt   528:
1.15      markus    529:   while ((ch = getopt(ac, av, "cks")) != -1)
                    530:     {
                    531:       switch (ch)
                    532:        {
                    533:        case 'c':
                    534:          if (s_flag)
                    535:            usage();
                    536:          c_flag++;
                    537:          break;
                    538:        case 'k':
                    539:          k_flag++;
                    540:          break;
                    541:        case 's':
                    542:          if (c_flag)
                    543:            usage();
                    544:          s_flag++;
                    545:          break;
                    546:        default:
                    547:          usage();
                    548:        }
                    549:     }
                    550:   ac -= optind;
                    551:   av += optind;
                    552:
                    553:   if (ac > 0 && (c_flag || k_flag || s_flag))
                    554:     usage();
                    555:
                    556:   if (ac == 0 && !c_flag && !k_flag && !s_flag)
                    557:     {
                    558:       shell = getenv("SHELL");
                    559:       if (shell != NULL && strncmp(shell + strlen(shell) - 3, "csh", 3) == 0)
                    560:        c_flag = 1;
                    561:     }
                    562:
                    563:   if (k_flag)
1.1       deraadt   564:     {
1.15      markus    565:       pidstr = getenv(SSH_AGENTPID_ENV_NAME);
                    566:       if (pidstr == NULL)
                    567:        {
                    568:          fprintf(stderr, "%s not set, cannot kill agent\n",
                    569:                  SSH_AGENTPID_ENV_NAME);
                    570:          exit(1);
                    571:        }
                    572:       pid = atoi(pidstr);
                    573:       if (pid < 1)             /* XXX PID_MAX check too */
                    574:        {
                    575:          fprintf(stderr, "%s=\"%s\", which is not a good PID\n",
                    576:                  SSH_AGENTPID_ENV_NAME, pidstr);
                    577:          exit(1);
                    578:        }
                    579:       if (kill(pid, SIGTERM) == -1)
                    580:        {
                    581:          perror("kill");
                    582:          exit(1);
                    583:        }
                    584:       format = c_flag ? "unsetenv %s;\n" : "unset %s;\n";
                    585:       printf(format, SSH_AUTHSOCKET_ENV_NAME);
                    586:       printf(format, SSH_AGENTPID_ENV_NAME);
                    587:       printf("echo Agent pid %d killed;\n", pid);
                    588:       exit(0);
1.1       deraadt   589:     }
                    590:
1.9       markus    591:   parent_pid = getpid();
1.10      markus    592:
                    593:   /* Create private directory for agent socket */
                    594:   strlcpy(socket_dir, "/tmp/ssh-XXXXXXXX", sizeof socket_dir);
                    595:   if (mkdtemp(socket_dir) == NULL) {
                    596:       perror("mkdtemp: private socket dir");
                    597:       exit(1);
                    598:   }
1.15      markus    599:   snprintf(socket_name, sizeof socket_name, "%s/agent.%d", socket_dir,
                    600:           parent_pid);
1.10      markus    601:
1.15      markus    602:   /* Create socket early so it will exist before command gets run from
                    603:      the parent.  */
1.9       markus    604:   sock = socket(AF_UNIX, SOCK_STREAM, 0);
                    605:   if (sock < 0)
1.1       deraadt   606:     {
1.9       markus    607:       perror("socket");
1.15      markus    608:       cleanup_exit(1);
1.9       markus    609:     }
                    610:   memset(&sunaddr, 0, sizeof(sunaddr));
                    611:   sunaddr.sun_family = AF_UNIX;
                    612:   strlcpy(sunaddr.sun_path, socket_name, sizeof(sunaddr.sun_path));
                    613:   if (bind(sock, (struct sockaddr *)&sunaddr, sizeof(sunaddr)) < 0)
                    614:     {
                    615:       perror("bind");
1.15      markus    616:       cleanup_exit(1);
1.9       markus    617:     }
                    618:   if (listen(sock, 5) < 0)
                    619:     {
                    620:       perror("listen");
1.15      markus    621:       cleanup_exit(1);
                    622:     }
                    623:
                    624:   /* Fork, and have the parent execute the command, if any, or present the
                    625:      socket data.  The child continues as the authentication agent. */
                    626:   pid = fork();
                    627:   if (pid == -1)
                    628:     {
                    629:       perror("fork");
                    630:       exit(1);
                    631:     }
                    632:   if (pid != 0)
                    633:     { /* Parent - execute the given command. */
                    634:       close(sock);
                    635:       snprintf(pidstrbuf, sizeof pidstrbuf, "%d", pid);
                    636:       if (ac == 0)
                    637:        {
                    638:          format = c_flag ? "setenv %s %s;\n" : "%s=%s; export %s;\n";
                    639:          printf(format, SSH_AUTHSOCKET_ENV_NAME, socket_name,
                    640:                 SSH_AUTHSOCKET_ENV_NAME);
                    641:          printf(format, SSH_AGENTPID_ENV_NAME, pidstrbuf,
                    642:                 SSH_AGENTPID_ENV_NAME);
                    643:          printf("echo Agent pid %d;\n", pid);
                    644:          exit(0);
                    645:        }
                    646:
                    647:       setenv(SSH_AUTHSOCKET_ENV_NAME, socket_name, 1);
                    648:       setenv(SSH_AGENTPID_ENV_NAME, pidstrbuf, 1);
                    649:       execvp(av[0], av);
                    650:       perror(av[0]);
1.9       markus    651:       exit(1);
1.1       deraadt   652:     }
1.15      markus    653:
                    654:   close(0);
                    655:   close(1);
                    656:   close(2);
                    657:
1.16      markus    658:   if (setsid() == -1)
                    659:     {
                    660:       perror("setsid");
                    661:       cleanup_exit(1);
                    662:     }
1.15      markus    663:
                    664:   if (atexit(cleanup_socket) < 0)
1.16      markus    665:     {
                    666:       perror("atexit");
                    667:       cleanup_exit(1);
                    668:     }
1.15      markus    669:
1.9       markus    670:   new_socket(AUTH_SOCKET, sock);
1.15      markus    671:   if (ac > 0)
                    672:     {
                    673:       signal(SIGALRM, check_parent_exists);
                    674:       alarm(10);
                    675:     }
1.1       deraadt   676:
                    677:   signal(SIGINT, SIG_IGN);
1.15      markus    678:   signal(SIGPIPE, SIG_IGN);
1.1       deraadt   679:   while (1)
                    680:     {
                    681:       FD_ZERO(&readset);
                    682:       FD_ZERO(&writeset);
                    683:       prepare_select(&readset, &writeset);
                    684:       if (select(max_fd + 1, &readset, &writeset, NULL, NULL) < 0)
                    685:        {
                    686:          if (errno == EINTR)
                    687:            continue;
                    688:          exit(1);
                    689:        }
                    690:       after_select(&readset, &writeset);
                    691:     }
                    692:   /*NOTREACHED*/
                    693: }