Back to index

courier  0.68.2
Classes | Defines | Functions | Variables
gpg.c File Reference
#include "sqwebmail.h"
#include "config.h"
#include "gpg.h"
#include "pref.h"
#include "cgi/cgi.h"
#include "gpglib/gpglib.h"
#include "unicode/unicode.h"
#include "numlib/numlib.h"
#include "rfc822/rfc822.h"
#include "htmllibdir.h"
#include <stdio.h>
#include <string.h>
#include <errno.h>

Go to the source code of this file.

Classes

struct  listinfo

Defines

#define WEXITSTATUS(stat_val)   ((unsigned)(stat_val) >> 8)
#define WIFEXITED(stat_val)   (((stat_val) & 255) == 0)

Functions

void output_scriptptrget ()
void print_attrencodedlen (const char *, size_t, int, FILE *)
void print_safe (const char *)
static void gpginiterr ()
static int gpg_error (const char *p, size_t l, void *dummy)
static void gpg_error_save (const char *errmsg, void *dummy)
int gpgbadarg (const char *p)
static void dump_error ()
static int show_key (const char *fingerprint, const char *shortname, const char *key, int invalid, struct gpg_list_info *gli)
static void listpubsec (int flag, int(*callback_func)(const char *, const char *, const char *, int, struct gpg_list_info *), const char *default_key)
void gpglistpub ()
void gpglistsec ()
static int select_key (const char *fingerprint, const char *shortname, const char *key, struct gpg_list_info *gli, int is_select)
static int select_key_default (const char *fingerprint, const char *shortname, const char *key, int invalid, struct gpg_list_info *gli)
void gpgselectkey ()
void gpgselectpubkey ()
void gpgselectprivkey ()
static int knownkey (const char *shortname, const char *known_keys)
static int encrypt_key_default (const char *fingerprint, const char *shortname, const char *key, int invalid, struct gpg_list_info *gli)
void gpgencryptkeys (const char *select_keys)
static int dump_func (const char *p, size_t l, void *vp)
static int timeout_func (void *vp)
void gpgcreate ()
static void delkey (const char *keyname, int flag)
static FILE * passphrasefp ()
static void signkey (const char *signthis, const char *signwith, const char *trustlevel)
static void setdefault (const char *def)
void gpgdo ()
static int read_fd (char *buf, size_t cnt, void *vp)
static void write_fd (const char *p, size_t n, void *dummy)
int gpgdomsg (int in_fd, int out_fd, const char *signkey, const char *encryptkeys)
void sent_gpgerrtxt ()
void sent_gpgerrresume ()
int gpgdecode (int in_fd, int out_fd)
int gpgexportkey (const char *fingerprint, int issecret, int(*func)(const char *, size_t, void *), void *arg)

Variables

const char * sqwebmail_content_charset
static char gpgerrbuf [1024]
static size_t gpgerrcnt = 0

Class Documentation

struct listinfo

Definition at line 543 of file pcp.c.

Collaboration diagram for listinfo:
Class Members
int cnt
const char * default_key
unsigned i_cnt
struct listinfo_index * index_list
int issecret
const char * list_event_id
time_t list_from
time_t list_to

Define Documentation

#define WEXITSTATUS (   stat_val)    ((unsigned)(stat_val) >> 8)

Definition at line 30 of file gpg.c.

#define WIFEXITED (   stat_val)    (((stat_val) & 255) == 0)

Definition at line 33 of file gpg.c.


Function Documentation

static void delkey ( const char *  keyname,
int  flag 
) [static]

Definition at line 385 of file gpg.c.

{
       int rc;

       if (gpgbadarg(keyname))
              return;

       gpginiterr();

       rc=libmail_gpg_deletekey(GPGDIR, flag, keyname, gpg_error, NULL);

       if (rc)
       {
              printf("<div class=\"indent\">%s\n", getarg("DELETEFAIL"));
              dump_error();
              printf("</div>\n");
       }
}

Here is the call graph for this function:

Here is the caller graph for this function:

static void dump_error ( ) [static]

Definition at line 77 of file gpg.c.

