File: globus_gatekeeper_utils.c

package info (click to toggle)
globus-gatekeeper 10.12-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 1,576 kB
  • ctags: 285
  • sloc: sh: 11,711; ansic: 2,528; makefile: 138
file content (453 lines) | stat: -rw-r--r-- 9,848 bytes parent folder | download | duplicates (5)
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
/*
 * Copyright 1999-2006 University of Chicago
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */


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

globus_gatekeeper_utils.c

Description:
	Some common routines used by globus_gatekeeper 
	and globus_gram_k5.c


CVS Information:
	$Source$
	$Date$
	$Revision$
	$Author$

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

/*****************************************************************************
							Include header files
******************************************************************************/
#include "globus_config.h"
#include "globus_gatekeeper_config.h"
#include <stdio.h>
#include <stdlib.h>
#include <locale.h>
#include <pwd.h>
#include <string.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include <errno.h>
#include <unistd.h>

#ifdef HAVE_MALLOC_H
#   include <malloc.h>
#endif

#include "globus_gatekeeper_utils.h"

/******************************************************************************
                               Type definitions
******************************************************************************/

/******************************************************************************
                          Module specific prototypes
******************************************************************************/

static
char * fgetscont(char *line, int size, FILE* fd);

/******************************************************************************
                       Define module specific variables
******************************************************************************/

static
char * fgetscont(char * line, int size, FILE* fd)
{
	int i;
	int len;
	char * cp;

	i = 2;
	len = size;
    cp = line;
	*cp = '\0';

	while(fgets(cp, len, fd) &&  
		(i = strlen(line)) > 2 && 
		line[i-1] == '\n' && line[i-2] == '\\') {
      len = size - i - 2;
	  cp = line + i - 2;
	}
	if (*cp == '\0') {
		return NULL;
	}
	return line;
}
/******************************************************************************
Function:   globus_gatekeeper_util_globusxmap()
Description:
	Given a index, find the command to be issued. For example
	this could be a service name, or a globusID, or K5 principal

Parameters:
	index
	A pointer to a char *. will strdup with command
Returns:
******************************************************************************/
int
globus_gatekeeper_util_globusxmap( char * filename, char * index, char ** command)
{

	FILE * fd;
	int    rc;
	int	   i;
    int    offset;
	char   f_index[256]; 
	char   line[4096];

	*command = NULL;

  if ((fd = fopen(filename, "r")) != NULL) {

    while(fgetscont(line, sizeof(line), fd)) {
      i = strlen(line);
	  if (line[0] != '#') {   /* comment line */
	    if (line[i - 1] == '\n') {
			line[i - 1] = '\0';
		}
                if (!index)
                {
                        *command = strdup(line);
                        fclose(fd);
                        return(0);
                }
 
		rc = sscanf(line, " \"%255[^\"]%*c%n %n", 
						f_index, &offset, &offset);
		if (rc != 1) {
        	rc = sscanf(line, "%255s%n %n",
					 f_index, &offset, &offset);
		}
        if (rc == 1) {
	      if (!strcmp(index, f_index)) {
		    *command = strdup(&line[offset]);
            fclose(fd);
		    return(0);
 
	      }
	    }
	  }
	}
	fclose(fd);
	return(-1); /* not found */	
  }
  return(-2);   /* open failed */
}
/******************************************************************************
Function:   globus_gatekeeper_util_tokenize()

Description:
	Breakup the command in to args, pointing the args array
	at the tokens. Replace white space at the end of each
	token with a null. A token maybe in quotes. 

Parameters:
	The command line to be parsed.
	A pointer to an array of pointers to be filled it
	Size of the array, on input, and set to size used on output. 

Returns:
	0 on success. 
	-1 on to malloc
	-2 on to many args
	-3 on quote not matched
******************************************************************************/

int
globus_gatekeeper_util_tokenize(char * command, 
								char ** args,
								int * n,
								char * sep)
{
  int i;
  char * cp;
  char * pp;
  char * qp;
  char ** arg;

  arg = args;
  i = *n - 1;

  cp = command;
  while (*cp)
  {
  	/* skip leading sep characters */
	while (*cp && strchr(sep, *cp))
	{
		cp++;
	}
	pp = NULL;
	if (*cp == '\"')
	{
		cp++;
		pp = cp;
		if ((qp = strchr(cp,'\"')) == NULL)
		{
			return -3;
		}
		cp = qp + 1;
		
	}
	else if (*cp)
	{
		pp = cp;
		if ((qp = strpbrk(cp,sep)) == NULL)
		{
			qp = strchr(cp,'\0');
		}
		cp = qp;
	}
	if (pp)
	{
		*arg = (char*)malloc((qp - pp) + 1);
		if (*arg == NULL)
		{
			return -1;
		}
		memcpy(*arg,pp,qp - pp);
		*(*arg + (qp - pp)) = '\0';
		i--;
    	if (i == 0)
	  		return(-2); /* to many args */
    	arg++;
  	}
  }
  *arg = (char *) 0;
  *n = *n - i - 1;
  return(0);
}

