1 /*
2    ethers.c - NSS lookup functions for ethers database
3 
4    Copyright (C) 2006 West Consulting
5    Copyright (C) 2006-2015 Arthur de Jong
6    Copyright (C) 2010 Symas Corporation
7 
8    This library is free software; you can redistribute it and/or
9    modify it under the terms of the GNU Lesser General Public
10    License as published by the Free Software Foundation; either
11    version 2.1 of the License, or (at your option) any later version.
12 
13    This library is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16    Lesser General Public License for more details.
17 
18    You should have received a copy of the GNU Lesser General Public
19    License along with this library; if not, write to the Free Software
20    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21    02110-1301 USA
22 */
23 
24 #include "config.h"
25 
26 #include <string.h>
27 #include <errno.h>
28 
29 #include "prototypes.h"
30 #include "common.h"
31 #include "compat/attrs.h"
32 
33 /* read an ethernet entry from the stream */
read_etherent(TFILE * fp,struct etherent * result,char * buffer,size_t buflen,int * errnop)34 static nss_status_t read_etherent(TFILE *fp, struct etherent *result,
35                                   char *buffer, size_t buflen, int *errnop)
36 {
37   int32_t tmpint32;
38   size_t bufptr = 0;
39   memset(result, 0, sizeof(struct etherent));
40   READ_BUF_STRING(fp, result->e_name);
41   READ(fp, &(result->e_addr), sizeof(uint8_t[6]));
42   return NSS_STATUS_SUCCESS;
43 }
44 
45 #ifdef NSS_FLAVOUR_GLIBC
46 
47 /* map a hostname to the corresponding ethernet address */
NSS_NAME(gethostton_r)48 nss_status_t NSS_NAME(gethostton_r)(const char *name,
49                                     struct etherent *result, char *buffer,
50                                     size_t buflen, int *errnop)
51 {
52   NSS_GETONE(NSLCD_ACTION_ETHER_BYNAME,
53              WRITE_STRING(fp, name),
54              read_etherent(fp, result, buffer, buflen, errnop));
55 }
56 
57 /* map an ethernet address to the corresponding hostname */
NSS_NAME(getntohost_r)58 nss_status_t NSS_NAME(getntohost_r)(const struct ether_addr *addr,
59                                     struct etherent *result, char *buffer,
60                                     size_t buflen, int *errnop)
61 {
62   NSS_GETONE(NSLCD_ACTION_ETHER_BYETHER,
63              WRITE(fp, addr, sizeof(uint8_t[6])),
64              read_etherent(fp, result, buffer, buflen, errnop));
65 }
66 
67 /* thread-local file pointer to an ongoing request */
68 static TLS TFILE *etherentfp;
69 
70 /* open a connection to read all ether entries */
NSS_NAME(setetherent)71 nss_status_t NSS_NAME(setetherent)(int UNUSED(stayopen))
72 {
73   NSS_SETENT(etherentfp);
74 }
75 
76 /* read a single ethernet entry from the stream */
NSS_NAME(getetherent_r)77 nss_status_t NSS_NAME(getetherent_r)(struct etherent *result,
78                                      char *buffer, size_t buflen, int *errnop)
79 {
80   NSS_GETENT(etherentfp, NSLCD_ACTION_ETHER_ALL,
81              read_etherent(etherentfp, result, buffer, buflen, errnop));
82 }
83 
84 /* close the stream opened with setetherent() above */
NSS_NAME(endetherent)85 nss_status_t NSS_NAME(endetherent)(void)
86 {
87   NSS_ENDENT(etherentfp);
88 }
89 
90 #endif /* NSS_FLAVOUR_GLIBC */
91 
92 #ifdef NSS_FLAVOUR_SOLARIS
93 
94 /* we disable NSS_BUFCHECK because these functions do not use the buffer */
95 #undef NSS_BUFCHECK
96 #define NSS_BUFCHECK ;
97 
98 /* provide a fallback definition */
99 #ifndef NSS_BUFLEN_ETHERS
100 #define NSS_BUFLEN_ETHERS HOST_NAME_MAX
101 #endif /* NSS_BUFLEN_ETHERS */
102 
103 #ifdef HAVE_STRUCT_NSS_XBYY_ARGS_RETURNLEN
etherent2str(struct etherent * result,char * buffer,size_t buflen)104 static char *etherent2str(struct etherent *result, char *buffer,
105                           size_t buflen)
106 {
107   int res;
108   res = snprintf(buffer, buflen, "%s %s", ether_ntoa(&result->e_addr),
109                  result->e_name);
110   if ((res < 0) || (res >= (int)buflen))
111     return NULL;
112   return buffer;
113 }
114 #endif /* HAVE_STRUCT_NSS_XBYY_ARGS_RETURNLEN */
115 
read_result(TFILE * fp,nss_XbyY_args_t * args,int wantname)116 static nss_status_t read_result(TFILE *fp, nss_XbyY_args_t *args, int wantname)
117 {
118   struct etherent result;
119   char buffer[NSS_BUFLEN_ETHERS];
120   nss_status_t retv;
121   /* read the result entry from the stream */
122   retv = read_etherent(fp, &result, buffer, sizeof(buffer), &args->erange);
123   if (retv != NSS_STATUS_SUCCESS)
124     return retv;
125 #ifdef HAVE_STRUCT_NSS_XBYY_ARGS_RETURNLEN
126   /* try to return in string format if requested */
127   if ((args->buf.buffer != NULL) && (args->buf.buflen > 0))
128   {
129     if (etherent2str(&result, args->buf.buffer, args->buf.buflen) == NULL)
130     {
131       args->erange = 1;
132       return NSS_NOTFOUND;
133     }
134     args->returnval = args->buf.buffer;
135     args->returnlen = strlen(args->returnval);
136     return NSS_SUCCESS;
137   }
138 #endif /* HAVE_STRUCT_NSS_XBYY_ARGS_RETURNLEN */
139   /* return the result entry */
140   if (wantname)
141   {
142     /* we expect the buffer to have enough room for the name (buflen == 0) */
143     strcpy(args->buf.buffer, result.e_name);
144     args->returnval = args->buf.buffer;
145   }
146   else /* address */
147   {
148     memcpy(args->buf.result, &result.e_addr, sizeof(result.e_addr));
149     args->returnval = args->buf.result;
150   }
151   return NSS_SUCCESS;
152 }
153 
154 /* map a hostname to the corresponding ethernet address */
ethers_gethostton(nss_backend_t UNUSED (* be),void * args)155 static nss_status_t ethers_gethostton(nss_backend_t UNUSED(*be), void *args)
156 {
157   NSS_GETONE(NSLCD_ACTION_ETHER_BYNAME,
158              WRITE_STRING(fp, NSS_ARGS(args)->key.name),
159              read_result(fp, args, 0));
160 }
161 
162 /* map an ethernet address to the corresponding hostname */
ethers_getntohost(nss_backend_t UNUSED (* be),void * args)163 static nss_status_t ethers_getntohost(nss_backend_t UNUSED(*be), void *args)
164 {
165   struct ether_addr *addr = (struct ether_addr *)(NSS_ARGS(args)->key.ether);
166   NSS_GETONE(NSLCD_ACTION_ETHER_BYETHER,
167              WRITE(fp, addr, sizeof(uint8_t[6])),
168              read_result(fp, args, 1));
169 }
170 
171 static nss_backend_op_t ethers_ops[] = {
172   nss_ldap_destructor,
173   ethers_gethostton,
174   ethers_getntohost
175 };
176 
NSS_NAME(ethers_constr)177 nss_backend_t *NSS_NAME(ethers_constr)(const char UNUSED(*db_name),
178                                        const char UNUSED(*src_name),
179                                        const char UNUSED(*cfg_args))
180 {
181   return nss_ldap_constructor(ethers_ops, sizeof(ethers_ops));
182 }
183 
184 #endif /* NSS_FLAVOUR_SOLARIS */
185