File: gtm_permissions.c

package info (click to toggle)
fis-gtm 6.2-000-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 30,784 kB
  • ctags: 42,554
  • sloc: ansic: 358,483; asm: 4,847; csh: 4,574; sh: 2,261; awk: 200; makefile: 86; sed: 13
file content (258 lines) | stat: -rw-r--r-- 9,020 bytes parent folder | download
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
/****************************************************************
 *								*
 *	Copyright 2009, 2013 Fidelity Information Services, Inc	*
 *								*
 *	This source code contains the intellectual property	*
 *	of its copyright holder(s), and is made available	*
 *	under a license.  If you do not know the terms of	*
 *	the license, please stop and do not read further.	*
 *								*
 ****************************************************************/

#include "mdef.h"
#include "eintr_wrappers.h"
#include "gtm_string.h"
#include "gtm_stdio.h"
#include "gtm_stdlib.h"
#include "gtm_limits.h"
#include "gtm_unistd.h"
#include "gtm_fcntl.h"
#include "gtm_stat.h"
#include "gtm_pwd.h"
#include <grp.h>
#include "gtm_permissions.h"
#include "send_msg.h"

#if defined(__hpux) && defined(__hppa)
#	define LIBGTMSHR "%s/libgtmshr.sl"
#elif defined(__MVS__)
#	define LIBGTMSHR "%s/libgtmshr.dll"
#else
#	define LIBGTMSHR "%s/libgtmshr.so"
#endif
/* Return the group id of the distribution based on libgtmshr.xx[x]. If there is some
   problem accessing that file then return -1 which signals no change to group.  Otherwise,
   the pointer to the stat buffer will contain the result of the call to STAT_FILE */
int gtm_get_group_id(struct stat *stat_buff)
{
	char		*env_var;
	int		ret_stat;
	char		temp[PATH_MAX + SIZEOF("libgtmshr.dll")];

	env_var = GETENV("gtm_dist");
	if (NULL != env_var)
	{
		/* build a path to libgtmshr.so or .sl on hpux or .dll on zos */
		SNPRINTF(temp, SIZEOF(temp), LIBGTMSHR, env_var);
		STAT_FILE(temp, stat_buff, ret_stat);
		if (0 == ret_stat)
			return(stat_buff->st_gid);
	}
	/* return a -1 if $gtm_dist found or if STAT_FILE returned a -1 */
	return(-1);
}

/* Return TRUE is the "uid" parameter is a member of the "gid" group parameter.
   Return FALSE if it is not. */
int gtm_member_group_id(int uid, int gid)
{
	struct group	*grp;
	struct passwd	*pwd, *pwd2;

	/* check effective group if current effective uid */
	if ((GETEUID() == uid) && (GETEGID() == gid))
		return(TRUE);
	/* get group id for database */
	grp = getgrgid(gid);
	if (NULL == grp)
		return(FALSE); 	/* if group id not found then assume uid not a member */
	pwd = getpwuid(uid);
	if (NULL == pwd)
		return(FALSE); 	/* if user id not found then assume uid not a member */
	/* if the gid of the file is the same as the gid for the process uid we are done */
	if (gid == pwd->pw_gid)
		return(TRUE);
	else
	{
		/*
		 * Otherwise we have to compare the name stored in pwd struct
		 * with the names of the group members in the group struct.
		 */
		while (NULL != *(grp->gr_mem))
		{
			if (!strcmp(pwd->pw_name, *(grp->gr_mem++)))
				return(TRUE);
		}

		return(FALSE);
	}
}

/* Based on security rules in this routine, set *group_id of the group to be used
 * for shared resources, journals, and temp files.  If a no change then it will be set to -1.
 * Also, set *perm to the permissions to be used.  The precalculated world_write_perm (need to
 * change name to masked_permissions) is to be used in the one case indicated below.
 * If the user is root, the *user_id may be set to a target uid if needed; otherwise, it will
 * be set to -1.
 *
 * Populates pdd struct and returns negative value for error, returns non-negative for success.
 */
int gtm_permissions(struct stat *stat_buff, int *user_id, int *group_id, int *perm, enum perm_target_types target_type,
			   struct perm_diag_data *pdd)
{
	int		lib_gid = -1;
	int		use_world_writeable;
	uid_t		process_uid;
	gid_t		process_gid;
	uid_t		db_uid;
	struct stat	dist_stat_buff;
	int		opener_is_file_owner;
	int		opener_is_root;
	int		opener_in_file_group;
	int		owner_in_file_group;
	int		gtm_group_restricted;

	/* get process_uid/gid */
	process_uid = GETEUID();
	process_gid = GETEGID();
	/* get database uid */
	db_uid = stat_buff->st_uid;
	/* set variables for permission logic */
	opener_is_file_owner = (process_uid == db_uid);
	opener_is_root = (process_uid == 0);
	opener_in_file_group = gtm_member_group_id(process_uid, stat_buff->st_gid);
	owner_in_file_group = gtm_member_group_id(db_uid, stat_buff->st_gid);
	/* find restricted group, if any */
	lib_gid = gtm_get_group_id(&dist_stat_buff);
	gtm_group_restricted = (lib_gid != -1) && !(dist_stat_buff.st_mode & 01);	/* not world executable */

