summaryrefslogtreecommitdiffstats
path: root/source3/winbindd/idmap_hash/mapfile.c
blob: 82812a1a72d164c76f416bf29e8396c21a69c5c1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/*
 *  mapfile.c
 *
 *  Copyright (C) Gerald Carter  <jerry@samba.org>
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, see <http://www.gnu.org/licenses/>.
 *
 */

#include "includes.h"
#include "system/filesys.h"
#include "winbindd/winbindd.h"
#include "idmap.h"
#include "idmap_hash.h"

static FILE *lw_map_file = NULL;

/*********************************************************************
 ********************************************************************/

static bool mapfile_open(void)
{
	const char *mapfile_name = NULL;

	/* If we have an open handle, just reset it */

	if (lw_map_file) {
		return (fseek(lw_map_file, 0, SEEK_SET) == 0);
	}

	mapfile_name = lp_parm_const_string(-1, "idmap_hash", "name_map", NULL);
	if (!mapfile_name) {
		return false;
	}

	lw_map_file = fopen(mapfile_name, "r");
	if (!lw_map_file) {
		DEBUG(0,("can't open idmap_hash:name_map (%s). Error %s\n",
			 mapfile_name, strerror(errno) ));
		return false;
	}

	return true;
}

/*********************************************************************
 ********************************************************************/

static bool mapfile_read_line(fstring key, fstring value)
{
	char buffer[1024];
	char *p;
	int len;

	if (!lw_map_file)
		return false;

	p = fgets(buffer, sizeof(buffer)-1, lw_map_file);
	if (p == NULL) {
		return false;
	}

	/* Strip newlines and carriage returns */

	len = strlen_m(buffer);
	if (len == 0) {
		return false;
	}
	len -= 1;

	while ((buffer[len] == '\n') || (buffer[len] == '\r')) {
		buffer[len--] = '\0';
	}


	if ((p = strchr_m(buffer, '=')) == NULL ) {
		DEBUG(0,("idmap_hash: Bad line in name_map (%s)\n", buffer));
		return false;
	}

	*p = '\0';
	p++;

	strlcpy(key, buffer, sizeof(fstring));
	strlcpy(value, p, sizeof(fstring));

	/* Eat whitespace */

	if (!trim_char(key, ' ', ' '))
		return false;

	if (!trim_char(value, ' ', ' '))
		return false;

	return true;
}

/*********************************************************************
 ********************************************************************/

static bool mapfile_close(void)
{
	int ret = 0;
	if (lw_map_file) {
		ret = fclose(lw_map_file);
		lw_map_file = NULL;
	}

	return (ret == 0);
}


/*********************************************************************
 ********************************************************************/

NTSTATUS mapfile_lookup_key(TALLOC_CTX *ctx, const char *value, char **key)
{
	fstring r_key, r_value;
	NTSTATUS ret = NT_STATUS_NOT_FOUND;

	if (!mapfile_open())
		return NT_STATUS_OBJECT_PATH_NOT_FOUND;

	while (mapfile_read_line(r_key, r_value))
	{
		if (strequal(r_value, value)) {
			ret = NT_STATUS_OK;

			/* We're done once finishing this block */
			*key = talloc_strdup(ctx, r_key);
			if (!*key) {
				ret = NT_STATUS_NO_MEMORY;
			}
			break;
		}
	}

	mapfile_close();

	return ret;
}

/*********************************************************************
 ********************************************************************/

NTSTATUS mapfile_lookup_value(TALLOC_CTX *ctx, const char *key, char **value)
{
	fstring r_key, r_value;
	NTSTATUS ret = NT_STATUS_NOT_FOUND;

	if (!mapfile_open())
		return NT_STATUS_OBJECT_PATH_NOT_FOUND;

	while (mapfile_read_line(r_key, r_value))
	{
		if (strequal(r_key, key)) {
			ret = NT_STATUS_OK;

			/* We're done once finishing this block */
			*value = talloc_strdup(ctx, r_value);
			if (!*key) {
				ret = NT_STATUS_NO_MEMORY;
			}
			break;
		}
	}

	mapfile_close();

	return ret;
}