aboutsummaryrefslogtreecommitdiffstats
path: root/idmap_plugin.c
blob: 8fcb84607d3562b92af2ce2c69e159bc8bae2694 (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
/*
 * ID Mapping Plugin interface for cifs-utils
 * Copyright (C) 2012 Jeff Layton (jlayton@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/>.
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif /* HAVE_CONFIG_H */

#include <dlfcn.h>
#include <errno.h>
#include <stdint.h>
#include <sys/types.h>

#include "cifsidmap.h"
#include "idmap_plugin.h"

const char *plugin_errmsg;
static void *plugin;

static void *
resolve_symbol(const char *symbol_name)
{
	void *symbol;

	dlerror();
	symbol = dlsym(plugin, symbol_name);
	if (!symbol)
		plugin_errmsg = dlerror();
	return symbol;
}

/*
 * open the plugin. Note that we leave it open over the life of the
 * program. It gets closed on exit.
 */
static int
open_plugin(void)
{
	if (plugin)
		return 0;

	plugin = dlopen(IDMAP_PLUGIN_PATH, RTLD_LAZY);
	if (!plugin) {
		plugin_errmsg = dlerror();
		return -EIO;
	}

	return 0;
}

int
init_plugin(void **handle)
{
	int ret;
	int (*init)(void **, const char **);

	ret = open_plugin();
	if (ret)
		return ret;

	init = resolve_symbol("cifs_idmap_init_plugin");
	if (!init) {
		plugin_errmsg = "cifs_idmap_init_plugin not implemented";
		return -ENOSYS;
	}
	return (*init)(handle, &plugin_errmsg);
}

void
exit_plugin(void *handle)
{
	int (*exit)(void *);

	exit = resolve_symbol("cifs_idmap_exit_plugin");
	if (exit)
		(*exit)(handle);
}

int
sid_to_str(void *handle, const struct cifs_sid *sid, char **name)
{
	int (*entry)(void *, const struct cifs_sid *, char **);

	*(void **)(&entry) = resolve_symbol("cifs_idmap_sid_to_str");
	if (!entry) {
		plugin_errmsg = "cifs_idmap_sid_to_str not implemented";
		return -ENOSYS;
	}

	return (*entry)(handle, sid, name);
}

int
str_to_sid(void *handle, const char *name, struct cifs_sid *sid)
{
	int (*entry)(void *, const char *, struct cifs_sid *);

	*(void **)(&entry) = resolve_symbol("cifs_idmap_str_to_sid");
	if (!entry) {
		plugin_errmsg = "cifs_idmap_str_to_sid not implemented";
		return -ENOSYS;
	}

	return (*entry)(handle, name, sid);
}

int
sids_to_ids(void *handle, const struct cifs_sid *sid, const size_t num,
	  struct cifs_uxid *cuxid)
{
	int (*entry)(void *handle, const struct cifs_sid *sids,
			const size_t num, struct cifs_uxid *cuxid);

	*(void **)(&entry) = resolve_symbol("cifs_idmap_sids_to_ids");
	if (!entry) {
		plugin_errmsg = "cifs_idmap_sids_to_ids not implemented";
		return -ENOSYS;
	}

	return (*entry)(handle, sid, num, cuxid);
}

int
ids_to_sids(void *handle, const struct cifs_uxid *cuxid, const size_t num,
		struct cifs_sid *sid)
{
	int (*entry)(void *handle, const struct cifs_uxid *cuxid,
			const size_t num, struct cifs_sid *sid);

	*(void **)(&entry) = resolve_symbol("cifs_idmap_ids_to_sids");
	if (!entry) {
		plugin_errmsg = "cifs_idmap_ids_to_sids not implemented";
		return -ENOSYS;
	}

	return (*entry)(handle, cuxid, num, sid);
}