{
       if (gpgerrcnt >= 0)
       {
              printf("<span style=\"color: #e00000\"><pre class=\"gpgerroutput\">");
              print_attrencodedlen (gpgerrbuf, gpgerrcnt, 1, stdout);
              printf("</pre></span>\n");
       }
}

Here is the call graph for this function:

Here is the caller graph for this function:

static int dump_func ( const char *  p,
size_t  l,
void *  vp 
) [static]

Definition at line 292 of file gpg.c.

{
       int *ip=(int *)vp;

       while (l)
       {
              if (*ip >= 80)
              {
                     printf("\n");
                     *ip=0;
              }

              ++*ip;

              switch (*p) {
              case '<':
                     printf("&lt;");
                     break;
              case '>':
                     printf("&gt;");
                     break;
              case '\n':
                     *ip=0;
                     /* FALLTHROUGH */
              default:
                     putchar(*p);
                     break;
              }

              ++p;
              --l;
       }
       fflush(stdout);
       return (0);
}

Here is the caller graph for this function:

static int encrypt_key_default ( const char *  fingerprint,
const char *  shortname,
const char *  key,
int  invalid,
struct gpg_list_info gli 
) [static]

Definition at line 268 of file gpg.c.

{
       struct listinfo *li=(struct listinfo *)gli->voidarg;

       if (invalid)
              return (0);

       return (select_key(fingerprint, shortname, key, gli,
                        knownkey(shortname, li->default_key)));
}

Here is the call graph for this function:

Here is the caller graph for this function:

static int gpg_error ( const char *  p,
size_t  l,
void *  dummy 
) [static]

Definition at line 50 of file gpg.c.

{
       while (l && gpgerrcnt < sizeof(gpgerrbuf)-1)
       {
              gpgerrbuf[gpgerrcnt++]= *p++;
              --l;
       }
       return (0);
}

Here is the caller graph for this function:

static void gpg_error_save ( const char *  errmsg,
void *  dummy 
) [static]

Definition at line 60 of file gpg.c.

{
       gpg_error(errmsg, strlen(errmsg), dummy);
}

Here is the call graph for this function:

Here is the caller graph for this function:

int gpgbadarg ( const char *  p)

Definition at line 65 of file gpg.c.

{
       for ( ; *p; p++)
       {
              int c=(unsigned char)*p;

              if (c < ' ' || strchr("\",'`;*?()<>", c))
                     return (1);
       }
       return (0);
}

Here is the caller graph for this function:

void gpgcreate ( )

Definition at line 333 of file gpg.c.

{
       int linelen;

       const char *newname=cgi("newname");
       const char *newaddress=cgi("newaddress");
       const char *newcomment=cgi("newcomment");
       unsigned skl=atoi(cgi("skeylength"));
       unsigned ekl=atoi(cgi("ekeylength"));
       unsigned newexpire=atoi(cgi("newexpire"));
       char newexpirewhen=*cgi("newexpirewhen");
       const char *passphrase, *p;

       if (*newname == 0 || *newaddress == 0 || strchr(newaddress, '@') == 0
           || gpgbadarg(newname) || gpgbadarg(newaddress)
           || gpgbadarg(newcomment)
           || ekl < 512 || ekl > 2048 || skl < 512 || skl > 1024)
       {
              printf("%s\n", getarg("BADARGS"));
              return;
       }
       passphrase=cgi("passphrase");
       if (strcmp(passphrase, cgi("passphrase2")))
       {
              printf("%s\n", getarg("PASSPHRASEFAIL"));
              return;
       }

       for (p=passphrase; *p; p++)
       {
              if ((int)(unsigned char)*p < ' ')
              {
                     printf("%s\n", getarg("PASSPHRASEFAIL"));
                     return;
              }
       }

       printf("<pre class=\"gpgcreate\">");

       linelen=0;

       libmail_gpg_genkey(GPGDIR, sqwebmail_content_charset,
                        newname, newaddress, newcomment,
                        skl, ekl,
                        newexpire, newexpirewhen,
                        passphrase,
                        &dump_func,
                        &timeout_func,
                        &linelen);
       printf("</pre>");
}

Here is the call graph for this function:

Here is the caller graph for this function:

