From 0a42f97e43077a11b90feb689add430d3b4fc820 Mon Sep 17 00:00:00 2001 From: David Shaw Date: Thu, 13 Jan 2005 23:22:10 +0000 Subject: [PATCH] * gpgkeys_finger.c, gpgkeys_hkp.c, gpgkeys_http.c, gpgkeys_ldap.c: Part 1 of a minor cleanup to use #defines instead of hard-coded sizes. --- keyserver/ChangeLog | 4 +++ keyserver/gpgkeys_finger.c | 32 ++++++++++++++---------- keyserver/gpgkeys_hkp.c | 50 ++++++++++++++++++++++---------------- keyserver/gpgkeys_http.c | 48 +++++++++++++++++++++--------------- keyserver/gpgkeys_ldap.c | 48 +++++++++++++++++++++--------------- 5 files changed, 109 insertions(+), 73 deletions(-) diff --git a/keyserver/ChangeLog b/keyserver/ChangeLog index a5bcb7d70..37cb73acc 100644 --- a/keyserver/ChangeLog +++ b/keyserver/ChangeLog @@ -1,5 +1,9 @@ 2005-01-13 David Shaw + * gpgkeys_finger.c, gpgkeys_hkp.c, gpgkeys_http.c, gpgkeys_ldap.c: + Part 1 of a minor cleanup to use #defines instead of hard-coded + sizes. + * gpgkeys_finger.c (connect_server): Use INADDR_NONE instead of SOCKET_ERROR. Noted by Timo. diff --git a/keyserver/gpgkeys_finger.c b/keyserver/gpgkeys_finger.c index cf06feccf..535c91966 100644 --- a/keyserver/gpgkeys_finger.c +++ b/keyserver/gpgkeys_finger.c @@ -57,9 +57,15 @@ extern int optind; #define GET 0 #define MAX_LINE 80 +#define MAX_COMMAND 7 +#define MAX_OPAQUE 1024 +#define MAX_OPTION 256 + +#define STRINGIFY(x) #x +#define MKSTRING(x) STRINGIFY(x) static int verbose=0; -static char path[1024]; +static char path[MAX_OPAQUE+1]; static FILE *input, *output, *console; #define BEGIN "-----BEGIN PGP PUBLIC KEY BLOCK-----" @@ -433,8 +439,8 @@ main(int argc,char *argv[]) while(fgets(line,MAX_LINE,input)!=NULL) { int version; - char commandstr[7]; - char optionstr[256]; + char command[MAX_COMMAND+1]; + char option[MAX_OPTION+1]; char hash; if(line[0]=='\n') @@ -443,11 +449,11 @@ main(int argc,char *argv[]) if(sscanf(line,"%c",&hash)==1 && hash=='#') continue; - if(sscanf(line,"COMMAND %6s\n",commandstr)==1) + if(sscanf(line,"COMMAND %" MKSTRING(MAX_COMMAND) "s\n",command)==1) { - commandstr[6]='\0'; + command[MAX_COMMAND]='\0'; - if(strcasecmp(commandstr,"get")==0) + if(strcasecmp(command,"get")==0) action=GET; continue; @@ -461,9 +467,9 @@ main(int argc,char *argv[]) goto fail; } - if(sscanf(line,"OPAQUE %1023s\n",path)==1) + if(sscanf(line,"OPAQUE %" MKSTRING(MAX_OPAQUE) "s\n",path)==1) { - path[1023]='\0'; + path[MAX_OPAQUE]='\0'; continue; } @@ -478,17 +484,17 @@ main(int argc,char *argv[]) continue; } - if(sscanf(line,"OPTION %255s\n",optionstr)==1) + if(sscanf(line,"OPTION %" MKSTRING(MAX_OPTION) "s\n",option)==1) { int no=0; - char *start=&optionstr[0]; + char *start=&option[0]; - optionstr[255]='\0'; + option[MAX_OPTION]='\0'; - if(strncasecmp(optionstr,"no-",3)==0) + if(strncasecmp(option,"no-",3)==0) { no=1; - start=&optionstr[3]; + start=&option[3]; } if(strcasecmp(start,"verbose")==0) diff --git a/keyserver/gpgkeys_hkp.c b/keyserver/gpgkeys_hkp.c index db1e7ea0e..4639a5081 100644 --- a/keyserver/gpgkeys_hkp.c +++ b/keyserver/gpgkeys_hkp.c @@ -41,10 +41,18 @@ extern int optind; #define SEND 1 #define SEARCH 2 #define MAX_LINE 80 +#define MAX_HOST 80 +#define MAX_PORT 10 +#define MAX_PROXY 80 +#define MAX_COMMAND 7 +#define MAX_OPTION 110 + +#define STRINGIFY(x) #x +#define MKSTRING(x) STRINGIFY(x) static int verbose=0,include_revoked=0,include_disabled=0; static unsigned int http_flags=0; -static char host[80]={'\0'},proxy[80]={'\0'},port[10]={'\0'}; +static char host[MAX_HOST+1]={'\0'},proxy[MAX_PROXY+1]={'\0'},port[MAX_PORT+1]={'\0'}; static FILE *input=NULL,*output=NULL,*console=NULL; #define BEGIN "-----BEGIN PGP PUBLIC KEY BLOCK-----" @@ -831,8 +839,8 @@ main(int argc,char *argv[]) while(fgets(line,MAX_LINE,input)!=NULL) { int version; - char commandstr[7]; - char optionstr[110]; + char command[MAX_COMMAND+1]; + char option[MAX_OPTION+1]; char hash; if(line[0]=='\n') @@ -841,29 +849,29 @@ main(int argc,char *argv[]) if(sscanf(line,"%c",&hash)==1 && hash=='#') continue; - if(sscanf(line,"COMMAND %6s\n",commandstr)==1) + if(sscanf(line,"COMMAND %" MKSTRING(MAX_COMMAND) "s\n",command)==1) { - commandstr[6]='\0'; + command[MAX_COMMAND]='\0'; - if(strcasecmp(commandstr,"get")==0) + if(strcasecmp(command,"get")==0) action=GET; - else if(strcasecmp(commandstr,"send")==0) + else if(strcasecmp(command,"send")==0) action=SEND; - else if(strcasecmp(commandstr,"search")==0) + else if(strcasecmp(command,"search")==0) action=SEARCH; continue; } - if(sscanf(line,"HOST %79s\n",host)==1) + if(sscanf(line,"HOST %" MKSTRING(MAX_HOST) "s\n",host)==1) { - host[79]='\0'; + host[MAX_HOST]='\0'; continue; } - if(sscanf(line,"PORT %9s\n",port)==1) + if(sscanf(line,"PORT %" MKSTRING(MAX_PORT) "s\n",port)==1) { - port[9]='\0'; + port[MAX_PORT]='\0'; continue; } @@ -878,17 +886,17 @@ main(int argc,char *argv[]) continue; } - if(sscanf(line,"OPTION %109s\n",optionstr)==1) + if(sscanf(line,"OPTION %" MKSTRING(MAX_OPTION) "s\n",option)==1) { int no=0; - char *start=&optionstr[0]; + char *start=&option[0]; - optionstr[109]='\0'; + option[MAX_OPTION]='\0'; - if(strncasecmp(optionstr,"no-",3)==0) + if(strncasecmp(option,"no-",3)==0) { no=1; - start=&optionstr[3]; + start=&option[3]; } if(strcasecmp(start,"verbose")==0) @@ -918,16 +926,16 @@ main(int argc,char *argv[]) proxy[0]='\0'; else if(start[10]=='=') { - strncpy(proxy,&start[11],79); - proxy[79]='\0'; + strncpy(proxy,&start[11],MAX_PROXY); + proxy[MAX_PROXY]='\0'; } else if(start[10]=='\0') { char *http_proxy=getenv(HTTP_PROXY_ENV); if(http_proxy) { - strncpy(proxy,http_proxy,79); - proxy[79]='\0'; + strncpy(proxy,http_proxy,MAX_PROXY); + proxy[MAX_PROXY]='\0'; } } } diff --git a/keyserver/gpgkeys_http.c b/keyserver/gpgkeys_http.c index 76da9e00c..606e26fe5 100644 --- a/keyserver/gpgkeys_http.c +++ b/keyserver/gpgkeys_http.c @@ -38,10 +38,20 @@ extern int optind; #define GET 0 #define MAX_LINE 80 +#define MAX_AUTH 128 +#define MAX_HOST 80 +#define MAX_PROXY 80 +#define MAX_PORT 10 +#define MAX_PATH 1024 +#define MAX_COMMAND 7 +#define MAX_OPTION 256 + +#define STRINGIFY(x) #x +#define MKSTRING(x) STRINGIFY(x) static int verbose=0; static unsigned int http_flags=0; -static char auth[128]={'\0'},host[80]={'\0'},proxy[80]={'\0'},port[10]={'\0'},path[1024]={'\0'}; +static char auth[MAX_AUTH+1]={'\0'},host[MAX_HOST+1]={'\0'},proxy[MAX_PROXY+1]={'\0'},port[MAX_PORT+1]={'\0'},path[MAX_PATH+1]={'\0'}; static FILE *input=NULL,*output=NULL,*console=NULL; #define BEGIN "-----BEGIN PGP PUBLIC KEY BLOCK-----" @@ -199,8 +209,8 @@ main(int argc,char *argv[]) while(fgets(line,MAX_LINE,input)!=NULL) { int version; - char commandstr[7]; - char optionstr[256]; + char command[MAX_COMMAND+1]; + char option[MAX_OPTION+1]; char hash; if(line[0]=='\n') @@ -209,37 +219,37 @@ main(int argc,char *argv[]) if(sscanf(line,"%c",&hash)==1 && hash=='#') continue; - if(sscanf(line,"COMMAND %6s\n",commandstr)==1) + if(sscanf(line,"COMMAND %" MKSTRING(MAX_COMMAND) "s\n",command)==1) { - commandstr[6]='\0'; + command[MAX_COMMAND]='\0'; - if(strcasecmp(commandstr,"get")==0) + if(strcasecmp(command,"get")==0) action=GET; continue; } - if(sscanf(line,"AUTH %127s\n",auth)==1) + if(sscanf(line,"AUTH %" MKSTRING(MAX_AUTH) "s\n",auth)==1) { - auth[127]='\0'; + auth[MAX_AUTH]='\0'; continue; } - if(sscanf(line,"HOST %79s\n",host)==1) + if(sscanf(line,"HOST %" MKSTRING(MAX_HOST) "s\n",host)==1) { - host[79]='\0'; + host[MAX_HOST]='\0'; continue; } - if(sscanf(line,"PORT %9s\n",port)==1) + if(sscanf(line,"PORT %" MKSTRING(MAX_PORT) "s\n",port)==1) { - port[9]='\0'; + port[MAX_PORT]='\0'; continue; } - if(sscanf(line,"PATH %1023s\n",path)==1) + if(sscanf(line,"PATH %" MKSTRING(MAX_PATH) "s\n",path)==1) { - path[1023]='\0'; + path[MAX_PATH]='\0'; continue; } @@ -254,17 +264,17 @@ main(int argc,char *argv[]) continue; } - if(sscanf(line,"OPTION %255s\n",optionstr)==1) + if(sscanf(line,"OPTION %" MKSTRING(MAX_OPTION) "s\n",option)==1) { int no=0; - char *start=&optionstr[0]; + char *start=&option[0]; - optionstr[255]='\0'; + option[MAX_OPTION]='\0'; - if(strncasecmp(optionstr,"no-",3)==0) + if(strncasecmp(option,"no-",3)==0) { no=1; - start=&optionstr[3]; + start=&option[3]; } if(strcasecmp(start,"verbose")==0) diff --git a/keyserver/gpgkeys_ldap.c b/keyserver/gpgkeys_ldap.c index bc603e725..8c1335d6d 100644 --- a/keyserver/gpgkeys_ldap.c +++ b/keyserver/gpgkeys_ldap.c @@ -50,13 +50,21 @@ extern int optind; #define GET 0 #define SEND 1 #define SEARCH 2 -#define MAX_LINE 256 +#define MAX_LINE 255 +#define MAX_HOST 80 +#define MAX_SCHEME 20 +#define MAX_PORT 10 +#define MAX_OPTION 255 +#define MAX_COMMAND 7 + +#define STRINGIFY(x) #x +#define MKSTRING(x) STRINGIFY(x) static int verbose=0,include_disabled=0,include_revoked=0,include_subkeys=0; static int real_ldap=0; static char *basekeyspacedn=NULL; -static char host[80]={'\0'}; -static char portstr[10]={'\0'}; +static char host[MAX_HOST+1]={'\0'}; +static char portstr[MAX_PORT+1]={'\0'}; static char *pgpkeystr="pgpKey"; static FILE *input=NULL,*output=NULL,*console=NULL; static LDAP *ldap=NULL; @@ -1609,9 +1617,9 @@ main(int argc,char *argv[]) while(fgets(line,MAX_LINE,input)!=NULL) { - char commandstr[7]; - char optionstr[256]; - char schemestr[80]; + char command[MAX_COMMAND+1]; + char optionstr[MAX_OPTION+1]; + char scheme[MAX_SCHEME+1]; char hash; if(line[0]=='\n') @@ -1620,37 +1628,37 @@ main(int argc,char *argv[]) if(sscanf(line,"%c",&hash)==1 && hash=='#') continue; - if(sscanf(line,"COMMAND %6s\n",commandstr)==1) + if(sscanf(line,"COMMAND %" MKSTRING(MAX_COMMAND) "s\n",command)==1) { - commandstr[6]='\0'; + command[MAX_COMMAND]='\0'; - if(strcasecmp(commandstr,"get")==0) + if(strcasecmp(command,"get")==0) action=GET; - else if(strcasecmp(commandstr,"send")==0) + else if(strcasecmp(command,"send")==0) action=SEND; - else if(strcasecmp(commandstr,"search")==0) + else if(strcasecmp(command,"search")==0) action=SEARCH; continue; } - if(sscanf(line,"HOST %79s\n",host)==1) + if(sscanf(line,"HOST %" MKSTRING(MAX_HOST) "s\n",host)==1) { - host[79]='\0'; + host[MAX_HOST]='\0'; continue; } - if(sscanf(line,"PORT %9s\n",portstr)==1) + if(sscanf(line,"PORT %" MKSTRING(MAX_PORT) "s\n",portstr)==1) { - portstr[9]='\0'; + portstr[MAX_PORT]='\0'; port=atoi(portstr); continue; } - if(sscanf(line,"SCHEME %79s\n",schemestr)==1) + if(sscanf(line,"SCHEME %" MKSTRING(MAX_SCHEME) "s\n",scheme)==1) { - schemestr[79]='\0'; - if(strcasecmp(schemestr,"ldaps")==0) + scheme[MAX_SCHEME]='\0'; + if(strcasecmp(scheme,"ldaps")==0) { port=636; use_ssl=1; @@ -1669,12 +1677,12 @@ main(int argc,char *argv[]) continue; } - if(sscanf(line,"OPTION %255[^\n]\n",optionstr)==1) + if(sscanf(line,"OPTION %" MKSTRING(MAX_OPTION) "[^\n]\n",optionstr)==1) { int no=0; char *start=&optionstr[0]; - optionstr[255]='\0'; + optionstr[MAX_OPTION]='\0'; if(strncasecmp(optionstr,"no-",3)==0) {