]> andersk Git - openssh.git/blobdiff - ssh-add.c
- jakob@cvs.openbsd.org 2001/08/02 16:14:05
[openssh.git] / ssh-add.c
index cf181ca467826ab031fa6e1ebef8af8b16fdcc48..b44c306b275a4dc7a06a2aff9b18248856452a34 100644 (file)
--- a/ssh-add.c
+++ b/ssh-add.c
@@ -11,7 +11,7 @@
  * called by a name other than "ssh" or "Secure Shell".
  *
  * SSH2 implementation,
- * Copyright (c) 2000 Markus Friedl.  All rights reserved.
+ * Copyright (c) 2000, 2001 Markus Friedl.  All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions
@@ -35,7 +35,7 @@
  */
 
 #include "includes.h"
-RCSID("$OpenBSD: ssh-add.c,v 1.37 2001/05/02 16:41:20 markus Exp $");
+RCSID("$OpenBSD: ssh-add.c,v 1.44 2001/08/01 22:03:33 markus Exp $");
 
 #include <openssl/evp.h>
 
@@ -57,7 +57,7 @@ char *__progname;
 
 /* we keep a cache of one passphrases */
 static char *pass = NULL;
-void
+static void
 clear_pass(void)
 {
        if (pass) {
@@ -67,7 +67,7 @@ clear_pass(void)
        }
 }
 
-void
+static void
 delete_file(AuthenticationConnection *ac, const char *filename)
 {
        Key *public;
@@ -87,7 +87,7 @@ delete_file(AuthenticationConnection *ac, const char *filename)
 }
 
 /* Send a request to remove all identities. */
-void
+static void
 delete_all(AuthenticationConnection *ac)
 {
        int success = 1;
@@ -103,7 +103,7 @@ delete_all(AuthenticationConnection *ac)
                fprintf(stderr, "Failed to remove all identities.\n");
 }
 
-void
+static void
 add_file(AuthenticationConnection *ac, const char *filename)
 {
        struct stat st;
@@ -125,11 +125,10 @@ add_file(AuthenticationConnection *ac, const char *filename)
        if (private == NULL) {
                /* clear passphrase since it did not work */
                clear_pass();
-               printf("Need passphrase for %.200s\n", filename);
                snprintf(msg, sizeof msg, "Enter passphrase for %.200s: ",
                   comment);
                for (;;) {
-                       pass = read_passphrase(msg, 1);
+                       pass = read_passphrase(msg, RP_ALLOW_STDIN);
                        if (strcmp(pass, "") == 0) {
                                clear_pass();
                                xfree(comment);
@@ -150,7 +149,18 @@ add_file(AuthenticationConnection *ac, const char *filename)
        key_free(private);
 }
 
-void
+static void
+update_card(AuthenticationConnection *ac, int add, const char *id)
+{
+       if (ssh_update_card(ac, add, id))
+               fprintf(stderr, "Card %s: %s\n",
+                    add ? "added" : "removed", id);
+       else
+               fprintf(stderr, "Could not %s card: %s\n",
+                    add ? "add" : "remove", id);
+}
+
+static void
 list_identities(AuthenticationConnection *ac, int do_fp)
 {
        Key *key;
@@ -182,15 +192,27 @@ list_identities(AuthenticationConnection *ac, int do_fp)
                printf("The agent has no identities.\n");
 }
 
+static void
+usage(void)
+{
+       printf("Usage: ssh-add [options]\n");
+       printf("    -l, -L        : list identities\n");
+       printf("    -d            : delete identity\n");
+       printf("    -D            : delete all identities\n");
+       printf("    -s reader_num : add key in the smartcard in reader_num.\n");
+       printf("    -e reader_num : remove key in the smartcard in reader_num.\n");
+}
+
 int
 main(int argc, char **argv)
 {
+       extern char *optarg;
+       extern int optind;
        AuthenticationConnection *ac = NULL;
        struct passwd *pw;
        char buf[1024];
-       int no_files = 1;
-       int i;
-       int deleting = 0;
+       char *sc_reader_id = NULL;
+       int i, ch, deleting = 0;
 
        __progname = get_progname(argv[0]);
        init_rng();
@@ -204,30 +226,40 @@ main(int argc, char **argv)
                fprintf(stderr, "Could not open a connection to your authentication agent.\n");
                exit(1);
        }
-       for (i = 1; i < argc; i++) {
-               if ((strcmp(argv[i], "-l") == 0) ||
-                   (strcmp(argv[i], "-L") == 0)) {
-                       list_identities(ac, argv[i][1] == 'l' ? 1 : 0);
-                       /* Don't default-add/delete if -l. */
-                       no_files = 0;
-                       continue;
-               }
-               if (strcmp(argv[i], "-d") == 0) {
+        while ((ch = getopt(argc, argv, "lLdDe:s:")) != -1) {
+               switch (ch) {
+               case 'l':
+               case 'L':
+                       list_identities(ac, ch == 'l' ? 1 : 0);
+                       goto done;
+                       break;
+               case 'd':
                        deleting = 1;
-                       continue;
-               }
-               if (strcmp(argv[i], "-D") == 0) {
+                       break;
+               case 'D':
                        delete_all(ac);
-                       no_files = 0;
-                       continue;
+                       goto done;
+                       break;
+               case 's':
+                       sc_reader_id = optarg;
+                       break;
+               case 'e':
+                       deleting = 1; 
+                       sc_reader_id = optarg;
+                       break;
+               default:
+                       usage();
+                       exit(1);
+                       break;
                }
-               no_files = 0;
-               if (deleting)
-                       delete_file(ac, argv[i]);
-               else
-                       add_file(ac, argv[i]);
        }
-       if (no_files) {
+       argc -= optind;
+       argv += optind;
+       if (sc_reader_id != NULL) {
+               update_card(ac, !deleting, sc_reader_id);
+               goto done;
+       }
+       if (argc == 0) {
                pw = getpwuid(getuid());
                if (!pw) {
                        fprintf(stderr, "No user found with uid %u\n",
@@ -240,8 +272,17 @@ main(int argc, char **argv)
                        delete_file(ac, buf);
                else
                        add_file(ac, buf);
+       } else {
+               for (i = 0; i < argc; i++) {
+                       if (deleting)
+                               delete_file(ac, argv[i]);
+                       else
+                               add_file(ac, argv[i]);
+               }
        }
        clear_pass();
+
+done:
        ssh_close_authentication_connection(ac);
        exit(0);
 }
This page took 0.469132 seconds and 4 git commands to generate.