568
568
int err,begin=0,end=0,keysize=1,ret=KEYSERVER_INTERNAL_ERROR;
569
569
char *dn=NULL,line[MAX_LINE],*key=NULL;
570
char keyid[17],state[6];
571
571
LDAPMod **modlist,**addlist,**ml;
573
573
modlist=malloc(sizeof(LDAPMod *));
608
608
/* Assemble the INFO stuff into LDAP attributes */
610
610
while(fgets(line,MAX_LINE,input)!=NULL)
611
if(sscanf(line,"INFO %16s BEGIN\n",keyid)==1)
611
if(sscanf(line,"INFO%*[ ]%16s%*[ ]%5s\n",keyid,state)==2
612
&& strcmp(state,"BEGIN")==0)
653
654
/* Now parse each line until we see the END */
655
656
while(fgets(line,MAX_LINE,input)!=NULL)
656
if(sscanf(line,"INFO %16s END\n",keyid)==1)
657
if(sscanf(line,"INFO%*[ ]%16s%*[ ]%3s\n",keyid,state)==2
658
&& strcmp(state,"END")==0)
674
676
/* Read and throw away stdin until we see the BEGIN */
676
678
while(fgets(line,MAX_LINE,input)!=NULL)
677
if(sscanf(line,"KEY %16s BEGIN\n",keyid)==1)
679
if(sscanf(line,"KEY%*[ ]%16s%*[ ]%5s\n",keyid,state)==2
680
&& strcmp(state,"BEGIN")==0)
692
695
/* Now slurp up everything until we see the END */
694
697
while(fgets(line,MAX_LINE,input)!=NULL)
695
if(sscanf(line,"KEY %16s END\n",keyid)==1)
698
if(sscanf(line,"KEY%*[ ]%16s%*[ ]%3s\n",keyid,state)==2
699
&& strcmp(state,"END")==0)
779
783
int err,begin=0,end=0,keysize=1,ret=KEYSERVER_INTERNAL_ERROR;
780
784
char *dn=NULL,line[MAX_LINE],*key[2]={NULL,NULL};
785
char keyid[17],state[6];
782
786
LDAPMod mod, *attrs[2];
784
788
memset(&mod,0,sizeof(mod));
812
816
/* Read and throw away stdin until we see the BEGIN */
814
818
while(fgets(line,MAX_LINE,input)!=NULL)
815
if(sscanf(line,"KEY %16s BEGIN\n",keyid)==1)
819
if(sscanf(line,"KEY%*[ ]%16s%*[ ]%5s\n",keyid,state)==2
820
&& strcmp(state,"BEGIN")==0)
830
835
/* Now slurp up everything until we see the END */
832
837
while(fgets(line,MAX_LINE,input)!=NULL)
833
if(sscanf(line,"KEY %16s END\n",keyid)==1)
838
if(sscanf(line,"KEY%*[ ]%16s%*[ ]%3s\n",keyid,state)==2
839
&& strcmp(state,"END")==0)
1122
1128
#define LDAP_ESCAPE_CHARS "*()\\"
1130
/* Append string to buffer in a LDAP-quoted way */
1125
1132
ldap_quote(char *buffer,const char *string)
1134
/* Find the end of buffer */
1135
buffer+=strlen(buffer);
1129
1137
for(;*string;string++)
1131
1139
if(strchr(LDAP_ESCAPE_CHARS,*string))
1135
sprintf(buffer,"\\%02X",*string);
1141
sprintf(buffer,"\\%02X",*string);
1156
1151
/* Note that key-not-found is not a fatal error */
1160
1155
LDAPMessage *res,*each;
1161
1156
int ret=KEYSERVER_INTERNAL_ERROR,err,count;
1162
char *expanded_search;
1163
1157
/* The maximum size of the search, including the optional stuff and
1164
1158
the trailing \0 */
1165
char search[2+11+3+MAX_LINE+2+15+14+1+1+20];
1159
char search[2+12+(MAX_LINE*3)+2+15+14+1+1+20];
1166
1160
/* This ordering is significant - specifically, "pgpcertid" needs to
1167
1161
be the second item in the list, since everything after it may be
1168
1162
discarded if the user isn't in verbose mode. */
1172
1166
attrs[0]=pgpkeystr; /* Some compilers don't like using variables as
1173
1167
array initializers. */
1175
expanded_search=malloc(ldap_quote(NULL,getkey)+1);
1176
if(!expanded_search)
1178
fprintf(output,"NAME %s FAILED %d\n",getkey,KEYSERVER_NO_MEMORY);
1179
fprintf(console,"Out of memory when quoting LDAP search string\n");
1180
return KEYSERVER_NO_MEMORY;
1183
ldap_quote(expanded_search,getkey);
1185
1169
/* Build the search string */
1187
sprintf(search,"%s(pgpuserid=*%s*)%s%s%s",
1188
(!(opt->flags.include_disabled&&opt->flags.include_revoked))?"(&":"",
1190
opt->flags.include_disabled?"":"(pgpdisabled=0)",
1191
opt->flags.include_revoked?"":"(pgprevoked=0)",
1192
!(opt->flags.include_disabled&&opt->flags.include_revoked)?")":"");
1194
free(expanded_search);
1173
if(!opt->flags.include_disabled || !opt->flags.include_revoked)
1174
strcat(search,"(&");
1176
strcat(search,"(pgpUserID=*");
1177
ldap_quote(search,getkey);
1178
strcat(search,"*)");
1180
if(!opt->flags.include_disabled)
1181
strcat(search,"(pgpDisabled=0)");
1183
if(!opt->flags.include_revoked)
1184
strcat(search,"(pgpRevoked=0)");
1186
if(!opt->flags.include_disabled || !opt->flags.include_revoked)
1196
1189
if(opt->verbose>2)
1197
1190
fprintf(console,"gpgkeys: LDAP fetch for: %s\n",search);
1288
1281
LDAPMessage *res,*each;
1289
1282
int err,count=0;
1290
1283
struct keylist *dupelist=NULL;
1291
char *expanded_search;
1292
1284
/* The maximum size of the search, including the optional stuff and
1293
1285
the trailing \0 */
1294
char search[2+11+3+MAX_LINE+2+15+14+1+1+20];
1286
char search[2+1+9+1+3+(MAX_LINE*3)+3+1+15+14+1+1+20];
1295
1287
char *attrs[]={"pgpcertid","pgpuserid","pgprevoked","pgpdisabled",
1296
1288
"pgpkeycreatetime","pgpkeyexpiretime","modifytimestamp",
1297
1289
"pgpkeysize","pgpkeytype",NULL};
1305
1297
fprintf(console,"search type is %d, and key is \"%s\"\n",
1306
1298
search_type,searchkey);
1308
expanded_search=malloc(ldap_quote(NULL,searchkey)+1);
1309
if(!expanded_search)
1311
fprintf(output,"SEARCH %s FAILED %d\n",searchkey,KEYSERVER_NO_MEMORY);
1312
fprintf(console,"Out of memory when quoting LDAP search string\n");
1313
return KEYSERVER_NO_MEMORY;
1316
ldap_quote(expanded_search,searchkey);
1318
1300
/* Build the search string */
1320
sprintf(search,"%s(pgpuserid=%s%s%s)%s%s%s",
1321
(!(opt->flags.include_disabled&&opt->flags.include_revoked))?"(&":"",
1322
(search_type==KS_SEARCH_EXACT)?"":
1323
(search_type==KS_SEARCH_MAILSUB)?"*<*":"*",
1325
(search_type==KS_SEARCH_EXACT
1326
|| search_type==KS_SEARCH_MAIL)?"":
1327
(search_type==KS_SEARCH_MAILSUB)?"*>":"*",
1328
opt->flags.include_disabled?"":"(pgpdisabled=0)",
1329
opt->flags.include_revoked?"":"(pgprevoked=0)",
1330
!(opt->flags.include_disabled&&opt->flags.include_revoked)?")":"");
1332
free(expanded_search);
1304
if(!opt->flags.include_disabled || !opt->flags.include_revoked)
1305
strcat(search,"(&");
1311
case KS_SEARCH_KEYID_SHORT:
1312
strcat(search,"pgpKeyID");
1315
case KS_SEARCH_KEYID_LONG:
1316
strcat(search,"pgpCertID");
1320
strcat(search,"pgpUserID");
1328
case KS_SEARCH_SUBSTR:
1332
case KS_SEARCH_MAIL:
1333
strcat(search,"*<");
1336
case KS_SEARCH_MAILSUB:
1337
strcat(search,"*<*");
1340
case KS_SEARCH_EXACT:
1341
case KS_SEARCH_KEYID_LONG:
1342
case KS_SEARCH_KEYID_SHORT:
1346
ldap_quote(search,searchkey);
1350
case KS_SEARCH_SUBSTR:
1354
case KS_SEARCH_MAIL:
1355
strcat(search,">*");
1358
case KS_SEARCH_MAILSUB:
1359
strcat(search,"*>*");
1362
case KS_SEARCH_EXACT:
1363
case KS_SEARCH_KEYID_LONG:
1364
case KS_SEARCH_KEYID_SHORT:
1370
if(!opt->flags.include_disabled)
1371
strcat(search,"(pgpDisabled=0)");
1373
if(!opt->flags.include_revoked)
1374
strcat(search,"(pgpRevoked=0)");
1376
if(!opt->flags.include_disabled || !opt->flags.include_revoked)
1334
1379
if(opt->verbose>2)
1335
1380
fprintf(console,"gpgkeys: LDAP search for: %s\n",search);