int gpgdecode ( int  in_fd,
int  out_fd 
)

Definition at line 665 of file gpg.c.

{
       char passfd_buf[NUMBUFSIZE];
       FILE *fp=passphrasefp();
       int in_dup, out_dup;
       FILE *in_fp, *out_fp;
       struct libmail_gpg_info gi;
       char *argvec[2];
       int i;

       gpginiterr();

       if ((in_dup=dup(in_fd)) < 0 || (in_fp=fdopen(in_dup, "r")) == NULL)
       {
              if (in_dup >= 0)
                     close(in_dup);
              fclose(fp);
              enomem();
              return 1;
       }

       if ((out_dup=dup(out_fd)) < 0 || (out_fp=fdopen(out_dup, "w")) == NULL)
       {
              if (out_dup >= 0)
                     close(out_dup);
              fclose(in_fp);
              close(in_dup);
              fclose(fp);
              enomem();
              return 1;
       }

       memset(&gi, 0, sizeof(gi));

       gi.gnupghome=GPGDIR;
       if (fp)
       {
              gi.passphrase_fd=libmail_str_size_t(fileno(fp), passfd_buf);
       }

       gi.input_func= read_fd;
       gi.input_func_arg= in_fp;
       gi.output_func= write_fd;
       gi.output_func_arg= out_fp;
       gi.errhandler_func= gpg_error_save;
       gi.errhandler_arg= NULL;

       argvec[0] = "--no-tty";
       argvec[1]=NULL;
       gi.argc=1;
       gi.argv=argvec;

       i=libmail_gpg_decode(LIBMAIL_GPG_UNENCRYPT|LIBMAIL_GPG_CHECKSIGN,
                          &gi);
       fclose(out_fp);
       close(out_dup);
       fclose(in_fp);
       close(in_dup);
       if (fp)
              fclose(fp);

       if (i)
       {
              printf("<div class=\"indent\"><pre style=\"color: red;\">");
              sent_gpgerrtxt();
              printf("</pre></div>\n");
       }
       return (i);
}

Here is the call graph for this function:

Here is the caller graph for this function:

void gpgdo ( )

Definition at line 469 of file gpg.c.

{
       if (*cgi("delpub"))
              delkey(cgi("pubkeyname"), 0);
       else if (*cgi("delsec") && *cgi("really"))
              delkey(cgi("seckeyname"), 1);
       else if (*cgi("sign"))
              signkey(cgi("pubkeyname"), cgi("seckeyname"),
                     cgi("signlevel"));
       else if (*cgi("setdefault"))
              setdefault(cgi("seckeyname"));
}

Here is the call graph for this function:

Here is the caller graph for this function:

int gpgdomsg ( int  in_fd,
int  out_fd,
const char *  signkey,
const char *  encryptkeys 
)

Definition at line 524 of file gpg.c.

