Back to index

courier  0.68.2
testgpg.c
Go to the documentation of this file.
00001 /*
00002 ** Copyright 2002-2006 Double Precision, Inc.  See COPYING for
00003 ** distribution information.
00004 */
00005 
00006 
00007 
00008 #include      "config.h"
00009 #include      "gpglib.h"
00010 
00011 #include      "unicode/unicode.h"
00012 #include      "numlib/numlib.h"
00013 
00014 #include      <stdio.h>
00015 #include      <stdlib.h>
00016 #include      <string.h>
00017 
00018 static int dump_stdout(const char *p, size_t l, void *dummy)
00019 {
00020        if (l == 0)
00021               return 0;
00022 
00023        if (fwrite(p, l, 1, stdout) != 1)
00024               exit(1);
00025        fflush(stdout);
00026        return (0);
00027 }
00028 
00029 static int poll_wait(void *dummy)
00030 {
00031        putchar('*');
00032        fflush(stdout);
00033        return (0);
00034 }
00035 
00036 static void genkey(const char *d)
00037 {
00038        libmail_gpg_genkey(d, "iso-8859-1",
00039                  "John Smith",
00040                  "john@example.com",
00041                  "Dummy ISO-8859 Tst key",
00042                  1024,
00043                  2048,
00044                  12,
00045                  'm',
00046                  "",
00047                  dump_stdout,
00048                  poll_wait,
00049                  NULL);
00050 }
00051 
00052 static int show_key(const char *fingerprint, const char *shortname,
00053                   const char *key, int invalid,
00054                   struct gpg_list_info *dummy)
00055 {
00056        printf("Fingerprint: %s\nShort: %s\n%s\n\n",
00057               fingerprint,
00058               shortname,
00059               key);
00060        fflush(stdout);
00061        return (0);
00062 }
00063 
00064 static void genlist(const char *d, int flag)
00065 {
00066        struct gpg_list_info gli;
00067 
00068        memset(&gli, 0, sizeof(gli));
00069 
00070        libmail_gpg_listkeys(d, flag, show_key, dump_stdout, &gli);
00071 }
00072 
00073 static int expkey(const char *d, const char *f, int flag)
00074 {
00075        return (libmail_gpg_exportkey(d, flag, f, dump_stdout, dump_stdout, NULL));
00076 }
00077 
00078 static int delkey(const char *d, const char *f, int flag)
00079 {
00080        return (libmail_gpg_deletekey(d, flag, f, dump_stdout, NULL));
00081 }
00082 
00083 static int signkey(const char *d, const char *signthis, const char *signwith)
00084 {
00085        return (libmail_gpg_signkey(d, signthis, signwith, -1, dump_stdout, 0, NULL));
00086 }
00087 
00088 static int checksign(const char *d, const char *stuff, const char *sig)
00089 {
00090        return (libmail_gpg_checksign(d, stuff, sig, dump_stdout, NULL));
00091 }
00092 
00093 static int import(const char *filename)
00094 {
00095        FILE *f=fopen(filename, "r");
00096        int rc;
00097 
00098        if (!f)
00099        {
00100               perror(filename);
00101               return (1);
00102        }
00103 
00104        if ((rc=libmail_gpg_import_start(NULL, 1)) == 0)
00105        {
00106               char buf[BUFSIZ];
00107               int n;
00108 
00109               while ((n=fread(buf, 1, sizeof(buf), f)) > 0)
00110               {
00111                      if ((rc=libmail_gpg_import_do(buf, n,
00112                                                 dump_stdout, NULL)) != 0)
00113                             return (rc);
00114               }
00115 
00116               rc=libmail_gpg_import_finish(dump_stdout, NULL);
00117        }
00118        return (rc);
00119 }
00120 
00121 int main(int argc, char **argv)
00122 {
00123        if (argc < 3)
00124               return (0);
00125 
00126        if (strcmp(argv[2], "gen") == 0)
00127        {
00128               genkey(argv[1]);
00129               return (0);
00130        }
00131 
00132        if (strcmp(argv[2], "listpub") == 0)
00133        {
00134               genlist(argv[1], 0);
00135               return (0);
00136        }
00137 
00138        if (strcmp(argv[2], "listsec") == 0)
00139        {
00140               genlist(argv[1], 1);
00141               return (0);
00142        }
00143 
00144        if (strcmp(argv[2], "exppub") == 0 && argc >= 4)
00145        {
00146               exit(expkey(argv[1], argv[3], 0));
00147               return (0);
00148        }
00149 
00150        if (strcmp(argv[2], "expsec") == 0 && argc >= 4)
00151        {
00152               exit(expkey(argv[1], argv[3], 1));
00153               return (0);
00154        }
00155 
00156        if (strcmp(argv[2], "delpub") == 0 && argc >= 4)
00157        {
00158               exit (delkey(argv[1], argv[3], 0));
00159               return (0);
00160        }
00161 
00162        if (strcmp(argv[2], "delsec") == 0 && argc >= 4)
00163        {
00164               exit (delkey(argv[1], argv[3], 1));
00165               return (0);
00166        }
00167 
00168        if (strcmp(argv[2], "sign") == 0 && argc >= 5)
00169        {
00170               exit(signkey(argv[1], argv[3], argv[4]));
00171               return (0);
00172        }
00173 
00174        if (strcmp(argv[2], "checksign") == 0 && argc >= 5)
00175        {
00176               exit(checksign(argv[1], argv[3], argv[4]));
00177               return (0);
00178        }
00179 
00180        if (strcmp(argv[2], "import") == 0)
00181               exit(import(argv[1]));
00182 
00183        return (0);
00184 }