1 /* gpgkeys_ldap.c - talk to a LDAP keyserver
2 * Copyright (C) 2001, 2002, 2004, 2005, 2006 Free Software Foundation, Inc.
4 * This file is part of GnuPG.
6 * GnuPG is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * GnuPG is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
40 /* For OpenLDAP, to enable the API that we're using. */
41 #define LDAP_DEPRECATED 1
46 #include "keyserver.h"
56 static int real_ldap
=0;
57 static char *basekeyspacedn
=NULL
;
58 static char *pgpkeystr
="pgpKey";
59 static FILE *input
=NULL
,*output
=NULL
,*console
=NULL
;
60 static LDAP
*ldap
=NULL
;
61 static struct ks_options
*opt
;
64 time_t timegm(struct tm
*tm
);
68 ldap_err_to_gpg_err(int err
)
74 case LDAP_ALREADY_EXISTS
:
75 ret
=KEYSERVER_KEY_EXISTS
;
78 case LDAP_SERVER_DOWN
:
79 ret
=KEYSERVER_UNREACHABLE
;
83 ret
=KEYSERVER_GENERAL_ERROR
;
91 ldap_to_gpg_err(LDAP
*ld
)
93 #if defined(HAVE_LDAP_GET_OPTION) && defined(LDAP_OPT_ERROR_NUMBER)
97 if(ldap_get_option(ld
,LDAP_OPT_ERROR_NUMBER
,&err
)==0)
98 return ldap_err_to_gpg_err(err
);
100 return KEYSERVER_GENERAL_ERROR
;
102 #elif defined(HAVE_LDAP_LD_ERRNO)
104 return ldap_err_to_gpg_err(ld
->ld_errno
);
108 /* We should never get here since the LDAP library should always
109 have either ldap_get_option or ld_errno, but just in case... */
110 return KEYSERVER_GENERAL_ERROR
;
116 key_in_keylist(const char *key
,struct keylist
*list
)
118 struct keylist
*keyptr
=list
;
122 if(strcasecmp(key
,keyptr
->str
)==0)
132 add_key_to_keylist(const char *key
,struct keylist
**list
)
134 struct keylist
*keyptr
=malloc(sizeof(struct keylist
));
138 fprintf(console
,"gpgkeys: out of memory when deduping "
140 return KEYSERVER_NO_MEMORY
;
143 strncpy(keyptr
->str
,key
,MAX_LINE
);
144 keyptr
->str
[MAX_LINE
-1]='\0';
152 free_keylist(struct keylist
*list
)
156 struct keylist
*keyptr
=list
;
164 ldap2epochtime(const char *timestr
)
169 memset(&pgptime
,0,sizeof(pgptime
));
171 /* YYYYMMDDHHmmssZ */
173 sscanf(timestr
,"%4d%2d%2d%2d%2d%2d",
181 pgptime
.tm_year
-=1900;
185 /* mktime() takes the timezone into account, so we use timegm() */
187 answer
=timegm(&pgptime
);
192 /* Caller must free */
194 epoch2ldaptime(time_t stamp
)
199 ldaptime
=gmtime(&stamp
);
201 ldaptime
->tm_year
+=1900;
204 /* YYYYMMDDHHmmssZ */
206 sprintf(buf
,"%04d%02d%02d%02d%02d%02dZ",
217 /* Append two onto the end of one. Two is not freed, but its pointers
218 are now part of one. Make sure you don't free them both! */
220 join_two_modlists(LDAPMod
***one
,LDAPMod
**two
)
222 int i
,one_count
=0,two_count
=0;
225 for(grow
=*one
;*grow
;grow
++)
228 for(grow
=two
;*grow
;grow
++)
231 grow
=realloc(*one
,sizeof(LDAPMod
*)*(one_count
+two_count
+1));
235 for(i
=0;i
<two_count
;i
++)
236 grow
[one_count
+i
]=two
[i
];
238 grow
[one_count
+i
]=NULL
;
245 /* Passing a NULL for value effectively deletes that attribute. This
246 doesn't mean "delete" in the sense of removing something from the
247 modlist, but "delete" in the LDAP sense of adding a modlist item
248 that specifies LDAP_MOD_REPLACE and a null attribute for the given
249 attribute. LDAP_MOD_DELETE doesn't work here as we don't know if
250 the attribute in question exists or not. */
253 make_one_attr(LDAPMod
***modlist
,char *attr
,const char *value
)
258 /* Search modlist for the attribute we're playing with. */
259 for(m
=*modlist
;*m
;m
++)
261 if(strcasecmp((*m
)->mod_type
,attr
)==0)
263 char **ptr
=(*m
)->mod_values
;
266 /* We have this attribute already, so when the REPLACE
267 happens, the server attributes will be replaced
273 for(ptr
=(*m
)->mod_values
;*ptr
;ptr
++)
275 /* Duplicate value */
276 if(strcmp(*ptr
,value
)==0)
281 ptr
=realloc((*m
)->mod_values
,sizeof(char *)*(numvalues
+2));
285 (*m
)->mod_values
=ptr
;
286 ptr
[numvalues
]=strdup(value
);
290 ptr
[numvalues
+1]=NULL
;
297 /* We didn't find the attr, so make one and add it to the end */
302 grow
=realloc(*modlist
,sizeof(LDAPMod
*)*(nummods
+2));
307 grow
[nummods
]=malloc(sizeof(LDAPMod
));
310 grow
[nummods
]->mod_op
=LDAP_MOD_REPLACE
;
311 grow
[nummods
]->mod_type
=attr
;
314 grow
[nummods
]->mod_values
=malloc(sizeof(char *)*2);
315 if(!grow
[nummods
]->mod_values
)
321 /* Is this the right thing? Can a UTF8-encoded user ID have
323 grow
[nummods
]->mod_values
[0]=strdup(value
);
324 if(!grow
[nummods
]->mod_values
[0])
326 free(grow
[nummods
]->mod_values
);
331 grow
[nummods
]->mod_values
[1]=NULL
;
334 grow
[nummods
]->mod_values
=NULL
;
336 grow
[nummods
+1]=NULL
;
343 build_attrs(LDAPMod
***modlist
,char *line
)
348 /* Remove trailing whitespace */
349 for(i
=strlen(line
);i
>0;i
--)
350 if(ascii_isspace(line
[i
-1]))
355 if((record
=strsep(&line
,":"))==NULL
)
358 if(ascii_strcasecmp("pub",record
)==0)
361 int disabled
=0,revoked
=0;
364 if((tok
=strsep(&line
,":"))==NULL
)
369 make_one_attr(modlist
,"pgpCertID",tok
);
370 make_one_attr(modlist
,"pgpKeyID",&tok
[8]);
375 /* The primary pubkey algo */
376 if((tok
=strsep(&line
,":"))==NULL
)
382 make_one_attr(modlist
,"pgpKeyType","RSA");
386 make_one_attr(modlist
,"pgpKeyType","DSS/DH");
390 /* Size of primary key */
391 if((tok
=strsep(&line
,":"))==NULL
)
399 /* We zero pad this on the left to make PGP happy. */
401 if(val
<99999 && val
>0)
403 sprintf(padded
,"%05u",atoi(tok
));
404 make_one_attr(modlist
,"pgpKeySize",padded
);
409 if((tok
=strsep(&line
,":"))==NULL
)
414 char *stamp
=epoch2ldaptime(atoi(tok
));
417 make_one_attr(modlist
,"pgpKeyCreateTime",stamp
);
423 if((tok
=strsep(&line
,":"))==NULL
)
428 char *stamp
=epoch2ldaptime(atoi(tok
));
431 make_one_attr(modlist
,"pgpKeyExpireTime",stamp
);
437 if((tok
=strsep(&line
,":"))==NULL
)
455 Note that we always create the pgpDisabled and pgpRevoked
456 attributes, regardless of whether the key is disabled/revoked
457 or not. This is because a very common search is like
458 "(&(pgpUserID=*isabella*)(pgpDisabled=0))"
461 make_one_attr(modlist
,"pgpDisabled",disabled
?"1":"0");
462 make_one_attr(modlist
,"pgpRevoked",revoked
?"1":"0");
464 else if(ascii_strcasecmp("sub",record
)==0)
469 if((tok
=strsep(&line
,":"))==NULL
)
473 make_one_attr(modlist
,"pgpSubKeyID",tok
);
477 /* The subkey algo */
478 if((tok
=strsep(&line
,":"))==NULL
)
482 if((tok
=strsep(&line
,":"))==NULL
)
490 /* We zero pad this on the left to make PGP happy. */
492 if(val
<99999 && val
>0)
494 sprintf(padded
,"%05u",atoi(tok
));
495 make_one_attr(modlist
,"pgpKeySize",padded
);
499 /* Ignore the rest of the items for subkeys since the LDAP
500 schema doesn't store them. */
502 else if(ascii_strcasecmp("uid",record
)==0)
506 /* The user ID string */
507 if((tok
=strsep(&line
,":"))==NULL
)
515 /* By definition, de-%-encoding is always smaller than the
516 original string so we can decode in place. */
521 if(tok
[0]=='%' && tok
[1] && tok
[2])
523 if((userid
[i
]=hextobyte(&tok
[1]))==-1)
534 /* We don't care about the other info provided in the uid: line
535 since the LDAP schema doesn't need it. */
537 make_one_attr(modlist
,"pgpUserID",userid
);
539 else if(ascii_strcasecmp("sig",record
)==0)
543 if((tok
=strsep(&line
,":"))==NULL
)
547 make_one_attr(modlist
,"pgpSignerID",tok
);
552 free_mod_values(LDAPMod
*mod
)
559 for(ptr
=mod
->mod_values
;*ptr
;ptr
++)
562 free(mod
->mod_values
);
568 int err
,begin
=0,end
=0,keysize
=1,ret
=KEYSERVER_INTERNAL_ERROR
;
569 char *dn
=NULL
,line
[MAX_LINE
],*key
=NULL
;
571 LDAPMod
**modlist
,**addlist
,**ml
;
573 modlist
=malloc(sizeof(LDAPMod
*));
576 fprintf(console
,"gpgkeys: can't allocate memory for keyserver record\n");
577 ret
=KEYSERVER_NO_MEMORY
;
583 addlist
=malloc(sizeof(LDAPMod
*));
586 fprintf(console
,"gpgkeys: can't allocate memory for keyserver record\n");
587 ret
=KEYSERVER_NO_MEMORY
;
593 /* Start by nulling out all attributes. We try and do a modify
594 operation first, so this ensures that we don't leave old
595 attributes lying around. */
596 make_one_attr(&modlist
,"pgpDisabled",NULL
);
597 make_one_attr(&modlist
,"pgpKeyID",NULL
);
598 make_one_attr(&modlist
,"pgpKeyType",NULL
);
599 make_one_attr(&modlist
,"pgpUserID",NULL
);
600 make_one_attr(&modlist
,"pgpKeyCreateTime",NULL
);
601 make_one_attr(&modlist
,"pgpSignerID",NULL
);
602 make_one_attr(&modlist
,"pgpRevoked",NULL
);
603 make_one_attr(&modlist
,"pgpSubKeyID",NULL
);
604 make_one_attr(&modlist
,"pgpKeySize",NULL
);
605 make_one_attr(&modlist
,"pgpKeyExpireTime",NULL
);
606 make_one_attr(&modlist
,"pgpCertID",NULL
);
608 /* Assemble the INFO stuff into LDAP attributes */
610 while(fgets(line
,MAX_LINE
,input
)!=NULL
)
611 if(sscanf(line
,"INFO %16s BEGIN\n",keyid
)==1)
619 /* i.e. eof before the INFO BEGIN was found. This isn't an
626 if(strlen(keyid
)!=16)
629 ret
=KEYSERVER_KEY_INCOMPLETE
;
633 dn
=malloc(strlen("pgpCertID=")+16+1+strlen(basekeyspacedn
)+1);
636 fprintf(console
,"gpgkeys: can't allocate memory for keyserver record\n");
637 ret
=KEYSERVER_NO_MEMORY
;
641 sprintf(dn
,"pgpCertID=%s,%s",keyid
,basekeyspacedn
);
646 fprintf(console
,"gpgkeys: unable to allocate memory for key\n");
647 ret
=KEYSERVER_NO_MEMORY
;
653 /* Now parse each line until we see the END */
655 while(fgets(line
,MAX_LINE
,input
)!=NULL
)
656 if(sscanf(line
,"INFO %16s END\n",keyid
)==1)
662 build_attrs(&addlist
,line
);
666 fprintf(console
,"gpgkeys: no INFO %s END found\n",keyid
);
668 ret
=KEYSERVER_KEY_INCOMPLETE
;
674 /* Read and throw away stdin until we see the BEGIN */
676 while(fgets(line
,MAX_LINE
,input
)!=NULL
)
677 if(sscanf(line
,"KEY %16s BEGIN\n",keyid
)==1)
685 /* i.e. eof before the KEY BEGIN was found. This isn't an
692 /* Now slurp up everything until we see the END */
694 while(fgets(line
,MAX_LINE
,input
)!=NULL
)
695 if(sscanf(line
,"KEY %16s END\n",keyid
)==1)
703 keysize
+=strlen(line
);
704 tempkey
=realloc(key
,keysize
);
707 fprintf(console
,"gpgkeys: unable to reallocate for key\n");
708 ret
=KEYSERVER_NO_MEMORY
;
719 fprintf(console
,"gpgkeys: no KEY %s END found\n",keyid
);
721 ret
=KEYSERVER_KEY_INCOMPLETE
;
725 make_one_attr(&addlist
,"objectClass","pgpKeyInfo");
726 make_one_attr(&addlist
,"pgpKey",key
);
728 /* Now append addlist onto modlist */
729 if(!join_two_modlists(&modlist
,addlist
))
731 fprintf(console
,"gpgkeys: unable to merge LDAP modification lists\n");
732 ret
=KEYSERVER_NO_MEMORY
;
736 /* Going on the assumption that modify operations are more frequent
737 than adds, we try a modify first. If it's not there, we just
738 turn around and send an add command for the same key. Otherwise,
739 the modify brings the server copy into compliance with our copy.
740 Note that unlike the LDAP keyserver (and really, any other
741 keyserver) this does NOT merge signatures, but replaces the whole
742 key. This should make some people very happy. */
744 err
=ldap_modify_s(ldap
,dn
,modlist
);
745 if(err
==LDAP_NO_SUCH_OBJECT
)
746 err
=ldap_add_s(ldap
,dn
,addlist
);
748 if(err
!=LDAP_SUCCESS
)
750 fprintf(console
,"gpgkeys: error adding key %s to keyserver: %s\n",
751 keyid
,ldap_err2string(err
));
752 ret
=ldap_err_to_gpg_err(err
);
759 /* Unwind and free the whole modlist structure */
760 for(ml
=modlist
;*ml
;ml
++)
762 free_mod_values(*ml
);
771 fprintf(output
,"KEY %s FAILED %d\n",keyid
,ret
);
777 send_key_keyserver(int *eof
)
779 int err
,begin
=0,end
=0,keysize
=1,ret
=KEYSERVER_INTERNAL_ERROR
;
780 char *dn
=NULL
,line
[MAX_LINE
],*key
[2]={NULL
,NULL
};
782 LDAPMod mod
, *attrs
[2];
784 memset(&mod
,0,sizeof(mod
));
785 mod
.mod_op
=LDAP_MOD_ADD
;
786 mod
.mod_type
=pgpkeystr
;
791 dn
=malloc(strlen("pgpCertid=virtual,")+strlen(basekeyspacedn
)+1);
794 fprintf(console
,"gpgkeys: can't allocate memory for keyserver record\n");
795 ret
=KEYSERVER_NO_MEMORY
;
799 strcpy(dn
,"pgpCertid=virtual,");
800 strcat(dn
,basekeyspacedn
);
805 fprintf(console
,"gpgkeys: unable to allocate memory for key\n");
806 ret
=KEYSERVER_NO_MEMORY
;
812 /* Read and throw away stdin until we see the BEGIN */
814 while(fgets(line
,MAX_LINE
,input
)!=NULL
)
815 if(sscanf(line
,"KEY %16s BEGIN\n",keyid
)==1)
823 /* i.e. eof before the KEY BEGIN was found. This isn't an
830 /* Now slurp up everything until we see the END */
832 while(fgets(line
,MAX_LINE
,input
)!=NULL
)
833 if(sscanf(line
,"KEY %16s END\n",keyid
)==1)
840 keysize
+=strlen(line
);
841 key
[0]=realloc(key
[0],keysize
);
844 fprintf(console
,"gpgkeys: unable to reallocate for key\n");
845 ret
=KEYSERVER_NO_MEMORY
;
854 fprintf(console
,"gpgkeys: no KEY %s END found\n",keyid
);
856 ret
=KEYSERVER_KEY_INCOMPLETE
;
860 err
=ldap_add_s(ldap
,dn
,attrs
);
861 if(err
!=LDAP_SUCCESS
)
863 fprintf(console
,"gpgkeys: error adding key %s to keyserver: %s\n",
864 keyid
,ldap_err2string(err
));
865 ret
=ldap_err_to_gpg_err(err
);
877 fprintf(output
,"KEY %s FAILED %d\n",keyid
,ret
);
879 /* Not a fatal error */
880 if(ret
==KEYSERVER_KEY_EXISTS
)
887 build_info(const char *certid
,LDAPMessage
*each
)
891 fprintf(output
,"INFO %s BEGIN\n",certid
);
893 fprintf(output
,"pub:%s:",certid
);
895 vals
=ldap_get_values(ldap
,each
,"pgpkeytype");
898 if(strcmp(vals
[0],"RSA")==0)
900 else if(strcmp(vals
[0],"DSS/DH")==0)
901 fprintf(output
,"17");
902 ldap_value_free(vals
);
907 vals
=ldap_get_values(ldap
,each
,"pgpkeysize");
911 fprintf(output
,"%d",atoi(vals
[0]));
912 ldap_value_free(vals
);
917 vals
=ldap_get_values(ldap
,each
,"pgpkeycreatetime");
920 if(strlen(vals
[0])==15)
921 fprintf(output
,"%u",(unsigned int)ldap2epochtime(vals
[0]));
922 ldap_value_free(vals
);
927 vals
=ldap_get_values(ldap
,each
,"pgpkeyexpiretime");
930 if(strlen(vals
[0])==15)
931 fprintf(output
,"%u",(unsigned int)ldap2epochtime(vals
[0]));
932 ldap_value_free(vals
);
937 vals
=ldap_get_values(ldap
,each
,"pgprevoked");
942 ldap_value_free(vals
);
945 fprintf(output
,"\n");
947 vals
=ldap_get_values(ldap
,each
,"pgpuserid");
953 fprintf(output
,"uid:%s\n",vals
[i
]);
954 ldap_value_free(vals
);
957 fprintf(output
,"INFO %s END\n",certid
);
960 /* Note that key-not-found is not a fatal error */
962 get_key(char *getkey
)
964 LDAPMessage
*res
,*each
;
965 int ret
=KEYSERVER_INTERNAL_ERROR
,err
,count
;
966 struct keylist
*dupelist
=NULL
;
968 /* This ordering is significant - specifically, "pgpcertid" needs to
969 be the second item in the list, since everything after it may be
970 discarded if the user isn't in verbose mode. */
971 char *attrs
[]={"replaceme","pgpcertid","pgpuserid","pgpkeyid","pgprevoked",
972 "pgpdisabled","pgpkeycreatetime","modifytimestamp",
973 "pgpkeysize","pgpkeytype",NULL
};
974 attrs
[0]=pgpkeystr
; /* Some compilers don't like using variables as
975 array initializers. */
977 /* Build the search string */
979 /* GPG can send us a v4 fingerprint, a v3 or v4 long key id, or a v3
980 or v4 short key id */
982 if(strncmp(getkey
,"0x",2)==0)
985 if(strlen(getkey
)==32)
988 "gpgkeys: LDAP keyservers do not support v3 fingerprints\n");
989 fprintf(output
,"KEY 0x%s BEGIN\n",getkey
);
990 fprintf(output
,"KEY 0x%s FAILED %d\n",getkey
,KEYSERVER_NOT_SUPPORTED
);
991 return KEYSERVER_NOT_SUPPORTED
;
994 if(strlen(getkey
)>16)
996 char *offset
=&getkey
[strlen(getkey
)-16];
998 /* fingerprint. Take the last 16 characters and treat it like a
1001 if(opt
->flags
.include_subkeys
)
1002 sprintf(search
,"(|(pgpcertid=%.16s)(pgpsubkeyid=%.16s))",
1005 sprintf(search
,"(pgpcertid=%.16s)",offset
);
1007 else if(strlen(getkey
)>8)
1011 if(opt
->flags
.include_subkeys
)
1012 sprintf(search
,"(|(pgpcertid=%.16s)(pgpsubkeyid=%.16s))",
1015 sprintf(search
,"(pgpcertid=%.16s)",getkey
);
1021 sprintf(search
,"(pgpkeyid=%.8s)",getkey
);
1025 fprintf(console
,"gpgkeys: LDAP fetch for: %s\n",search
);
1028 attrs
[2]=NULL
; /* keep only pgpkey(v2) and pgpcertid */
1030 err
=ldap_search_s(ldap
,basekeyspacedn
,
1031 LDAP_SCOPE_SUBTREE
,search
,attrs
,0,&res
);
1034 int errtag
=ldap_err_to_gpg_err(err
);
1036 fprintf(console
,"gpgkeys: LDAP search error: %s\n",ldap_err2string(err
));
1037 fprintf(output
,"KEY 0x%s BEGIN\n",getkey
);
1038 fprintf(output
,"KEY 0x%s FAILED %d\n",getkey
,errtag
);
1042 count
=ldap_count_entries(ldap
,res
);
1045 fprintf(console
,"gpgkeys: key %s not found on keyserver\n",getkey
);
1046 fprintf(output
,"KEY 0x%s BEGIN\n",getkey
);
1047 fprintf(output
,"KEY 0x%s FAILED %d\n",getkey
,KEYSERVER_KEY_NOT_FOUND
);
1051 /* There may be more than one unique result for a given keyID,
1052 so we should fetch them all (test this by fetching short key
1055 each
=ldap_first_entry(ldap
,res
);
1058 char **vals
,**certid
;
1060 /* Use the long keyid to remove duplicates. The LDAP server
1061 returns the same keyid more than once if there are
1062 multiple user IDs on the key. Note that this does NOT
1063 mean that a keyid that exists multiple times on the
1064 keyserver will not be fetched. It means that each KEY,
1065 no matter how many user IDs share its keyid, will be
1066 fetched only once. If a keyid that belongs to more than
1067 one key is fetched, the server quite properly responds
1068 with all matching keys. -ds */
1070 certid
=ldap_get_values(ldap
,each
,"pgpcertid");
1073 if(!key_in_keylist(certid
[0],dupelist
))
1075 /* it's not a duplicate, so add it */
1077 int rc
=add_key_to_keylist(certid
[0],&dupelist
);
1084 build_info(certid
[0],each
);
1086 fprintf(output
,"KEY 0x%s BEGIN\n",getkey
);
1088 vals
=ldap_get_values(ldap
,each
,pgpkeystr
);
1091 int errtag
=ldap_to_gpg_err(ldap
);
1093 fprintf(console
,"gpgkeys: unable to retrieve key %s "
1094 "from keyserver\n",getkey
);
1095 fprintf(output
,"KEY 0x%s FAILED %d\n",getkey
,errtag
);
1099 print_nocr(output
,vals
[0]);
1100 fprintf(output
,"\nKEY 0x%s END\n",getkey
);
1102 ldap_value_free(vals
);
1106 ldap_value_free(certid
);
1109 each
=ldap_next_entry(ldap
,each
);
1117 free_keylist(dupelist
);
1122 #define LDAP_ESCAPE_CHARS "*()\\"
1124 /* Append string to buffer in a LDAP-quoted way */
1126 ldap_quote(char *buffer
,const char *string
)
1128 /* Find the end of buffer */
1129 buffer
+=strlen(buffer
);
1131 for(;*string
;string
++)
1133 if(strchr(LDAP_ESCAPE_CHARS
,*string
))
1135 sprintf(buffer
,"\\%02X",*string
);
1145 /* Note that key-not-found is not a fatal error */
1147 get_name(char *getkey
)
1149 LDAPMessage
*res
,*each
;
1150 int ret
=KEYSERVER_INTERNAL_ERROR
,err
,count
;
1151 /* The maximum size of the search, including the optional stuff and
1153 char search
[2+12+(MAX_LINE
*3)+2+15+14+1+1+20];
1154 /* This ordering is significant - specifically, "pgpcertid" needs to
1155 be the second item in the list, since everything after it may be
1156 discarded if the user isn't in verbose mode. */
1157 char *attrs
[]={"replaceme","pgpcertid","pgpuserid","pgpkeyid","pgprevoked",
1158 "pgpdisabled","pgpkeycreatetime","modifytimestamp",
1159 "pgpkeysize","pgpkeytype",NULL
};
1160 attrs
[0]=pgpkeystr
; /* Some compilers don't like using variables as
1161 array initializers. */
1163 /* Build the search string */
1167 if(!opt
->flags
.include_disabled
|| !opt
->flags
.include_revoked
)
1168 strcat(search
,"(&");
1170 strcat(search
,"(pgpUserID=*");
1171 ldap_quote(search
,getkey
);
1172 strcat(search
,"*)");
1174 if(!opt
->flags
.include_disabled
)
1175 strcat(search
,"(pgpDisabled=0)");
1177 if(!opt
->flags
.include_revoked
)
1178 strcat(search
,"(pgpRevoked=0)");
1180 if(!opt
->flags
.include_disabled
|| !opt
->flags
.include_revoked
)
1184 fprintf(console
,"gpgkeys: LDAP fetch for: %s\n",search
);
1187 attrs
[2]=NULL
; /* keep only pgpkey(v2) and pgpcertid */
1189 err
=ldap_search_s(ldap
,basekeyspacedn
,
1190 LDAP_SCOPE_SUBTREE
,search
,attrs
,0,&res
);
1193 int errtag
=ldap_err_to_gpg_err(err
);
1195 fprintf(console
,"gpgkeys: LDAP search error: %s\n",ldap_err2string(err
));
1196 fprintf(output
,"NAME %s BEGIN\n",getkey
);
1197 fprintf(output
,"NAME %s FAILED %d\n",getkey
,errtag
);
1201 count
=ldap_count_entries(ldap
,res
);
1204 fprintf(console
,"gpgkeys: key %s not found on keyserver\n",getkey
);
1205 fprintf(output
,"NAME %s BEGIN\n",getkey
);
1206 fprintf(output
,"NAME %s FAILED %d\n",getkey
,KEYSERVER_KEY_NOT_FOUND
);
1210 /* There may be more than one result, but we return them all. */
1212 each
=ldap_first_entry(ldap
,res
);
1215 char **vals
,**certid
;
1217 certid
=ldap_get_values(ldap
,each
,"pgpcertid");
1220 build_info(certid
[0],each
);
1222 fprintf(output
,"NAME %s BEGIN\n",getkey
);
1224 vals
=ldap_get_values(ldap
,each
,pgpkeystr
);
1227 int errtag
=ldap_to_gpg_err(ldap
);
1229 fprintf(console
,"gpgkeys: unable to retrieve key %s "
1230 "from keyserver\n",getkey
);
1231 fprintf(output
,"NAME %s FAILED %d\n",getkey
,errtag
);
1235 print_nocr(output
,vals
[0]);
1236 fprintf(output
,"\nNAME %s END\n",getkey
);
1238 ldap_value_free(vals
);
1241 ldap_value_free(certid
);
1244 each
=ldap_next_entry(ldap
,each
);
1256 printquoted(FILE *stream
,char *string
,char delim
)
1260 if(*string
==delim
|| *string
=='%')
1261 fprintf(stream
,"%%%02x",*string
);
1263 fputc(*string
,stream
);
1269 /* Returns 0 on success and -1 on error. Note that key-not-found is
1272 search_key(const char *searchkey
)
1275 LDAPMessage
*res
,*each
;
1277 struct keylist
*dupelist
=NULL
;
1278 /* The maximum size of the search, including the optional stuff and
1280 char search
[2+1+9+1+3+(MAX_LINE
*3)+3+1+15+14+1+1+20];
1281 char *attrs
[]={"pgpcertid","pgpuserid","pgprevoked","pgpdisabled",
1282 "pgpkeycreatetime","pgpkeyexpiretime","modifytimestamp",
1283 "pgpkeysize","pgpkeytype",NULL
};
1284 enum ks_search_type search_type
;
1286 fprintf(output
,"SEARCH %s BEGIN\n",searchkey
);
1288 search_type
=classify_ks_search(&searchkey
);
1291 fprintf(console
,"search type is %d, and key is \"%s\"\n",
1292 search_type
,searchkey
);
1294 /* Build the search string */
1298 if(!opt
->flags
.include_disabled
|| !opt
->flags
.include_revoked
)
1299 strcat(search
,"(&");
1305 case KS_SEARCH_KEYID_SHORT
:
1306 strcat(search
,"pgpKeyID");
1309 case KS_SEARCH_KEYID_LONG
:
1310 strcat(search
,"pgpCertID");
1314 strcat(search
,"pgpUserID");
1322 case KS_SEARCH_SUBSTR
:
1326 case KS_SEARCH_MAIL
:
1327 strcat(search
,"*<");
1330 case KS_SEARCH_MAILSUB
:
1331 strcat(search
,"*<*");
1334 case KS_SEARCH_EXACT
:
1335 case KS_SEARCH_KEYID_LONG
:
1336 case KS_SEARCH_KEYID_SHORT
:
1340 ldap_quote(search
,searchkey
);
1344 case KS_SEARCH_SUBSTR
:
1348 case KS_SEARCH_MAIL
:
1349 strcat(search
,">*");
1352 case KS_SEARCH_MAILSUB
:
1353 strcat(search
,"*>*");
1356 case KS_SEARCH_EXACT
:
1357 case KS_SEARCH_KEYID_LONG
:
1358 case KS_SEARCH_KEYID_SHORT
:
1364 if(!opt
->flags
.include_disabled
)
1365 strcat(search
,"(pgpDisabled=0)");
1367 if(!opt
->flags
.include_revoked
)
1368 strcat(search
,"(pgpRevoked=0)");
1370 if(!opt
->flags
.include_disabled
|| !opt
->flags
.include_revoked
)
1374 fprintf(console
,"gpgkeys: LDAP search for: %s\n",search
);
1376 err
=ldap_search_s(ldap
,basekeyspacedn
,
1377 LDAP_SCOPE_SUBTREE
,search
,attrs
,0,&res
);
1378 if(err
!=LDAP_SUCCESS
&& err
!=LDAP_SIZELIMIT_EXCEEDED
)
1380 int errtag
=ldap_err_to_gpg_err(err
);
1382 fprintf(output
,"SEARCH %s FAILED %d\n",searchkey
,errtag
);
1383 fprintf(console
,"gpgkeys: LDAP search error: %s\n",ldap_err2string(err
));
1387 /* The LDAP server doesn't return a real count of unique keys, so we
1388 can't use ldap_count_entries here. */
1389 each
=ldap_first_entry(ldap
,res
);
1392 char **certid
=ldap_get_values(ldap
,each
,"pgpcertid");
1396 if(!key_in_keylist(certid
[0],dupelist
))
1398 int rc
=add_key_to_keylist(certid
[0],&dupelist
);
1401 fprintf(output
,"SEARCH %s FAILED %d\n",searchkey
,rc
);
1402 free_keylist(dupelist
);
1410 each
=ldap_next_entry(ldap
,each
);
1413 if(err
==LDAP_SIZELIMIT_EXCEEDED
)
1416 fprintf(console
,"gpgkeys: search results exceeded server limit."
1417 " First %d result shown.\n",count
);
1419 fprintf(console
,"gpgkeys: search results exceeded server limit."
1420 " First %d results shown.\n",count
);
1423 free_keylist(dupelist
);
1427 fprintf(output
,"info:1:0\n");
1430 fprintf(output
,"info:1:%d\n",count
);
1432 each
=ldap_first_entry(ldap
,res
);
1437 certid
=ldap_get_values(ldap
,each
,"pgpcertid");
1442 /* Have we seen this certid before? */
1443 if(!key_in_keylist(certid
[0],dupelist
))
1445 int rc
=add_key_to_keylist(certid
[0],&dupelist
);
1448 fprintf(output
,"SEARCH %s FAILED %d\n",searchkey
,rc
);
1449 free_keylist(dupelist
);
1450 ldap_value_free(certid
);
1455 fprintf(output
,"pub:%s:",certid
[0]);
1457 vals
=ldap_get_values(ldap
,each
,"pgpkeytype");
1460 /* The LDAP server doesn't exactly handle this
1462 if(strcasecmp(vals
[0],"RSA")==0)
1463 fprintf(output
,"1");
1464 else if(strcasecmp(vals
[0],"DSS/DH")==0)
1465 fprintf(output
,"17");
1466 ldap_value_free(vals
);
1471 vals
=ldap_get_values(ldap
,each
,"pgpkeysize");
1474 /* Not sure why, but some keys are listed with a
1475 key size of 0. Treat that like an
1478 fprintf(output
,"%d",atoi(vals
[0]));
1479 ldap_value_free(vals
);
1484 /* YYYYMMDDHHmmssZ */
1486 vals
=ldap_get_values(ldap
,each
,"pgpkeycreatetime");
1487 if(vals
!=NULL
&& strlen(vals
[0])==15)
1489 fprintf(output
,"%u",
1490 (unsigned int)ldap2epochtime(vals
[0]));
1491 ldap_value_free(vals
);
1496 vals
=ldap_get_values(ldap
,each
,"pgpkeyexpiretime");
1497 if(vals
!=NULL
&& strlen(vals
[0])==15)
1499 fprintf(output
,"%u",
1500 (unsigned int)ldap2epochtime(vals
[0]));
1501 ldap_value_free(vals
);
1506 vals
=ldap_get_values(ldap
,each
,"pgprevoked");
1509 if(atoi(vals
[0])==1)
1510 fprintf(output
,"r");
1511 ldap_value_free(vals
);
1514 vals
=ldap_get_values(ldap
,each
,"pgpdisabled");
1517 if(atoi(vals
[0])==1)
1518 fprintf(output
,"d");
1519 ldap_value_free(vals
);
1523 /* This is not yet specified in the keyserver
1524 protocol, but may be someday. */
1527 vals
=ldap_get_values(ldap
,each
,"modifytimestamp");
1528 if(vals
!=NULL
&& strlen(vals
[0])==15)
1530 fprintf(output
,"%u",
1531 (unsigned int)ldap2epochtime(vals
[0]));
1532 ldap_value_free(vals
);
1536 fprintf(output
,"\n");
1538 /* Now print all the uids that have this certid */
1539 uids
=ldap_first_entry(ldap
,res
);
1542 vals
=ldap_get_values(ldap
,uids
,"pgpcertid");
1545 if(strcasecmp(certid
[0],vals
[0])==0)
1549 fprintf(output
,"uid:");
1551 uidvals
=ldap_get_values(ldap
,uids
,"pgpuserid");
1554 /* Need to escape any colons */
1555 printquoted(output
,uidvals
[0],':');
1556 ldap_value_free(uidvals
);
1559 fprintf(output
,"\n");
1562 ldap_value_free(vals
);
1565 uids
=ldap_next_entry(ldap
,uids
);
1569 ldap_value_free(certid
);
1572 each
=ldap_next_entry(ldap
,each
);
1577 free_keylist(dupelist
);
1579 fprintf(output
,"SEARCH %s END\n",searchkey
);
1581 return KEYSERVER_OK
;
1585 fail_all(struct keylist
*keylist
,int err
)
1590 if(opt
->action
==KS_SEARCH
)
1592 fprintf(output
,"SEARCH ");
1595 fprintf(output
,"%s ",keylist
->str
);
1596 keylist
=keylist
->next
;
1598 fprintf(output
,"FAILED %d\n",err
);
1603 fprintf(output
,"KEY %s FAILED %d\n",keylist
->str
,err
);
1604 keylist
=keylist
->next
;
1609 find_basekeyspacedn(void)
1612 char *attr
[]={"namingContexts",NULL
,NULL
,NULL
};
1616 /* Look for namingContexts */
1617 err
=ldap_search_s(ldap
,"",LDAP_SCOPE_BASE
,"(objectClass=*)",attr
,0,&res
);
1618 if(err
==LDAP_SUCCESS
)
1620 context
=ldap_get_values(ldap
,res
,"namingContexts");
1623 attr
[0]="pgpBaseKeySpaceDN";
1624 attr
[1]="pgpVersion";
1625 attr
[2]="pgpSoftware";
1629 /* We found some, so try each namingContext as the search base
1630 and look for pgpBaseKeySpaceDN. Because we found this, we
1631 know we're talking to a regular-ish LDAP server and not a
1634 for(i
=0;context
[i
] && !basekeyspacedn
;i
++)
1637 LDAPMessage
*si_res
;
1640 object
=malloc(17+strlen(context
[i
])+1);
1644 strcpy(object
,"cn=pgpServerInfo,");
1645 strcat(object
,context
[i
]);
1647 err
=ldap_search_s(ldap
,object
,LDAP_SCOPE_BASE
,
1648 "(objectClass=*)",attr
,0,&si_res
);
1651 if(err
==LDAP_NO_SUCH_OBJECT
)
1653 else if(err
!=LDAP_SUCCESS
)
1656 vals
=ldap_get_values(ldap
,si_res
,"pgpBaseKeySpaceDN");
1659 basekeyspacedn
=strdup(vals
[0]);
1660 ldap_value_free(vals
);
1665 vals
=ldap_get_values(ldap
,si_res
,"pgpSoftware");
1668 fprintf(console
,"Server: \t%s\n",vals
[0]);
1669 ldap_value_free(vals
);
1672 vals
=ldap_get_values(ldap
,si_res
,"pgpVersion");
1675 fprintf(console
,"Version:\t%s\n",vals
[0]);
1676 ldap_value_free(vals
);
1680 ldap_msgfree(si_res
);
1683 ldap_value_free(context
);
1690 /* We don't have an answer yet, which means the server might be
1691 a LDAP keyserver. */
1693 LDAPMessage
*si_res
;
1695 attr
[0]="pgpBaseKeySpaceDN";
1699 err
=ldap_search_s(ldap
,"cn=pgpServerInfo",LDAP_SCOPE_BASE
,
1700 "(objectClass=*)",attr
,0,&si_res
);
1701 if(err
!=LDAP_SUCCESS
)
1704 /* For the LDAP keyserver, this is always "OU=ACTIVE,O=PGP
1705 KEYSPACE,C=US", but it might not be in the future. */
1707 vals
=ldap_get_values(ldap
,si_res
,"baseKeySpaceDN");
1710 basekeyspacedn
=strdup(vals
[0]);
1711 ldap_value_free(vals
);
1716 vals
=ldap_get_values(ldap
,si_res
,"software");
1719 fprintf(console
,"Server: \t%s\n",vals
[0]);
1720 ldap_value_free(vals
);
1724 vals
=ldap_get_values(ldap
,si_res
,"version");
1728 fprintf(console
,"Version:\t%s\n",vals
[0]);
1730 /* If the version is high enough, use the new pgpKeyV2
1731 attribute. This design if iffy at best, but it matches how
1732 PGP does it. I figure the NAI folks assumed that there would
1733 never be a LDAP keyserver vendor with a different numbering
1736 pgpkeystr
="pgpKeyV2";
1738 ldap_value_free(vals
);
1741 ldap_msgfree(si_res
);
1744 return LDAP_SUCCESS
;
1748 show_help (FILE *fp
)
1750 fprintf (fp
,"-h\thelp\n");
1751 fprintf (fp
,"-V\tversion\n");
1752 fprintf (fp
,"-o\toutput to this file\n");
1756 main(int argc
,char *argv
[])
1758 int port
=0,arg
,err
,ret
=KEYSERVER_INTERNAL_ERROR
;
1759 char line
[MAX_LINE
],*binddn
=NULL
,*bindpw
=NULL
;
1760 int failed
=0,use_ssl
=0,use_tls
=0,bound
=0;
1761 struct keylist
*keylist
=NULL
,*keyptr
=NULL
;
1765 /* Kludge to implement standard GNU options. */
1766 if (argc
> 1 && !strcmp (argv
[1], "--version"))
1768 fputs ("gpgkeys_ldap (GnuPG) " VERSION
"\n", stdout
);
1771 else if (argc
> 1 && !strcmp (argv
[1], "--help"))
1777 while((arg
=getopt(argc
,argv
,"hVo:"))!=-1)
1782 show_help (console
);
1783 return KEYSERVER_OK
;
1786 fprintf(stdout
,"%d\n%s\n",KEYSERVER_PROTO_VERSION
,VERSION
);
1787 return KEYSERVER_OK
;
1790 output
=fopen(optarg
,"w");
1793 fprintf(console
,"gpgkeys: Cannot open output file `%s': %s\n",
1794 optarg
,strerror(errno
));
1795 return KEYSERVER_INTERNAL_ERROR
;
1803 input
=fopen(argv
[optind
],"r");
1806 fprintf(console
,"gpgkeys: Cannot open input file `%s': %s\n",
1807 argv
[optind
],strerror(errno
));
1808 return KEYSERVER_INTERNAL_ERROR
;
1818 opt
=init_ks_options();
1820 return KEYSERVER_NO_MEMORY
;
1822 /* Get the command and info block */
1824 while(fgets(line
,MAX_LINE
,input
)!=NULL
)
1826 char optionstr
[MAX_OPTION
+1];
1831 err
=parse_ks_options(line
,opt
);
1840 if(sscanf(line
,"OPTION %" MKSTRING(MAX_OPTION
) "[^\n]\n",optionstr
)==1)
1843 char *start
=&optionstr
[0];
1845 optionstr
[MAX_OPTION
]='\0';
1847 if(strncasecmp(optionstr
,"no-",3)==0)
1850 start
=&optionstr
[3];
1853 if(strncasecmp(start
,"tls",3)==0)
1857 else if(start
[3]=='=')
1859 if(strcasecmp(&start
[4],"no")==0)
1861 else if(strcasecmp(&start
[4],"try")==0)
1863 else if(strcasecmp(&start
[4],"warn")==0)
1865 else if(strcasecmp(&start
[4],"require")==0)
1870 else if(start
[3]=='\0')
1873 else if(strncasecmp(start
,"basedn",6)==0)
1877 free(basekeyspacedn
);
1878 basekeyspacedn
=NULL
;
1880 else if(start
[6]=='=')
1882 free(basekeyspacedn
);
1883 basekeyspacedn
=strdup(&start
[7]);
1886 fprintf(console
,"gpgkeys: out of memory while creating "
1888 ret
=KEYSERVER_NO_MEMORY
;
1895 else if(strncasecmp(start
,"binddn",6)==0)
1902 else if(start
[6]=='=')
1905 binddn
=strdup(&start
[7]);
1908 fprintf(console
,"gpgkeys: out of memory while creating "
1910 ret
=KEYSERVER_NO_MEMORY
;
1917 else if(strncasecmp(start
,"bindpw",6)==0)
1924 else if(start
[6]=='=')
1927 bindpw
=strdup(&start
[7]);
1930 fprintf(console
,"gpgkeys: out of memory while creating "
1932 ret
=KEYSERVER_NO_MEMORY
;
1946 fprintf(console
,"gpgkeys: no scheme supplied!\n");
1947 ret
=KEYSERVER_SCHEME_NOT_FOUND
;
1951 if(strcasecmp(opt
->scheme
,"ldaps")==0)
1958 port
=atoi(opt
->port
);
1962 fprintf(console
,"gpgkeys: no keyserver host provided\n");
1966 if(opt
->timeout
&& register_timeout()==-1)
1968 fprintf(console
,"gpgkeys: unable to register timeout handler\n");
1969 return KEYSERVER_INTERNAL_ERROR
;
1972 #if defined(LDAP_OPT_X_TLS_CACERTFILE) && defined(HAVE_LDAP_SET_OPTION)
1974 if(opt
->ca_cert_file
)
1976 err
=ldap_set_option(NULL
,LDAP_OPT_X_TLS_CACERTFILE
,opt
->ca_cert_file
);
1977 if(err
!=LDAP_SUCCESS
)
1979 fprintf(console
,"gpgkeys: unable to set ca-cert-file: %s\n",
1980 ldap_err2string(err
));
1981 ret
=KEYSERVER_INTERNAL_ERROR
;
1985 #endif /* LDAP_OPT_X_TLS_CACERTFILE && HAVE_LDAP_SET_OPTION */
1987 /* SSL trumps TLS */
1991 /* If it's a GET or a SEARCH, the next thing to come in is the
1992 keyids. If it's a SEND, then there are no keyids. */
1994 if(opt
->action
==KS_SEND
)
1995 while(fgets(line
,MAX_LINE
,input
)!=NULL
&& line
[0]!='\n');
1996 else if(opt
->action
==KS_GET
1997 || opt
->action
==KS_GETNAME
|| opt
->action
==KS_SEARCH
)
2001 struct keylist
*work
;
2003 if(fgets(line
,MAX_LINE
,input
)==NULL
)
2007 if(line
[0]=='\n' || line
[0]=='\0')
2010 work
=malloc(sizeof(struct keylist
));
2013 fprintf(console
,"gpgkeys: out of memory while "
2014 "building key list\n");
2015 ret
=KEYSERVER_NO_MEMORY
;
2019 strcpy(work
->str
,line
);
2021 /* Trim the trailing \n */
2022 work
->str
[strlen(line
)-1]='\0';
2026 /* Always attach at the end to keep the list in proper
2027 order for searching */
2039 fprintf(console
,"gpgkeys: no keyserver command specified\n");
2043 /* Send the response */
2045 fprintf(output
,"VERSION %d\n",KEYSERVER_PROTO_VERSION
);
2046 fprintf(output
,"PROGRAM %s\n\n",VERSION
);
2050 fprintf(console
,"Host:\t\t%s\n",opt
->host
);
2052 fprintf(console
,"Port:\t\t%d\n",port
);
2053 fprintf(console
,"Command:\t%s\n",ks_action_to_string(opt
->action
));
2058 #if defined(LDAP_OPT_DEBUG_LEVEL) && defined(HAVE_LDAP_SET_OPTION)
2059 err
=ldap_set_option(NULL
,LDAP_OPT_DEBUG_LEVEL
,&opt
->debug
);
2060 if(err
!=LDAP_SUCCESS
)
2061 fprintf(console
,"gpgkeys: unable to set debug mode: %s\n",
2062 ldap_err2string(err
));
2064 fprintf(console
,"gpgkeys: debug level %d\n",opt
->debug
);
2066 fprintf(console
,"gpgkeys: not built with debugging support\n");
2070 /* We have a timeout set for the setup stuff since it could time out
2072 set_timeout(opt
->timeout
);
2074 /* Note that this tries all A records on a given host (or at least,
2076 ldap
=ldap_init(opt
->host
,port
);
2079 fprintf(console
,"gpgkeys: internal LDAP init error: %s\n",
2081 fail_all(keylist
,KEYSERVER_INTERNAL_ERROR
);
2087 #if defined(LDAP_OPT_X_TLS) && defined(HAVE_LDAP_SET_OPTION)
2088 int ssl
=LDAP_OPT_X_TLS_HARD
;
2090 err
=ldap_set_option(ldap
,LDAP_OPT_X_TLS
,&ssl
);
2091 if(err
!=LDAP_SUCCESS
)
2093 fprintf(console
,"gpgkeys: unable to make SSL connection: %s\n",
2094 ldap_err2string(err
));
2095 fail_all(keylist
,ldap_err_to_gpg_err(err
));
2099 if(!opt
->flags
.check_cert
)
2100 ssl
=LDAP_OPT_X_TLS_NEVER
;
2102 err
=ldap_set_option(NULL
,LDAP_OPT_X_TLS_REQUIRE_CERT
,&ssl
);
2103 if(err
!=LDAP_SUCCESS
)
2106 "gpgkeys: unable to set certificate validation: %s\n",
2107 ldap_err2string(err
));
2108 fail_all(keylist
,ldap_err_to_gpg_err(err
));
2112 fprintf(console
,"gpgkeys: unable to make SSL connection: %s\n",
2113 "not built with LDAPS support");
2114 fail_all(keylist
,KEYSERVER_INTERNAL_ERROR
);
2120 if((err
=find_basekeyspacedn()) || !basekeyspacedn
)
2122 fprintf(console
,"gpgkeys: unable to retrieve LDAP base: %s\n",
2123 err
?ldap_err2string(err
):"not found");
2124 fail_all(keylist
,ldap_err_to_gpg_err(err
));
2128 /* use_tls: 0=don't use, 1=try silently to use, 2=try loudly to use,
2135 fprintf(console
,"gpgkeys: unable to start TLS: %s\n",
2136 "not supported by the NAI LDAP keyserver");
2139 fail_all(keylist
,KEYSERVER_INTERNAL_ERROR
);
2145 #if defined(HAVE_LDAP_START_TLS_S) && defined(HAVE_LDAP_SET_OPTION)
2146 int ver
=LDAP_VERSION3
;
2148 err
=ldap_set_option(ldap
,LDAP_OPT_PROTOCOL_VERSION
,&ver
);
2150 #ifdef LDAP_OPT_X_TLS
2151 if(err
==LDAP_SUCCESS
)
2153 if(opt
->flags
.check_cert
)
2154 ver
=LDAP_OPT_X_TLS_HARD
;
2156 ver
=LDAP_OPT_X_TLS_NEVER
;
2158 err
=ldap_set_option(ldap
,LDAP_OPT_X_TLS_REQUIRE_CERT
,&ver
);
2162 if(err
==LDAP_SUCCESS
)
2163 err
=ldap_start_tls_s(ldap
,NULL
,NULL
);
2165 if(err
!=LDAP_SUCCESS
)
2167 if(use_tls
>=2 || opt
->verbose
>2)
2168 fprintf(console
,"gpgkeys: unable to start TLS: %s\n",
2169 ldap_err2string(err
));
2170 /* Are we forcing it? */
2173 fail_all(keylist
,ldap_err_to_gpg_err(err
));
2177 else if(opt
->verbose
>1)
2178 fprintf(console
,"gpgkeys: TLS started successfully.\n");
2181 fprintf(console
,"gpgkeys: unable to start TLS: %s\n",
2182 "not built with TLS support");
2185 fail_all(keylist
,KEYSERVER_INTERNAL_ERROR
);
2192 /* By default we don't bind as there is usually no need to. For
2193 cases where the server needs some authentication, the user can
2194 use binddn and bindpw for auth. */
2198 #ifdef HAVE_LDAP_SET_OPTION
2199 int ver
=LDAP_VERSION3
;
2201 err
=ldap_set_option(ldap
,LDAP_OPT_PROTOCOL_VERSION
,&ver
);
2202 if(err
!=LDAP_SUCCESS
)
2204 fprintf(console
,"gpgkeys: unable to go to LDAP 3: %s\n",
2205 ldap_err2string(err
));
2206 fail_all(keylist
,ldap_err_to_gpg_err(err
));
2212 fprintf(console
,"gpgkeys: LDAP bind to %s, pw %s\n",binddn
,
2213 bindpw
?">not shown<":">none<");
2214 err
=ldap_simple_bind_s(ldap
,binddn
,bindpw
);
2215 if(err
!=LDAP_SUCCESS
)
2217 fprintf(console
,"gpgkeys: internal LDAP bind error: %s\n",
2218 ldap_err2string(err
));
2219 fail_all(keylist
,ldap_err_to_gpg_err(err
));
2226 if(opt
->action
==KS_GET
)
2232 set_timeout(opt
->timeout
);
2234 if(get_key(keyptr
->str
)!=KEYSERVER_OK
)
2237 keyptr
=keyptr
->next
;
2240 else if(opt
->action
==KS_GETNAME
)
2246 set_timeout(opt
->timeout
);
2248 if(get_name(keyptr
->str
)!=KEYSERVER_OK
)
2251 keyptr
=keyptr
->next
;
2254 else if(opt
->action
==KS_SEND
)
2260 set_timeout(opt
->timeout
);
2264 if(send_key(&eof
)!=KEYSERVER_OK
)
2269 if(send_key_keyserver(&eof
)!=KEYSERVER_OK
)
2275 else if(opt
->action
==KS_SEARCH
)
2277 char *searchkey
=NULL
;
2280 set_timeout(opt
->timeout
);
2282 /* To search, we stick a * in between each key to search for.
2283 This means that if the user enters words, they'll get
2284 "enters*words". If the user "enters words", they'll get
2290 len
+=strlen(keyptr
->str
)+1;
2291 keyptr
=keyptr
->next
;
2294 searchkey
=malloc(len
+1);
2297 ret
=KEYSERVER_NO_MEMORY
;
2298 fail_all(keylist
,KEYSERVER_NO_MEMORY
);
2307 strcat(searchkey
,keyptr
->str
);
2308 strcat(searchkey
,"*");
2309 keyptr
=keyptr
->next
;
2312 /* Nail that last "*" */
2314 searchkey
[strlen(searchkey
)-1]='\0';
2316 if(search_key(searchkey
)!=KEYSERVER_OK
)
2329 while(keylist
!=NULL
)
2331 struct keylist
*current
=keylist
;
2332 keylist
=keylist
->next
;
2342 free_ks_options(opt
);
2344 if(ldap
!=NULL
&& bound
)
2345 ldap_unbind_s(ldap
);
2347 free(basekeyspacedn
);