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