File: rfio_callhandlers.c

package info (click to toggle)
lcgdm 1.10.0-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 17,532 kB
  • sloc: ansic: 226,887; sh: 13,562; perl: 11,575; python: 11,572; cpp: 5,716; sql: 1,824; makefile: 1,301; fortran: 113
file content (462 lines) | stat: -rw-r--r-- 12,288 bytes parent folder | download | duplicates (2)
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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
/*
 * $Id: rfio_callhandlers.c 9756 2013-11-28 16:17:08Z dhsmith $
 */

/*
 * Copyright (C) 2005-2007 by CERN/IT/GD/CT
 * All rights reserved
 */

#ifndef lint
static char sccsid[] = "@(#)$RCSfile: rfio_callhandlers.c,v $ $Revision: 9756 $ $Date: 2013-11-28 17:17:08 +0100 (Thu, 28 Nov 2013) $ CERN IT-GD/CT Jean-Philippe Baud";
#endif /* not lint */

#include <errno.h>
#include <fcntl.h>
#include <sys/types.h>
#include <pwd.h>
#include <stdlib.h>
#include <string.h>
#if defined(_WIN32)
#define MAXHOSTNAMELEN 64
#else
#include <netdb.h>
#include <sys/param.h>          /* For MAXHOSTNAMELEN definition  */
#endif
#ifndef MAXHOSTNAMELEN
#define MAXHOSTNAMELEN 64
#endif
#include <sys/stat.h>
#if defined(_WIN32)
#define R_OK 4
#define W_OK 2
#define X_OK 1
#define F_OK 0
#else
#include <unistd.h>
#endif
#include "Csec_api.h"
#include "dpm_api.h"
#include "dpns_api.h"
#include "log.h"
#include "rfio_api.h"
#include "serrno.h"
extern Csec_context_t sec_ctx;
extern int set_authorization_id;
extern char data_interface[MAXHOSTNAMELEN];
extern char localdomain[MAXHOSTNAMELEN];
extern struct passwd stagersuperuser;

static struct {
	char *principal;
	uid_t uid;
	gid_t gid;
	char **fqan;
	char *voname;
	char *user_ca;
	int nbfqans;
} stored;

int rfio_handle_store_virtualid(const char *principal)
{
#ifdef CSEC
	if (set_authorization_id) {
#ifdef VIRTUAL_ID
		stored.user_ca = Csec_server_get_client_ca (&sec_ctx);
#ifdef USE_VOMS
		stored.voname = Csec_server_get_client_vo (&sec_ctx);
		stored.fqan = Csec_server_get_client_fqans (&sec_ctx, &stored.nbfqans);
#endif
		if (Cns_getidmapc (principal, stored.user_ca, stored.nbfqans > 1 ? 1 : stored.nbfqans,
		    (const char **)stored.fqan, &stored.uid, &stored.gid)) {
			log (LOG_ERR, "Could not verify %s by getting a virtual id from the dpm: %s !\n", principal, sstrerror (serrno));
			return (-1);
		}

		stored.principal = strdup (principal);
#endif
	}
#endif
	return (0);
}

