1 /* gpgkeys_ldap.c - talk to a LDAP keyserver
2 * Copyright (C) 2001, 2002, 2004, 2005, 2006
3 * 2007 Free Software Foundation, Inc.
5 * This file is part of GnuPG.
7 * GnuPG is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 3 of the License, or
10 * (at your option) any later version.
12 * GnuPG is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, see <http://www.gnu.org/licenses/>.
20 * In addition, as a special exception, the Free Software Foundation
21 * gives permission to link the code of the keyserver helper tools:
22 * gpgkeys_ldap, gpgkeys_curl and gpgkeys_hkp with the OpenSSL
23 * project's "OpenSSL" library (or with modified versions of it that
24 * use the same license as the "OpenSSL" library), and distribute the
25 * linked executables. You must obey the GNU General Public License
26 * in all respects for all of the code used other than "OpenSSL". If
27 * you modify this file, you may extend this exception to your version
28 * of the file, but you are not obligated to do so. If you do not
29 * wish to do so, delete this exception statement from your version.
51 /* For OpenLDAP, to enable the API that we're using. */
52 #define LDAP_DEPRECATED 1
57 #include "keyserver.h"
67 static int real_ldap
=0;
68 static char *basekeyspacedn
=NULL
;
69 static char *pgpkeystr
="pgpKey";
70 static FILE *input
=NULL
,*output
=NULL
,*console
=NULL
;
71 static LDAP
*ldap
=NULL
;
72 static struct ks_options
*opt
;
75 time_t timegm(struct tm
*tm
);
79 ldap_err_to_gpg_err(int err
)
85 case LDAP_ALREADY_EXISTS
:
86 ret
=KEYSERVER_KEY_EXISTS
;
89 case LDAP_SERVER_DOWN
:
90 ret
=KEYSERVER_UNREACHABLE
;
94 ret
=KEYSERVER_GENERAL_ERROR
;
102 ldap_to_gpg_err(LDAP
*ld
)
104 #if defined(HAVE_LDAP_GET_OPTION) && defined(LDAP_OPT_ERROR_NUMBER)
108 if(ldap_get_option(ld
,LDAP_OPT_ERROR_NUMBER
,&err
)==0)
109 return ldap_err_to_gpg_err(err
);
111 return KEYSERVER_GENERAL_ERROR
;
113 #elif defined(HAVE_LDAP_LD_ERRNO)
115 return ldap_err_to_gpg_err(ld
->ld_errno
);
119 /* We should never get here since the LDAP library should always
120 have either ldap_get_option or ld_errno, but just in case... */
121 return KEYSERVER_GENERAL_ERROR
;
127 key_in_keylist(const char *key
,struct keylist
*list
)
129 struct keylist
*keyptr
=list
;
133 if(strcasecmp(key
,keyptr
->str
)==0)
143 add_key_to_keylist(const char *key
,struct keylist
**list
)
145 struct keylist
*keyptr
=malloc(sizeof(struct keylist
));
149 fprintf(console
,"gpgkeys: out of memory when deduping "
151 return KEYSERVER_NO_MEMORY
;
154 strncpy(keyptr
->str
,key
,MAX_LINE
);
155 keyptr
->str
[MAX_LINE
-1]='\0';
163 free_keylist(struct keylist
*list
)
167 struct keylist
*keyptr
=list
;
175 ldap2epochtime(const char *timestr
)
180 memset(&pgptime
,0,sizeof(pgptime
));
182 /* YYYYMMDDHHmmssZ */
184 sscanf(timestr
,"%4d%2d%2d%2d%2d%2d",
192 pgptime
.tm_year
-=1900;
196 /* mktime() takes the timezone into account, so we use timegm() */
198 answer
=timegm(&pgptime
);
203 /* Caller must free */
205 epoch2ldaptime(time_t stamp
)
210 ldaptime
=gmtime(&stamp
);
212 ldaptime
->tm_year
+=1900;
215 /* YYYYMMDDHHmmssZ */
217 sprintf(buf
,"%04d%02d%02d%02d%02d%02dZ",
228 /* Append two onto the end of one. Two is not freed, but its pointers
229 are now part of one. Make sure you don't free them both! */
231 join_two_modlists(LDAPMod
***one
,LDAPMod
**two
)
233 int i
,one_count
=0,two_count
=0;
236 for(grow
=*one
;*grow
;grow
++)
239 for(grow
=two
;*grow
;grow
++)
242 grow
=realloc(*one
,sizeof(LDAPMod
*)*(one_count
+two_count
+1));
246 for(i
=0;i
<two_count
;i
++)
247 grow
[one_count
+i
]=two
[i
];
249 grow
[one_count
+i
]=NULL
;
256 /* Passing a NULL for value effectively deletes that attribute. This
257 doesn't mean "delete" in the sense of removing something from the
258 modlist, but "delete" in the LDAP sense of adding a modlist item
259 that specifies LDAP_MOD_REPLACE and a null attribute for the given
260 attribute. LDAP_MOD_DELETE doesn't work here as we don't know if
261 the attribute in question exists or not. */
264 make_one_attr(LDAPMod
***modlist
,char *attr
,const char *value
)
269 /* Search modlist for the attribute we're playing with. */
270 for(m
=*modlist
;*m
;m
++)
272 if(strcasecmp((*m
)->mod_type
,attr
)==0)
274 char **ptr
=(*m
)->mod_values
;
277 /* We have this attribute already, so when the REPLACE
278 happens, the server attributes will be replaced
284 for(ptr
=(*m
)->mod_values
;*ptr
;ptr
++)
286 /* Duplicate value */
287 if(strcmp(*ptr
,value
)==0)
292 ptr
=realloc((*m
)->mod_values
,sizeof(char *)*(numvalues
+2));
296 (*m
)->mod_values
=ptr
;
297 ptr
[numvalues
]=strdup(value
);
301 ptr
[numvalues
+1]=NULL
;
308 /* We didn't find the attr, so make one and add it to the end */
313 grow
=realloc(*modlist
,sizeof(LDAPMod
*)*(nummods
+2));
318 grow
[nummods
]=malloc(sizeof(LDAPMod
));
321 grow
[nummods
]->mod_op
=LDAP_MOD_REPLACE
;
322 grow
[nummods
]->mod_type
=attr
;
325 grow
[nummods
]->mod_values
=malloc(sizeof(char *)*2);
326 if(!grow
[nummods
]->mod_values
)
332 /* Is this the right thing? Can a UTF8-encoded user ID have
334 grow
[nummods
]->mod_values
[0]=strdup(value
);
335 if(!grow
[nummods
]->mod_values
[0])
337 free(grow
[nummods
]->mod_values
);
342 grow
[nummods
]->mod_values
[1]=NULL
;
345 grow
[nummods
]->mod_values
=NULL
;
347 grow
[nummods
+1]=NULL
;
354 build_attrs(LDAPMod
***modlist
,char *line
)
359 /* Remove trailing whitespace */
360 for(i
=strlen(line
);i
>0;i
--)
361 if(ascii_isspace(line
[i
-1]))
366 if((record
=strsep(&line
,":"))==NULL
)
369 if(ks_strcasecmp("pub",record
)==0)
372 int disabled
=0,revoked
=0;
375 if((tok
=strsep(&line
,":"))==NULL
)
380 make_one_attr(modlist
,"pgpCertID",tok
);
381 make_one_attr(modlist
,"pgpKeyID",&tok
[8]);
386 /* The primary pubkey algo */
387 if((tok
=strsep(&line
,":"))==NULL
)
393 make_one_attr(modlist
,"pgpKeyType","RSA");
397 make_one_attr(modlist
,"pgpKeyType","DSS/DH");
401 /* Size of primary key */
402 if((tok
=strsep(&line
,":"))==NULL
)
410 /* We zero pad this on the left to make PGP happy. */
412 if(val
<99999 && val
>0)
414 sprintf(padded
,"%05u",atoi(tok
));
415 make_one_attr(modlist
,"pgpKeySize",padded
);
420 if((tok
=strsep(&line
,":"))==NULL
)
425 char *stamp
=epoch2ldaptime(atoi(tok
));
428 make_one_attr(modlist
,"pgpKeyCreateTime",stamp
);
434 if((tok
=strsep(&line
,":"))==NULL
)
439 char *stamp
=epoch2ldaptime(atoi(tok
));
442 make_one_attr(modlist
,"pgpKeyExpireTime",stamp
);
448 if((tok
=strsep(&line
,":"))==NULL
)
466 Note that we always create the pgpDisabled and pgpRevoked
467 attributes, regardless of whether the key is disabled/revoked
468 or not. This is because a very common search is like
469 "(&(pgpUserID=*isabella*)(pgpDisabled=0))"
472 make_one_attr(modlist
,"pgpDisabled",disabled
?"1":"0");
473 make_one_attr(modlist
,"pgpRevoked",revoked
?"1":"0");
475 else if(ks_strcasecmp("sub",record
)==0)
480 if((tok
=strsep(&line
,":"))==NULL
)
484 make_one_attr(modlist
,"pgpSubKeyID",tok
);
488 /* The subkey algo */
489 if((tok
=strsep(&line
,":"))==NULL
)
493 if((tok
=strsep(&line
,":"))==NULL
)
501 /* We zero pad this on the left to make PGP happy. */
503 if(val
<99999 && val
>0)
505 sprintf(padded
,"%05u",atoi(tok
));
506 make_one_attr(modlist
,"pgpKeySize",padded
);
510 /* Ignore the rest of the items for subkeys since the LDAP
511 schema doesn't store them. */
513 else if(ks_strcasecmp("uid",record
)==0)
517 /* The user ID string */
518 if((tok
=strsep(&line
,":"))==NULL
)
526 /* By definition, de-%-encoding is always smaller than the
527 original string so we can decode in place. */
532 if(tok
[0]=='%' && tok
[1] && tok
[2])
536 userid
[i
] = (c
=hextobyte(&tok
[1])) == -1 ? '?' : c
;
545 /* We don't care about the other info provided in the uid: line
546 since the LDAP schema doesn't need it. */
548 make_one_attr(modlist
,"pgpUserID",userid
);
550 else if(ks_strcasecmp("sig",record
)==0)
554 if((tok
=strsep(&line
,":"))==NULL
)
558 make_one_attr(modlist
,"pgpSignerID",tok
);
563 free_mod_values(LDAPMod
*mod
)
570 for(ptr
=mod
->mod_values
;*ptr
;ptr
++)
573 free(mod
->mod_values
);
579 int err
,begin
=0,end
=0,keysize
=1,ret
=KEYSERVER_INTERNAL_ERROR
;
580 char *dn
=NULL
,line
[MAX_LINE
],*key
=NULL
;
581 char keyid
[17],state
[6];
582 LDAPMod
**modlist
,**addlist
,**ml
;
584 modlist
=malloc(sizeof(LDAPMod
*));
587 fprintf(console
,"gpgkeys: can't allocate memory for keyserver record\n");
588 ret
=KEYSERVER_NO_MEMORY
;
594 addlist
=malloc(sizeof(LDAPMod
*));
597 fprintf(console
,"gpgkeys: can't allocate memory for keyserver record\n");
598 ret
=KEYSERVER_NO_MEMORY
;
604 /* Start by nulling out all attributes. We try and do a modify
605 operation first, so this ensures that we don't leave old
606 attributes lying around. */
607 make_one_attr(&modlist
,"pgpDisabled",NULL
);
608 make_one_attr(&modlist
,"pgpKeyID",NULL
);
609 make_one_attr(&modlist
,"pgpKeyType",NULL
);
610 make_one_attr(&modlist
,"pgpUserID",NULL
);
611 make_one_attr(&modlist
,"pgpKeyCreateTime",NULL
);
612 make_one_attr(&modlist
,"pgpSignerID",NULL
);
613 make_one_attr(&modlist
,"pgpRevoked",NULL
);
614 make_one_attr(&modlist
,"pgpSubKeyID",NULL
);
615 make_one_attr(&modlist
,"pgpKeySize",NULL
);
616 make_one_attr(&modlist
,"pgpKeyExpireTime",NULL
);
617 make_one_attr(&modlist
,"pgpCertID",NULL
);
619 /* Assemble the INFO stuff into LDAP attributes */
621 while(fgets(line
,MAX_LINE
,input
)!=NULL
)
622 if(sscanf(line
,"INFO%*[ ]%16s%*[ ]%5s\n",keyid
,state
)==2
623 && strcmp(state
,"BEGIN")==0)
631 /* i.e. eof before the INFO BEGIN was found. This isn't an
638 if(strlen(keyid
)!=16)
641 ret
=KEYSERVER_KEY_INCOMPLETE
;
645 dn
=malloc(strlen("pgpCertID=")+16+1+strlen(basekeyspacedn
)+1);
648 fprintf(console
,"gpgkeys: can't allocate memory for keyserver record\n");
649 ret
=KEYSERVER_NO_MEMORY
;
653 sprintf(dn
,"pgpCertID=%s,%s",keyid
,basekeyspacedn
);
658 fprintf(console
,"gpgkeys: unable to allocate memory for key\n");
659 ret
=KEYSERVER_NO_MEMORY
;
665 /* Now parse each line until we see the END */
667 while(fgets(line
,MAX_LINE
,input
)!=NULL
)
668 if(sscanf(line
,"INFO%*[ ]%16s%*[ ]%3s\n",keyid
,state
)==2
669 && strcmp(state
,"END")==0)
675 build_attrs(&addlist
,line
);
679 fprintf(console
,"gpgkeys: no INFO %s END found\n",keyid
);
681 ret
=KEYSERVER_KEY_INCOMPLETE
;
687 /* Read and throw away stdin until we see the BEGIN */
689 while(fgets(line
,MAX_LINE
,input
)!=NULL
)
690 if(sscanf(line
,"KEY%*[ ]%16s%*[ ]%5s\n",keyid
,state
)==2
691 && strcmp(state
,"BEGIN")==0)
699 /* i.e. eof before the KEY BEGIN was found. This isn't an
706 /* Now slurp up everything until we see the END */
708 while(fgets(line
,MAX_LINE
,input
)!=NULL
)
709 if(sscanf(line
,"KEY%*[ ]%16s%*[ ]%3s\n",keyid
,state
)==2
710 && strcmp(state
,"END")==0)
718 keysize
+=strlen(line
);
719 tempkey
=realloc(key
,keysize
);
722 fprintf(console
,"gpgkeys: unable to reallocate for key\n");
723 ret
=KEYSERVER_NO_MEMORY
;
734 fprintf(console
,"gpgkeys: no KEY %s END found\n",keyid
);
736 ret
=KEYSERVER_KEY_INCOMPLETE
;
740 make_one_attr(&addlist
,"objectClass","pgpKeyInfo");
741 make_one_attr(&addlist
,"pgpKey",key
);
743 /* Now append addlist onto modlist */
744 if(!join_two_modlists(&modlist
,addlist
))
746 fprintf(console
,"gpgkeys: unable to merge LDAP modification lists\n");
747 ret
=KEYSERVER_NO_MEMORY
;
751 /* Going on the assumption that modify operations are more frequent
752 than adds, we try a modify first. If it's not there, we just
753 turn around and send an add command for the same key. Otherwise,
754 the modify brings the server copy into compliance with our copy.
755 Note that unlike the LDAP keyserver (and really, any other
756 keyserver) this does NOT merge signatures, but replaces the whole
757 key. This should make some people very happy. */
759 err
=ldap_modify_s(ldap
,dn
,modlist
);
760 if(err
==LDAP_NO_SUCH_OBJECT
)
761 err
=ldap_add_s(ldap
,dn
,addlist
);
763 if(err
!=LDAP_SUCCESS
)
765 fprintf(console
,"gpgkeys: error adding key %s to keyserver: %s\n",
766 keyid
,ldap_err2string(err
));
767 ret
=ldap_err_to_gpg_err(err
);
776 /* Unwind and free the whole modlist structure */
777 for(ml
=modlist
;*ml
;ml
++)
779 free_mod_values(*ml
);
789 fprintf(output
,"KEY %s FAILED %d\n",keyid
,ret
);
795 send_key_keyserver(int *r_eof
)
797 int err
,begin
=0,end
=0,keysize
=1,ret
=KEYSERVER_INTERNAL_ERROR
;
798 char *dn
=NULL
,line
[MAX_LINE
],*key
[2]={NULL
,NULL
};
799 char keyid
[17],state
[6];
800 LDAPMod mod
, *attrs
[2];
802 memset(&mod
,0,sizeof(mod
));
803 mod
.mod_op
=LDAP_MOD_ADD
;
804 mod
.mod_type
=pgpkeystr
;
809 dn
=malloc(strlen("pgpCertid=virtual,")+strlen(basekeyspacedn
)+1);
812 fprintf(console
,"gpgkeys: can't allocate memory for keyserver record\n");
813 ret
=KEYSERVER_NO_MEMORY
;
817 strcpy(dn
,"pgpCertid=virtual,");
818 strcat(dn
,basekeyspacedn
);
823 fprintf(console
,"gpgkeys: unable to allocate memory for key\n");
824 ret
=KEYSERVER_NO_MEMORY
;
830 /* Read and throw away stdin until we see the BEGIN */
832 while(fgets(line
,MAX_LINE
,input
)!=NULL
)
833 if(sscanf(line
,"KEY%*[ ]%16s%*[ ]%5s\n",keyid
,state
)==2
834 && strcmp(state
,"BEGIN")==0)
842 /* i.e. eof before the KEY BEGIN was found. This isn't an
849 /* Now slurp up everything until we see the END */
851 while(fgets(line
,MAX_LINE
,input
)!=NULL
)
852 if(sscanf(line
,"KEY%*[ ]%16s%*[ ]%3s\n",keyid
,state
)==2
853 && strcmp(state
,"END")==0)
860 keysize
+=strlen(line
);
861 key
[0]=realloc(key
[0],keysize
);
864 fprintf(console
,"gpgkeys: unable to reallocate for key\n");
865 ret
=KEYSERVER_NO_MEMORY
;
874 fprintf(console
,"gpgkeys: no KEY %s END found\n",keyid
);
876 ret
=KEYSERVER_KEY_INCOMPLETE
;
880 err
=ldap_add_s(ldap
,dn
,attrs
);
881 if(err
!=LDAP_SUCCESS
)
883 fprintf(console
,"gpgkeys: error adding key %s to keyserver: %s\n",
884 keyid
,ldap_err2string(err
));
885 ret
=ldap_err_to_gpg_err(err
);
897 fprintf(output
,"KEY %s FAILED %d\n",keyid
,ret
);
899 /* Not a fatal error */
900 if(ret
==KEYSERVER_KEY_EXISTS
)
907 build_info(const char *certid
,LDAPMessage
*each
)
911 fprintf(output
,"INFO %s BEGIN\n",certid
);
913 fprintf(output
,"pub:%s:",certid
);
915 vals
=ldap_get_values(ldap
,each
,"pgpkeytype");
918 if(strcmp(vals
[0],"RSA")==0)
920 else if(strcmp(vals
[0],"DSS/DH")==0)
921 fprintf(output
,"17");
922 ldap_value_free(vals
);
927 vals
=ldap_get_values(ldap
,each
,"pgpkeysize");
931 fprintf(output
,"%d",atoi(vals
[0]));
932 ldap_value_free(vals
);
937 vals
=ldap_get_values(ldap
,each
,"pgpkeycreatetime");
940 if(strlen(vals
[0])==15)
941 fprintf(output
,"%u",(unsigned int)ldap2epochtime(vals
[0]));
942 ldap_value_free(vals
);
947 vals
=ldap_get_values(ldap
,each
,"pgpkeyexpiretime");
950 if(strlen(vals
[0])==15)
951 fprintf(output
,"%u",(unsigned int)ldap2epochtime(vals
[0]));
952 ldap_value_free(vals
);
957 vals
=ldap_get_values(ldap
,each
,"pgprevoked");
962 ldap_value_free(vals
);
965 fprintf(output
,"\n");
967 vals
=ldap_get_values(ldap
,each
,"pgpuserid");
973 fprintf(output
,"uid:%s\n",vals
[i
]);
974 ldap_value_free(vals
);
977 fprintf(output
,"INFO %s END\n",certid
);
980 /* Note that key-not-found is not a fatal error */
982 get_key(char *getkey
)
984 LDAPMessage
*res
,*each
;
985 int ret
=KEYSERVER_INTERNAL_ERROR
,err
,count
;
986 struct keylist
*dupelist
=NULL
;
988 /* This ordering is significant - specifically, "pgpcertid" needs to
989 be the second item in the list, since everything after it may be
990 discarded if the user isn't in verbose mode. */
991 char *attrs
[]={"replaceme","pgpcertid","pgpuserid","pgpkeyid","pgprevoked",
992 "pgpdisabled","pgpkeycreatetime","modifytimestamp",
993 "pgpkeysize","pgpkeytype",NULL
};
994 attrs
[0]=pgpkeystr
; /* Some compilers don't like using variables as
995 array initializers. */
997 /* Build the search string */
999 /* GPG can send us a v4 fingerprint, a v3 or v4 long key id, or a v3
1000 or v4 short key id */
1002 if(strncmp(getkey
,"0x",2)==0)
1005 if(strlen(getkey
)==32)
1008 "gpgkeys: LDAP keyservers do not support v3 fingerprints\n");
1009 fprintf(output
,"KEY 0x%s BEGIN\n",getkey
);
1010 fprintf(output
,"KEY 0x%s FAILED %d\n",getkey
,KEYSERVER_NOT_SUPPORTED
);
1011 return KEYSERVER_NOT_SUPPORTED
;
1014 if(strlen(getkey
)>16)
1016 char *offset
=&getkey
[strlen(getkey
)-16];
1018 /* fingerprint. Take the last 16 characters and treat it like a
1021 if(opt
->flags
.include_subkeys
)
1022 sprintf(search
,"(|(pgpcertid=%.16s)(pgpsubkeyid=%.16s))",
1025 sprintf(search
,"(pgpcertid=%.16s)",offset
);
1027 else if(strlen(getkey
)>8)
1031 if(opt
->flags
.include_subkeys
)
1032 sprintf(search
,"(|(pgpcertid=%.16s)(pgpsubkeyid=%.16s))",
1035 sprintf(search
,"(pgpcertid=%.16s)",getkey
);
1041 sprintf(search
,"(pgpkeyid=%.8s)",getkey
);
1045 fprintf(console
,"gpgkeys: LDAP fetch for: %s\n",search
);
1048 attrs
[2]=NULL
; /* keep only pgpkey(v2) and pgpcertid */
1050 err
=ldap_search_s(ldap
,basekeyspacedn
,
1051 LDAP_SCOPE_SUBTREE
,search
,attrs
,0,&res
);
1054 int errtag
=ldap_err_to_gpg_err(err
);
1056 fprintf(console
,"gpgkeys: LDAP search error: %s\n",ldap_err2string(err
));
1057 fprintf(output
,"KEY 0x%s BEGIN\n",getkey
);
1058 fprintf(output
,"KEY 0x%s FAILED %d\n",getkey
,errtag
);
1062 count
=ldap_count_entries(ldap
,res
);
1065 fprintf(console
,"gpgkeys: key %s not found on keyserver\n",getkey
);
1066 fprintf(output
,"KEY 0x%s BEGIN\n",getkey
);
1067 fprintf(output
,"KEY 0x%s FAILED %d\n",getkey
,KEYSERVER_KEY_NOT_FOUND
);
1071 /* There may be more than one unique result for a given keyID,
1072 so we should fetch them all (test this by fetching short key
1075 each
=ldap_first_entry(ldap
,res
);
1078 char **vals
,**certid
;
1080 /* Use the long keyid to remove duplicates. The LDAP server
1081 returns the same keyid more than once if there are
1082 multiple user IDs on the key. Note that this does NOT
1083 mean that a keyid that exists multiple times on the
1084 keyserver will not be fetched. It means that each KEY,
1085 no matter how many user IDs share its keyid, will be
1086 fetched only once. If a keyid that belongs to more than
1087 one key is fetched, the server quite properly responds
1088 with all matching keys. -ds */
1090 certid
=ldap_get_values(ldap
,each
,"pgpcertid");
1093 if(!key_in_keylist(certid
[0],dupelist
))
1095 /* it's not a duplicate, so add it */
1097 int rc
=add_key_to_keylist(certid
[0],&dupelist
);
1104 build_info(certid
[0],each
);
1106 fprintf(output
,"KEY 0x%s BEGIN\n",getkey
);
1108 vals
=ldap_get_values(ldap
,each
,pgpkeystr
);
1111 int errtag
=ldap_to_gpg_err(ldap
);
1113 fprintf(console
,"gpgkeys: unable to retrieve key %s "
1114 "from keyserver\n",getkey
);
1115 fprintf(output
,"KEY 0x%s FAILED %d\n",getkey
,errtag
);
1119 print_nocr(output
,vals
[0]);
1120 fprintf(output
,"\nKEY 0x%s END\n",getkey
);
1122 ldap_value_free(vals
);
1126 ldap_value_free(certid
);
1129 each
=ldap_next_entry(ldap
,each
);
1137 free_keylist(dupelist
);
1142 #define LDAP_ESCAPE_CHARS "*()\\"
1144 /* Append string to buffer in a LDAP-quoted way */
1146 ldap_quote(char *buffer
,const char *string
)
1148 /* Find the end of buffer */
1149 buffer
+=strlen(buffer
);
1151 for(;*string
;string
++)
1153 if(strchr(LDAP_ESCAPE_CHARS
,*string
))
1155 sprintf(buffer
,"\\%02X",*string
);
1165 /* Note that key-not-found is not a fatal error */
1167 get_name(char *getkey
)
1169 LDAPMessage
*res
,*each
;
1170 int ret
=KEYSERVER_INTERNAL_ERROR
,err
,count
;
1171 /* The maximum size of the search, including the optional stuff and
1173 char search
[2+12+(MAX_LINE
*3)+2+15+14+1+1+20];
1174 /* This ordering is significant - specifically, "pgpcertid" needs to
1175 be the second item in the list, since everything after it may be
1176 discarded if the user isn't in verbose mode. */
1177 char *attrs
[]={"replaceme","pgpcertid","pgpuserid","pgpkeyid","pgprevoked",
1178 "pgpdisabled","pgpkeycreatetime","modifytimestamp",
1179 "pgpkeysize","pgpkeytype",NULL
};
1180 attrs
[0]=pgpkeystr
; /* Some compilers don't like using variables as
1181 array initializers. */
1183 /* Build the search string */
1187 if(!opt
->flags
.include_disabled
|| !opt
->flags
.include_revoked
)
1188 strcat(search
,"(&");
1190 strcat(search
,"(pgpUserID=*");
1191 ldap_quote(search
,getkey
);
1192 strcat(search
,"*)");
1194 if(!opt
->flags
.include_disabled
)
1195 strcat(search
,"(pgpDisabled=0)");
1197 if(!opt
->flags
.include_revoked
)
1198 strcat(search
,"(pgpRevoked=0)");
1200 if(!opt
->flags
.include_disabled
|| !opt
->flags
.include_revoked
)
1204 fprintf(console
,"gpgkeys: LDAP fetch for: %s\n",search
);
1207 attrs
[2]=NULL
; /* keep only pgpkey(v2) and pgpcertid */
1209 err
=ldap_search_s(ldap
,basekeyspacedn
,
1210 LDAP_SCOPE_SUBTREE
,search
,attrs
,0,&res
);
1213 int errtag
=ldap_err_to_gpg_err(err
);
1215 fprintf(console
,"gpgkeys: LDAP search error: %s\n",ldap_err2string(err
));
1216 fprintf(output
,"NAME %s BEGIN\n",getkey
);
1217 fprintf(output
,"NAME %s FAILED %d\n",getkey
,errtag
);
1221 count
=ldap_count_entries(ldap
,res
);
1224 fprintf(console
,"gpgkeys: key %s not found on keyserver\n",getkey
);
1225 fprintf(output
,"NAME %s BEGIN\n",getkey
);
1226 fprintf(output
,"NAME %s FAILED %d\n",getkey
,KEYSERVER_KEY_NOT_FOUND
);
1230 /* There may be more than one result, but we return them all. */
1232 each
=ldap_first_entry(ldap
,res
);
1235 char **vals
,**certid
;
1237 certid
=ldap_get_values(ldap
,each
,"pgpcertid");
1240 build_info(certid
[0],each
);
1242 fprintf(output
,"NAME %s BEGIN\n",getkey
);
1244 vals
=ldap_get_values(ldap
,each
,pgpkeystr
);
1247 int errtag
=ldap_to_gpg_err(ldap
);
1249 fprintf(console
,"gpgkeys: unable to retrieve key %s "
1250 "from keyserver\n",getkey
);
1251 fprintf(output
,"NAME %s FAILED %d\n",getkey
,errtag
);
1255 print_nocr(output
,vals
[0]);
1256 fprintf(output
,"\nNAME %s END\n",getkey
);
1258 ldap_value_free(vals
);
1261 ldap_value_free(certid
);
1264 each
=ldap_next_entry(ldap
,each
);
1276 printquoted(FILE *stream
,char *string
,char delim
)
1280 if(*string
==delim
|| *string
=='%')
1281 fprintf(stream
,"%%%02x",*string
);
1283 fputc(*string
,stream
);
1289 /* Returns 0 on success and -1 on error. Note that key-not-found is
1292 search_key(const char *searchkey
)
1294 char **vals
,*search
;
1295 LDAPMessage
*res
,*each
;
1297 struct keylist
*dupelist
=NULL
;
1298 /* The maximum size of the search, including the optional stuff and
1300 char *attrs
[]={"pgpcertid","pgpuserid","pgprevoked","pgpdisabled",
1301 "pgpkeycreatetime","pgpkeyexpiretime","modifytimestamp",
1302 "pgpkeysize","pgpkeytype",NULL
};
1303 enum ks_search_type search_type
;
1305 search
=malloc(2+1+9+1+3+strlen(searchkey
)+3+1+15+14+1+1+20);
1308 fprintf(console
,"gpgkeys: out of memory when building search list\n");
1309 fprintf(output
,"SEARCH %s FAILED %d\n",searchkey
,KEYSERVER_NO_MEMORY
);
1310 return KEYSERVER_NO_MEMORY
;
1313 fprintf(output
,"SEARCH %s BEGIN\n",searchkey
);
1315 search_type
=classify_ks_search(&searchkey
);
1318 fprintf(console
,"search type is %d, and key is \"%s\"\n",
1319 search_type
,searchkey
);
1321 /* Build the search string */
1325 if(!opt
->flags
.include_disabled
|| !opt
->flags
.include_revoked
)
1326 strcat(search
,"(&");
1332 case KS_SEARCH_KEYID_SHORT
:
1333 strcat(search
,"pgpKeyID");
1336 case KS_SEARCH_KEYID_LONG
:
1337 strcat(search
,"pgpCertID");
1341 strcat(search
,"pgpUserID");
1349 case KS_SEARCH_SUBSTR
:
1353 case KS_SEARCH_MAIL
:
1354 strcat(search
,"*<");
1357 case KS_SEARCH_MAILSUB
:
1358 strcat(search
,"*<*");
1361 case KS_SEARCH_EXACT
:
1362 case KS_SEARCH_KEYID_LONG
:
1363 case KS_SEARCH_KEYID_SHORT
:
1367 strcat(search
,searchkey
);
1371 case KS_SEARCH_SUBSTR
:
1375 case KS_SEARCH_MAIL
:
1376 strcat(search
,">*");
1379 case KS_SEARCH_MAILSUB
:
1380 strcat(search
,"*>*");
1383 case KS_SEARCH_EXACT
:
1384 case KS_SEARCH_KEYID_LONG
:
1385 case KS_SEARCH_KEYID_SHORT
:
1391 if(!opt
->flags
.include_disabled
)
1392 strcat(search
,"(pgpDisabled=0)");
1394 if(!opt
->flags
.include_revoked
)
1395 strcat(search
,"(pgpRevoked=0)");
1397 if(!opt
->flags
.include_disabled
|| !opt
->flags
.include_revoked
)
1401 fprintf(console
,"gpgkeys: LDAP search for: %s\n",search
);
1403 err
=ldap_search_s(ldap
,basekeyspacedn
,
1404 LDAP_SCOPE_SUBTREE
,search
,attrs
,0,&res
);
1406 if(err
!=LDAP_SUCCESS
&& err
!=LDAP_SIZELIMIT_EXCEEDED
)
1408 int errtag
=ldap_err_to_gpg_err(err
);
1410 fprintf(output
,"SEARCH %s FAILED %d\n",searchkey
,errtag
);
1411 fprintf(console
,"gpgkeys: LDAP search error: %s\n",ldap_err2string(err
));
1415 /* The LDAP server doesn't return a real count of unique keys, so we
1416 can't use ldap_count_entries here. */
1417 each
=ldap_first_entry(ldap
,res
);
1420 char **certid
=ldap_get_values(ldap
,each
,"pgpcertid");
1424 if(!key_in_keylist(certid
[0],dupelist
))
1426 int rc
=add_key_to_keylist(certid
[0],&dupelist
);
1429 fprintf(output
,"SEARCH %s FAILED %d\n",searchkey
,rc
);
1430 free_keylist(dupelist
);
1438 each
=ldap_next_entry(ldap
,each
);
1441 if(err
==LDAP_SIZELIMIT_EXCEEDED
)
1444 fprintf(console
,"gpgkeys: search results exceeded server limit."
1445 " First %d result shown.\n",count
);
1447 fprintf(console
,"gpgkeys: search results exceeded server limit."
1448 " First %d results shown.\n",count
);
1451 free_keylist(dupelist
);
1455 fprintf(output
,"info:1:0\n");
1458 fprintf(output
,"info:1:%d\n",count
);
1460 each
=ldap_first_entry(ldap
,res
);
1465 certid
=ldap_get_values(ldap
,each
,"pgpcertid");
1470 /* Have we seen this certid before? */
1471 if(!key_in_keylist(certid
[0],dupelist
))
1473 int rc
=add_key_to_keylist(certid
[0],&dupelist
);
1476 fprintf(output
,"SEARCH %s FAILED %d\n",searchkey
,rc
);
1477 free_keylist(dupelist
);
1478 ldap_value_free(certid
);
1483 fprintf(output
,"pub:%s:",certid
[0]);
1485 vals
=ldap_get_values(ldap
,each
,"pgpkeytype");
1488 /* The LDAP server doesn't exactly handle this
1490 if(strcasecmp(vals
[0],"RSA")==0)
1491 fprintf(output
,"1");
1492 else if(strcasecmp(vals
[0],"DSS/DH")==0)
1493 fprintf(output
,"17");
1494 ldap_value_free(vals
);
1499 vals
=ldap_get_values(ldap
,each
,"pgpkeysize");
1502 /* Not sure why, but some keys are listed with a
1503 key size of 0. Treat that like an
1506 fprintf(output
,"%d",atoi(vals
[0]));
1507 ldap_value_free(vals
);
1512 /* YYYYMMDDHHmmssZ */
1514 vals
=ldap_get_values(ldap
,each
,"pgpkeycreatetime");
1515 if(vals
!=NULL
&& strlen(vals
[0])==15)
1517 fprintf(output
,"%u",
1518 (unsigned int)ldap2epochtime(vals
[0]));
1519 ldap_value_free(vals
);
1524 vals
=ldap_get_values(ldap
,each
,"pgpkeyexpiretime");
1525 if(vals
!=NULL
&& strlen(vals
[0])==15)
1527 fprintf(output
,"%u",
1528 (unsigned int)ldap2epochtime(vals
[0]));
1529 ldap_value_free(vals
);
1534 vals
=ldap_get_values(ldap
,each
,"pgprevoked");
1537 if(atoi(vals
[0])==1)
1538 fprintf(output
,"r");
1539 ldap_value_free(vals
);
1542 vals
=ldap_get_values(ldap
,each
,"pgpdisabled");
1545 if(atoi(vals
[0])==1)
1546 fprintf(output
,"d");
1547 ldap_value_free(vals
);
1551 /* This is not yet specified in the keyserver
1552 protocol, but may be someday. */
1555 vals
=ldap_get_values(ldap
,each
,"modifytimestamp");
1556 if(vals
!=NULL
&& strlen(vals
[0])==15)
1558 fprintf(output
,"%u",
1559 (unsigned int)ldap2epochtime(vals
[0]));
1560 ldap_value_free(vals
);
1564 fprintf(output
,"\n");
1566 /* Now print all the uids that have this certid */
1567 uids
=ldap_first_entry(ldap
,res
);
1570 vals
=ldap_get_values(ldap
,uids
,"pgpcertid");
1573 if(strcasecmp(certid
[0],vals
[0])==0)
1577 fprintf(output
,"uid:");
1579 uidvals
=ldap_get_values(ldap
,uids
,"pgpuserid");
1582 /* Need to escape any colons */
1583 printquoted(output
,uidvals
[0],':');
1584 ldap_value_free(uidvals
);
1587 fprintf(output
,"\n");
1590 ldap_value_free(vals
);
1593 uids
=ldap_next_entry(ldap
,uids
);
1597 ldap_value_free(certid
);
1600 each
=ldap_next_entry(ldap
,each
);
1605 free_keylist(dupelist
);
1607 fprintf(output
,"SEARCH %s END\n",searchkey
);
1609 return KEYSERVER_OK
;
1613 fail_all(struct keylist
*keylist
,int err
)
1618 if(opt
->action
==KS_SEARCH
)
1620 fprintf(output
,"SEARCH ");
1623 fprintf(output
,"%s ",keylist
->str
);
1624 keylist
=keylist
->next
;
1626 fprintf(output
,"FAILED %d\n",err
);
1631 fprintf(output
,"KEY %s FAILED %d\n",keylist
->str
,err
);
1632 keylist
=keylist
->next
;
1637 find_basekeyspacedn(void)
1640 char *attr
[]={"namingContexts",NULL
,NULL
,NULL
};
1644 /* Look for namingContexts */
1645 err
=ldap_search_s(ldap
,"",LDAP_SCOPE_BASE
,"(objectClass=*)",attr
,0,&res
);
1646 if(err
==LDAP_SUCCESS
)
1648 context
=ldap_get_values(ldap
,res
,"namingContexts");
1651 attr
[0]="pgpBaseKeySpaceDN";
1652 attr
[1]="pgpVersion";
1653 attr
[2]="pgpSoftware";
1657 /* We found some, so try each namingContext as the search base
1658 and look for pgpBaseKeySpaceDN. Because we found this, we
1659 know we're talking to a regular-ish LDAP server and not a
1662 for(i
=0;context
[i
] && !basekeyspacedn
;i
++)
1665 LDAPMessage
*si_res
;
1668 object
=malloc(17+strlen(context
[i
])+1);
1672 strcpy(object
,"cn=pgpServerInfo,");
1673 strcat(object
,context
[i
]);
1675 err
=ldap_search_s(ldap
,object
,LDAP_SCOPE_BASE
,
1676 "(objectClass=*)",attr
,0,&si_res
);
1679 if(err
==LDAP_NO_SUCH_OBJECT
)
1681 else if(err
!=LDAP_SUCCESS
)
1684 vals
=ldap_get_values(ldap
,si_res
,"pgpBaseKeySpaceDN");
1687 basekeyspacedn
=strdup(vals
[0]);
1688 ldap_value_free(vals
);
1693 vals
=ldap_get_values(ldap
,si_res
,"pgpSoftware");
1696 fprintf(console
,"Server: \t%s\n",vals
[0]);
1697 ldap_value_free(vals
);
1700 vals
=ldap_get_values(ldap
,si_res
,"pgpVersion");
1703 fprintf(console
,"Version:\t%s\n",vals
[0]);
1704 ldap_value_free(vals
);
1708 ldap_msgfree(si_res
);
1711 ldap_value_free(context
);
1718 /* We don't have an answer yet, which means the server might be
1719 a LDAP keyserver. */
1721 LDAPMessage
*si_res
;
1723 attr
[0]="pgpBaseKeySpaceDN";
1727 err
=ldap_search_s(ldap
,"cn=pgpServerInfo",LDAP_SCOPE_BASE
,
1728 "(objectClass=*)",attr
,0,&si_res
);
1729 if(err
!=LDAP_SUCCESS
)
1732 /* For the LDAP keyserver, this is always "OU=ACTIVE,O=PGP
1733 KEYSPACE,C=US", but it might not be in the future. */
1735 vals
=ldap_get_values(ldap
,si_res
,"baseKeySpaceDN");
1738 basekeyspacedn
=strdup(vals
[0]);
1739 ldap_value_free(vals
);
1744 vals
=ldap_get_values(ldap
,si_res
,"software");
1747 fprintf(console
,"Server: \t%s\n",vals
[0]);
1748 ldap_value_free(vals
);
1752 vals
=ldap_get_values(ldap
,si_res
,"version");
1756 fprintf(console
,"Version:\t%s\n",vals
[0]);
1758 /* If the version is high enough, use the new pgpKeyV2
1759 attribute. This design if iffy at best, but it matches how
1760 PGP does it. I figure the NAI folks assumed that there would
1761 never be a LDAP keyserver vendor with a different numbering
1764 pgpkeystr
="pgpKeyV2";
1766 ldap_value_free(vals
);
1769 ldap_msgfree(si_res
);
1772 return LDAP_SUCCESS
;
1776 show_help (FILE *fp
)
1778 fprintf (fp
,"-h, --help\thelp\n");
1779 fprintf (fp
,"-V\t\tmachine readable version\n");
1780 fprintf (fp
,"--version\thuman readable version\n");
1781 fprintf (fp
,"-o\t\toutput to this file\n");
1785 main(int argc
,char *argv
[])
1787 int port
=0,arg
,err
,ret
=KEYSERVER_INTERNAL_ERROR
;
1788 char line
[MAX_LINE
],*binddn
=NULL
,*bindpw
=NULL
;
1789 int failed
=0,use_ssl
=0,use_tls
=0,bound
=0;
1790 struct keylist
*keylist
=NULL
,*keyptr
=NULL
;
1794 /* Kludge to implement standard GNU options. */
1795 if (argc
> 1 && !strcmp (argv
[1], "--version"))
1797 fputs ("gpgkeys_ldap (GnuPG) " VERSION
"\n", stdout
);
1800 else if (argc
> 1 && !strcmp (argv
[1], "--help"))
1806 while((arg
=getopt(argc
,argv
,"hVo:"))!=-1)
1811 show_help (console
);
1812 return KEYSERVER_OK
;
1815 fprintf(stdout
,"%d\n%s\n",KEYSERVER_PROTO_VERSION
,VERSION
);
1816 return KEYSERVER_OK
;
1819 output
=fopen(optarg
,"w");
1822 fprintf(console
,"gpgkeys: Cannot open output file `%s': %s\n",
1823 optarg
,strerror(errno
));
1824 return KEYSERVER_INTERNAL_ERROR
;
1832 input
=fopen(argv
[optind
],"r");
1835 fprintf(console
,"gpgkeys: Cannot open input file `%s': %s\n",
1836 argv
[optind
],strerror(errno
));
1837 return KEYSERVER_INTERNAL_ERROR
;
1847 opt
=init_ks_options();
1849 return KEYSERVER_NO_MEMORY
;
1851 /* Get the command and info block */
1853 while(fgets(line
,MAX_LINE
,input
)!=NULL
)
1855 char optionstr
[MAX_OPTION
+1];
1860 err
=parse_ks_options(line
,opt
);
1869 if(sscanf(line
,"OPTION %" MKSTRING(MAX_OPTION
) "[^\n]\n",optionstr
)==1)
1872 char *start
=&optionstr
[0];
1874 optionstr
[MAX_OPTION
]='\0';
1876 if(strncasecmp(optionstr
,"no-",3)==0)
1879 start
=&optionstr
[3];
1882 if(strncasecmp(start
,"tls",3)==0)
1886 else if(start
[3]=='=')
1888 if(strcasecmp(&start
[4],"no")==0)
1890 else if(strcasecmp(&start
[4],"try")==0)
1892 else if(strcasecmp(&start
[4],"warn")==0)
1894 else if(strcasecmp(&start
[4],"require")==0)
1899 else if(start
[3]=='\0')
1902 else if(strncasecmp(start
,"basedn",6)==0)
1906 free(basekeyspacedn
);
1907 basekeyspacedn
=NULL
;
1909 else if(start
[6]=='=')
1911 free(basekeyspacedn
);
1912 basekeyspacedn
=strdup(&start
[7]);
1915 fprintf(console
,"gpgkeys: out of memory while creating "
1917 ret
=KEYSERVER_NO_MEMORY
;
1924 else if(strncasecmp(start
,"binddn",6)==0)
1931 else if(start
[6]=='=')
1934 binddn
=strdup(&start
[7]);
1937 fprintf(console
,"gpgkeys: out of memory while creating "
1939 ret
=KEYSERVER_NO_MEMORY
;
1946 else if(strncasecmp(start
,"bindpw",6)==0)
1953 else if(start
[6]=='=')
1956 bindpw
=strdup(&start
[7]);
1959 fprintf(console
,"gpgkeys: out of memory while creating "
1961 ret
=KEYSERVER_NO_MEMORY
;
1975 fprintf(console
,"gpgkeys: no scheme supplied!\n");
1976 ret
=KEYSERVER_SCHEME_NOT_FOUND
;
1980 if(strcasecmp(opt
->scheme
,"ldaps")==0)
1987 port
=atoi(opt
->port
);
1991 fprintf(console
,"gpgkeys: no keyserver host provided\n");
1995 if(opt
->timeout
&& register_timeout()==-1)
1997 fprintf(console
,"gpgkeys: unable to register timeout handler\n");
1998 return KEYSERVER_INTERNAL_ERROR
;
2001 #if defined(LDAP_OPT_X_TLS_CACERTFILE) && defined(HAVE_LDAP_SET_OPTION)
2003 if(opt
->ca_cert_file
)
2005 err
=ldap_set_option(NULL
,LDAP_OPT_X_TLS_CACERTFILE
,opt
->ca_cert_file
);
2006 if(err
!=LDAP_SUCCESS
)
2008 fprintf(console
,"gpgkeys: unable to set ca-cert-file: %s\n",
2009 ldap_err2string(err
));
2010 ret
=KEYSERVER_INTERNAL_ERROR
;
2014 #endif /* LDAP_OPT_X_TLS_CACERTFILE && HAVE_LDAP_SET_OPTION */
2016 /* SSL trumps TLS */
2020 /* If it's a GET or a SEARCH, the next thing to come in is the
2021 keyids. If it's a SEND, then there are no keyids. */
2023 if(opt
->action
==KS_SEND
)
2024 while(fgets(line
,MAX_LINE
,input
)!=NULL
&& line
[0]!='\n');
2025 else if(opt
->action
==KS_GET
2026 || opt
->action
==KS_GETNAME
|| opt
->action
==KS_SEARCH
)
2030 struct keylist
*work
;
2032 if(fgets(line
,MAX_LINE
,input
)==NULL
)
2036 if(line
[0]=='\n' || line
[0]=='\0')
2039 work
=malloc(sizeof(struct keylist
));
2042 fprintf(console
,"gpgkeys: out of memory while "
2043 "building key list\n");
2044 ret
=KEYSERVER_NO_MEMORY
;
2048 strcpy(work
->str
,line
);
2050 /* Trim the trailing \n */
2051 work
->str
[strlen(line
)-1]='\0';
2055 /* Always attach at the end to keep the list in proper
2056 order for searching */
2068 fprintf(console
,"gpgkeys: no keyserver command specified\n");
2072 /* Send the response */
2074 fprintf(output
,"VERSION %d\n",KEYSERVER_PROTO_VERSION
);
2075 fprintf(output
,"PROGRAM %s\n\n",VERSION
);
2079 fprintf(console
,"Host:\t\t%s\n",opt
->host
);
2081 fprintf(console
,"Port:\t\t%d\n",port
);
2082 fprintf(console
,"Command:\t%s\n",ks_action_to_string(opt
->action
));
2087 #if defined(LDAP_OPT_DEBUG_LEVEL) && defined(HAVE_LDAP_SET_OPTION)
2088 err
=ldap_set_option(NULL
,LDAP_OPT_DEBUG_LEVEL
,&opt
->debug
);
2089 if(err
!=LDAP_SUCCESS
)
2090 fprintf(console
,"gpgkeys: unable to set debug mode: %s\n",
2091 ldap_err2string(err
));
2093 fprintf(console
,"gpgkeys: debug level %d\n",opt
->debug
);
2095 fprintf(console
,"gpgkeys: not built with debugging support\n");
2099 /* We have a timeout set for the setup stuff since it could time out
2101 set_timeout(opt
->timeout
);
2103 /* Note that this tries all A records on a given host (or at least,
2105 ldap
=ldap_init(opt
->host
,port
);
2108 fprintf(console
,"gpgkeys: internal LDAP init error: %s\n",
2110 fail_all(keylist
,KEYSERVER_INTERNAL_ERROR
);
2116 #if defined(LDAP_OPT_X_TLS) && defined(HAVE_LDAP_SET_OPTION)
2117 int ssl
=LDAP_OPT_X_TLS_HARD
;
2119 err
=ldap_set_option(ldap
,LDAP_OPT_X_TLS
,&ssl
);
2120 if(err
!=LDAP_SUCCESS
)
2122 fprintf(console
,"gpgkeys: unable to make SSL connection: %s\n",
2123 ldap_err2string(err
));
2124 fail_all(keylist
,ldap_err_to_gpg_err(err
));
2128 if(!opt
->flags
.check_cert
)
2129 ssl
=LDAP_OPT_X_TLS_NEVER
;
2131 err
=ldap_set_option(NULL
,LDAP_OPT_X_TLS_REQUIRE_CERT
,&ssl
);
2132 if(err
!=LDAP_SUCCESS
)
2135 "gpgkeys: unable to set certificate validation: %s\n",
2136 ldap_err2string(err
));
2137 fail_all(keylist
,ldap_err_to_gpg_err(err
));
2141 fprintf(console
,"gpgkeys: unable to make SSL connection: %s\n",
2142 "not built with LDAPS support");
2143 fail_all(keylist
,KEYSERVER_INTERNAL_ERROR
);
2149 if((err
=find_basekeyspacedn()) || !basekeyspacedn
)
2151 fprintf(console
,"gpgkeys: unable to retrieve LDAP base: %s\n",
2152 err
?ldap_err2string(err
):"not found");
2153 fail_all(keylist
,ldap_err_to_gpg_err(err
));
2157 /* use_tls: 0=don't use, 1=try silently to use, 2=try loudly to use,
2164 fprintf(console
,"gpgkeys: unable to start TLS: %s\n",
2165 "not supported by the NAI LDAP keyserver");
2168 fail_all(keylist
,KEYSERVER_INTERNAL_ERROR
);
2174 #if defined(HAVE_LDAP_START_TLS_S) && defined(HAVE_LDAP_SET_OPTION)
2175 int ver
=LDAP_VERSION3
;
2177 err
=ldap_set_option(ldap
,LDAP_OPT_PROTOCOL_VERSION
,&ver
);
2179 #ifdef LDAP_OPT_X_TLS
2180 if(err
==LDAP_SUCCESS
)
2182 if(opt
->flags
.check_cert
)
2183 ver
=LDAP_OPT_X_TLS_HARD
;
2185 ver
=LDAP_OPT_X_TLS_NEVER
;
2187 err
=ldap_set_option(NULL
,LDAP_OPT_X_TLS_REQUIRE_CERT
,&ver
);
2191 if(err
==LDAP_SUCCESS
)
2192 err
=ldap_start_tls_s(ldap
,NULL
,NULL
);
2194 if(err
!=LDAP_SUCCESS
)
2196 if(use_tls
>=2 || opt
->verbose
>2)
2197 fprintf(console
,"gpgkeys: unable to start TLS: %s\n",
2198 ldap_err2string(err
));
2199 /* Are we forcing it? */
2202 fail_all(keylist
,ldap_err_to_gpg_err(err
));
2206 else if(opt
->verbose
>1)
2207 fprintf(console
,"gpgkeys: TLS started successfully.\n");
2210 fprintf(console
,"gpgkeys: unable to start TLS: %s\n",
2211 "not built with TLS support");
2214 fail_all(keylist
,KEYSERVER_INTERNAL_ERROR
);
2221 /* By default we don't bind as there is usually no need to. For
2222 cases where the server needs some authentication, the user can
2223 use binddn and bindpw for auth. */
2227 #ifdef HAVE_LDAP_SET_OPTION
2228 int ver
=LDAP_VERSION3
;
2230 err
=ldap_set_option(ldap
,LDAP_OPT_PROTOCOL_VERSION
,&ver
);
2231 if(err
!=LDAP_SUCCESS
)
2233 fprintf(console
,"gpgkeys: unable to go to LDAP 3: %s\n",
2234 ldap_err2string(err
));
2235 fail_all(keylist
,ldap_err_to_gpg_err(err
));
2241 fprintf(console
,"gpgkeys: LDAP bind to %s, pw %s\n",binddn
,
2242 bindpw
?">not shown<":">none<");
2243 err
=ldap_simple_bind_s(ldap
,binddn
,bindpw
);
2244 if(err
!=LDAP_SUCCESS
)
2246 fprintf(console
,"gpgkeys: internal LDAP bind error: %s\n",
2247 ldap_err2string(err
));
2248 fail_all(keylist
,ldap_err_to_gpg_err(err
));
2255 if(opt
->action
==KS_GET
)
2261 set_timeout(opt
->timeout
);
2263 if(get_key(keyptr
->str
)!=KEYSERVER_OK
)
2266 keyptr
=keyptr
->next
;
2269 else if(opt
->action
==KS_GETNAME
)
2275 set_timeout(opt
->timeout
);
2277 if(get_name(keyptr
->str
)!=KEYSERVER_OK
)
2280 keyptr
=keyptr
->next
;
2283 else if(opt
->action
==KS_SEND
)
2289 set_timeout(opt
->timeout
);
2293 if (send_key(&eof_seen
) != KEYSERVER_OK
)
2298 if (send_key_keyserver(&eof_seen
) != KEYSERVER_OK
)
2304 else if(opt
->action
==KS_SEARCH
)
2306 char *searchkey
=NULL
;
2309 set_timeout(opt
->timeout
);
2311 /* To search, we stick a * in between each key to search for.
2312 This means that if the user enters words, they'll get
2313 "enters*words". If the user "enters words", they'll get
2319 len
+=strlen(keyptr
->str
)+1;
2320 keyptr
=keyptr
->next
;
2323 searchkey
=malloc((len
*3)+1);
2326 ret
=KEYSERVER_NO_MEMORY
;
2327 fail_all(keylist
,KEYSERVER_NO_MEMORY
);
2336 ldap_quote(searchkey
,keyptr
->str
);
2337 strcat(searchkey
,"*");
2338 keyptr
=keyptr
->next
;
2341 /* Nail that last "*" */
2343 searchkey
[strlen(searchkey
)-1]='\0';
2345 if(search_key(searchkey
)!=KEYSERVER_OK
)
2351 assert (!"bad action");
2358 while(keylist
!=NULL
)
2360 struct keylist
*current
=keylist
;
2361 keylist
=keylist
->next
;
2371 free_ks_options(opt
);
2373 if(ldap
!=NULL
&& bound
)
2374 ldap_unbind_s(ldap
);
2376 free(basekeyspacedn
);