xref: /csrg-svn/sys/kern/kern_prot.c (revision 7498)
1*7498Sroot /*	kern_prot.c	5.3	82/07/24	*/
27420Sroot 
37420Sroot /*
4*7498Sroot  * System calls related to processes and protection
57420Sroot  */
67420Sroot 
7*7498Sroot /* NEED ALLOCATION AND PROTECTION MECHANISM FOR PROCESS GROUPS */
8*7498Sroot 
97420Sroot #include "../h/param.h"
107420Sroot #include "../h/systm.h"
117420Sroot #include "../h/dir.h"
127420Sroot #include "../h/user.h"
137420Sroot #include "../h/reg.h"
147420Sroot #include "../h/inode.h"
157420Sroot #include "../h/proc.h"
167420Sroot #include "../h/clock.h"
177420Sroot #include "../h/mtpr.h"
187420Sroot #include "../h/timeb.h"
197420Sroot #include "../h/times.h"
207420Sroot #include "../h/reboot.h"
217420Sroot #include "../h/fs.h"
227420Sroot #include "../h/conf.h"
237420Sroot #include "../h/buf.h"
247420Sroot #include "../h/mount.h"
257489Skre #include "../h/quota.h"
267420Sroot 
27*7498Sroot getpid()
28*7498Sroot {
29*7498Sroot 
30*7498Sroot 	u.u_r.r_val1 = u.u_procp->p_pid;
31*7498Sroot 	u.u_r.r_val2 = u.u_procp->p_ppid;
32*7498Sroot }
33*7498Sroot 
34*7498Sroot getpgrp()
35*7498Sroot {
36*7498Sroot 	register struct a {
37*7498Sroot 		int	pid;
38*7498Sroot 	} *uap = (struct a *)u.u_ap;
39*7498Sroot 	register struct proc *p;
40*7498Sroot 
41*7498Sroot 	if (uap->pid == 0)
42*7498Sroot 		uap->pid = u.u_procp->p_pid;
43*7498Sroot 	p = pfind(uap->pid);
44*7498Sroot 	if (p == 0) {
45*7498Sroot 		u.u_error = ESRCH;
46*7498Sroot 		return;
47*7498Sroot 	}
48*7498Sroot 	u.u_r.r_val1 = p->p_pgrp;
49*7498Sroot }
50*7498Sroot 
517420Sroot getuid()
527420Sroot {
537420Sroot 
547420Sroot 	u.u_r.r_val1 = u.u_ruid;
557420Sroot 	u.u_r.r_val2 = u.u_uid;
567420Sroot }
577420Sroot 
58*7498Sroot getgid()
59*7498Sroot {
60*7498Sroot 
61*7498Sroot 	u.u_r.r_val1 = u.u_rgid;
62*7498Sroot 	u.u_r.r_val2 = u.u_gid;
63*7498Sroot }
64*7498Sroot 
65*7498Sroot getgrp()
66*7498Sroot {
67*7498Sroot 	register struct	a {
68*7498Sroot 		int	*gidset;
69*7498Sroot 	} *uap = (struct a *)u.u_ap;
70*7498Sroot 
71*7498Sroot 	if (copyout((caddr_t)u.u_grps, (caddr_t)uap->gidset,
72*7498Sroot 	    sizeof (u.u_grps))) {
73*7498Sroot 		u.u_error = EFAULT;
74*7498Sroot 		return;
75*7498Sroot 	}
76*7498Sroot }
77*7498Sroot 
78*7498Sroot setpgrp()
79*7498Sroot {
80*7498Sroot 	register struct proc *p;
81*7498Sroot 	register struct a {
82*7498Sroot 		int	pid;
83*7498Sroot 		int	pgrp;
84*7498Sroot 	} *uap = (struct a *)u.u_ap;
85*7498Sroot 
86*7498Sroot 	if (uap->pid == 0)
87*7498Sroot 		uap->pid = u.u_procp->p_pid;
88*7498Sroot 	p = pfind(uap->pid);
89*7498Sroot 	if (p == 0) {
90*7498Sroot 		u.u_error = ESRCH;
91*7498Sroot 		return;
92*7498Sroot 	}
93*7498Sroot 	if (p->p_uid != u.u_uid && u.u_uid && !inferior(p)) {
94*7498Sroot 		u.u_error = EPERM;
95*7498Sroot 		return;
96*7498Sroot 	}
97*7498Sroot 	p->p_pgrp = uap->pgrp;
98*7498Sroot }
99*7498Sroot 
1007420Sroot setuid()
1017420Sroot {
1027420Sroot 	register uid;
1037420Sroot 	register struct a {
1047420Sroot 		int	uid;
1057420Sroot 	} *uap;
1067420Sroot 
1077420Sroot 	uap = (struct a *)u.u_ap;
1087420Sroot 	uid = uap->uid;
1097420Sroot 	if (u.u_ruid == uid || u.u_uid == uid || suser()) {
1107489Skre #ifdef QUOTA
1117489Skre 		if (u.u_quota->q_uid != uid) {
1127489Skre 			qclean();
1137489Skre 			qstart(getquota(uid, 0, 0));
1147489Skre 		}
1157489Skre #endif
1167420Sroot 		u.u_uid = uid;
1177420Sroot 		u.u_procp->p_uid = uid;
1187420Sroot 		u.u_ruid = uid;
1197420Sroot 	}
1207420Sroot }
1217420Sroot 
1227420Sroot setgid()
1237420Sroot {
1247420Sroot 	register gid;
1257420Sroot 	register struct a {
1267420Sroot 		int	gid;
1277420Sroot 	} *uap;
1287420Sroot 
1297420Sroot 	uap = (struct a *)u.u_ap;
1307420Sroot 	gid = uap->gid;
1317420Sroot 	if (u.u_rgid == gid || u.u_gid == gid || suser()) {
1327420Sroot 		u.u_gid = gid;
1337420Sroot 		u.u_rgid = gid;
1347420Sroot 	}
1357420Sroot }
136*7498Sroot 
137*7498Sroot setgrp()
138*7498Sroot {
139*7498Sroot 	register struct	a {
140*7498Sroot 		int	*gidset;
141*7498Sroot 	} *uap = (struct a *)u.u_ap;
142*7498Sroot 
143*7498Sroot 	if (suser())
144*7498Sroot 		return;
145*7498Sroot 	if (copyin((caddr_t)uap->gidset, (caddr_t)u.u_grps,
146*7498Sroot 	    sizeof (u.u_grps))) {
147*7498Sroot 		u.u_error = EFAULT;
148*7498Sroot 		return;
149*7498Sroot 	}
150*7498Sroot }
151*7498Sroot 
152*7498Sroot /* BEGIN DEFUNCT */
153*7498Sroot osetgrp()
154*7498Sroot {
155*7498Sroot 	register struct	a {
156*7498Sroot 		int *ngrps;
157*7498Sroot 		int *ogrps;
158*7498Sroot 	} *uap = (struct a *)u.u_ap;
159*7498Sroot 	int thegroups[NGRPS/(sizeof(int)*8)];
160*7498Sroot 
161*7498Sroot 	if (uap->ogrps && copyout((caddr_t)u.u_grps, (caddr_t)uap->ogrps,
162*7498Sroot 	    sizeof (thegroups))) {
163*7498Sroot 		u.u_error = EFAULT;
164*7498Sroot 		return;
165*7498Sroot 	}
166*7498Sroot 	if (uap->ngrps == 0)
167*7498Sroot 		return;
168*7498Sroot 	if (copyin((caddr_t)uap->ngrps, (caddr_t)thegroups,
169*7498Sroot 	    sizeof (thegroups))) {
170*7498Sroot 		u.u_error = EFAULT;
171*7498Sroot 		return;
172*7498Sroot 	}
173*7498Sroot 	if (suser())
174*7498Sroot 		bcopy((caddr_t)thegroups, (caddr_t)u.u_grps, sizeof (u.u_grps));
175*7498Sroot }
176*7498Sroot 
177*7498Sroot /*
178*7498Sroot  * Pid of zero implies current process.
179*7498Sroot  * Pgrp -1 is getpgrp system call returning
180*7498Sroot  * current process group.
181*7498Sroot  */
182*7498Sroot osetpgrp()
183*7498Sroot {
184*7498Sroot 	register struct proc *p;
185*7498Sroot 	register struct a {
186*7498Sroot 		int	pid;
187*7498Sroot 		int	pgrp;
188*7498Sroot 	} *uap;
189*7498Sroot 
190*7498Sroot 	uap = (struct a *)u.u_ap;
191*7498Sroot 	if (uap->pid == 0)
192*7498Sroot 		p = u.u_procp;
193*7498Sroot 	else {
194*7498Sroot 		p = pfind(uap->pid);
195*7498Sroot 		if (p == 0) {
196*7498Sroot 			u.u_error = ESRCH;
197*7498Sroot 			return;
198*7498Sroot 		}
199*7498Sroot 	}
200*7498Sroot 	if (uap->pgrp <= 0) {
201*7498Sroot 		u.u_r.r_val1 = p->p_pgrp;
202*7498Sroot 		return;
203*7498Sroot 	}
204*7498Sroot 	if (p->p_uid != u.u_uid && u.u_uid && !inferior(p)) {
205*7498Sroot 		u.u_error = EPERM;
206*7498Sroot 		return;
207*7498Sroot 	}
208*7498Sroot 	p->p_pgrp = uap->pgrp;
209*7498Sroot }
210*7498Sroot /* END DEFUNCT */
211