Back to index

courier  0.68.2
genkey.c
Go to the documentation of this file.
00001 /*
00002 ** Copyright 2001-2011 Double Precision, Inc.  See COPYING for
00003 ** distribution information.
00004 */
00005 
00006 
00007 #include      "config.h"
00008 #include      <stdio.h>
00009 #include      <stdlib.h>
00010 #include      <string.h>
00011 #include      <unistd.h>
00012 #include      <signal.h>
00013 #include      <errno.h>
00014 #include      <sys/types.h>
00015 #include      <sys/stat.h>
00016 #include      <sys/time.h>
00017 
00018 #include      "gpg.h"
00019 #include      "gpglib.h"
00020 
00021 #include      "unicode/unicode.h"
00022 #include      "numlib/numlib.h"
00023 
00024 extern int libmail_gpg_stdin, libmail_gpg_stdout, libmail_gpg_stderr;
00025 extern pid_t libmail_gpg_pid;
00026 
00027 
00028 /*
00029 ** Generate a new key.
00030 */
00031 
00032 static int dogenkey(const char *, const char *, const char *,
00033                   int,
00034                   int,
00035                   unsigned,
00036                   char,
00037                   const char *passphrase,
00038                   int (*)(const char *, size_t, void *),
00039                   int (*)(void *),
00040                   void *);
00041 
00042 int libmail_gpg_genkey(const char *gpgdir,
00043                      const char *charset,
00044                      const char *name,
00045                      const char *addr,
00046                      const char *comment,
00047                      int skeylen,
00048                      int ekeylen,
00049                      unsigned expire,
00050                      char expire_unit,
00051                      const char *passphrase,
00052                      int (*dump_func)(const char *, size_t, void *),
00053                      int (*timeout_func)(void *),
00054                      void *voidarg)
00055 {
00056        char *name_u, *addr_u, *comment_u;
00057        char *argvec[4];
00058        int rc;
00059 
00060        name_u=libmail_u_convert_toutf8(name, charset, NULL);
00061 
00062        if (!name_u)
00063               return (-1);
00064 
00065        addr_u=libmail_u_convert_toutf8(addr, charset, NULL);
00066        if (!addr_u)
00067        {
00068               free(name_u);
00069               return (-1);
00070        }
00071 
00072        comment_u=libmail_u_convert_toutf8(comment, charset, NULL);
00073        if (!comment_u)
00074               return (-1);
00075 
00076 
00077        argvec[0]="gpg";
00078        argvec[1]="--gen-key";
00079        argvec[2]="--batch";
00080        argvec[3]=NULL;
00081 
00082        if (libmail_gpg_fork(&libmail_gpg_stdin, &libmail_gpg_stdout, NULL,
00083                           gpgdir, argvec) < 0)
00084               rc= -1;
00085        else
00086        {
00087               int rc2;
00088 
00089               rc=dogenkey(name_u, addr_u, comment_u,
00090                          skeylen, ekeylen, expire, expire_unit,
00091                          passphrase,
00092                          dump_func, timeout_func, voidarg);
00093               rc2=libmail_gpg_cleanup();
00094               if (rc2)
00095                      rc=rc2;
00096        }
00097        free(comment_u);
00098        free(name_u);
00099        free(addr_u);
00100        return (rc);
00101 }
00102 
00103 static char *mkcmdbuf(const char *, const char *, const char *,
00104                     int,
00105                     int,
00106                     unsigned,
00107                     char, const char *);
00108 
00109 static int dogenkey(const char *name, const char *addr, const char *comment,
00110                   int skeylen,
00111                   int ekeylen,
00112                   unsigned expire,
00113                   char expire_unit,
00114                   const char *passphrase,
00115                   int (*dump_func)(const char *, size_t, void *),
00116                   int (*timeout_func)(void *),
00117                   void *voidarg)
00118 {
00119        char *cmd_buf=mkcmdbuf(name, addr, comment, skeylen, ekeylen,
00120                             expire, expire_unit, passphrase);
00121 
00122        int rc=libmail_gpg_write(cmd_buf, strlen(cmd_buf), dump_func, NULL,
00123                       timeout_func, 5, voidarg);
00124        int rc2;
00125 
00126        free(cmd_buf);
00127        if (rc == 0)
00128               rc=libmail_gpg_read(dump_func, NULL, timeout_func, 5, voidarg);
00129        rc2=libmail_gpg_cleanup();
00130        if (rc == 0)
00131               rc=rc2;
00132        return (rc);
00133 }
00134 
00135 static char *mkcmdbuf(const char *name, const char *addr, const char *comment,
00136                     int skeylen,
00137                     int ekeylen,
00138                     unsigned expire,
00139                     char expire_unit,
00140                     const char *passphrase)
00141 {
00142        static const char genkey_cmd[]=
00143               "Key-Type: DSA\n"
00144               "Key-Length: %s\n"
00145               "Subkey-Type: ELG-E\n"
00146               "Subkey-Length: %s\n"
00147               "%s%s%s"
00148               "%s%s%s"
00149               "%s%s%s"
00150               "Name-Email: %s\n"
00151               "Expire-Date: %s\n"
00152               "%%commit\n";
00153 
00154        static const char namereal1[]="Name-Real: ";
00155        static const char namereal2[]="\n";
00156        static const char comment1[]="Name-Comment: ";
00157        static const char comment2[]="\n";
00158        static const char passphrase1[]="Passphrase: ";
00159        static const char passphrase2[]="\n";
00160 
00161        char skl_buf[NUMBUFSIZE];
00162        char kl_buf[NUMBUFSIZE];
00163        char exp_buf[NUMBUFSIZE+1];
00164 
00165        char *p;
00166 
00167        libmail_str_size_t(skeylen, skl_buf);
00168        libmail_str_size_t(ekeylen, kl_buf);
00169        if (expire == 0)
00170        {
00171               strcpy(exp_buf, "0");
00172        }
00173        else
00174        {
00175               char b[2];
00176 
00177               b[0]=expire_unit;
00178               b[1]=0;
00179 
00180               strcat(libmail_str_size_t(expire, exp_buf), b);
00181        }
00182 
00183        p=malloc(512+strlen(kl_buf)+strlen(skl_buf)+strlen(exp_buf)
00184                +strlen(name)+strlen(addr)+strlen(comment)
00185                +strlen(passphrase));
00186 
00187        if (!p)
00188               return (NULL);
00189 
00190        while (*comment == ' ' || *comment == '\t')
00191               ++comment;
00192 
00193        sprintf(p, genkey_cmd, skl_buf, kl_buf,
00194 
00195               *name ? namereal1:"",
00196               name,
00197               *name ? namereal2:"",
00198 
00199               *comment ? comment1:"",
00200               comment,
00201               *comment ? comment2:"",
00202 
00203               *passphrase ? passphrase1:"",
00204               passphrase,
00205               *passphrase ? passphrase2:"",
00206 
00207               addr, exp_buf);
00208        return (p);
00209 }