WHATSNEW: Add release notes for Samba 4.0.24.
[Samba.git] / source3 / winbindd / idmap_hash / mapfile.c
bloba0e2b489cbb0e1f76a6caaa60adeb1a63f112e45
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"
26 #include <stdio.h>
28 XFILE *lw_map_file = NULL;
30 /*********************************************************************
31 ********************************************************************/
33 static bool mapfile_open(void)
35 const char *mapfile_name = NULL;
37 /* If we have an open handle, just reset it */
39 if (lw_map_file) {
40 return (x_tseek(lw_map_file, 0, SEEK_SET) == 0);
43 mapfile_name = lp_parm_const_string(-1, "idmap_hash", "name_map", NULL);
44 if (!mapfile_name) {
45 return false;
48 lw_map_file = x_fopen(mapfile_name, O_RDONLY, 0);
49 if (!lw_map_file) {
50 DEBUG(0,("can't open idmap_hash:name_map (%s). Error %s\n",
51 mapfile_name, strerror(errno) ));
52 return false;
55 return true;
58 /*********************************************************************
59 ********************************************************************/
61 static bool mapfile_read_line(fstring key, fstring value)
63 char buffer[1024];
64 char *p;
65 int len;
67 if (!lw_map_file)
68 return false;
70 p = x_fgets(buffer, sizeof(buffer)-1, lw_map_file);
71 if (p == NULL) {
72 return false;
75 /* Strip newlines and carriage returns */
77 len = strlen_m(buffer);
78 if (len == 0) {
79 return false;
81 len -= 1;
83 while ((buffer[len] == '\n') || (buffer[len] == '\r')) {
84 buffer[len--] = '\0';
88 if ((p = strchr_m(buffer, '=')) == NULL ) {
89 DEBUG(0,("idmap_hash: Bad line in name_map (%s)\n", buffer));
90 return false;
93 *p = '\0';
94 p++;
96 strlcpy(key, buffer, sizeof(fstring));
97 strlcpy(value, p, sizeof(fstring));
99 /* Eat whitespace */
101 if (!trim_char(key, ' ', ' '))
102 return false;
104 if (!trim_char(value, ' ', ' '))
105 return false;
107 return true;
110 /*********************************************************************
111 ********************************************************************/
113 static bool mapfile_close(void)
115 int ret = 0;
116 if (lw_map_file) {
117 ret = x_fclose(lw_map_file);
118 lw_map_file = NULL;
121 return (ret == 0);
125 /*********************************************************************
126 ********************************************************************/
128 NTSTATUS mapfile_lookup_key(TALLOC_CTX *ctx, const char *value, char **key)
130 fstring r_key, r_value;
131 NTSTATUS ret = NT_STATUS_NOT_FOUND;
133 if (!mapfile_open())
134 return NT_STATUS_OBJECT_PATH_NOT_FOUND;
136 while (mapfile_read_line(r_key, r_value))
138 if (strequal(r_value, value)) {
139 ret = NT_STATUS_OK;
141 /* We're done once finishing this block */
142 *key = talloc_strdup(ctx, r_key);
143 if (!*key) {
144 ret = NT_STATUS_NO_MEMORY;
146 break;
150 mapfile_close();
152 return ret;
155 /*********************************************************************
156 ********************************************************************/
158 NTSTATUS mapfile_lookup_value(TALLOC_CTX *ctx, const char *key, char **value)
160 fstring r_key, r_value;
161 NTSTATUS ret = NT_STATUS_NOT_FOUND;
163 if (!mapfile_open())
164 return NT_STATUS_OBJECT_PATH_NOT_FOUND;
166 while (mapfile_read_line(r_key, r_value))
168 if (strequal(r_key, key)) {
169 ret = NT_STATUS_OK;
171 /* We're done once finishing this block */
172 *value = talloc_strdup(ctx, r_value);
173 if (!*key) {
174 ret = NT_STATUS_NO_MEMORY;
176 break;
180 mapfile_close();
182 return ret;