diff options
author | Arthur de Jong <arthur@arthurdejong.org> | 2006-11-24 12:10:14 +0100 |
---|---|---|
committer | Arthur de Jong <arthur@arthurdejong.org> | 2006-11-24 12:10:14 +0100 |
commit | 6b65c39a6ade0c5ee7608dc54cedd916eec6ae09 (patch) | |
tree | 704763a97a4035c0f959e86d924f606f17b298ab | |
parent | 693dd91108ec97ff9c69095f454ef68d530774fd (diff) |
implement server end of host name lookups (without IPv6 support sofar)
git-svn-id: http://arthurdejong.org/svn/nss-pam-ldapd/libnss_ldapd@115 ef36b2f9-881f-0410-afb5-c4e39611909c
-rw-r--r-- | nslcd-server.c | 2 | ||||
-rw-r--r-- | server/host.c | 236 | ||||
-rw-r--r-- | testnss.c | 119 |
3 files changed, 182 insertions, 175 deletions
diff --git a/nslcd-server.c b/nslcd-server.c index 47c7cd2..d8624d8 100644 --- a/nslcd-server.c +++ b/nslcd-server.c @@ -162,11 +162,9 @@ void nslcd_server_handlerequest(int sock) case NSLCD_ACTION_GROUP_BYGID: nslcd_group_bygid(fp); break; case NSLCD_ACTION_GROUP_BYMEMBER: nslcd_group_bymember(fp); break; case NSLCD_ACTION_GROUP_ALL: nslcd_group_all(fp); break; -/* case NSLCD_ACTION_HOST_BYNAME: nslcd_host_byname(fp); break; case NSLCD_ACTION_HOST_BYADDR: nslcd_host_byaddr(fp); break; case NSLCD_ACTION_HOST_ALL: nslcd_host_all(fp); break; -*/ case NSLCD_ACTION_NETGROUP_BYNAME: nslcd_netgroup_byname(fp); break; /* case NSLCD_ACTION_NETWORK_BYNAME: nslcd_network_byname(fp); break; diff --git a/server/host.c b/server/host.c index fbd2f62..eee4fd0 100644 --- a/server/host.c +++ b/server/host.c @@ -51,12 +51,36 @@ #include "ldap-nss.h" #include "util.h" +#include "nslcd-server.h" +#include "common.h" +#include "log.h" #ifndef MAXALIASES #define MAXALIASES 35 #endif -static struct ent_context *hosts_context = NULL; +/* write a single host entry to the stream */ +static int write_hostent(FILE *fp,struct hostent *result) +{ + int32_t tmpint32,tmp2int32,tmp3int32; + int numaddr,i; + /* write the host entry */ + WRITE_STRING(fp,result->h_name); + /* write the alias list */ + WRITE_STRINGLIST_NULLTERM(fp,result->h_aliases); + /* write the number of addresses */ + for (numaddr=0;result->h_addr_list[numaddr]!=NULL;numaddr++) + /*noting*/ ; + WRITE_INT32(fp,numaddr); + /* write the addresses */ + for (i=0;i<numaddr;i++) + { + WRITE_INT32(fp,result->h_addrtype); + WRITE_INT32(fp,result->h_length); + WRITE(fp,result->h_addr_list[i],result->h_length); + } + return 0; +} static enum nss_status _nss_ldap_parse_host (LDAPMessage * e, @@ -221,110 +245,138 @@ _nss_ldap_parse_hostv6 (LDAPMessage * e, } #endif -enum nss_status _nss_ldap_gethostbyname2_r (const char *name, int af, struct hostent * result, - char *buffer, size_t buflen, int *errnop, - int *h_errnop) +int nslcd_host_byname(FILE *fp) { - enum nss_status status; + int32_t tmpint32; + char *name; struct ldap_args a; - - LA_INIT (a); - LA_STRING (a) = name; - LA_TYPE (a) = LA_TYPE_STRING; - - status = _nss_ldap_getbyname (&a, - result, - buffer, - buflen, - errnop, - _nss_ldap_filt_gethostbyname, - LM_HOSTS, -#ifdef INET6 - (af == AF_INET6) ? - _nss_ldap_parse_hostv6 : -#endif - _nss_ldap_parse_hostv4); - - MAP_H_ERRNO (status, *h_errnop); - - return status; -} - -enum nss_status _nss_ldap_gethostbyname_r (const char *name, struct hostent * result, - char *buffer, size_t buflen, int *errnop, - int *h_errnop) -{ - return _nss_ldap_gethostbyname2_r (name, + int retv; + struct hostent result; + char buffer[1024]; + int errnop; + /* read request parameters */ + READ_STRING_ALLOC(fp,name); + /* log call */ + log_log(LOG_DEBUG,"nslcd_host_byname(%s)",name); + /* write the response header */ + WRITE_INT32(fp,NSLCD_VERSION); + WRITE_INT32(fp,NSLCD_ACTION_HOST_BYNAME); + /* do the LDAP request */ + LA_INIT(a); + LA_STRING(a)=name; + LA_TYPE(a)=LA_TYPE_STRING; + retv=nss2nslcd(_nss_ldap_getbyname(&a,&result,buffer,1024,&errnop,_nss_ldap_filt_gethostbyname,LM_HOSTS, #ifdef INET6 - (_res.options & RES_USE_INET6) ? - AF_INET6 : + (af == AF_INET6)?_nss_ldap_parse_hostv6:_nss_ldap_parse_hostv4)); +#else + _nss_ldap_parse_hostv4)); #endif - AF_INET, result, buffer, buflen, - errnop, h_errnop); + /* no more need for this string */ + free(name); + /* write the response */ + WRITE_INT32(fp,retv); + if (retv==NSLCD_RESULT_SUCCESS) + write_hostent(fp,&result); + WRITE_FLUSH(fp); + /* we're done */ + return 0; } -enum nss_status _nss_ldap_gethostbyaddr_r (struct in_addr * addr, int len, int type, - struct hostent * result, char *buffer, - size_t buflen, int *errnop, int *h_errnop) +int nslcd_host_byaddr(FILE *fp) { - enum nss_status status; + int32_t tmpint32; + int af; + int len; + char addr[64],name[1024]; struct ldap_args a; - - /* if querying by IPv6 address, make sure the address is "normalized" -- - * it should contain no leading zeros and all components of the address. - * still we can't fit an IPv6 address in an int, so who cares for now. - */ - - LA_INIT (a); - LA_STRING (a) = inet_ntoa (*addr); - LA_TYPE (a) = LA_TYPE_STRING; - - status = _nss_ldap_getbyname (&a, - result, - buffer, - buflen, - errnop, - _nss_ldap_filt_gethostbyaddr, - LM_HOSTS, + int retv; + struct hostent result; + char buffer[1024]; + int errnop; + /* read address family */ + READ_INT32(fp,af); + if ((af!=AF_INET)&&(af!=AF_INET6)) + { + log_log(LOG_WARNING,"incorrect address family specified: %d",af); + return -1; + } + /* read address length */ + READ_INT32(fp,len); + if ((len>64)||(len<=0)) + { + log_log(LOG_WARNING,"address length incorrect: %d",len); + return -1; + } + /* read address */ + READ(fp,addr,len); + /* translate the address to a string */ + if (inet_ntop(af,addr,name,1024)==NULL) + { + log_log(LOG_WARNING,"unable to convert address to string"); + return -1; + } + /* log call */ + log_log(LOG_DEBUG,"nslcd_host_byaddr(%s)",name); + /* write the response header */ + WRITE_INT32(fp,NSLCD_VERSION); + WRITE_INT32(fp,NSLCD_ACTION_HOST_BYADDR); + /* do the LDAP request */ + LA_INIT(a); + LA_STRING(a)=name; + LA_TYPE(a)=LA_TYPE_STRING; + retv=nss2nslcd(_nss_ldap_getbyname(&a,&result,buffer,1024,&errnop,_nss_ldap_filt_gethostbyaddr,LM_HOSTS, #ifdef INET6 - (type == AF_INET6) ? - _nss_ldap_parse_hostv6 : + (af == AF_INET6)?_nss_ldap_parse_hostv6:_nss_ldap_parse_hostv4)); +#else + _nss_ldap_parse_hostv4)); #endif - _nss_ldap_parse_hostv4); - - MAP_H_ERRNO (status, *h_errnop); - - return status; -} - -enum nss_status _nss_ldap_sethostent (void) -{ - LOOKUP_SETENT (hosts_context); + /* write the response */ + WRITE_INT32(fp,retv); + if (retv==NSLCD_RESULT_SUCCESS) + write_hostent(fp,&result); + WRITE_FLUSH(fp); + /* we're done */ + return 0; } -enum nss_status _nss_ldap_endhostent (void) +int nslcd_host_all(FILE *fp) { - LOOKUP_ENDENT (hosts_context); -} - -enum nss_status _nss_ldap_gethostent_r (struct hostent * result, char *buffer, size_t buflen, - int *errnop, int *h_errnop) -{ - enum nss_status status; - - status = _nss_ldap_getent (&hosts_context, - result, - buffer, - buflen, - errnop, - _nss_ldap_filt_gethostent, LM_HOSTS, + int32_t tmpint32; + static struct ent_context *host_context; + /* these are here for now until we rewrite the LDAP code */ + struct hostent result; + char buffer[1024]; + int errnop; + int retv; + /* log call */ + log_log(LOG_DEBUG,"nslcd_shadow_all()"); + /* write the response header */ + WRITE_INT32(fp,NSLCD_VERSION); + WRITE_INT32(fp,NSLCD_ACTION_HOST_ALL); + /* initialize context */ + if (_nss_ldap_ent_context_init(&host_context)==NULL) + return -1; + /* loop over all results */ + while ((retv=nss2nslcd(_nss_ldap_getent(&host_context,&result,buffer,1024,&errnop,_nss_ldap_filt_gethostent,LM_HOSTS, #ifdef INET6 - (_res.options & RES_USE_INET6) ? - _nss_ldap_parse_hostv6 : + (_res.options&RES_USE_INET6)?_nss_ldap_parse_hostv6:_nss_ldap_parse_hostv4 +#else + _nss_ldap_parse_hostv4 #endif - _nss_ldap_parse_hostv4); - - MAP_H_ERRNO (status, *h_errnop); - - return status; + )))==NSLCD_RESULT_SUCCESS) + { + /* write the result */ + WRITE_INT32(fp,retv); + if (retv==NSLCD_RESULT_SUCCESS) + write_hostent(fp,&result); + } + /* write the final result code */ + WRITE_INT32(fp,retv); + WRITE_FLUSH(fp); + /* FIXME: if a previous call returns what happens to the context? */ + _nss_ldap_enter(); + _nss_ldap_ent_context_release(host_context); + _nss_ldap_leave(); + /* we're done */ + return 0; } @@ -90,7 +90,6 @@ static void printhost(struct hostent *host) { int i,j; char buffer[1024]; - const char *res; printf("struct hostent {\n" " h_name=\"%s\",\n", host->h_name); @@ -107,9 +106,8 @@ static void printhost(struct hostent *host) printf(" h_length=%d,\n",host->h_length); for (i=0;host->h_addr_list[i]!=NULL;i++) { - res=inet_ntop(host->h_addrtype,host->h_addr_list[i], - buffer,host->h_length); - if (res!=NULL) + if (inet_ntop(host->h_addrtype,host->h_addr_list[i], + buffer,1024)!=NULL) { printf(" h_addr_list[%d]=%s,\n",i,buffer); } @@ -117,11 +115,12 @@ static void printhost(struct hostent *host) { printf(" h_addr_list[%d]=",i); for (j=0;j<host->h_length;j++) - printf("%02x",(int)host->h_addr_list[i][j]); + printf("%02x",(int)((const uint8_t*)host->h_addr_list[i])[j]); printf(",\n"); } } - printf(" h_addr_list[%d]=NULL\n",i); + printf(" h_addr_list[%d]=NULL\n" + "}\n",i); } static void printether(struct etherent *ether) @@ -204,10 +203,7 @@ int main(int argc,char *argv[]) if (res==NSS_STATUS_SUCCESS) printpasswd(&passwdresult); else - { - printf("errno=%d:%s\n",(int)errno,strerror(errno)); - printf("errnocp=%d:%s\n",(int)errnocp,strerror(errnocp)); - } + printf("errno=%d:%s\n",(int)errnocp,strerror(errnocp)); /* test getpwnam() with non-existing user */ printf("\nTEST getpwnam() with non-existing user\n"); @@ -216,10 +212,7 @@ int main(int argc,char *argv[]) if (res==NSS_STATUS_SUCCESS) printpasswd(&passwdresult); else - { - printf("errno=%d:%s\n",(int)errno,strerror(errno)); - printf("errnocp=%d:%s\n",(int)errnocp,strerror(errnocp)); - } + printf("errno=%d:%s\n",(int)errnocp,strerror(errnocp)); /* test getpwuid() */ printf("\nTEST getpwuid()\n"); @@ -228,10 +221,7 @@ int main(int argc,char *argv[]) if (res==NSS_STATUS_SUCCESS) printpasswd(&passwdresult); else - { - printf("errno=%d:%s\n",(int)errno,strerror(errno)); - printf("errnocp=%d:%s\n",(int)errnocp,strerror(errnocp)); - } + printf("errno=%d:%s\n",(int)errnocp,strerror(errnocp)); /* test {set,get,end}pwent() */ printf("\nTEST {set,get,end}pwent()\n"); @@ -243,8 +233,7 @@ int main(int argc,char *argv[]) printpasswd(&passwdresult); } printf("status=%s\n",nssstatus(res)); - printf("errno=%d:%s\n",(int)errno,strerror(errno)); - printf("errnocp=%d:%s\n",(int)errnocp,strerror(errnocp)); + printf("errno=%d:%s\n",(int)errnocp,strerror(errnocp)); res=_nss_ldap_endpwent(); printf("status=%s\n",nssstatus(res)); @@ -255,10 +244,7 @@ int main(int argc,char *argv[]) if (res==NSS_STATUS_SUCCESS) printalias(&aliasresult); else - { - printf("errno=%d:%s\n",(int)errno,strerror(errno)); - printf("errnocp=%d:%s\n",(int)errnocp,strerror(errnocp)); - } + printf("errno=%d:%s\n",(int)errnocp,strerror(errnocp)); /* test {set,get,end}aliasent() */ printf("\nTEST {set,get,end}aliasent()\n"); @@ -270,8 +256,7 @@ int main(int argc,char *argv[]) printalias(&aliasresult); } printf("status=%s\n",nssstatus(res)); - printf("errno=%d:%s\n",(int)errno,strerror(errno)); - printf("errnocp=%d:%s\n",(int)errnocp,strerror(errnocp)); + printf("errno=%d:%s\n",(int)errnocp,strerror(errnocp)); res=_nss_ldap_endaliasent(); printf("status=%s\n",nssstatus(res)); @@ -282,10 +267,7 @@ int main(int argc,char *argv[]) if (res==NSS_STATUS_SUCCESS) printgroup(&groupresult); else - { - printf("errno=%d:%s\n",(int)errno,strerror(errno)); - printf("errnocp=%d:%s\n",(int)errnocp,strerror(errnocp)); - } + printf("errno=%d:%s\n",(int)errnocp,strerror(errnocp)); /* test getgrgid() */ printf("\nTEST getgrgid()\n"); @@ -294,10 +276,7 @@ int main(int argc,char *argv[]) if (res==NSS_STATUS_SUCCESS) printgroup(&groupresult); else - { - printf("errno=%d:%s\n",(int)errno,strerror(errno)); - printf("errnocp=%d:%s\n",(int)errnocp,strerror(errnocp)); - } + printf("errno=%d:%s\n",(int)errnocp,strerror(errnocp)); /* test initgroups() */ printf("\nTEST initgroups()\n"); @@ -311,10 +290,7 @@ int main(int argc,char *argv[]) } } else - { - printf("errno=%d:%s\n",(int)errno,strerror(errno)); - printf("errnocp=%d:%s\n",(int)errnocp,strerror(errnocp)); - } + printf("errno=%d:%s\n",(int)errnocp,strerror(errnocp)); /* test {set,get,end}grent() */ printf("\nTEST {set,get,end}grent()\n"); @@ -326,8 +302,7 @@ int main(int argc,char *argv[]) printgroup(&groupresult); } printf("status=%s\n",nssstatus(res)); - printf("errno=%d:%s\n",(int)errno,strerror(errno)); - printf("errnocp=%d:%s\n",(int)errnocp,strerror(errnocp)); + printf("errno=%d:%s\n",(int)errnocp,strerror(errnocp)); res=_nss_ldap_endgrent(); printf("status=%s\n",nssstatus(res)); @@ -339,57 +314,53 @@ int main(int argc,char *argv[]) printhost(&hostresult); else { - printf("errno=%d:%s\n",(int)errno,strerror(errno)); - printf("errnocp=%d:%s\n",(int)errnocp,strerror(errnocp)); - printf("h_errno=%d:%s\n",(int)h_errno,hstrerror(h_errno)); - printf("h_errnocp=%d:%s\n",(int)h_errnocp,hstrerror(h_errnocp)); + printf("errno=%d:%s\n",(int)errnocp,strerror(errnocp)); + printf("h_errno=%d:%s\n",(int)h_errnocp,hstrerror(h_errnocp)); } /* test gethostbyname2(AF_INET6) */ +/* this is currently unsupported printf("\nTEST gethostbyname2(AF_INET6)\n"); - res=_nss_ldap_gethostbyname2_r("oostc",AF_INET6,&hostresult,buffer,1024,&errnocp,&h_errnocp); + res=_nss_ldap_gethostbyname2_r("appelscha",AF_INET6,&hostresult,buffer,1024,&errnocp,&h_errnocp); printf("status=%s\n",nssstatus(res)); if (res==NSS_STATUS_SUCCESS) printhost(&hostresult); else { - printf("errno=%d:%s\n",(int)errno,strerror(errno)); - printf("errnocp=%d:%s\n",(int)errnocp,strerror(errnocp)); - printf("h_errno=%d:%s\n",(int)h_errno,hstrerror(h_errno)); - printf("h_errnocp=%d:%s\n",(int)h_errnocp,hstrerror(h_errnocp)); + printf("errno=%d:%s\n",(int)errnocp,strerror(errnocp)); + printf("h_errno=%d:%s\n",(int)h_errnocp,hstrerror(h_errnocp)); } +*/ /* test gethostbyaddr(AF_INET) */ printf("\nTEST gethostbyaddr(AF_INET)\n"); inet_pton(AF_INET,"192.43.210.81",address); - res=_nss_ldap_gethostbyaddr_r((void *)address,sizeof(struct in_addr),AF_INET, + res=_nss_ldap_gethostbyaddr_r(address,sizeof(struct in_addr),AF_INET, &hostresult,buffer,1024,&errnocp,&h_errnocp); printf("status=%s\n",nssstatus(res)); if (res==NSS_STATUS_SUCCESS) printhost(&hostresult); else { - printf("errno=%d:%s\n",(int)errno,strerror(errno)); - printf("errnocp=%d:%s\n",(int)errnocp,strerror(errnocp)); - printf("h_errno=%d:%s\n",(int)h_errno,hstrerror(h_errno)); - printf("h_errnocp=%d:%s\n",(int)h_errnocp,hstrerror(h_errnocp)); + printf("errno=%d:%s\n",(int)errnocp,strerror(errnocp)); + printf("h_errno=%d:%s\n",(int)h_errnocp,hstrerror(h_errnocp)); } /* test gethostbyaddr(AF_INET6) */ +/* this is currently unsupported printf("\nTEST gethostbyaddr(AF_INET6)\n"); inet_pton(AF_INET6,"2001:200:0:8002:203:47ff:fea5:3085",address); - res=_nss_ldap_gethostbyaddr_r((void *)address,sizeof(struct in6_addr),AF_INET6, + res=_nss_ldap_gethostbyaddr_r(address,sizeof(struct in6_addr),AF_INET6, &hostresult,buffer,1024,&errnocp,&h_errnocp); printf("status=%s\n",nssstatus(res)); if (res==NSS_STATUS_SUCCESS) printhost(&hostresult); else { - printf("errno=%d:%s\n",(int)errno,strerror(errno)); - printf("errnocp=%d:%s\n",(int)errnocp,strerror(errnocp)); - printf("h_errno=%d:%s\n",(int)h_errno,hstrerror(h_errno)); - printf("h_errnocp=%d:%s\n",(int)h_errnocp,hstrerror(h_errnocp)); + printf("errno=%d:%s\n",(int)errnocp,strerror(errnocp)); + printf("h_errno=%d:%s\n",(int)h_errnocp,hstrerror(h_errnocp)); } +*/ /* test {set,get,end}hostent() */ printf("\nTEST {set,get,end}hostent()\n"); @@ -401,10 +372,8 @@ int main(int argc,char *argv[]) printhost(&hostresult); } printf("status=%s\n",nssstatus(res)); - printf("errno=%d:%s\n",(int)errno,strerror(errno)); - printf("errnocp=%d:%s\n",(int)errnocp,strerror(errnocp)); - printf("h_errno=%d:%s\n",(int)h_errno,hstrerror(h_errno)); - printf("h_errnocp=%d:%s\n",(int)h_errnocp,hstrerror(h_errnocp)); + printf("errno=%d:%s\n",(int)errnocp,strerror(errnocp)); + printf("h_errno=%d:%s\n",(int)h_errnocp,hstrerror(h_errnocp)); res=_nss_ldap_endhostent(); printf("status=%s\n",nssstatus(res)); @@ -415,10 +384,7 @@ int main(int argc,char *argv[]) if (res==NSS_STATUS_SUCCESS) printether(ðerresult); else - { - printf("errno=%d:%s\n",(int)errno,strerror(errno)); - printf("errnocp=%d:%s\n",(int)errnocp,strerror(errnocp)); - } + printf("errno=%d:%s\n",(int)errnocp,strerror(errnocp)); /* test ether_ntohost() */ printf("\nTEST ether_ntohost()\n"); @@ -428,10 +394,7 @@ int main(int argc,char *argv[]) if (res==NSS_STATUS_SUCCESS) printether(ðerresult); else - { - printf("errno=%d:%s\n",(int)errno,strerror(errno)); - printf("errnocp=%d:%s\n",(int)errnocp,strerror(errnocp)); - } + printf("errno=%d:%s\n",(int)errnocp,strerror(errnocp)); /* test {set,get,end}etherent() */ printf("\nTEST {set,get,end}etherent()\n"); @@ -443,8 +406,7 @@ int main(int argc,char *argv[]) printether(ðerresult); } printf("status=%s\n",nssstatus(res)); - printf("errno=%d:%s\n",(int)errno,strerror(errno)); - printf("errnocp=%d:%s\n",(int)errnocp,strerror(errnocp)); + printf("errno=%d:%s\n",(int)errnocp,strerror(errnocp)); res=_nss_ldap_endetherent(); printf("status=%s\n",nssstatus(res)); @@ -455,10 +417,7 @@ int main(int argc,char *argv[]) if (res==NSS_STATUS_SUCCESS) printshadow(&shadowresult); else - { - printf("errno=%d:%s\n",(int)errno,strerror(errno)); - printf("errnocp=%d:%s\n",(int)errnocp,strerror(errnocp)); - } + printf("errno=%d:%s\n",(int)errnocp,strerror(errnocp)); /* test {set,get,end}spent() */ printf("\nTEST {set,get,end}spent()\n"); @@ -470,8 +429,7 @@ int main(int argc,char *argv[]) printshadow(&shadowresult); } printf("status=%s\n",nssstatus(res)); - printf("errno=%d:%s\n",(int)errno,strerror(errno)); - printf("errnocp=%d:%s\n",(int)errnocp,strerror(errnocp)); + printf("errno=%d:%s\n",(int)errnocp,strerror(errnocp)); res=_nss_ldap_endspent(); printf("status=%s\n",nssstatus(res)); @@ -485,8 +443,7 @@ int main(int argc,char *argv[]) printnetgroup(&netgroupresult); } printf("status=%s\n",nssstatus(res)); - printf("errno=%d:%s\n",(int)errno,strerror(errno)); - printf("errnocp=%d:%s\n",(int)errnocp,strerror(errnocp)); + printf("errno=%d:%s\n",(int)errnocp,strerror(errnocp)); res=_nss_ldap_endnetgrent(&netgroupresult); printf("status=%s\n",nssstatus(res)); |