{
       char *k=strdup(encryptkeys ? encryptkeys:"");
       int n;
       int i;
       char *p;
       char **argvec;
       FILE *passfd=NULL;
       char passfd_buf[NUMBUFSIZE];
       struct libmail_gpg_info gi;

       int in_dup, out_dup;
       FILE *in_fp, *out_fp;

       gpginiterr();

       if (!k)
       {
              enomem();
              return 1;
       }

       if ((in_dup=dup(in_fd)) < 0 || (in_fp=fdopen(in_dup, "r")) == NULL)
       {
              if (in_dup >= 0)
                     close(in_dup);
              free(k);
              enomem();
              return 1;
       }

       if ((out_dup=dup(out_fd)) < 0 || (out_fp=fdopen(out_dup, "w")) == NULL)
       {
              if (out_dup >= 0)
                     close(out_dup);
              fclose(in_fp);
              close(in_dup);
              free(k);
              enomem();
              return 1;
       }

       passfd=passphrasefp();

       n=0;
       for (p=k; (p=strtok(p, " ")) != NULL; p=NULL)
              ++n;

       argvec=malloc((n * 2 + 22)*sizeof(char *));
       if (!argvec)
       {
              fclose(out_fp);
              close(out_dup);
              fclose(in_fp);
              close(in_dup);
              free(k);
              enomem();
              return 1;
       }

       memset(&gi, 0, sizeof(gi));

       gi.gnupghome=GPGDIR;
       if (passfd)
       {
              gi.passphrase_fd=libmail_str_size_t(fileno(passfd),
                                              passfd_buf);
       }

       gi.input_func= read_fd;
       gi.input_func_arg= in_fp;
       gi.output_func= write_fd;
       gi.output_func_arg= out_fp;
       gi.errhandler_func= gpg_error_save;
       gi.errhandler_arg= NULL;


       i=0;
       argvec[i++] = "--no-tty";
       if (signkey)
       {
              argvec[i++]="--default-key";
              argvec[i++]=(char *)signkey;
       }

       argvec[i++]="--always-trust";

       for (p=strcpy(k, encryptkeys ? encryptkeys:"");
            (p=strtok(p, " ")) != NULL; p=NULL)
       {
              argvec[i++]="-r";
              argvec[i++]=p;
       }
       argvec[i]=0;
       gi.argc=i;
       gi.argv=argvec;

       i=libmail_gpg_signencode(signkey ? 1:0,
                             n > 0 ? LIBMAIL_GPG_ENCAPSULATE:0,
                             &gi);

       free(argvec);
       fclose(out_fp);
       close(out_dup);
       fclose(in_fp);
       close(in_dup);
       free(k);
       if (passfd)
              fclose(passfd);

       return i;
}

Here is the call graph for this function:

Here is the caller graph for this function:

void gpgencryptkeys ( const char *  select_keys)

Definition at line 282 of file gpg.c.

{
       listpubsec(0, encrypt_key_default, select_keys);
}

Here is the call graph for this function:

Here is the caller graph for this function:

int gpgexportkey ( const char *  fingerprint,
int  issecret,
int(*)(const char *, size_t, void *)  func,
void *  arg 
)

Definition at line 735 of file gpg.c.

{
       gpginiterr();

       return (libmail_gpg_exportkey(GPGDIR, issecret, fingerprint,
                                  func,
                                  gpg_error,
                                  arg));
}

Here is the call graph for this function:

Here is the caller graph for this function:

static void gpginiterr ( ) [static]

Definition at line 45 of file gpg.c.

{
       gpgerrcnt=0;
}

Here is the caller graph for this function:

void gpglistpub ( )

Definition at line 147 of file gpg.c.

{
       printf("<table width=\"100%%\" border=\"0\" cellspacing=\"2\" cellpadding=\"0\" class=\"gpgpubkeys\">");
       listpubsec(0, show_key, NULL);
       printf("</table>");
}

Here is the call graph for this function:

Here is the caller graph for this function:

void gpglistsec ( )

Definition at line 154 of file gpg.c.

{
       printf("<table width=\"100%%\" border=\"0\" cellspacing=\"2\" cellpadding=\"0\" class=\"gpgseckeys\">");
       listpubsec(1, show_key, NULL);
       printf("</table>");
}

Here is the call graph for this function:

Here is the caller graph for this function:

void gpgselectkey ( )

Definition at line 188 of file gpg.c.

{
       char *default_key=pref_getdefaultgpgkey();

       listpubsec(1, select_key_default, default_key);

       if (default_key)
              free(default_key);
}

Here is the call graph for this function:

Here is the caller graph for this function:

void gpgselectprivkey ( )

Definition at line 203 of file gpg.c.

Here is the call graph for this function:

Here is the caller graph for this function:

void gpgselectpubkey ( )

Definition at line 198 of file gpg.c.

Here is the call graph for this function:

Here is the caller graph for this function:

static int knownkey ( const char *  shortname,
const char *  known_keys 
) [static]

Definition at line 213 of file gpg.c.

