s3/packaging: pam_winbind has been moved to section 8.
[Samba/ekacnet.git] / source3 / winbindd / idmap_hash / mapfile.c
blob5ab1142ffe0182e63e1988f5f5765cd0e165decc
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 "winbindd/winbindd.h"
23 #include "idmap_hash.h"
24 #include <stdio.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 */
37 if (lw_map_file) {
38 return (x_tseek(lw_map_file, 0, SEEK_SET) == 0);
41 mapfile_name = lp_parm_const_string(-1, "idmap_hash", "name_map", NULL);
42 if (!mapfile_name) {
43 return false;
46 lw_map_file = x_fopen(mapfile_name, O_RDONLY, 0);
47 if (!lw_map_file) {
48 DEBUG(0,("can't open idmap_hash:name_map (%s). Error %s\n",
49 mapfile_name, strerror(errno) ));
50 return false;
53 return true;
56 /*********************************************************************
57 ********************************************************************/
59 static bool mapfile_read_line(fstring key, fstring value)
61 char buffer[1024];
62 char *p;
63 int len;
65 if (!lw_map_file)
66 return false;
68 if ((p = x_fgets(buffer, sizeof(buffer)-1, lw_map_file)) == NULL) {
69 return false;
72 /* Strip newlines and carriage returns */
74 len = strlen_m(buffer) - 1;
75 while ((buffer[len] == '\n') || (buffer[len] == '\r')) {
76 buffer[len--] = '\0';
80 if ((p = strchr_m(buffer, '=')) == NULL ) {
81 DEBUG(0,("idmap_hash: Bad line in name_map (%s)\n", buffer));
82 return false;
85 *p = '\0';
86 p++;
88 fstrcpy(key, buffer);
89 fstrcpy(value, p);
91 /* Eat whitespace */
93 if (!trim_char(key, ' ', ' '))
94 return false;
96 if (!trim_char(value, ' ', ' '))
97 return false;
99 return true;
102 /*********************************************************************
103 ********************************************************************/
105 static bool mapfile_close(void)
107 int ret = 0;
108 if (lw_map_file) {
109 ret = x_fclose(lw_map_file);
110 lw_map_file = NULL;
113 return (ret == 0);
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;
125 if (!mapfile_open())
126 return NT_STATUS_OBJECT_PATH_NOT_FOUND;
128 while (mapfile_read_line(r_key, r_value))
130 if (strequal(r_value, value)) {
131 ret = NT_STATUS_OK;
133 /* We're done once finishing this block */
134 *key = talloc_strdup(ctx, r_key);
135 if (!*key) {
136 ret = NT_STATUS_NO_MEMORY;
138 break;
142 mapfile_close();
144 return ret;
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;
155 if (!mapfile_open())
156 return NT_STATUS_OBJECT_PATH_NOT_FOUND;
158 while (mapfile_read_line(r_key, r_value))
160 if (strequal(r_key, key)) {
161 ret = NT_STATUS_OK;
163 /* We're done once finishing this block */
164 *value = talloc_strdup(ctx, r_value);
165 if (!*key) {
166 ret = NT_STATUS_NO_MEMORY;
168 break;
172 mapfile_close();
174 return ret;