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
);
774 /* Unwind and free the whole modlist structure */
775 for(ml
=modlist
;*ml
;ml
++)
777 free_mod_values(*ml
);
787 fprintf(output
,"KEY %s FAILED %d\n",keyid
,ret
);
793 send_key_keyserver(int *r_eof
)
795 int err
,begin
=0,end
=0,keysize
=1,ret
=KEYSERVER_INTERNAL_ERROR
;
796 char *dn
=NULL
,line
[MAX_LINE
],*key
[2]={NULL
,NULL
};
797 char keyid
[17],state
[6];
798 LDAPMod mod
, *attrs
[2];
800 memset(&mod
,0,sizeof(mod
));
801 mod
.mod_op
=LDAP_MOD_ADD
;
802 mod
.mod_type
=pgpkeystr
;
807 dn
=malloc(strlen("pgpCertid=virtual,")+strlen(basekeyspacedn
)+1);
810 fprintf(console
,"gpgkeys: can't allocate memory for keyserver record\n");
811 ret
=KEYSERVER_NO_MEMORY
;
815 strcpy(dn
,"pgpCertid=virtual,");
816 strcat(dn
,basekeyspacedn
);
821 fprintf(console
,"gpgkeys: unable to allocate memory for key\n");
822 ret
=KEYSERVER_NO_MEMORY
;
828 /* Read and throw away stdin until we see the BEGIN */
830 while(fgets(line
,MAX_LINE
,input
)!=NULL
)
831 if(sscanf(line
,"KEY%*[ ]%16s%*[ ]%5s\n",keyid
,state
)==2
832 && strcmp(state
,"BEGIN")==0)
840 /* i.e. eof before the KEY BEGIN was found. This isn't an
847 /* Now slurp up everything until we see the END */
849 while(fgets(line
,MAX_LINE
,input
)!=NULL
)
850 if(sscanf(line
,"KEY%*[ ]%16s%*[ ]%3s\n",keyid
,state
)==2
851 && strcmp(state
,"END")==0)
858 keysize
+=strlen(line
);
859 key
[0]=realloc(key
[0],keysize
);
862 fprintf(console
,"gpgkeys: unable to reallocate for key\n");
863 ret
=KEYSERVER_NO_MEMORY
;
872 fprintf(console
,"gpgkeys: no KEY %s END found\n",keyid
);
874 ret
=KEYSERVER_KEY_INCOMPLETE
;
878 err
=ldap_add_s(ldap
,dn
,attrs
);
879 if(err
!=LDAP_SUCCESS
)
881 fprintf(console
,"gpgkeys: error adding key %s to keyserver: %s\n",
882 keyid
,ldap_err2string(err
));
883 ret
=ldap_err_to_gpg_err(err
);
895 fprintf(output
,"KEY %s FAILED %d\n",keyid
,ret
);
897 /* Not a fatal error */
898 if(ret
==KEYSERVER_KEY_EXISTS
)
905 build_info(const char *certid
,LDAPMessage
*each
)
909 fprintf(output
,"INFO %s BEGIN\n",certid
);
911 fprintf(output
,"pub:%s:",certid
);
913 vals
=ldap_get_values(ldap
,each
,"pgpkeytype");
916 if(strcmp(vals
[0],"RSA")==0)
918 else if(strcmp(vals
[0],"DSS/DH")==0)
919 fprintf(output
,"17");
920 ldap_value_free(vals
);
925 vals
=ldap_get_values(ldap
,each
,"pgpkeysize");
929 fprintf(output
,"%d",atoi(vals
[0]));
930 ldap_value_free(vals
);
935 vals
=ldap_get_values(ldap
,each
,"pgpkeycreatetime");
938 if(strlen(vals
[0])==15)
939 fprintf(output
,"%u",(unsigned int)ldap2epochtime(vals
[0]));
940 ldap_value_free(vals
);
945 vals
=ldap_get_values(ldap
,each
,"pgpkeyexpiretime");
948 if(strlen(vals
[0])==15)
949 fprintf(output
,"%u",(unsigned int)ldap2epochtime(vals
[0]));
950 ldap_value_free(vals
);
955 vals
=ldap_get_values(ldap
,each
,"pgprevoked");
960 ldap_value_free(vals
);
963 fprintf(output
,"\n");
965 vals
=ldap_get_values(ldap
,each
,"pgpuserid");
971 fprintf(output
,"uid:%s\n",vals
[i
]);
972 ldap_value_free(vals
);
975 fprintf(output
,"INFO %s END\n",certid
);
978 /* Note that key-not-found is not a fatal error */
980 get_key(char *getkey
)
982 LDAPMessage
*res
,*each
;
983 int ret
=KEYSERVER_INTERNAL_ERROR
,err
,count
;
984 struct keylist
*dupelist
=NULL
;
986 /* This ordering is significant - specifically, "pgpcertid" needs to
987 be the second item in the list, since everything after it may be
988 discarded if the user isn't in verbose mode. */
989 char *attrs
[]={"replaceme","pgpcertid","pgpuserid","pgpkeyid","pgprevoked",
990 "pgpdisabled","pgpkeycreatetime","modifytimestamp",
991 "pgpkeysize","pgpkeytype",NULL
};
992 attrs
[0]=pgpkeystr
; /* Some compilers don't like using variables as
993 array initializers. */
995 /* Build the search string */
997 /* GPG can send us a v4 fingerprint, a v3 or v4 long key id, or a v3
998 or v4 short key id */
1000 if(strncmp(getkey
,"0x",2)==0)
1003 if(strlen(getkey
)==32)
1006 "gpgkeys: LDAP keyservers do not support v3 fingerprints\n");
1007 fprintf(output
,"KEY 0x%s BEGIN\n",getkey
);
1008 fprintf(output
,"KEY 0x%s FAILED %d\n",getkey
,KEYSERVER_NOT_SUPPORTED
);
1009 return KEYSERVER_NOT_SUPPORTED
;
1012 if(strlen(getkey
)>16)
1014 char *offset
=&getkey
[strlen(getkey
)-16];
1016 /* fingerprint. Take the last 16 characters and treat it like a
1019 if(opt
->flags
.include_subkeys
)
1020 sprintf(search
,"(|(pgpcertid=%.16s)(pgpsubkeyid=%.16s))",
1023 sprintf(search
,"(pgpcertid=%.16s)",offset
);
1025 else if(strlen(getkey
)>8)
1029 if(opt
->flags
.include_subkeys
)
1030 sprintf(search
,"(|(pgpcertid=%.16s)(pgpsubkeyid=%.16s))",
1033 sprintf(search
,"(pgpcertid=%.16s)",getkey
);
1039 sprintf(search
,"(pgpkeyid=%.8s)",getkey
);
1043 fprintf(console
,"gpgkeys: LDAP fetch for: %s\n",search
);
1046 attrs
[2]=NULL
; /* keep only pgpkey(v2) and pgpcertid */
1048 err
=ldap_search_s(ldap
,basekeyspacedn
,
1049 LDAP_SCOPE_SUBTREE
,search
,attrs
,0,&res
);
1052 int errtag
=ldap_err_to_gpg_err(err
);
1054 fprintf(console
,"gpgkeys: LDAP search error: %s\n",ldap_err2string(err
));
1055 fprintf(output
,"KEY 0x%s BEGIN\n",getkey
);
1056 fprintf(output
,"KEY 0x%s FAILED %d\n",getkey
,errtag
);
1060 count
=ldap_count_entries(ldap
,res
);
1063 fprintf(console
,"gpgkeys: key %s not found on keyserver\n",getkey
);
1064 fprintf(output
,"KEY 0x%s BEGIN\n",getkey
);
1065 fprintf(output
,"KEY 0x%s FAILED %d\n",getkey
,KEYSERVER_KEY_NOT_FOUND
);
1069 /* There may be more than one unique result for a given keyID,
1070 so we should fetch them all (test this by fetching short key
1073 each
=ldap_first_entry(ldap
,res
);
1076 char **vals
,**certid
;
1078 /* Use the long keyid to remove duplicates. The LDAP server
1079 returns the same keyid more than once if there are
1080 multiple user IDs on the key. Note that this does NOT
1081 mean that a keyid that exists multiple times on the
1082 keyserver will not be fetched. It means that each KEY,
1083 no matter how many user IDs share its keyid, will be
1084 fetched only once. If a keyid that belongs to more than
1085 one key is fetched, the server quite properly responds
1086 with all matching keys. -ds */
1088 certid
=ldap_get_values(ldap
,each
,"pgpcertid");
1091 if(!key_in_keylist(certid
[0],dupelist
))
1093 /* it's not a duplicate, so add it */
1095 int rc
=add_key_to_keylist(certid
[0],&dupelist
);
1102 build_info(certid
[0],each
);
1104 fprintf(output
,"KEY 0x%s BEGIN\n",getkey
);
1106 vals
=ldap_get_values(ldap
,each
,pgpkeystr
);
1109 int errtag
=ldap_to_gpg_err(ldap
);
1111 fprintf(console
,"gpgkeys: unable to retrieve key %s "
1112 "from keyserver\n",getkey
);
1113 fprintf(output
,"KEY 0x%s FAILED %d\n",getkey
,errtag
);
1117 print_nocr(output
,vals
[0]);
1118 fprintf(output
,"\nKEY 0x%s END\n",getkey
);
1120 ldap_value_free(vals
);
1124 ldap_value_free(certid
);
1127 each
=ldap_next_entry(ldap
,each
);
1135 free_keylist(dupelist
);
1140 #define LDAP_ESCAPE_CHARS "*()\\"
1142 /* Append string to buffer in a LDAP-quoted way */
1144 ldap_quote(char *buffer
,const char *string
)
1146 /* Find the end of buffer */
1147 buffer
+=strlen(buffer
);
1149 for(;*string
;string
++)
1151 if(strchr(LDAP_ESCAPE_CHARS
,*string
))
1153 sprintf(buffer
,"\\%02X",*string
);
1163 /* Note that key-not-found is not a fatal error */
1165 get_name(char *getkey
)
1167 LDAPMessage
*res
,*each
;
1168 int ret
=KEYSERVER_INTERNAL_ERROR
,err
,count
;
1169 /* The maximum size of the search, including the optional stuff and
1171 char search
[2+12+(MAX_LINE
*3)+2+15+14+1+1+20];
1172 /* This ordering is significant - specifically, "pgpcertid" needs to
1173 be the second item in the list, since everything after it may be
1174 discarded if the user isn't in verbose mode. */
1175 char *attrs
[]={"replaceme","pgpcertid","pgpuserid","pgpkeyid","pgprevoked",
1176 "pgpdisabled","pgpkeycreatetime","modifytimestamp",
1177 "pgpkeysize","pgpkeytype",NULL
};
1178 attrs
[0]=pgpkeystr
; /* Some compilers don't like using variables as
1179 array initializers. */
1181 /* Build the search string */
1185 if(!opt
->flags
.include_disabled
|| !opt
->flags
.include_revoked
)
1186 strcat(search
,"(&");
1188 strcat(search
,"(pgpUserID=*");
1189 ldap_quote(search
,getkey
);
1190 strcat(search
,"*)");
1192 if(!opt
->flags
.include_disabled
)
1193 strcat(search
,"(pgpDisabled=0)");
1195 if(!opt
->flags
.include_revoked
)
1196 strcat(search
,"(pgpRevoked=0)");
1198 if(!opt
->flags
.include_disabled
|| !opt
->flags
.include_revoked
)
1202 fprintf(console
,"gpgkeys: LDAP fetch for: %s\n",search
);
1205 attrs
[2]=NULL
; /* keep only pgpkey(v2) and pgpcertid */
1207 err
=ldap_search_s(ldap
,basekeyspacedn
,
1208 LDAP_SCOPE_SUBTREE
,search
,attrs
,0,&res
);
1211 int errtag
=ldap_err_to_gpg_err(err
);
1213 fprintf(console
,"gpgkeys: LDAP search error: %s\n",ldap_err2string(err
));
1214 fprintf(output
,"NAME %s BEGIN\n",getkey
);
1215 fprintf(output
,"NAME %s FAILED %d\n",getkey
,errtag
);
1219 count
=ldap_count_entries(ldap
,res
);
1222 fprintf(console
,"gpgkeys: key %s not found on keyserver\n",getkey
);
1223 fprintf(output
,"NAME %s BEGIN\n",getkey
);
1224 fprintf(output
,"NAME %s FAILED %d\n",getkey
,KEYSERVER_KEY_NOT_FOUND
);
1228 /* There may be more than one result, but we return them all. */
1230 each
=ldap_first_entry(ldap
,res
);
1233 char **vals
,**certid
;
1235 certid
=ldap_get_values(ldap
,each
,"pgpcertid");
1238 build_info(certid
[0],each
);
1240 fprintf(output
,"NAME %s BEGIN\n",getkey
);
1242 vals
=ldap_get_values(ldap
,each
,pgpkeystr
);
1245 int errtag
=ldap_to_gpg_err(ldap
);
1247 fprintf(console
,"gpgkeys: unable to retrieve key %s "
1248 "from keyserver\n",getkey
);
1249 fprintf(output
,"NAME %s FAILED %d\n",getkey
,errtag
);
1253 print_nocr(output
,vals
[0]);
1254 fprintf(output
,"\nNAME %s END\n",getkey
);
1256 ldap_value_free(vals
);
1259 ldap_value_free(certid
);
1262 each
=ldap_next_entry(ldap
,each
);
1274 printquoted(FILE *stream
,char *string
,char delim
)
1278 if(*string
==delim
|| *string
=='%')
1279 fprintf(stream
,"%%%02x",*string
);
1281 fputc(*string
,stream
);
1287 /* Returns 0 on success and -1 on error. Note that key-not-found is
1290 search_key(const char *searchkey
)
1292 char **vals
,*search
;
1293 LDAPMessage
*res
,*each
;
1295 struct keylist
*dupelist
=NULL
;
1296 /* The maximum size of the search, including the optional stuff and
1298 char *attrs
[]={"pgpcertid","pgpuserid","pgprevoked","pgpdisabled",
1299 "pgpkeycreatetime","pgpkeyexpiretime","modifytimestamp",
1300 "pgpkeysize","pgpkeytype",NULL
};
1301 enum ks_search_type search_type
;
1303 search
=malloc(2+1+9+1+3+strlen(searchkey
)+3+1+15+14+1+1+20);
1306 fprintf(console
,"gpgkeys: out of memory when building search list\n");
1307 fprintf(output
,"SEARCH %s FAILED %d\n",searchkey
,KEYSERVER_NO_MEMORY
);
1308 return KEYSERVER_NO_MEMORY
;
1311 fprintf(output
,"SEARCH %s BEGIN\n",searchkey
);
1313 search_type
=classify_ks_search(&searchkey
);
1316 fprintf(console
,"search type is %d, and key is \"%s\"\n",
1317 search_type
,searchkey
);
1319 /* Build the search string */
1323 if(!opt
->flags
.include_disabled
|| !opt
->flags
.include_revoked
)
1324 strcat(search
,"(&");
1330 case KS_SEARCH_KEYID_SHORT
:
1331 strcat(search
,"pgpKeyID");
1334 case KS_SEARCH_KEYID_LONG
:
1335 strcat(search
,"pgpCertID");
1339 strcat(search
,"pgpUserID");
1347 case KS_SEARCH_SUBSTR
:
1351 case KS_SEARCH_MAIL
:
1352 strcat(search
,"*<");
1355 case KS_SEARCH_MAILSUB
:
1356 strcat(search
,"*<*");
1359 case KS_SEARCH_EXACT
:
1360 case KS_SEARCH_KEYID_LONG
:
1361 case KS_SEARCH_KEYID_SHORT
:
1365 strcat(search
,searchkey
);
1369 case KS_SEARCH_SUBSTR
:
1373 case KS_SEARCH_MAIL
:
1374 strcat(search
,">*");
1377 case KS_SEARCH_MAILSUB
:
1378 strcat(search
,"*>*");
1381 case KS_SEARCH_EXACT
:
1382 case KS_SEARCH_KEYID_LONG
:
1383 case KS_SEARCH_KEYID_SHORT
:
1389 if(!opt
->flags
.include_disabled
)
1390 strcat(search
,"(pgpDisabled=0)");
1392 if(!opt
->flags
.include_revoked
)
1393 strcat(search
,"(pgpRevoked=0)");
1395 if(!opt
->flags
.include_disabled
|| !opt
->flags
.include_revoked
)
1399 fprintf(console
,"gpgkeys: LDAP search for: %s\n",search
);
1401 err
=ldap_search_s(ldap
,basekeyspacedn
,
1402 LDAP_SCOPE_SUBTREE
,search
,attrs
,0,&res
);
1404 if(err
!=LDAP_SUCCESS
&& err
!=LDAP_SIZELIMIT_EXCEEDED
)
1406 int errtag
=ldap_err_to_gpg_err(err
);
1408 fprintf(output
,"SEARCH %s FAILED %d\n",searchkey
,errtag
);
1409 fprintf(console
,"gpgkeys: LDAP search error: %s\n",ldap_err2string(err
));
1413 /* The LDAP server doesn't return a real count of unique keys, so we
1414 can't use ldap_count_entries here. */
1415 each
=ldap_first_entry(ldap
,res
);
1418 char **certid
=ldap_get_values(ldap
,each
,"pgpcertid");
1422 if(!key_in_keylist(certid
[0],dupelist
))
1424 int rc
=add_key_to_keylist(certid
[0],&dupelist
);
1427 fprintf(output
,"SEARCH %s FAILED %d\n",searchkey
,rc
);
1428 free_keylist(dupelist
);
1436 each
=ldap_next_entry(ldap
,each
);
1439 if(err
==LDAP_SIZELIMIT_EXCEEDED
)
1442 fprintf(console
,"gpgkeys: search results exceeded server limit."
1443 " First %d result shown.\n",count
);
1445 fprintf(console
,"gpgkeys: search results exceeded server limit."
1446 " First %d results shown.\n",count
);
1449 free_keylist(dupelist
);
1453 fprintf(output
,"info:1:0\n");
1456 fprintf(output
,"info:1:%d\n",count
);
1458 each
=ldap_first_entry(ldap
,res
);
1463 certid
=ldap_get_values(ldap
,each
,"pgpcertid");
1468 /* Have we seen this certid before? */
1469 if(!key_in_keylist(certid
[0],dupelist
))
1471 int rc
=add_key_to_keylist(certid
[0],&dupelist
);
1474 fprintf(output
,"SEARCH %s FAILED %d\n",searchkey
,rc
);
1475 free_keylist(dupelist
);
1476 ldap_value_free(certid
);
1481 fprintf(output
,"pub:%s:",certid
[0]);
1483 vals
=ldap_get_values(ldap
,each
,"pgpkeytype");
1486 /* The LDAP server doesn't exactly handle this
1488 if(strcasecmp(vals
[0],"RSA")==0)
1489 fprintf(output
,"1");
1490 else if(strcasecmp(vals
[0],"DSS/DH")==0)
1491 fprintf(output
,"17");
1492 ldap_value_free(vals
);
1497 vals
=ldap_get_values(ldap
,each
,"pgpkeysize");
1500 /* Not sure why, but some keys are listed with a
1501 key size of 0. Treat that like an
1504 fprintf(output
,"%d",atoi(vals
[0]));
1505 ldap_value_free(vals
);
1510 /* YYYYMMDDHHmmssZ */
1512 vals
=ldap_get_values(ldap
,each
,"pgpkeycreatetime");
1513 if(vals
!=NULL
&& strlen(vals
[0])==15)
1515 fprintf(output
,"%u",
1516 (unsigned int)ldap2epochtime(vals
[0]));
1517 ldap_value_free(vals
);
1522 vals
=ldap_get_values(ldap
,each
,"pgpkeyexpiretime");
1523 if(vals
!=NULL
&& strlen(vals
[0])==15)
1525 fprintf(output
,"%u",
1526 (unsigned int)ldap2epochtime(vals
[0]));
1527 ldap_value_free(vals
);
1532 vals
=ldap_get_values(ldap
,each
,"pgprevoked");
1535 if(atoi(vals
[0])==1)
1536 fprintf(output
,"r");
1537 ldap_value_free(vals
);
1540 vals
=ldap_get_values(ldap
,each
,"pgpdisabled");
1543 if(atoi(vals
[0])==1)
1544 fprintf(output
,"d");
1545 ldap_value_free(vals
);
1549 /* This is not yet specified in the keyserver
1550 protocol, but may be someday. */
1553 vals
=ldap_get_values(ldap
,each
,"modifytimestamp");
1554 if(vals
!=NULL
&& strlen(vals
[0])==15)
1556 fprintf(output
,"%u",
1557 (unsigned int)ldap2epochtime(vals
[0]));
1558 ldap_value_free(vals
);
1562 fprintf(output
,"\n");
1564 /* Now print all the uids that have this certid */
1565 uids
=ldap_first_entry(ldap
,res
);
1568 vals
=ldap_get_values(ldap
,uids
,"pgpcertid");
1571 if(strcasecmp(certid
[0],vals
[0])==0)
1575 fprintf(output
,"uid:");
1577 uidvals
=ldap_get_values(ldap
,uids
,"pgpuserid");
1580 /* Need to escape any colons */
1581 printquoted(output
,uidvals
[0],':');
1582 ldap_value_free(uidvals
);
1585 fprintf(output
,"\n");
1588 ldap_value_free(vals
);
1591 uids
=ldap_next_entry(ldap
,uids
);
1595 ldap_value_free(certid
);
1598 each
=ldap_next_entry(ldap
,each
);
1603 free_keylist(dupelist
);
1605 fprintf(output
,"SEARCH %s END\n",searchkey
);
1607 return KEYSERVER_OK
;
1611 fail_all(struct keylist
*keylist
,int err
)
1616 if(opt
->action
==KS_SEARCH
)
1618 fprintf(output
,"SEARCH ");
1621 fprintf(output
,"%s ",keylist
->str
);
1622 keylist
=keylist
->next
;
1624 fprintf(output
,"FAILED %d\n",err
);
1629 fprintf(output
,"KEY %s FAILED %d\n",keylist
->str
,err
);
1630 keylist
=keylist
->next
;
1635 find_basekeyspacedn(void)
1638 char *attr
[]={"namingContexts",NULL
,NULL
,NULL
};
1642 /* Look for namingContexts */
1643 err
=ldap_search_s(ldap
,"",LDAP_SCOPE_BASE
,"(objectClass=*)",attr
,0,&res
);
1644 if(err
==LDAP_SUCCESS
)
1646 context
=ldap_get_values(ldap
,res
,"namingContexts");
1649 attr
[0]="pgpBaseKeySpaceDN";
1650 attr
[1]="pgpVersion";
1651 attr
[2]="pgpSoftware";
1655 /* We found some, so try each namingContext as the search base
1656 and look for pgpBaseKeySpaceDN. Because we found this, we
1657 know we're talking to a regular-ish LDAP server and not a
1660 for(i
=0;context
[i
] && !basekeyspacedn
;i
++)
1663 LDAPMessage
*si_res
;
1666 object
=malloc(17+strlen(context
[i
])+1);
1670 strcpy(object
,"cn=pgpServerInfo,");
1671 strcat(object
,context
[i
]);
1673 err
=ldap_search_s(ldap
,object
,LDAP_SCOPE_BASE
,
1674 "(objectClass=*)",attr
,0,&si_res
);
1677 if(err
==LDAP_NO_SUCH_OBJECT
)
1679 else if(err
!=LDAP_SUCCESS
)
1682 vals
=ldap_get_values(ldap
,si_res
,"pgpBaseKeySpaceDN");
1685 basekeyspacedn
=strdup(vals
[0]);
1686 ldap_value_free(vals
);
1691 vals
=ldap_get_values(ldap
,si_res
,"pgpSoftware");
1694 fprintf(console
,"Server: \t%s\n",vals
[0]);
1695 ldap_value_free(vals
);
1698 vals
=ldap_get_values(ldap
,si_res
,"pgpVersion");
1701 fprintf(console
,"Version:\t%s\n",vals
[0]);
1702 ldap_value_free(vals
);
1706 ldap_msgfree(si_res
);
1709 ldap_value_free(context
);
1716 /* We don't have an answer yet, which means the server might be
1717 a LDAP keyserver. */
1719 LDAPMessage
*si_res
;
1721 attr
[0]="pgpBaseKeySpaceDN";
1725 err
=ldap_search_s(ldap
,"cn=pgpServerInfo",LDAP_SCOPE_BASE
,
1726 "(objectClass=*)",attr
,0,&si_res
);
1727 if(err
!=LDAP_SUCCESS
)
1730 /* For the LDAP keyserver, this is always "OU=ACTIVE,O=PGP
1731 KEYSPACE,C=US", but it might not be in the future. */
1733 vals
=ldap_get_values(ldap
,si_res
,"baseKeySpaceDN");
1736 basekeyspacedn
=strdup(vals
[0]);
1737 ldap_value_free(vals
);
1742 vals
=ldap_get_values(ldap
,si_res
,"software");
1745 fprintf(console
,"Server: \t%s\n",vals
[0]);
1746 ldap_value_free(vals
);
1750 vals
=ldap_get_values(ldap
,si_res
,"version");
1754 fprintf(console
,"Version:\t%s\n",vals
[0]);
1756 /* If the version is high enough, use the new pgpKeyV2
1757 attribute. This design if iffy at best, but it matches how
1758 PGP does it. I figure the NAI folks assumed that there would
1759 never be a LDAP keyserver vendor with a different numbering
1762 pgpkeystr
="pgpKeyV2";
1764 ldap_value_free(vals
);
1767 ldap_msgfree(si_res
);
1770 return LDAP_SUCCESS
;
1774 show_help (FILE *fp
)
1776 fprintf (fp
,"-h\thelp\n");
1777 fprintf (fp
,"-V\tversion\n");
1778 fprintf (fp
,"-o\toutput to this file\n");
1782 main(int argc
,char *argv
[])
1784 int port
=0,arg
,err
,ret
=KEYSERVER_INTERNAL_ERROR
;
1785 char line
[MAX_LINE
],*binddn
=NULL
,*bindpw
=NULL
;
1786 int failed
=0,use_ssl
=0,use_tls
=0,bound
=0;
1787 struct keylist
*keylist
=NULL
,*keyptr
=NULL
;
1791 /* Kludge to implement standard GNU options. */
1792 if (argc
> 1 && !strcmp (argv
[1], "--version"))
1794 fputs ("gpgkeys_ldap (GnuPG) " VERSION
"\n", stdout
);
1797 else if (argc
> 1 && !strcmp (argv
[1], "--help"))
1803 while((arg
=getopt(argc
,argv
,"hVo:"))!=-1)
1808 show_help (console
);
1809 return KEYSERVER_OK
;
1812 fprintf(stdout
,"%d\n%s\n",KEYSERVER_PROTO_VERSION
,VERSION
);
1813 return KEYSERVER_OK
;
1816 output
=fopen(optarg
,"w");
1819 fprintf(console
,"gpgkeys: Cannot open output file `%s': %s\n",
1820 optarg
,strerror(errno
));
1821 return KEYSERVER_INTERNAL_ERROR
;
1829 input
=fopen(argv
[optind
],"r");
1832 fprintf(console
,"gpgkeys: Cannot open input file `%s': %s\n",
1833 argv
[optind
],strerror(errno
));
1834 return KEYSERVER_INTERNAL_ERROR
;
1844 opt
=init_ks_options();
1846 return KEYSERVER_NO_MEMORY
;
1848 /* Get the command and info block */
1850 while(fgets(line
,MAX_LINE
,input
)!=NULL
)
1852 char optionstr
[MAX_OPTION
+1];
1857 err
=parse_ks_options(line
,opt
);
1866 if(sscanf(line
,"OPTION %" MKSTRING(MAX_OPTION
) "[^\n]\n",optionstr
)==1)
1869 char *start
=&optionstr
[0];
1871 optionstr
[MAX_OPTION
]='\0';
1873 if(strncasecmp(optionstr
,"no-",3)==0)
1876 start
=&optionstr
[3];
1879 if(strncasecmp(start
,"tls",3)==0)
1883 else if(start
[3]=='=')
1885 if(strcasecmp(&start
[4],"no")==0)
1887 else if(strcasecmp(&start
[4],"try")==0)
1889 else if(strcasecmp(&start
[4],"warn")==0)
1891 else if(strcasecmp(&start
[4],"require")==0)
1896 else if(start
[3]=='\0')
1899 else if(strncasecmp(start
,"basedn",6)==0)
1903 free(basekeyspacedn
);
1904 basekeyspacedn
=NULL
;
1906 else if(start
[6]=='=')
1908 free(basekeyspacedn
);
1909 basekeyspacedn
=strdup(&start
[7]);
1912 fprintf(console
,"gpgkeys: out of memory while creating "
1914 ret
=KEYSERVER_NO_MEMORY
;
1921 else if(strncasecmp(start
,"binddn",6)==0)
1928 else if(start
[6]=='=')
1931 binddn
=strdup(&start
[7]);
1934 fprintf(console
,"gpgkeys: out of memory while creating "
1936 ret
=KEYSERVER_NO_MEMORY
;
1943 else if(strncasecmp(start
,"bindpw",6)==0)
1950 else if(start
[6]=='=')
1953 bindpw
=strdup(&start
[7]);
1956 fprintf(console
,"gpgkeys: out of memory while creating "
1958 ret
=KEYSERVER_NO_MEMORY
;
1972 fprintf(console
,"gpgkeys: no scheme supplied!\n");
1973 ret
=KEYSERVER_SCHEME_NOT_FOUND
;
1977 if(strcasecmp(opt
->scheme
,"ldaps")==0)
1984 port
=atoi(opt
->port
);
1988 fprintf(console
,"gpgkeys: no keyserver host provided\n");
1992 if(opt
->timeout
&& register_timeout()==-1)
1994 fprintf(console
,"gpgkeys: unable to register timeout handler\n");
1995 return KEYSERVER_INTERNAL_ERROR
;
1998 #if defined(LDAP_OPT_X_TLS_CACERTFILE) && defined(HAVE_LDAP_SET_OPTION)
2000 if(opt
->ca_cert_file
)
2002 err
=ldap_set_option(NULL
,LDAP_OPT_X_TLS_CACERTFILE
,opt
->ca_cert_file
);
2003 if(err
!=LDAP_SUCCESS
)
2005 fprintf(console
,"gpgkeys: unable to set ca-cert-file: %s\n",
2006 ldap_err2string(err
));
2007 ret
=KEYSERVER_INTERNAL_ERROR
;
2011 #endif /* LDAP_OPT_X_TLS_CACERTFILE && HAVE_LDAP_SET_OPTION */
2013 /* SSL trumps TLS */
2017 /* If it's a GET or a SEARCH, the next thing to come in is the
2018 keyids. If it's a SEND, then there are no keyids. */
2020 if(opt
->action
==KS_SEND
)
2021 while(fgets(line
,MAX_LINE
,input
)!=NULL
&& line
[0]!='\n');
2022 else if(opt
->action
==KS_GET
2023 || opt
->action
==KS_GETNAME
|| opt
->action
==KS_SEARCH
)
2027 struct keylist
*work
;
2029 if(fgets(line
,MAX_LINE
,input
)==NULL
)
2033 if(line
[0]=='\n' || line
[0]=='\0')
2036 work
=malloc(sizeof(struct keylist
));
2039 fprintf(console
,"gpgkeys: out of memory while "
2040 "building key list\n");
2041 ret
=KEYSERVER_NO_MEMORY
;
2045 strcpy(work
->str
,line
);
2047 /* Trim the trailing \n */
2048 work
->str
[strlen(line
)-1]='\0';
2052 /* Always attach at the end to keep the list in proper
2053 order for searching */
2065 fprintf(console
,"gpgkeys: no keyserver command specified\n");
2069 /* Send the response */
2071 fprintf(output
,"VERSION %d\n",KEYSERVER_PROTO_VERSION
);
2072 fprintf(output
,"PROGRAM %s\n\n",VERSION
);
2076 fprintf(console
,"Host:\t\t%s\n",opt
->host
);
2078 fprintf(console
,"Port:\t\t%d\n",port
);
2079 fprintf(console
,"Command:\t%s\n",ks_action_to_string(opt
->action
));
2084 #if defined(LDAP_OPT_DEBUG_LEVEL) && defined(HAVE_LDAP_SET_OPTION)
2085 err
=ldap_set_option(NULL
,LDAP_OPT_DEBUG_LEVEL
,&opt
->debug
);
2086 if(err
!=LDAP_SUCCESS
)
2087 fprintf(console
,"gpgkeys: unable to set debug mode: %s\n",
2088 ldap_err2string(err
));
2090 fprintf(console
,"gpgkeys: debug level %d\n",opt
->debug
);
2092 fprintf(console
,"gpgkeys: not built with debugging support\n");
2096 /* We have a timeout set for the setup stuff since it could time out
2098 set_timeout(opt
->timeout
);
2100 /* Note that this tries all A records on a given host (or at least,
2102 ldap
=ldap_init(opt
->host
,port
);
2105 fprintf(console
,"gpgkeys: internal LDAP init error: %s\n",
2107 fail_all(keylist
,KEYSERVER_INTERNAL_ERROR
);
2113 #if defined(LDAP_OPT_X_TLS) && defined(HAVE_LDAP_SET_OPTION)
2114 int ssl
=LDAP_OPT_X_TLS_HARD
;
2116 err
=ldap_set_option(ldap
,LDAP_OPT_X_TLS
,&ssl
);
2117 if(err
!=LDAP_SUCCESS
)
2119 fprintf(console
,"gpgkeys: unable to make SSL connection: %s\n",
2120 ldap_err2string(err
));
2121 fail_all(keylist
,ldap_err_to_gpg_err(err
));
2125 if(!opt
->flags
.check_cert
)
2126 ssl
=LDAP_OPT_X_TLS_NEVER
;
2128 err
=ldap_set_option(NULL
,LDAP_OPT_X_TLS_REQUIRE_CERT
,&ssl
);
2129 if(err
!=LDAP_SUCCESS
)
2132 "gpgkeys: unable to set certificate validation: %s\n",
2133 ldap_err2string(err
));
2134 fail_all(keylist
,ldap_err_to_gpg_err(err
));
2138 fprintf(console
,"gpgkeys: unable to make SSL connection: %s\n",
2139 "not built with LDAPS support");
2140 fail_all(keylist
,KEYSERVER_INTERNAL_ERROR
);
2146 if((err
=find_basekeyspacedn()) || !basekeyspacedn
)
2148 fprintf(console
,"gpgkeys: unable to retrieve LDAP base: %s\n",
2149 err
?ldap_err2string(err
):"not found");
2150 fail_all(keylist
,ldap_err_to_gpg_err(err
));
2154 /* use_tls: 0=don't use, 1=try silently to use, 2=try loudly to use,
2161 fprintf(console
,"gpgkeys: unable to start TLS: %s\n",
2162 "not supported by the NAI LDAP keyserver");
2165 fail_all(keylist
,KEYSERVER_INTERNAL_ERROR
);
2171 #if defined(HAVE_LDAP_START_TLS_S) && defined(HAVE_LDAP_SET_OPTION)
2172 int ver
=LDAP_VERSION3
;
2174 err
=ldap_set_option(ldap
,LDAP_OPT_PROTOCOL_VERSION
,&ver
);
2176 #ifdef LDAP_OPT_X_TLS
2177 if(err
==LDAP_SUCCESS
)
2179 if(opt
->flags
.check_cert
)
2180 ver
=LDAP_OPT_X_TLS_HARD
;
2182 ver
=LDAP_OPT_X_TLS_NEVER
;
2184 err
=ldap_set_option(NULL
,LDAP_OPT_X_TLS_REQUIRE_CERT
,&ver
);
2188 if(err
==LDAP_SUCCESS
)
2189 err
=ldap_start_tls_s(ldap
,NULL
,NULL
);
2191 if(err
!=LDAP_SUCCESS
)
2193 if(use_tls
>=2 || opt
->verbose
>2)
2194 fprintf(console
,"gpgkeys: unable to start TLS: %s\n",
2195 ldap_err2string(err
));
2196 /* Are we forcing it? */
2199 fail_all(keylist
,ldap_err_to_gpg_err(err
));
2203 else if(opt
->verbose
>1)
2204 fprintf(console
,"gpgkeys: TLS started successfully.\n");
2207 fprintf(console
,"gpgkeys: unable to start TLS: %s\n",
2208 "not built with TLS support");
2211 fail_all(keylist
,KEYSERVER_INTERNAL_ERROR
);
2218 /* By default we don't bind as there is usually no need to. For
2219 cases where the server needs some authentication, the user can
2220 use binddn and bindpw for auth. */
2224 #ifdef HAVE_LDAP_SET_OPTION
2225 int ver
=LDAP_VERSION3
;
2227 err
=ldap_set_option(ldap
,LDAP_OPT_PROTOCOL_VERSION
,&ver
);
2228 if(err
!=LDAP_SUCCESS
)
2230 fprintf(console
,"gpgkeys: unable to go to LDAP 3: %s\n",
2231 ldap_err2string(err
));
2232 fail_all(keylist
,ldap_err_to_gpg_err(err
));
2238 fprintf(console
,"gpgkeys: LDAP bind to %s, pw %s\n",binddn
,
2239 bindpw
?">not shown<":">none<");
2240 err
=ldap_simple_bind_s(ldap
,binddn
,bindpw
);
2241 if(err
!=LDAP_SUCCESS
)
2243 fprintf(console
,"gpgkeys: internal LDAP bind error: %s\n",
2244 ldap_err2string(err
));
2245 fail_all(keylist
,ldap_err_to_gpg_err(err
));
2252 if(opt
->action
==KS_GET
)
2258 set_timeout(opt
->timeout
);
2260 if(get_key(keyptr
->str
)!=KEYSERVER_OK
)
2263 keyptr
=keyptr
->next
;
2266 else if(opt
->action
==KS_GETNAME
)
2272 set_timeout(opt
->timeout
);
2274 if(get_name(keyptr
->str
)!=KEYSERVER_OK
)
2277 keyptr
=keyptr
->next
;
2280 else if(opt
->action
==KS_SEND
)
2286 set_timeout(opt
->timeout
);
2290 if (send_key(&eof_seen
) != KEYSERVER_OK
)
2295 if (send_key_keyserver(&eof_seen
) != KEYSERVER_OK
)
2301 else if(opt
->action
==KS_SEARCH
)
2303 char *searchkey
=NULL
;
2306 set_timeout(opt
->timeout
);
2308 /* To search, we stick a * in between each key to search for.
2309 This means that if the user enters words, they'll get
2310 "enters*words". If the user "enters words", they'll get
2316 len
+=strlen(keyptr
->str
)+1;
2317 keyptr
=keyptr
->next
;
2320 searchkey
=malloc((len
*3)+1);
2323 ret
=KEYSERVER_NO_MEMORY
;
2324 fail_all(keylist
,KEYSERVER_NO_MEMORY
);
2333 ldap_quote(searchkey
,keyptr
->str
);
2334 strcat(searchkey
,"*");
2335 keyptr
=keyptr
->next
;
2338 /* Nail that last "*" */
2340 searchkey
[strlen(searchkey
)-1]='\0';
2342 if(search_key(searchkey
)!=KEYSERVER_OK
)
2348 assert (!"bad action");
2355 while(keylist
!=NULL
)
2357 struct keylist
*current
=keylist
;
2358 keylist
=keylist
->next
;
2368 free_ks_options(opt
);
2370 if(ldap
!=NULL
&& bound
)
2371 ldap_unbind_s(ldap
);
2373 free(basekeyspacedn
);