File: context.c

package info (click to toggle)
deliver 2.1.14-2
  • links: PTS
  • area: main
  • in suites: potato
  • size: 488 kB
  • ctags: 914
  • sloc: ansic: 6,050; yacc: 405; makefile: 131; sh: 34
file content (188 lines) | stat: -rw-r--r-- 3,453 bytes parent folder | download | duplicates (3)
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
/* $Id: context.c,v 1.3 1991/08/26 17:42:02 chip Exp $
 *
 * User context manager.
 * This module exists for efficiency reasons; I could just call getpwnam()
 * every time I need context info.
 *
 * $Log: context.c,v $
 * Revision 1.3  1991/08/26 17:42:02  chip
 * Don't declare unused pw/gr functions.
 *
 * Revision 1.2  1991/06/04  18:16:28  chip
 * Feature-based configuration.
 *
 * Revision 1.1  91/05/13  18:36:55  chip
 * Initial revision
 * 
 */

#include "deliver.h"
#include <pwd.h>
#include <grp.h>

extern struct passwd *getpwnam();
extern struct passwd *getpwuid();

extern void setgrent();
extern struct group *getgrent();
extern struct group *getgrnam();
extern struct group *getgrgid();

/*
 * Local functions.
 */

static CONTEXT *new_context();

/*
 * Local data.
 */

static CONTEXT *ctlist;		/* Chain of CONTEXT structures.		*/

/*----------------------------------------------------------------------
 * Look up a context by user name.
 */

CONTEXT *
name_context(name)
char *name;
{
    struct passwd *pw;
    CONTEXT *ct;

    for (ct = ctlist; ct; ct = ct->ct_next)
    {
	if (strcmp(ct->ct_name, name) == 0)
	    return ct;
    }

    if ((pw = getpwnam(name)) == NULL)
	return NULL;

    return new_context(pw);
}

/*----------------------------------------------------------------------
 * Look up a context by user ID.
 */

CONTEXT *
uid_context(uid)
int uid;
{
    struct passwd *pw;
    CONTEXT *ct;

    for (ct = ctlist; ct; ct = ct->ct_next)
    {
	if (ct->ct_uid == uid)
	    return ct;
    }

    if ((pw = getpwuid(uid)) == NULL)
	return NULL;

    return new_context(pw);
}

/*----------------------------------------------------------------------
 * Local function -- create a new context structure and return
 * its address.
 */

static CONTEXT *
new_context(pw)
struct passwd *pw;
{
    CONTEXT *ct;
#ifdef GROUP_VECTOR
    struct group *gr;
#endif

    ct = talloc(CONTEXT, 1);
    ct->ct_uid = pw->pw_uid;
    ct->ct_gid = pw->pw_gid;
    ct->ct_name = copystr(pw->pw_name);
    ct->ct_home = copystr(pw->pw_dir);

#ifdef GROUP_VECTOR
    ct->ct_numgroups = 0;
    ct->ct_groups = (maxgroups > 0)
	? talloc(GRVEC_T, maxgroups)
	: (GRVEC_T *) NULL;

    setgrent();
    while ((gr = getgrent()) != NULL)
    {
	int i;

	for (i = 0; gr->gr_mem[i]; ++i)
	{
	    if (strcmp(ct->ct_name, gr->gr_mem[i]) == 0
		&& ct->ct_numgroups < maxgroups)
	    {
		ct->ct_groups[ct->ct_numgroups++] = gr->gr_gid;
		break;
	    }
	}
    }
#endif	/* GROUP_VECTOR */

    ct->ct_next = ctlist;
    ctlist = ct;

    return ct;
}

/*----------------------------------------------------------------------
 * Report whether is is possible or not to go from the given
 * effective uid/real uid/real gid to the given context.
 */

int
ok_context(euid, ruid, rgid, ct)
int euid, ruid, rgid;
CONTEXT *ct;
{
    if (!ct)
	return FALSE;

    if (euid == 0
	|| ((ruid == ct->ct_uid) && (rgid == ct->ct_gid)))
	return TRUE;
    else
	return FALSE;
}

/*----------------------------------------------------------------------
 * Look up a group name by ID.
 */

char *
group_name(id)
int id;
{
    struct group *grp;

    if ((grp = getgrgid(id)) == NULL)
	return NULL;

    return grp->gr_name;
}

/*----------------------------------------------------------------------
 * Look up a group ID by name.
 */

int
group_id(name)
char *name;
{
    struct group *grp;

    if ((grp = getgrnam(name)) == NULL)
	return -1;

    return grp->gr_gid;
}