int rfio_handle_access(const char *sfn, 
		     int mode,
		     const char *mech,
		     const char *principal,
		     uid_t uid,
		     gid_t gid,
		     int *need_user_check)
{
	char **fqan = NULL;
	int nbfqans = 0;
	char *p;
	char sfn1[CA_MAXSFNLEN+1];
	struct stat64 st;
	char *user_ca;
	char *voname = NULL;

	if (stat64 (sfn, &st) < 0) {
		serrno = errno;
		return (-1);
	}
	if ((st.st_mode & S_IFMT) != S_IFREG ||
	    (st.st_uid != stagersuperuser.pw_uid && st.st_gid != stagersuperuser.pw_gid)) {
		/* not a file managed by DPM */
		*need_user_check = 1;
		return (0);
	}
	strcpy (sfn1, data_interface);
	if (strchr (sfn1, '.') == NULL) {
		strcat (sfn1, ".");
		strcat (sfn1, localdomain);
	}
	p = sfn1 + strlen (sfn1);
	*p++ = ':';
	strcpy (p, sfn);

#ifdef CSEC
	if (stored.principal != NULL && !strcmp(stored.principal, principal)) {
		dpm_client_setAuthorizationId (stored.uid, stored.gid, (char *) mech, (char *) principal);
		Cns_client_setAuthorizationId (stored.uid, stored.gid, (char *) mech, (char *) principal);
		if (stored.voname && stored.fqan) {
			dpm_client_setVOMS_data (stored.voname, stored.fqan, stored.nbfqans);
			Cns_client_setVOMS_data (stored.voname, stored.fqan, stored.nbfqans);
		}
	} else if (set_authorization_id) {
#ifdef VIRTUAL_ID
		user_ca = Csec_server_get_client_ca (&sec_ctx);
#ifdef USE_VOMS
		voname = Csec_server_get_client_vo (&sec_ctx);
		fqan = Csec_server_get_client_fqans (&sec_ctx, &nbfqans);
#endif
		if (Cns_getidmapc (principal, user_ca, nbfqans > 1 ? 1 : nbfqans,
		    (const char **)fqan, &uid, &gid)) {
			log (LOG_ERR, "Could not get virtual id: %s !\n", sstrerror (serrno));
			return (-1);
		}
#endif
		dpm_client_setAuthorizationId (uid, gid, (char *) mech, (char *) principal);
		Cns_client_setAuthorizationId (uid, gid, (char *) mech, (char *) principal);
		if (voname && fqan) {
			dpm_client_setVOMS_data (voname, fqan, nbfqans);
			Cns_client_setVOMS_data (voname, fqan, nbfqans);
		}
	}
#endif
	Cns_set_selectsrvr (CNS_SSRV_NOTPATH);
	if (dpm_accessr (sfn1, mode) < 0) {
		if (serrno != SEOPNOTSUP || Cns_accessr (sfn1, mode) < 0)
			return (-1);
	}
	*need_user_check = 0;
	return (0);
}

int rfio_handle_open(const char *sfn, 
		     int flags,
		     int mode,
		     const char *mech,
		     const char *principal,
		     uid_t uid,
		     gid_t gid,
		     char **pfn, 
		     void **ctx,
		     int *need_user_check)
{
	int amode;
	char **fqan = NULL;
	int nbfqans = 0;
	char *p;
	char *parent;
	char sfn1[CA_MAXSFNLEN+1];
	struct stat64 st;
	char *user_ca;
	char *voname = NULL;

	if (stat64 (sfn, &st) < 0) {
		if (errno != ENOENT || (flags & O_CREAT) == 0) {
			serrno = errno;
			return (-1);
		}
		if ((parent = strdup (sfn)) == NULL) {
			serrno = errno;
			return (-1);
		}
		if ((p = strrchr (parent, '/')) == NULL) {
			free (parent);
			serrno = EINVAL;
			return (-1);
		}
		if (p != parent) {
			*p = '\0';
			p = parent;
		} else
			p = "/";
		if (stat64 (p, &st) < 0) {
			serrno = errno;
			free (parent);
			return (-1);
		}
		free (parent);
	}
	if (st.st_uid != stagersuperuser.pw_uid && st.st_gid != stagersuperuser.pw_gid) {
		/* not a file managed by DPM */
		*pfn = strdup(sfn);
		*need_user_check = 1;
		return (0);
	}
	strcpy (sfn1, data_interface);
	if (strchr (sfn1, '.') == NULL) {
		strcat (sfn1, ".");
		strcat (sfn1, localdomain);
	}
	p = sfn1 + strlen (sfn1);
	*p++ = ':';
	strcpy (p, sfn);
#ifdef CSEC
	if (stored.principal != NULL && !strcmp(stored.principal, principal)) {
		dpm_client_setAuthorizationId (stored.uid, stored.gid, (char *) mech, (char *) principal);
		Cns_client_setAuthorizationId (stored.uid, stored.gid, (char *) mech, (char *) principal);
		if (stored.voname && stored.fqan) {
			dpm_client_setVOMS_data (stored.voname, stored.fqan, stored.nbfqans);
			Cns_client_setVOMS_data (stored.voname, stored.fqan, stored.nbfqans);
		}
	} else if (set_authorization_id) {
#ifdef VIRTUAL_ID
		user_ca = Csec_server_get_client_ca (&sec_ctx);
#ifdef USE_VOMS
		voname = Csec_server_get_client_vo (&sec_ctx);
		fqan = Csec_server_get_client_fqans (&sec_ctx, &nbfqans);
#endif
		if (Cns_getidmapc (principal, user_ca, nbfqans > 1 ? 1 : nbfqans,
		    (const char **)fqan, &uid, &gid)) {
			log (LOG_ERR, "Could not get virtual id: %s !\n", sstrerror (serrno));
			return (-1);
		}
#endif
		dpm_client_setAuthorizationId (uid, gid, (char *) mech, (char *) principal);
		Cns_client_setAuthorizationId (uid, gid, (char *) mech, (char *) principal);
		if (voname && fqan) {
			dpm_client_setVOMS_data (voname, fqan, nbfqans);
			Cns_client_setVOMS_data (voname, fqan, nbfqans);
		}
	}
#endif
	Cns_set_selectsrvr (CNS_SSRV_NOTPATH);
	amode = (flags & (O_WRONLY|O_TRUNC|O_CREAT)) ? W_OK : R_OK;
	if (dpm_accessr (sfn1, amode) < 0) {
		if (serrno != SEOPNOTSUP || Cns_accessr (sfn1, amode) < 0)
			return (-1);
	}
	*pfn = strdup(sfn);
	*need_user_check = 0;
	return (0);
}

