ctdb-server: Remove duplicate logic
[samba4-gss.git] / source3 / winbindd / idmap_hash / mapfile.c
blob82812a1a72d164c76f416bf29e8396c21a69c5c1
1 /*
2 * mapfile.c
4 * Copyright (C) Gerald Carter <jerry@samba.org>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
21 #include "includes.h"
22 #include "system/filesys.h"
23 #include "winbindd/winbindd.h"
24 #include "idmap.h"
25 #include "idmap_hash.h"
27 static FILE *lw_map_file = NULL;
29 /*********************************************************************
30 ********************************************************************/
32 static bool mapfile_open(void)
34 const char *mapfile_name = NULL;
36 /* If we have an open handle, just reset it */
38 if (lw_map_file) {
39 return (fseek(lw_map_file, 0, SEEK_SET) == 0);
42 mapfile_name = lp_parm_const_string(-1, "idmap_hash", "name_map", NULL);
43 if (!mapfile_name) {
44 return false;
47 lw_map_file = fopen(mapfile_name, "r");
48 if (!lw_map_file) {
49 DEBUG(0,("can't open idmap_hash:name_map (%s). Error %s\n",
50 mapfile_name, strerror(errno) ));
51 return false;
54 return true;
57 /*********************************************************************
58 ********************************************************************/
60 static bool mapfile_read_line(fstring key, fstring value)
62 char buffer[1024];
63 char *p;
64 int len;
66 if (!lw_map_file)
67 return false;
69 p = fgets(buffer, sizeof(buffer)-1, lw_map_file);
70 if (p == NULL) {
71 return false;
74 /* Strip newlines and carriage returns */
76 len = strlen_m(buffer);
77 if (len == 0) {
78 return false;
80 len -= 1;
82 while ((buffer[len] == '\n') || (buffer[len] == '\r')) {
83 buffer[len--] = '\0';
87 if ((p = strchr_m(buffer, '=')) == NULL ) {
88 DEBUG(0,("idmap_hash: Bad line in name_map (%s)\n", buffer));
89 return false;
92 *p = '\0';
93 p++;
95 strlcpy(key, buffer, sizeof(fstring));
96 strlcpy(value, p, sizeof(fstring));
98 /* Eat whitespace */
100 if (!trim_char(key, ' ', ' '))
101 return false;
103 if (!trim_char(value, ' ', ' '))
104 return false;
106 return true;
109 /*********************************************************************
110 ********************************************************************/
112 static bool mapfile_close(void)
114 int ret = 0;
115 if (lw_map_file) {
116 ret = fclose(lw_map_file);
117 lw_map_file = NULL;
120 return (ret == 0);
124 /*********************************************************************
125 ********************************************************************/
127 NTSTATUS mapfile_lookup_key(TALLOC_CTX *ctx, const char *value, char **key)
129 fstring r_key, r_value;
130 NTSTATUS ret = NT_STATUS_NOT_FOUND;
132 if (!mapfile_open())
133 return NT_STATUS_OBJECT_PATH_NOT_FOUND;
135 while (mapfile_read_line(r_key, r_value))
137 if (strequal(r_value, value)) {
138 ret = NT_STATUS_OK;
140 /* We're done once finishing this block */
141 *key = talloc_strdup(ctx, r_key);
142 if (!*key) {
143 ret = NT_STATUS_NO_MEMORY;
145 break;
149 mapfile_close();
151 return ret;
154 /*********************************************************************
155 ********************************************************************/
157 NTSTATUS mapfile_lookup_value(TALLOC_CTX *ctx, const char *key, char **value)
159 fstring r_key, r_value;
160 NTSTATUS ret = NT_STATUS_NOT_FOUND;
162 if (!mapfile_open())
163 return NT_STATUS_OBJECT_PATH_NOT_FOUND;
165 while (mapfile_read_line(r_key, r_value))
167 if (strequal(r_key, key)) {
168 ret = NT_STATUS_OK;
170 /* We're done once finishing this block */
171 *value = talloc_strdup(ctx, r_value);
172 if (!*key) {
173 ret = NT_STATUS_NO_MEMORY;
175 break;
179 mapfile_close();
181 return ret;