{
       struct rfc822t *t=rfc822t_alloc_new(shortname, NULL, NULL);
       struct rfc822a *a;
       int i;

       if (!t)
              return (0);

       a=rfc822a_alloc(t);

       if (!a)
       {
              rfc822t_free(t);
              return (0);
       }

       for (i=0; i<a->naddrs; i++)
       {
              char *p=rfc822_getaddr(a, i);
              int plen;
              const char *q;

              if (!p)
                     continue;

              if (!*p)
              {
                     free(p);
                     continue;
              }

              plen=strlen(p);

              for (q=known_keys; *q; )
              {
                     if (strncasecmp(q, p, plen) == 0 && q[plen] == '\n')
                     {
                            free(p);
                            rfc822a_free(a);
                            rfc822t_free(t);
                            return (1);
                     }

                     while (*q)
                            if (*q++ == '\n')
                                   break;
              }
              free(p);
       }
       rfc822a_free(a);
       rfc822t_free(t);
       return (0);
}

Here is the call graph for this function:

Here is the caller graph for this function:

static void listpubsec ( int  flag,
int(*)(const char *, const char *, const char *, int, struct gpg_list_info *)  callback_func,
const char *  default_key 
) [static]

Definition at line 112 of file gpg.c.

{
       int rc;
       struct gpg_list_info gli;
       struct listinfo li;

       li.issecret=flag;

       li.default_key=default_key;

       memset(&gli, 0, sizeof(gli));
       gli.charset=sqwebmail_content_charset;

       gli.disabled_msg=getarg("DISABLED");
       gli.revoked_msg=getarg("REVOKED");
       gli.expired_msg=getarg("EXPIRED");
       gli.voidarg= &li;

       gpginiterr();

       rc=libmail_gpg_listkeys(GPGDIR, flag, callback_func, gpg_error, &gli);

       if (rc)
       {
              dump_error();
       }
}

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 384 of file sqwebmail.c.

{
char   *p=scriptptrget();

       printf("%s", p);
       free(p);
       return;
}
static FILE* passphrasefp ( ) [static]

Definition at line 404 of file gpg.c.

{
       FILE *fp=NULL;
       const char *passphrase;

       passphrase=cgi("passphrase");
       if (*passphrase)
       {
              fp=tmpfile();
              if (fp)
              {
                     fprintf(fp, "%s", passphrase);
                     if (fflush(fp) || ferror(fp)
                         || lseek(fileno(fp), 0L, SEEK_SET) < 0
                         || fcntl(fileno(fp), F_SETFD, 0) < 0)
                     {
                            fclose(fp);
                            fp=NULL;
                     }
              }
       }
       return (fp);
}

Here is the call graph for this function:

Here is the caller graph for this function:

void print_attrencodedlen ( const char *  ,
size_t  ,
int  ,
FILE *   
)

Definition at line 215 of file sqwebmail.c.

{
       for (; len; p++, --len)
       {
              switch (*p)   {
              case '<':
                     fprintf(fp, "&lt;");
                     continue;
              case '>':
                     fprintf(fp, "&gt;");
                     continue;
              case '&':
                     fprintf(fp, "&amp;");
                     continue;
              case '"':
                     fprintf(fp, "&quot;");
                     continue;
              case '\n':
                     if (oknl)
                     {
                            if (oknl == 2)
                            {
                                   fprintf(fp, "<br />");
                                   continue;
                            }
                            putc('\n', fp);
                            continue;
                     }
              default:
                     if (!ISCTRL(*p))
                     {
                            putc(*p, fp);
                            continue;
                     }
              }
              fprintf(fp, "&#%d;", (int)(unsigned char)*p);
       }
}
void print_safe ( const char *  )

Definition at line 150 of file folder.c.

static int read_fd ( char *  buf,
size_t  cnt,
void *  vp 
) [static]

Definition at line 484 of file gpg.c.

{
       FILE *fp=(FILE *)vp;
       size_t i;
       int c;

       if (cnt == 0)
              return -1;

       --cnt;

       for (i=0; i<cnt; i++)
       {
              if ((c=getc(fp)) == EOF)
              {
                     if (i == 0)
                            return -1;
                     break;
              }
              buf[i]=c;

              if (c == '\n')
              {
                     ++i;
                     break;
              }
       }
       buf[i]=0;
       return 0;
}

Here is the caller graph for this function:

static int select_key ( const char *  fingerprint,
const char *  shortname,
const char *  key,
struct gpg_list_info gli,
int  is_select 
) [static]

Definition at line 161 of file gpg.c.