int rfio_handle_firstwrite(void *ctx)
{
	return (0);
}

int rfio_handle_close(void *ctx,
		      struct stat64 *filestat,
		      int close_status)
{
	return (0);
}

int rfio_handle_remote_open(const char *sfn, 
		     const char *mech,
		     const char *principal,
		     uid_t uid,
		     gid_t gid)
{
	char **fqan = NULL;
	int nbfqans = 0;
	char *user_ca;
	char *voname = NULL;

#ifdef CSEC
	if (stored.principal != NULL && !strcmp(stored.principal, principal)) {
		rfio_client_setAuthorizationId (stored.uid, stored.gid, (char *) mech, (char *) principal);
		if (stored.voname && stored.fqan)
			rfio_client_setVOMS_data (stored.voname, stored.fqan, stored.nbfqans);
	} else if (set_authorization_id) {
#ifdef VIRTUAL_ID
		user_ca = Csec_server_get_client_ca (&sec_ctx);
#ifdef USE_VOMS
		voname = Csec_server_get_client_vo (&sec_ctx);
		fqan = Csec_server_get_client_fqans (&sec_ctx, &nbfqans);
#endif
		if (Cns_getidmapc (principal, user_ca, nbfqans > 1 ? 1 : nbfqans,
		    (const char **)fqan, &uid, &gid)) {
			log (LOG_ERR, "Could not get virtual id: %s !\n", sstrerror (serrno));
			return (-1);
		}
#endif
		rfio_client_setAuthorizationId (uid, gid, (char *) mech, (char *) principal);
		if (voname && fqan)
			rfio_client_setVOMS_data (voname, fqan, nbfqans);
	}
#endif
	return (0);
}

int rfio_handle_stat(const char *sfn, 
		     int lflag,
		     const char *mech,
		     const char *principal,
		     uid_t uid,
		     gid_t gid,
		     struct stat *st,
		     int *need_user_check)
{
	char **fqan = NULL;
	int nbfqans = 0;
	char *p;
	char sfn1[CA_MAXSFNLEN+1];
	struct Cns_filestatg statbuf;
	char *user_ca;
	char *voname = NULL;

#if !defined(_WIN32)
	if (lflag) {
		if (lstat (sfn, st) < 0) {
			serrno = errno;
			return (-1);
		}
	} else
#endif
		if (stat (sfn, st) < 0) {
			serrno = errno;
			return (-1);
		}
	if ((st->st_mode & S_IFMT) != S_IFREG ||
	    (st->st_uid != stagersuperuser.pw_uid && st->st_gid != stagersuperuser.pw_gid)) {
		/* not a file managed by DPM */
		*need_user_check = 1;
		return (0);
	}
	strcpy (sfn1, data_interface);
	if (strchr (sfn1, '.') == NULL) {
		strcat (sfn1, ".");
		strcat (sfn1, localdomain);
	}
	p = sfn1 + strlen (sfn1);
	*p++ = ':';
	strcpy (p, sfn);
#ifdef CSEC
	if (stored.principal != NULL && !strcmp(stored.principal, principal)) {
		Cns_client_setAuthorizationId (stored.uid, stored.gid, (char *) mech, (char *) principal);
		if (stored.voname && stored.fqan)
			Cns_client_setVOMS_data (stored.voname, stored.fqan, stored.nbfqans);
	} else if (set_authorization_id) {
#ifdef VIRTUAL_ID
		user_ca = Csec_server_get_client_ca (&sec_ctx);
#ifdef USE_VOMS
		voname = Csec_server_get_client_vo (&sec_ctx);
		fqan = Csec_server_get_client_fqans (&sec_ctx, &nbfqans);
#endif
		if (Cns_getidmapc (principal, user_ca, nbfqans > 1 ? 1 : nbfqans,
		    (const char **)fqan, &uid, &gid)) {
			log (LOG_ERR, "Could not get virtual id: %s !\n", sstrerror (serrno));
			return (-1);
		}
#endif
		Cns_client_setAuthorizationId (uid, gid, (char *) mech, (char *) principal);
		if (voname && fqan)
			Cns_client_setVOMS_data (voname, fqan, nbfqans);
	}
#endif
	Cns_set_selectsrvr (CNS_SSRV_NOTPATH);
	if (Cns_statr (sfn1, &statbuf) < 0)
		return (-1);
	st->st_uid = statbuf.uid;
	st->st_gid = statbuf.gid;
	st->st_mode = statbuf.filemode;
	*need_user_check = 0;
	return (0);
}

