Back to index

courier  0.68.2
sign.c
Go to the documentation of this file.
00001 /*
00002 ** Copyright 2001-2003 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      <sys/types.h>
00013 #include      <sys/stat.h>
00014 #include      <sys/time.h>
00015 #if HAVE_FCNTL_H
00016 #include      <fcntl.h>
00017 #endif
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 ** Sign a key.
00030 */
00031 
00032 static int dosignkey(int (*)(const char *, size_t, void *),
00033                    const char *cmdstr,
00034                    void *);
00035 
00036 int libmail_gpg_signkey(const char *gpgdir, const char *signthis, const char *signwith,
00037               int passphrase_fd,
00038               int (*dump_func)(const char *, size_t, void *),
00039               int trust_level,
00040               void *voidarg)
00041 {
00042        char *argvec[12];
00043        int rc;
00044        char passphrase_fd_buf[NUMBUFSIZE];
00045        int i;
00046 
00047        argvec[0]="gpg";
00048        argvec[1]="--command-fd";
00049        argvec[2]="0";
00050        argvec[3]="--default-key";
00051        argvec[4]=(char *)signwith;
00052        argvec[5]="-q";
00053        argvec[6]="--no-tty";
00054 
00055        i=7;
00056        if (passphrase_fd >= 0 && fcntl(passphrase_fd, F_SETFD, 0) >= 0)
00057        {
00058               GPGARGV_PASSPHRASE_FD(argvec, i, passphrase_fd,
00059                                   passphrase_fd_buf);
00060        }
00061 
00062        argvec[i++]="--sign-key";
00063        argvec[i++]=(char *)signthis;
00064        argvec[i]=0;
00065 
00066        if (libmail_gpg_fork(&libmail_gpg_stdin, &libmail_gpg_stdout, NULL, gpgdir, argvec) < 0)
00067               rc= -1;
00068        else
00069        {
00070               int rc2;
00071 
00072               char cmdstr[10];
00073 
00074 #if GPG_HAS_CERT_CHECK_LEVEL
00075 
00076               cmdstr[0]='0';
00077 
00078               if (trust_level > 0 && trust_level <= 9)
00079                      cmdstr[0]='0' + trust_level;
00080 
00081               strcpy(cmdstr+1, "\nY\n");
00082 
00083 #else
00084               strcpy(cmdstr, "Y\n");
00085 #endif
00086 
00087               rc=dosignkey(dump_func, cmdstr, voidarg);
00088               rc2=libmail_gpg_cleanup();
00089               if (rc2)
00090                      rc=rc2;
00091        }
00092        return (rc);
00093 }
00094 
00095 static int dosignkey(int (*dump_func)(const char *, size_t, void *),
00096                    const char *cmdstr,
00097                    void *voidarg)
00098 {
00099        int rc=libmail_gpg_write( cmdstr, strlen(cmdstr),
00100                       dump_func, NULL, NULL, 0, voidarg);
00101        int rc2;
00102 
00103        if (rc == 0)
00104               rc=libmail_gpg_read(dump_func, NULL, NULL, 0, voidarg);
00105        rc2=libmail_gpg_cleanup();
00106        if (rc == 0)
00107               rc=rc2;
00108        return (rc);
00109 }