diff options
author | Arthur de Jong <arthur@arthurdejong.org> | 2006-11-10 14:08:48 +0100 |
---|---|---|
committer | Arthur de Jong <arthur@arthurdejong.org> | 2006-11-10 14:08:48 +0100 |
commit | b279ad846720add3b262db9e5b10c96390393a44 (patch) | |
tree | 2a25cf3b47982aa448f267ca291927fe7adf15c2 | |
parent | 58a82990fc8284539113f0b4b05002a434eeeb59 (diff) |
implement NSS-side ethers database lookups plus test code
git-svn-id: http://arthurdejong.org/svn/nss-pam-ldapd/libnss_ldapd@79 ef36b2f9-881f-0410-afb5-c4e39611909c
-rw-r--r-- | nslcd.h | 4 | ||||
-rw-r--r-- | nss/Makefile.am | 2 | ||||
-rw-r--r-- | nss/ethers.c | 114 | ||||
-rw-r--r-- | nss/exports.h | 18 | ||||
-rw-r--r-- | testnss.c | 51 |
5 files changed, 184 insertions, 5 deletions
@@ -68,6 +68,7 @@ /* used for transferring mac addresses */ #define LDF_ETHER \ + LDF_STRING(ETHER_NAME) \ LDF_TYPE(ETHER_ADDR,u_int8_t[6]) /* used for transferring group and membership information */ @@ -129,6 +130,9 @@ /* Request types. */ #define NSLCD_ACTION_ALIAS_BYNAME 4001 #define NSLCD_ACTION_ALIAS_ALL 4002 +#define NSLCD_ACTION_ETHER_BYNAME 3001 +#define NSLCD_ACTION_ETHER_BYETHER 3002 +#define NSLCD_ACTION_ETHER_ALL 3005 #define NSLCD_ACTION_GROUP_BYNAME 5001 #define NSLCD_ACTION_GROUP_BYGID 5002 #define NSLCD_ACTION_GROUP_BYMEMBER 5003 diff --git a/nss/Makefile.am b/nss/Makefile.am index 0b099f5..e4cb367 100644 --- a/nss/Makefile.am +++ b/nss/Makefile.am @@ -22,4 +22,4 @@ noinst_LIBRARIES = libnss.a libnss_a_SOURCES = common.c common.h exports.h ../nslcd-client.h \ ../nslcd.h ../nslcd-common.h \ - aliases.c group.c hosts.c passwd.c + aliases.c ethers.c group.c hosts.c passwd.c diff --git a/nss/ethers.c b/nss/ethers.c new file mode 100644 index 0000000..2ce6450 --- /dev/null +++ b/nss/ethers.c @@ -0,0 +1,114 @@ +/* + ethers.c - NSS lookup functions for ethers database + + Copyright (C) 2006 West Consulting + Copyright (C) 2006 Arthur de Jong + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public + License as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public + License along with this library; if not, write to the Free + Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, + MA 02110-1301 USA +*/ + +#include "config.h" + +#include <string.h> +#include <nss.h> +#include <errno.h> + +#include "exports.h" +#include "nslcd-client.h" +#include "common.h" + +/* macros for expanding the LDF_ETHER macro */ +#define LDF_STRING(field) READ_STRING_BUF(fp,field) +#define LDF_TYPE(field,type) READ_TYPE(fp,field,type) +#define ETHER_NAME result->e_name +#define ETHER_ADDR result->e_addr + +/* map a hostname to the corresponding ethernet address */ +enum nss_status _nss_ldap_gethostton_r( + const char *name,struct etherent *result, + char *buffer,size_t buflen,int *errnop) +{ + FILE *fp; + int32_t tmpint32; + size_t bufptr=0; + /* open socket and write request */ + OPEN_SOCK(fp); + WRITE_REQUEST(fp,NSLCD_ACTION_ETHER_BYNAME); + WRITE_STRING(fp,name); + WRITE_FLUSH(fp); + /* read response */ + READ_RESPONSEHEADER(fp,NSLCD_ACTION_ETHER_BYNAME); + READ_RESPONSE_CODE(fp); + LDF_ETHER; + /* close socket and we're done */ + fclose(fp); + return NSS_STATUS_SUCCESS; +} + +/* map an ethernet address to the corresponding hostname */ +enum nss_status _nss_ldap_getntohost_r( + const struct ether_addr *addr,struct etherent *result, + char *buffer,size_t buflen,int *errnop) +{ + FILE *fp; + int32_t tmpint32; + size_t bufptr=0; + /* open socket and write request */ + OPEN_SOCK(fp); + WRITE_REQUEST(fp,NSLCD_ACTION_ETHER_BYNAME); + WRITE_TYPE(fp,addr,u_int8_t[6]); + WRITE_FLUSH(fp); + /* read response */ + READ_RESPONSEHEADER(fp,NSLCD_ACTION_ETHER_BYNAME); + READ_RESPONSE_CODE(fp); + LDF_ETHER; + /* close socket and we're done */ + fclose(fp); + return NSS_STATUS_SUCCESS; +} + +/* thread-local file pointer to an ongoing request */ +static __thread FILE *etherentfp; +#define fp etherentfp + +enum nss_status _nss_ldap_setetherent(int stayopen) +{ + NSS_SETENT(NSLCD_ACTION_ETHER_ALL); +} + +enum nss_status _nss_ldap_getetherent_r( + struct etherent *result, + char *buffer,size_t buflen,int *errnop) +{ + int32_t tmpint32; + size_t bufptr=0; + /* check that we have a valid file descriptor */ + if (fp==NULL) + { + *errnop=ENOENT; + return NSS_STATUS_UNAVAIL; + } + /* read a response */ + READ_RESPONSE_CODE(fp); + LDF_ETHER; + /* return result code */ + return NSS_STATUS_SUCCESS; +} + +enum nss_status _nss_ldap_endetherent(void) +{ + NSS_ENDENT(); +} diff --git a/nss/exports.h b/nss/exports.h index 5f8b123..d782dd5 100644 --- a/nss/exports.h +++ b/nss/exports.h @@ -32,6 +32,16 @@ #include <pwd.h> #include <shadow.h> +/* We define struct etherent here because it does not seem to + be defined in any publicly available header file exposed + by glibc. This is taken from include/netinet/ether.h + of the glibc source. */ +struct etherent +{ + const char *e_name; + struct ether_addr e_addr; +}; + /* These are prototypes for functions exported from the ldap NSS module. For more complete definitions of these functions check the GLIBC @@ -53,10 +63,10 @@ enum nss_status _nss_ldap_getautomntent_r(void *private,const char **key,const c enum nss_status _nss_ldap_endautomntent(void **private); /* ethers - ethernet numbers */ -enum nss_status _nss_ldap_gethostton_r(const char *name,struct ether_addr *result,char *buffer,size_t buflen,int *errnop); -enum nss_status _nss_ldap_getntohost_r(struct ether_addr *addr,struct ether_addr *result,char *buffer,size_t buflen,int *errnop); -enum nss_status _nss_ldap_setetherent(void); -enum nss_status _nss_ldap_getetherent_r(struct ether_addr *result,char *buffer,size_t buflen,int *errnop); +enum nss_status _nss_ldap_gethostton_r(const char *name,struct etherent *resut,char *buffer,size_t buflen,int *errnop); +enum nss_status _nss_ldap_getntohost_r(const struct ether_addr *addr,struct etherent *eth,char *buffer,size_t buflen,int *errnop); +enum nss_status _nss_ldap_setetherent(int stayopen); +enum nss_status _nss_ldap_getetherent_r(struct etherent *result,char *buffer,size_t buflen,int *errnop); enum nss_status _nss_ldap_endetherent(void); /* group - groups of users */ @@ -124,6 +124,15 @@ static void printhost(struct hostent *host) printf(" h_addr_list[%d]=NULL\n",i); } +static void printether(struct etherent *ether) +{ + printf("struct etherent {\n" + " e_name=\"%s\",\n" + " e_addr=%s\n" + "}\n", + ether->e_name,ether_ntoa(&(ether->e_addr))); +} + /* the main program... */ int main(int argc,char *argv[]) { @@ -131,6 +140,7 @@ int main(int argc,char *argv[]) struct aliasent aliasresult; struct group groupresult; struct hostent hostresult; + struct etherent etherresult; char buffer[1024]; enum nss_status res; int errnocp,h_errnocp; @@ -349,5 +359,46 @@ int main(int argc,char *argv[]) res=_nss_ldap_endhostent(); printf("status=%s\n",nssstatus(res)); + /* test ether_hostton() */ + printf("\nTEST ether_hostton()\n"); + res=_nss_ldap_gethostton_r("appelscha",ðerresult,buffer,1024,&errnocp); + printf("status=%s\n",nssstatus(res)); + 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)); + } + + /* test ether_ntohost() */ + printf("\nTEST ether_ntohost()\n"); + res=_nss_ldap_getntohost_r(ether_aton("0:13:72:a4:39:c7"), + ðerresult,buffer,1024,&errnocp); + printf("status=%s\n",nssstatus(res)); + 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)); + } + + + /* test {set,get,end}etherent() */ + printf("\nTEST {set,get,end}etherent()\n"); + res=_nss_ldap_setetherent(1); + printf("status=%s\n",nssstatus(res)); + while ((res=_nss_ldap_getetherent_r(ðerresult,buffer,1024,&errnocp))==NSS_STATUS_SUCCESS) + { + printf("status=%s\n",nssstatus(res)); + 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)); + res=_nss_ldap_endetherent(); + printf("status=%s\n",nssstatus(res)); + return 0; } |