/******************************************************************************
Function:   globus_gatekeeper_util_envsub()

Description:
	Substitute from environment string like ${text} 
	into arg.  arg will be freed and copied.
	Recursion is allowed. 

Parameters:
	arg a pointer to the string pointer. 

Returns:
	0 on success. 
	-1 on malloc
	-3 on env string not found
******************************************************************************/

int
globus_gatekeeper_util_envsub(char ** arg)
{
	char * cp;
	char * pp;
	char * qp;
	char * rp;
	char * narg;
	
	cp = *arg;
	while ((pp = strstr(cp,"${")))  /* for editor matching } */
	{
		pp+=2;
									/* for editor matching { */
		if(!(qp = strstr(pp,"}")))
		{
			return -2;  /* not terminated */
		}
		*(pp-2) = '\0'; /* term the prefix */
		*qp = '\0';     /* term the env name */
		qp++;
		if (!(rp = getenv(pp)))
		{
			return -3;
		}
		if (!(narg = (char *)malloc(strlen(cp) + 
							   strlen(rp) + 
							   strlen(qp) + 1)))
		{
			return -1;
		}
		strcpy(narg,cp);
		strcat(narg,rp);
		strcat(narg,qp);
		free(cp);
		cp = narg;
		*arg = cp;
	}
	return 0;
}

/******************************************************************************
Function:   globus_gatekeeper_util_exec()
Description:
Parameters:
Returns:
******************************************************************************/
int
globus_gatekeeper_util_exec(char *args[], 
					struct passwd *pw, 
					char * user, 
					char **errmsgp)
{

  int pid;
  int err; 
  int rc;
  char *path;
  char * cp;

#define WAIT_USES_INT
#ifdef  WAIT_USES_INT
  int wait_status;
#else   /* WAIT_USES_INT */
  union wait wait_status;
#endif  /* WAIT_USES_INT */

  pid = fork();
  if (pid <0) 
   return(-1);

  if (pid == 0) {  /* child process */

	/* If need to run child as user */
	if (pw != NULL) {
		if ((rc = globus_gatekeeper_util_trans_to_user(pw, user, errmsgp)) != 0) { 
			/* child failed to transfer to user context */
			fprintf(stderr,"Failed trying to run as user %s: %s\n",
				user, *errmsgp);
			exit(126);
		}
	}
		
    path = strdup(args[0]);
    cp = strrchr(path, '/');
    if (cp)
      cp++;
    else
      cp = path;
 
    args[0] = cp;

#ifdef DEBUG
	fprintf(stderr,"EXECING %s args=",path);
	{
		int n = 0;
		while (args[n]) {
			fprintf (stderr," %s \n",args[n]);
			n++;
		}
	fprintf(stderr,"\n");
	}
#endif
    execv(path, args);
	fprintf(stderr,"Failed to exec the child\n");
    exit(127);      /* in case execl fails */
  } 

  /* parent, wait for child to finish */

  wait_status = 0;
#ifdef  HAVE_WAITPID
  err = waitpid((pid_t) pid, &wait_status, 0);
#else   /* HAVE_WAITPID */
  err = wait4(pid, &wait_status, 0, (struct rusage *) NULL);
#endif  /* HAVE_WAITPID */

  /* if it worked or failed, continue on. */
  return(wait_status);
}

/******************************************************************************
Function:   globus_gatekeeper_util_trans_to_user()
Description:
	Transition the process from root to the user, 
	doing all the operating system specific stuff. 

Parameters:
Returns:
	 0 if OK
	<0 if errno is set;
	>0 if some other error.
	 1 if not root
	DEE needs work
******************************************************************************/

int
globus_gatekeeper_util_trans_to_user(struct passwd * pw,
			 char * userid,
			 char ** errmsg)
{

	uid_t myuid;

	/* must be root to use this */

	if ((myuid = getuid()) != 0)
	{
		if (myuid == pw->pw_uid)
			return 0;   /* already running as the user */
		else
			*errmsg = strdup("Can not run as another user");
			return 1;   /* can't run as another user */
	}

	/*
	 *DEE If we are root and want to run as root, should we continue
	 * here or just exit? Some logging might be usefull. 
	 */
	setgid(pw->pw_gid);
	initgroups(pw->pw_name, pw->pw_gid);

#	if defined(__hpux)
	{
		if (setresuid(pw->pw_uid, pw->pw_uid, -1) != 0)
		{
			 *errmsg = strdup("cannot setresuid");
			 return -2;
		}
	}
#	elif defined(TARGET_ARCH_SOLARIS) || \
		 defined(TARGET_ARCH_BSD) || \
		 defined(TARGET_ARCH_CYGWIN)
    {
		if (setuid(pw->pw_uid) != 0)
		{
		    *errmsg = strdup("cannot setuid");
			return -3;
		}
    }
#	else
    {
		if (seteuid(0) != 0)
		{
		    *errmsg = strdup("cannot seteuid");
			return -4;
		}
	
		if (setreuid(pw->pw_uid, pw->pw_uid) != 0)
		{
		    *errmsg = strdup("cannot setreuid");
			return -5;
		}
    }
#	endif

	return 0;

}