int rfio_handle_stat64(const char *sfn, 
		     int lflag,
		     const char *mech,
		     const char *principal,
		     uid_t uid,
		     gid_t gid,
		     struct stat64 *st,
		     int *need_user_check)
{
	char **fqan = NULL;
	int nbfqans = 0;
	char *p;
	char sfn1[CA_MAXSFNLEN+1];
	struct Cns_filestatg statbuf;
	char *user_ca;
	char *voname = NULL;

#if !defined(_WIN32)
	if (lflag) {
		if (lstat64 (sfn, st) < 0) {
			serrno = errno;
			return (-1);
		}
	} else
#endif
		if (stat64 (sfn, st) < 0) {
			serrno = errno;
			return (-1);
		}
	if ((st->st_mode & S_IFMT) != S_IFREG ||
	    (st->st_uid != stagersuperuser.pw_uid && st->st_gid != stagersuperuser.pw_gid)) {
		/* not a file managed by DPM */
		*need_user_check = 1;
		return (0);
	}
#ifdef CSEC
	if (uid == 0 && gid == 0) {
		*need_user_check = 0;
		return (0);
	}
#endif
	strcpy (sfn1, data_interface);
	if (strchr (sfn1, '.') == NULL) {
		strcat (sfn1, ".");
		strcat (sfn1, localdomain);
	}
	p = sfn1 + strlen (sfn1);
	*p++ = ':';
	strcpy (p, sfn);
#ifdef CSEC
	if (stored.principal != NULL && !strcmp(stored.principal, principal)) {
		Cns_client_setAuthorizationId (stored.uid, stored.gid, (char *) mech, (char *) principal);
		if (stored.voname && stored.fqan)
			Cns_client_setVOMS_data (stored.voname, stored.fqan, stored.nbfqans);
	} else if (set_authorization_id) {
#ifdef VIRTUAL_ID
		user_ca = Csec_server_get_client_ca (&sec_ctx);
#ifdef USE_VOMS
		voname = Csec_server_get_client_vo (&sec_ctx);
		fqan = Csec_server_get_client_fqans (&sec_ctx, &nbfqans);
#endif
		if (Cns_getidmapc (principal, user_ca, nbfqans > 1 ? 1 : nbfqans,
		    (const char **)fqan, &uid, &gid)) {
			log (LOG_ERR, "Could not get virtual id: %s !\n", sstrerror (serrno));
			return (-1);
		}
#endif
		Cns_client_setAuthorizationId (uid, gid, (char *) mech, (char *) principal);
		if (voname && fqan)
			Cns_client_setVOMS_data (voname, fqan, nbfqans);
	}
#endif
	Cns_set_selectsrvr (CNS_SSRV_NOTPATH);
	if (Cns_statr (sfn1, &statbuf) < 0)
		return (-1);
	st->st_uid = statbuf.uid;
	st->st_gid = statbuf.gid;
	st->st_mode = statbuf.filemode;
	*need_user_check = 0;
	return (0);
}