{
       printf("<option value=\"");
       print_attrencodedlen(fingerprint, strlen(fingerprint), 0, stdout);
       printf("\"%s>", is_select ? " selected='selected'":"");

       print_safe(shortname);
       printf("</option>");
       return (0);
}

Here is the call graph for this function:

Here is the caller graph for this function:

static int select_key_default ( const char *  fingerprint,
const char *  shortname,
const char *  key,
int  invalid,
struct gpg_list_info gli 
) [static]

Definition at line 175 of file gpg.c.

{
       struct listinfo *li=(struct listinfo *)gli->voidarg;

       return (select_key(fingerprint, shortname, key, gli,
                        li->default_key && strcmp(li->default_key,
                                               fingerprint)
                        == 0));
}

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 658 of file gpg.c.

{
       output_scriptptrget();
       printf("&form=newmsg&pos=%s&draft=%s", cgi("pos"),
              cgi("draftmessage"));
}

Here is the call graph for this function:

Here is the caller graph for this function:

void sent_gpgerrtxt ( )

Definition at line 638 of file gpg.c.

{
       const char *p;

       for (p=gpgerrbuf; *p; p++)
       {
              switch (*p) {
              case '<':
                     printf("&lt;");
                     break;
              case '>':
                     printf("&gt;");
                     break;
              default:
                     putchar((int)(unsigned char)*p);
                     break;
              }
       }
}

Here is the caller graph for this function:

static void setdefault ( const char *  def) [static]

Definition at line 461 of file gpg.c.

{
       if (gpgbadarg(def))
              return;

       pref_setdefaultgpgkey(def);
}

Here is the call graph for this function:

Here is the caller graph for this function:

static int show_key ( const char *  fingerprint,
const char *  shortname,
const char *  key,
int  invalid,
struct gpg_list_info gli 
) [static]

Definition at line 92 of file gpg.c.

{
       struct listinfo *li=(struct listinfo *)gli->voidarg;

       printf("<tr valign=\"middle\" class=\"%s\"><td>"
              "<input type=\"radio\" name=\"%s\" value=\"",
              li->issecret ? "gpgseckey":"gpgpubkey",
              li->issecret ? "seckeyname":"pubkeyname");

       print_attrencodedlen(fingerprint, strlen(fingerprint), 0, stdout);
       printf("\"%s /></td><td><span class=\"tt\">",
              li->default_key && strcmp(li->default_key, fingerprint) == 0
              ? " checked=\"checked\"":"");
       print_safe(key);
       printf("</span></td></tr>\n");
       return (0);
}

Here is the call graph for this function:

Here is the caller graph for this function:

static void signkey ( const char *  signthis,
const char *  signwith,
const char *  trustlevel 
) [static]

Definition at line 428 of file gpg.c.

{
       int rc;
       FILE *fp=NULL;

       int n=atoi(trustlevel);

       if (n < 0 || n > 9)
              n=0;

       if (gpgbadarg(signthis) || gpgbadarg(signwith))
              return;

       gpginiterr();


       fp=passphrasefp();

       rc=libmail_gpg_signkey(GPGDIR, signthis, signwith,
                            fp ? fileno(fp):-1, gpg_error, n, NULL);

       if (fp)
              fclose(fp);

       if (rc)
       {
              printf("<div class=\"indent\">%s\n", getarg("SIGNFAIL"));
              dump_error();
              printf("</div>\n");
       }
}

Here is the call graph for this function:

Here is the caller graph for this function:

static int timeout_func ( void *  vp) [static]

Definition at line 328 of file gpg.c.

{
       return (dump_func("*", 1, vp));
}

Here is the call graph for this function:

Here is the caller graph for this function:

static void write_fd ( const char *  p,
size_t  n,
void *  dummy 
) [static]

Definition at line 515 of file gpg.c.

{
       if (n == 0)
              return;

       if (fwrite(p, n, 1, (FILE *)dummy) != 1)
              exit(1);
}

Here is the caller graph for this function:


Variable Documentation

static char gpgerrbuf [static]

Definition at line 42 of file gpg.c.

size_t gpgerrcnt = 0 [static]

Definition at line 43 of file gpg.c.

Definition at line 39 of file gpg.c.