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/>.
22 #include "winbindd/winbindd.h"
23 #include "idmap_hash.h"
26 XFILE
*lw_map_file
= NULL
;
28 /*********************************************************************
29 ********************************************************************/
31 static bool mapfile_open(void)
33 const char *mapfile_name
= NULL
;
35 /* If we have an open handle, just reset it */
38 return (x_tseek(lw_map_file
, 0, SEEK_SET
) == 0);
41 mapfile_name
= lp_parm_const_string(-1, "idmap_hash", "name_map", NULL
);
46 lw_map_file
= x_fopen(mapfile_name
, O_RDONLY
, 0);
48 DEBUG(0,("can't open idmap_hash:name_map (%s). Error %s\n",
49 mapfile_name
, strerror(errno
) ));
56 /*********************************************************************
57 ********************************************************************/
59 static bool mapfile_read_line(fstring key
, fstring value
)
68 if ((p
= x_fgets(buffer
, sizeof(buffer
)-1, lw_map_file
)) == NULL
) {
72 /* Strip newlines and carriage returns */
74 len
= strlen_m(buffer
) - 1;
75 while ((buffer
[len
] == '\n') || (buffer
[len
] == '\r')) {
80 if ((p
= strchr_m(buffer
, '=')) == NULL
) {
81 DEBUG(0,("idmap_hash: Bad line in name_map (%s)\n", buffer
));
93 if (!trim_char(key
, ' ', ' '))
96 if (!trim_char(value
, ' ', ' '))
102 /*********************************************************************
103 ********************************************************************/
105 static bool mapfile_close(void)
109 ret
= x_fclose(lw_map_file
);
117 /*********************************************************************
118 ********************************************************************/
120 NTSTATUS
mapfile_lookup_key(TALLOC_CTX
*ctx
, const char *value
, char **key
)
122 fstring r_key
, r_value
;
123 NTSTATUS ret
= NT_STATUS_NOT_FOUND
;
126 return NT_STATUS_OBJECT_PATH_NOT_FOUND
;
128 while (mapfile_read_line(r_key
, r_value
))
130 if (strequal(r_value
, value
)) {
133 /* We're done once finishing this block */
134 *key
= talloc_strdup(ctx
, r_key
);
136 ret
= NT_STATUS_NO_MEMORY
;
147 /*********************************************************************
148 ********************************************************************/
150 NTSTATUS
mapfile_lookup_value(TALLOC_CTX
*ctx
, const char *key
, char **value
)
152 fstring r_key
, r_value
;
153 NTSTATUS ret
= NT_STATUS_NOT_FOUND
;
156 return NT_STATUS_OBJECT_PATH_NOT_FOUND
;
158 while (mapfile_read_line(r_key
, r_value
))
160 if (strequal(r_key
, key
)) {
163 /* We're done once finishing this block */
164 *value
= talloc_strdup(ctx
, r_value
);
166 ret
= NT_STATUS_NO_MEMORY
;