1 /*************************************************
2 *     Exim - an Internet mail transport agent    *
3 *************************************************/
4 
5 /* Copyright (c) University of Cambridge 1995 - 2015 */
6 /* Copyright (c) The Exim Maintainers 2020 */
7 /* See the file NOTICE for conditions of use and distribution. */
8 
9 #include "../exim.h"
10 #include "lf_functions.h"
11 
12 #include <rpcsvc/ypclnt.h>
13 
14 
15 /*************************************************
16 *              Open entry point                  *
17 *************************************************/
18 
19 /* See local README for interface description. This serves for both
20 the "nis" and "nis0" lookup types. */
21 
22 static void *
nis_open(const uschar * filename,uschar ** errmsg)23 nis_open(const uschar * filename, uschar ** errmsg)
24 {
25 char *nis_domain;
26 if (yp_get_default_domain(&nis_domain) != 0)
27   {
28   *errmsg = US"failed to get default NIS domain";
29   return NULL;
30   }
31 return nis_domain;
32 }
33 
34 
35 
36 /*************************************************
37 *           Find entry point for nis             *
38 *************************************************/
39 
40 /* See local README for interface description. A separate function is used
41 for nis0 because they are so short it isn't worth trying to use any common
42 code. */
43 
44 static int
nis_find(void * handle,const uschar * filename,const uschar * keystring,int length,uschar ** result,uschar ** errmsg,uint * do_cache,const uschar * opts)45 nis_find(void * handle, const uschar * filename, const uschar * keystring,
46   int length, uschar ** result, uschar ** errmsg, uint * do_cache,
47   const uschar * opts)
48 {
49 int rc;
50 uschar *nis_data;
51 int nis_data_length;
52 do_cache = do_cache;   /* Placate picky compilers */
53 if ((rc = yp_match(CCS handle, CCS filename, CCS keystring, length,
54     CSS &nis_data, &nis_data_length)) == 0)
55   {
56   *result = string_copy(nis_data);
57   (*result)[nis_data_length] = 0;    /* remove final '\n' */
58   return OK;
59   }
60 return (rc == YPERR_KEY || rc == YPERR_MAP)? FAIL : DEFER;
61 }
62 
63 
64 
65 /*************************************************
66 *           Find entry point for nis0            *
67 *************************************************/
68 
69 /* See local README for interface description. */
70 
71 static int
nis0_find(void * handle,const uschar * filename,const uschar * keystring,int length,uschar ** result,uschar ** errmsg,uint * do_cache,const uschar * opts)72 nis0_find(void * handle, const uschar * filename, const uschar * keystring,
73   int length, uschar ** result, uschar ** errmsg, uint * do_cache,
74   const uschar * opts)
75 {
76 int rc;
77 uschar *nis_data;
78 int nis_data_length;
79 do_cache = do_cache;   /* Placate picky compilers */
80 if ((rc = yp_match(CCS handle, CCS filename, CCS keystring, length + 1,
81     CSS &nis_data, &nis_data_length)) == 0)
82   {
83   *result = string_copy(nis_data);
84   (*result)[nis_data_length] = 0;    /* remove final '\n' */
85   return OK;
86   }
87 return (rc == YPERR_KEY || rc == YPERR_MAP)? FAIL : DEFER;
88 }
89 
90 
91 
92 /*************************************************
93 *         Version reporting entry point          *
94 *************************************************/
95 
96 /* See local README for interface description. */
97 
98 #include "../version.h"
99 
100 void
nis_version_report(FILE * f)101 nis_version_report(FILE *f)
102 {
103 #ifdef DYNLOOKUP
104 fprintf(f, "Library version: NIS: Exim version %s\n", EXIM_VERSION_STR);
105 #endif
106 }
107 
108 
109 static lookup_info nis_lookup_info = {
110   .name = US"nis",			/* lookup name */
111   .type = 0,				/* not abs file, not query style*/
112   .open = nis_open,			/* open function */
113   .check = NULL,			/* check function */
114   .find = nis_find,			/* find function */
115   .close = NULL,			/* no close function */
116   .tidy = NULL,				/* no tidy function */
117   .quote = NULL,			/* no quoting function */
118   .version_report = nis_version_report             /* version reporting */
119 };
120 
121 static lookup_info nis0_lookup_info = {
122   .name = US"nis0",			/* lookup name */
123   .type = 0,				/* not absfile, not query style */
124   .open = nis_open,			/* sic */         /* open function */
125   .check = NULL,			/* check function */
126   .find = nis0_find,			/* find function */
127   .close = NULL,			/* no close function */
128   .tidy = NULL,				/* no tidy function */
129   .quote = NULL,			/* no quoting function */
130   .version_report = NULL                           /* no version reporting (redundant) */
131 };
132 
133 #ifdef DYNLOOKUP
134 #define nis_lookup_module_info _lookup_module_info
135 #endif
136 
137 static lookup_info *_lookup_list[] = { &nis_lookup_info, &nis0_lookup_info };
138 lookup_module_info nis_lookup_module_info = { LOOKUP_MODULE_INFO_MAGIC, _lookup_list, 2 };
139 
140 /* End of lookups/nis.c */
141