	/* set default gid */
#ifdef __osf__
	*user_id = process_uid;
	*group_id = process_gid;
#else
	*user_id = -1;
	*group_id = -1;
#endif
	/* set no permissions as a default in case none of our conditions match */
	*perm = 0;

	assertpro((PERM_FILE == target_type) || (PERM_IPC == target_type));
	if (0006 & stat_buff->st_mode)
	{
		/* file is accessible to other */
		if (opener_is_root)					/* otherwise, use default uid */
			*user_id = stat_buff->st_uid;
		if (opener_in_file_group || opener_is_root)		/* otherwise, use default gid */
			*group_id = stat_buff->st_gid;
		if (PERM_FILE == target_type)
			*perm = (!opener_in_file_group && !opener_is_root && (0020 & stat_buff->st_mode))
					? 0666 : (stat_buff->st_mode & 0666);
		else if (PERM_IPC == target_type)
			*perm = 0666;
	} else if (0600 & stat_buff->st_mode && !(0066 & stat_buff->st_mode))
	{
		/* file is only user accessible */
		/* use default group */
		assert(opener_is_file_owner || opener_is_root);
		if (opener_is_root)					/* otherwise, use default uid/gid */
		{
			*user_id = stat_buff->st_uid;
			*group_id = stat_buff->st_gid;
		}
		if (PERM_FILE == target_type)
			*perm = 0600;			/* read write for user */
		else if (PERM_IPC == target_type)
			*perm = 0600;			/* read write for user */
	} else if (0060 & stat_buff->st_mode && !(0606 & stat_buff->st_mode))
	{
		/* file is only group accessible */
		if (opener_is_root)					/* otherwise, use default uid */
			*user_id = stat_buff->st_uid;
		*group_id = stat_buff->st_gid;			/* use file group */
		assert(opener_in_file_group || opener_is_root);
		if (PERM_FILE == target_type)
			*perm = stat_buff->st_mode & 0060;	/* use file permissions, masked for group read/write */
		if (PERM_IPC == target_type)
			*perm = 0660;			/* read/write for group - all readers need write for ipc */
	} else
	{
		/* file is accessible to user and group */
		if (opener_is_file_owner && opener_in_file_group || opener_is_root)
		{
			if (opener_is_root)					/* otherwise, use default uid */
				*user_id = stat_buff->st_uid;
			*group_id = stat_buff->st_gid;		/* use file group */
			if (PERM_FILE == target_type)
				*perm = stat_buff->st_mode & 0660;	/* use file permissions, masked for user/group read/write */
			if (PERM_IPC == target_type)
				*perm = 0660;			/* read/write for user/group - all readers need write for ipc */
		} else
		{
			if (opener_is_file_owner && !opener_in_file_group)
			{
				if (gtm_group_restricted)
				{
					*group_id = lib_gid;			/* use restricted group */
					assert(gtm_member_group_id(process_uid, *group_id));
					if (PERM_FILE == target_type)
						*perm = 0660;			/* user/group read/write */
					if (PERM_IPC == target_type)
						*perm = 0660;	/* read/write for user/group - all readers need write for ipc */
				} else
				{
					/* use default group */
					if (PERM_FILE == target_type)
						*perm = 0666;			/* read/write for all */
					else if (PERM_IPC == target_type)
						*perm = 0666;	/* read/write for all - all readers need write for ipc */
				}
			} else if (!opener_is_file_owner && opener_in_file_group)
			{
				/* opener has access either via file group membership */
				if (owner_in_file_group)
				{
					*group_id = stat_buff->st_gid;		/* use file group */
					if (PERM_FILE == target_type)
						*perm = stat_buff->st_mode & 0660;	/* use masked file permissions */
					if (PERM_IPC == target_type)
						*perm = 0660;	/* read/write for user/group - all readers need write for ipc */

				} else if (gtm_group_restricted)
				{
					*group_id = lib_gid;			/* use restricted group */
					assert(gtm_member_group_id(process_uid, *group_id));
					if (PERM_FILE == target_type)
						*perm = 0660;			/* user/group read/write */
					if (PERM_IPC == target_type)
						*perm = 0660;	/* read/write for user/group - all readers need write for ipc */
				} else
				{
					*group_id = stat_buff->st_gid;		/* use file group */
					if (PERM_FILE == target_type)
						*perm = 0666;	/* read/write for all - ensure file owner read/write access */
					else if (PERM_IPC == target_type)
						*perm = 0666;	/* read/write for all - all readers need write for ipc */
				}
			}
		}
	}

	/* if we never set *perm, return error value */
	if (*perm == 0)
	{
		/* populate perm diag data */
		pdd->process_uid = process_uid;
		pdd->process_gid = process_gid;
		pdd->file_uid = stat_buff->st_uid;
		pdd->file_gid = stat_buff->st_gid;
		SNPRINTF(pdd->file_perm, SIZEOF(pdd->file_perm), "%04o", stat_buff->st_mode & 07777);
		pdd->lib_gid = dist_stat_buff.st_gid;
		SNPRINTF(pdd->lib_perm, SIZEOF(pdd->lib_perm), "%04o", dist_stat_buff.st_mode & 07777);
		pdd->opener_in_file_group = opener_in_file_group;
		pdd->owner_in_file_group = owner_in_file_group;

		return -1;
	} else
		return 0;
}