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