41554a157203b5813bfbe16af5be07747d9ab94e
[samba.git] / source3 / winbindd / idmap_hash / mapfile.c
1 /*
2  *  mapfile.c
3  *
4  *  Copyright (C) Gerald Carter  <jerry@samba.org>
5  *
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.
10  *
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.
15  *
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/>.
18  *
19  */
20
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>
27 #include "lib/util/xfile.h"
28
29 XFILE *lw_map_file = NULL;
30
31 /*********************************************************************
32  ********************************************************************/
33
34 static bool mapfile_open(void)
35 {
36         const char *mapfile_name = NULL;
37
38         /* If we have an open handle, just reset it */
39
40         if (lw_map_file) {
41                 return (x_tseek(lw_map_file, 0, SEEK_SET) == 0);
42         }
43
44         mapfile_name = lp_parm_const_string(-1, "idmap_hash", "name_map", NULL);
45         if (!mapfile_name) {
46                 return false;
47         }
48
49         lw_map_file = x_fopen(mapfile_name, O_RDONLY, 0);
50         if (!lw_map_file) {
51                 DEBUG(0,("can't open idmap_hash:name_map (%s). Error %s\n",
52                          mapfile_name, strerror(errno) ));
53                 return false;
54         }
55
56         return true;
57 }
58
59 /*********************************************************************
60  ********************************************************************/
61
62 static bool mapfile_read_line(fstring key, fstring value)
63 {
64         char buffer[1024];
65         char *p;
66         int len;
67
68         if (!lw_map_file)
69                 return false;
70
71         p = x_fgets(buffer, sizeof(buffer)-1, lw_map_file);
72         if (p == NULL) {
73                 return false;
74         }
75
76         /* Strip newlines and carriage returns */
77
78         len = strlen_m(buffer);
79         if (len == 0) {
80                 return false;
81         }
82         len -= 1;
83
84         while ((buffer[len] == '\n') || (buffer[len] == '\r')) {
85                 buffer[len--] = '\0';
86         }
87
88
89         if ((p = strchr_m(buffer, '=')) == NULL ) {
90                 DEBUG(0,("idmap_hash: Bad line in name_map (%s)\n", buffer));
91                 return false;
92         }
93
94         *p = '\0';
95         p++;
96
97         strlcpy(key, buffer, sizeof(fstring));
98         strlcpy(value, p, sizeof(fstring));
99
100         /* Eat whitespace */
101
102         if (!trim_char(key, ' ', ' '))
103                 return false;
104
105         if (!trim_char(value, ' ', ' '))
106                 return false;
107
108         return true;
109 }
110
111 /*********************************************************************
112  ********************************************************************/
113
114 static bool mapfile_close(void)
115 {
116         int ret = 0;
117         if (lw_map_file) {
118                 ret = x_fclose(lw_map_file);
119                 lw_map_file = NULL;
120         }
121
122         return (ret == 0);
123 }
124
125
126 /*********************************************************************
127  ********************************************************************/
128
129 NTSTATUS mapfile_lookup_key(TALLOC_CTX *ctx, const char *value, char **key)
130 {
131         fstring r_key, r_value;
132         NTSTATUS ret = NT_STATUS_NOT_FOUND;
133
134         if (!mapfile_open())
135                 return NT_STATUS_OBJECT_PATH_NOT_FOUND;
136
137         while (mapfile_read_line(r_key, r_value))
138         {
139                 if (strequal(r_value, value)) {
140                         ret = NT_STATUS_OK;
141
142                         /* We're done once finishing this block */
143                         *key = talloc_strdup(ctx, r_key);
144                         if (!*key) {
145                                 ret = NT_STATUS_NO_MEMORY;
146                         }
147                         break;
148                 }
149         }
150
151         mapfile_close();
152
153         return ret;
154 }
155
156 /*********************************************************************
157  ********************************************************************/
158
159 NTSTATUS mapfile_lookup_value(TALLOC_CTX *ctx, const char *key, char **value)
160 {
161         fstring r_key, r_value;
162         NTSTATUS ret = NT_STATUS_NOT_FOUND;
163
164         if (!mapfile_open())
165                 return NT_STATUS_OBJECT_PATH_NOT_FOUND;
166
167         while (mapfile_read_line(r_key, r_value))
168         {
169                 if (strequal(r_key, key)) {
170                         ret = NT_STATUS_OK;
171
172                         /* We're done once finishing this block */
173                         *value = talloc_strdup(ctx, r_value);
174                         if (!*key) {
175                                 ret = NT_STATUS_NO_MEMORY;
176                         }
177                         break;
178                 }
179         }
180
181         mapfile_close();
182
183         return ret;
184 }