* ksutil.h, ksutil.c (init_ks_options, free_ks_options,
authorDavid Shaw <dshaw@jabberwocky.com>
Wed, 16 Mar 2005 15:17:03 +0000 (15:17 +0000)
committerDavid Shaw <dshaw@jabberwocky.com>
Wed, 16 Mar 2005 15:17:03 +0000 (15:17 +0000)
parse_ks_options): Pull a lot of duplicated code into a single options
parser for all keyserver helpers.

keyserver/ChangeLog
keyserver/ksutil.c
keyserver/ksutil.h

index 5734e0c..e167143 100644 (file)
@@ -1,3 +1,9 @@
+2005-03-16  David Shaw  <dshaw@jabberwocky.com>
+
+       * ksutil.h, ksutil.c (init_ks_options, free_ks_options,
+       parse_ks_options): Pull a lot of duplicated code into a single
+       options parser for all keyserver helpers.
+
 2005-02-11  David Shaw  <dshaw@jabberwocky.com>
 
        * curl-shim.c (curl_easy_perform): Fix compile warning.
index 51e527d..9700c03 100644 (file)
@@ -1,5 +1,5 @@
 /* ksutil.c - general keyserver utility functions
- * Copyright (C) 2004 Free Software Foundation, Inc.
+ * Copyright (C) 2004, 2005 Free Software Foundation, Inc.
  *
  * This file is part of GnuPG.
  *
@@ -21,6 +21,9 @@
 #include <config.h>
 #include <signal.h>
 #include <unistd.h>
+#include <stdio.h>
+#include <string.h>
+#include <stdlib.h>
 #include "keyserver.h"
 #include "ksutil.h"
 
@@ -61,3 +64,206 @@ register_timeout(void)
 #endif
 #endif
 }
+
+struct ks_options *
+init_ks_options(void)
+{
+  struct ks_options *opt;
+
+  opt=calloc(1,sizeof(struct ks_options));
+
+  if(opt)
+    {
+      opt->action=KS_UNKNOWN;
+      opt->flags.check_cert=1;
+      opt->timeout=DEFAULT_KEYSERVER_TIMEOUT;
+    }
+
+  return opt;
+}
+
+void
+free_ks_options(struct ks_options *opt)
+{
+  free(opt->host);
+  free(opt->port);
+  free(opt->scheme);
+  free(opt->auth);
+  free(opt->path);
+  free(opt->ca_cert_file);
+  free(opt);
+}
+
+/* Returns 0 if we "ate" the line.  Returns >0, a KEYSERVER_ error
+   code if that error applies.  Returns -1 if we did not match the
+   line at all. */
+int
+parse_ks_options(char *line,struct ks_options *opt)
+{
+  int version;
+  char command[MAX_COMMAND+1];
+  char host[MAX_HOST+1];
+  char port[MAX_PORT+1];
+  char scheme[MAX_SCHEME+1];
+  char auth[MAX_AUTH+1];
+  char path[URLMAX_PATH+1];
+  char option[MAX_OPTION+1];
+
+#if 0
+  if(sscanf(line,"%c",&hash)==1 && hash=='#')
+    continue;
+#endif
+
+  if(line[0]=='#')
+    return 0;
+
+  if(sscanf(line,"COMMAND %" MKSTRING(MAX_COMMAND) "s\n",command)==1)
+    {
+      command[MAX_COMMAND]='\0';
+
+      if(strcasecmp(command,"get")==0)
+       opt->action=KS_GET;
+      else if(strcasecmp(command,"send")==0)
+       opt->action=KS_SEND;
+      else if(strcasecmp(command,"search")==0)
+       opt->action=KS_SEARCH;
+
+      return 0;
+    }
+
+  if(sscanf(line,"HOST %" MKSTRING(MAX_HOST) "s\n",host)==1)
+    {
+      host[MAX_HOST]='\0';
+      opt->host=strdup(host);
+      if(!opt->host)
+       return KEYSERVER_NO_MEMORY;
+      return 0;
+    }
+
+  if(sscanf(line,"PORT %" MKSTRING(MAX_PORT) "s\n",port)==1)
+    {
+      port[MAX_PORT]='\0';
+      opt->port=strdup(port);
+      if(!opt->port)
+       return KEYSERVER_NO_MEMORY;
+      return 0;
+    }
+
+  if(sscanf(line,"SCHEME %" MKSTRING(MAX_SCHEME) "s\n",scheme)==1)
+    {
+      scheme[MAX_SCHEME]='\0';
+      opt->scheme=strdup(scheme);
+      if(!opt->scheme)
+       return KEYSERVER_NO_MEMORY;
+      return 0;
+    }
+
+  if(sscanf(line,"AUTH %" MKSTRING(MAX_AUTH) "s\n",auth)==1)
+    {
+      auth[MAX_AUTH]='\0';
+      opt->auth=strdup(auth);
+      if(!opt->auth)
+       return KEYSERVER_NO_MEMORY;
+      return 0;
+    }
+
+  if(sscanf(line,"PATH %" MKSTRING(URLMAX_PATH) "s\n",path)==1)
+    {
+      path[URLMAX_PATH]='\0';
+      opt->path=strdup(path);
+      if(!opt->path)
+       return KEYSERVER_NO_MEMORY;
+      return 0;
+    }
+
+  if(sscanf(line,"VERSION %d\n",&version)==1)
+    {
+      if(version!=KEYSERVER_PROTO_VERSION)
+       return KEYSERVER_VERSION_ERROR;
+
+      return 0;
+    }
+
+  if(sscanf(line,"OPTION %" MKSTRING(MAX_OPTION) "[^\n]\n",option)==1)
+    {
+      int no=0;
+      char *start=&option[0];
+
+      option[MAX_OPTION]='\0';
+
+      if(strncasecmp(option,"no-",3)==0)
+       {
+         no=1;
+         start=&option[3];
+       }
+
+      if(strcasecmp(start,"verbose")==0)
+       {
+         if(no)
+           opt->verbose--;
+         else
+           opt->verbose++;
+       }
+      else if(strcasecmp(start,"include-disabled")==0)
+       {
+         if(no)
+           opt->flags.include_disabled=0;
+         else
+           opt->flags.include_disabled=1;
+       }
+      else if(strcasecmp(start,"include-revoked")==0)
+       {
+         if(no)
+           opt->flags.include_revoked=0;
+         else
+           opt->flags.include_revoked=1;
+       }
+      else if(strcasecmp(start,"include-subkeys")==0)
+       {
+         if(no)
+           opt->flags.include_subkeys=0;
+         else
+           opt->flags.include_subkeys=1;
+       }
+      else if(strcasecmp(start,"check-cert")==0)
+       {
+         if(no)
+           opt->flags.check_cert=0;
+         else
+           opt->flags.check_cert=1;
+       }
+      else if(strncasecmp(start,"debug",5)==0)
+       {
+         if(no)
+           opt->debug=0;
+         else if(start[5]=='=')
+           opt->debug=atoi(&start[6]);
+       }
+      else if(strncasecmp(start,"timeout",7)==0)
+       {
+         if(no)
+           opt->timeout=0;
+         else if(start[7]=='=')
+           opt->timeout=atoi(&start[8]);
+         else if(start[7]=='\0')
+           opt->timeout=DEFAULT_KEYSERVER_TIMEOUT;
+       }
+      else if(strncasecmp(start,"ca-cert-file",12)==0)
+       {
+         if(no)
+           {
+             free(opt->ca_cert_file);
+             opt->ca_cert_file=NULL;
+           }
+         else if(start[12]=='=')
+           {
+             free(opt->ca_cert_file);
+             opt->ca_cert_file=strdup(&start[13]);
+             if(!opt->ca_cert_file)
+               return KEYSERVER_NO_MEMORY;
+           }
+       }
+    }
+
+  return -1;
+}
index e10d1fb..8559d8d 100644 (file)
@@ -59,4 +59,29 @@ struct keylist
 unsigned int set_timeout(unsigned int seconds);
 int register_timeout(void);
 
+struct ks_options
+{
+  enum {KS_UNKNOWN,KS_GET,KS_SEND,KS_SEARCH} action;
+  char *host;
+  char *port;
+  char *scheme;
+  char *auth;
+  char *path;
+  struct
+  {
+    unsigned int include_disabled:1;
+    unsigned int include_revoked:1;
+    unsigned int include_subkeys:1;
+    unsigned int check_cert:1;
+  } flags;
+  unsigned int verbose;
+  unsigned int debug;
+  unsigned int timeout;
+  char *ca_cert_file;
+};
+
+struct ks_options *init_ks_options(void);
+void free_ks_options(struct ks_options *opt);
+int parse_ks_options(char *line,struct ks_options *opt);
+
 #endif /* !_KSUTIL_H_ */