summaryrefslogtreecommitdiffstats
path: root/src/back-sch-sss_idmap.c
blob: 6a312675ffc6ed37a128e4e00bfdabed53e170d2 (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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
/*
 * Copyright 2013-2017 Red Hat, Inc.
 *
 * 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; version 2 of the License.
 *
 * 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, write to the
 *
 *   Free Software Foundation, Inc.
 *   59 Temple Place, Suite 330
 *   Boston, MA 02111-1307 USA
 *
 */

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

#include <sys/types.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include <errno.h>
#include <pwd.h>
#include <grp.h>
#include "back-sch-nss.h"

/* SSSD only exposes *_timeout() variants if the following symbol is defined */
#define IPA_389DS_PLUGIN_HELPER_CALLS
#include <sss_nss_idmap.h>

struct nss_ops_ctx {
	unsigned int timeout;
};

static enum nss_status __convert_sss_nss2nss_status(int errcode) {
	enum nss_status ret = NSS_STATUS_UNAVAIL;

	if (errcode == 0) {
		ret = NSS_STATUS_SUCCESS;
	} else if (errcode == ENOENT) {
		ret = NSS_STATUS_NOTFOUND;
	} else if(errcode == ERANGE) {
		ret = NSS_STATUS_TRYAGAIN;
	} else if(errcode == ETIMEDOUT) {
		ret = NSS_STATUS_UNAVAIL;
	} else if(errcode == ETIME) {
		ret = NSS_STATUS_TRYAGAIN;
	};

	return ret;
}

void backend_nss_init_context(struct nss_ops_ctx **nss_context)
{
	struct nss_ops_ctx *ctx = NULL;

	if (nss_context == NULL) {
		return;
	}

	ctx = calloc(1, sizeof(struct nss_ops_ctx));

	*nss_context = ctx;
}

void backend_nss_free_context(struct nss_ops_ctx **nss_context)
{
	if (nss_context == NULL) {
		return;
	}

	free((*nss_context));
	*nss_context = NULL;
}


void backend_nss_set_timeout(struct nss_ops_ctx *nss_context,
			     unsigned int timeout) {
	if (nss_context == NULL) {
		return;
	}

	nss_context->timeout = timeout;
}

/* TODO: handle buffers and memory allocation in this function */
void backend_nss_evict_user(struct nss_ops_ctx *nss_context,
			    const char *name) {
	if (nss_context == NULL) {
		return;
	}

	(void) sss_nss_getpwnam_timeout(name, NULL,
					NULL, 0,
					NULL,
					SSS_NSS_EX_FLAG_INVALIDATE_CACHE,
					nss_context->timeout);
}

/* TODO: handle buffers and memory allocation in this function */
void backend_nss_evict_group(struct nss_ops_ctx *nss_context,
			     const char *name) {
	if (nss_context == NULL) {
		return;
	}

	(void) sss_nss_getgrnam_timeout(name, NULL,
					NULL, 0,
					NULL,
					SSS_NSS_EX_FLAG_INVALIDATE_CACHE,
					nss_context->timeout);
}

enum nss_status backend_nss_getpwnam(struct nss_ops_ctx *nss_context,
				     const char *name, struct passwd *pwd,
				     char *buffer, size_t buflen,
				     struct passwd **result,
				     int *lerrno) {
	int ret = 0;

	if (nss_context == NULL) {
		return NSS_STATUS_UNAVAIL;
	}

	ret = sss_nss_getpwnam_timeout(name, pwd,
				       buffer, buflen,
				       result,
				       SSS_NSS_EX_FLAG_NO_FLAGS,
				       nss_context->timeout);

	if (ret != 0 && lerrno != NULL) {
		/* SSSD translates errno into return code */
		*lerrno = ret;
	}
	return __convert_sss_nss2nss_status(ret);
}

enum nss_status backend_nss_getpwuid(struct nss_ops_ctx *nss_context,
				     uid_t uid, struct passwd *pwd,
				     char *buffer, size_t buflen,
				     struct passwd **result,
				     int *lerrno) {

	int ret = 0;

	if (nss_context == NULL) {
		return NSS_STATUS_UNAVAIL;
	}

	ret = sss_nss_getpwuid_timeout(uid, pwd,
				       buffer, buflen,
				       result,
				       SSS_NSS_EX_FLAG_NO_FLAGS,
				       nss_context->timeout);
	if (ret != 0 && lerrno != NULL) {
		/* SSSD translates errno into return code */
		*lerrno = ret;
	}
	return __convert_sss_nss2nss_status(ret);
}

enum nss_status backend_nss_getgrnam(struct nss_ops_ctx *nss_context,
				     const char *name, struct group *grp,
				     char *buffer, size_t buflen,
				     struct group **result,
				     int *lerrno) {

	int ret = 0;

	if (nss_context == NULL) {
		return NSS_STATUS_UNAVAIL;
	}

	ret = sss_nss_getgrnam_timeout(name, grp,
				       buffer, buflen,
				       result,
				       SSS_NSS_EX_FLAG_NO_FLAGS,
				       nss_context->timeout);
	if (ret != 0 && lerrno != NULL) {
		/* SSSD translates errno into return code */
		*lerrno = ret;
	}
	return __convert_sss_nss2nss_status(ret);
}

enum nss_status backend_nss_getgrgid(struct nss_ops_ctx *nss_context,
				     gid_t gid, struct group *grp,
				     char *buffer, size_t buflen,
				     struct group **result,
				     int *lerrno) {

	int ret = 0;

	if (nss_context == NULL) {
		return NSS_STATUS_UNAVAIL;
	}

	ret = sss_nss_getgrgid_timeout(gid, grp,
				       buffer, buflen,
				       result,
				       SSS_NSS_EX_FLAG_NO_FLAGS,
				       nss_context->timeout);
	if (ret != 0 && lerrno != NULL) {
		/* SSSD translates errno into return code */
		*lerrno = ret;
	}
	return __convert_sss_nss2nss_status(ret);
}

enum nss_status backend_nss_getgrouplist(struct nss_ops_ctx *nss_context,
					 const char *name, gid_t group,
					 gid_t *groups, int *ngroups,
					 int *lerrno) {
	int ret = 0;

	if (nss_context == NULL) {
		return NSS_STATUS_UNAVAIL;
	}

	ret = sss_nss_getgrouplist_timeout(name, group,
					   groups, ngroups,
					   SSS_NSS_EX_FLAG_NO_FLAGS,
					   nss_context->timeout);
	if (ret != 0 && lerrno != NULL) {
		/* SSSD translates errno into return code */
		*lerrno = ret;
	}
	return __convert_sss_nss2nss_status(ret);
}