2010-08-02 13:44:45 +02:00
|
|
|
/* Created (MFS based):
|
|
|
|
* February 2010 (Evgeniy Ivanov)
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "fs.h"
|
|
|
|
#include "inode.h"
|
|
|
|
#include "super.h"
|
|
|
|
#include <minix/vfsif.h>
|
|
|
|
|
2012-03-25 20:25:53 +02:00
|
|
|
static int in_group(gid_t grp);
|
2010-08-02 13:44:45 +02:00
|
|
|
|
|
|
|
|
|
|
|
/*===========================================================================*
|
|
|
|
* fs_chmod *
|
|
|
|
*===========================================================================*/
|
2012-03-25 20:25:53 +02:00
|
|
|
int fs_chmod()
|
2010-08-02 13:44:45 +02:00
|
|
|
{
|
|
|
|
/* Perform the chmod(name, mode) system call. */
|
|
|
|
|
|
|
|
register struct inode *rip;
|
2013-03-07 15:46:21 +01:00
|
|
|
pmode_t mode;
|
2010-08-02 13:44:45 +02:00
|
|
|
|
2013-03-07 15:46:21 +01:00
|
|
|
mode = (pmode_t) fs_m_in.REQ_MODE;
|
2010-08-02 13:44:45 +02:00
|
|
|
|
|
|
|
/* Temporarily open the file. */
|
2013-03-07 15:46:21 +01:00
|
|
|
if( (rip = get_inode(fs_dev, (pino_t) fs_m_in.REQ_INODE_NR)) == NULL)
|
2010-08-02 13:44:45 +02:00
|
|
|
return(EINVAL);
|
|
|
|
|
|
|
|
/* Now make the change. Clear setgid bit if file is not in caller's grp */
|
|
|
|
rip->i_mode = (rip->i_mode & ~ALL_MODES) | (mode & ALL_MODES);
|
|
|
|
rip->i_update |= CTIME;
|
2012-10-16 17:40:39 +02:00
|
|
|
rip->i_dirt = IN_DIRTY;
|
2010-08-02 13:44:45 +02:00
|
|
|
|
|
|
|
/* Return full new mode to caller. */
|
|
|
|
fs_m_out.RES_MODE = rip->i_mode;
|
|
|
|
|
|
|
|
put_inode(rip);
|
|
|
|
return(OK);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*===========================================================================*
|
|
|
|
* fs_chown *
|
|
|
|
*===========================================================================*/
|
2012-03-25 20:25:53 +02:00
|
|
|
int fs_chown()
|
2010-08-02 13:44:45 +02:00
|
|
|
{
|
|
|
|
register struct inode *rip;
|
|
|
|
register int r;
|
|
|
|
|
|
|
|
/* Temporarily open the file. */
|
2013-03-07 15:46:21 +01:00
|
|
|
if( (rip = get_inode(fs_dev, (pino_t) fs_m_in.REQ_INODE_NR)) == NULL)
|
2010-08-02 13:44:45 +02:00
|
|
|
return(EINVAL);
|
|
|
|
|
|
|
|
/* Not permitted to change the owner of a file on a read-only file sys. */
|
|
|
|
r = read_only(rip);
|
|
|
|
if (r == OK) {
|
|
|
|
rip->i_uid = fs_m_in.REQ_UID;
|
|
|
|
rip->i_gid = fs_m_in.REQ_GID;
|
|
|
|
rip->i_mode &= ~(I_SET_UID_BIT | I_SET_GID_BIT);
|
|
|
|
rip->i_update |= CTIME;
|
2012-10-16 17:40:39 +02:00
|
|
|
rip->i_dirt = IN_DIRTY;
|
2010-08-02 13:44:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Update caller on current mode, as it may have changed. */
|
|
|
|
fs_m_out.RES_MODE = rip->i_mode;
|
|
|
|
put_inode(rip);
|
|
|
|
|
|
|
|
return(r);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*===========================================================================*
|
|
|
|
* forbidden *
|
|
|
|
*===========================================================================*/
|
2013-03-07 15:46:21 +01:00
|
|
|
int forbidden(register struct inode *rip, pmode_t access_desired)
|
2010-08-02 13:44:45 +02:00
|
|
|
{
|
|
|
|
/* Given a pointer to an inode, 'rip', and the access desired, determine
|
|
|
|
* if the access is allowed, and if not why not. The routine looks up the
|
|
|
|
* caller's uid in the 'fproc' table. If access is allowed, OK is returned
|
|
|
|
* if it is forbidden, EACCES is returned.
|
|
|
|
*/
|
|
|
|
|
|
|
|
register struct inode *old_rip = rip;
|
2013-03-07 15:46:21 +01:00
|
|
|
register pmode_t bits, perm_bits;
|
2010-08-02 13:44:45 +02:00
|
|
|
int r, shift;
|
|
|
|
|
|
|
|
/* Isolate the relevant rwx bits from the mode. */
|
|
|
|
bits = rip->i_mode;
|
|
|
|
if (caller_uid == SU_UID) {
|
|
|
|
/* Grant read and write permission. Grant search permission for
|
|
|
|
* directories. Grant execute permission (for non-directories) if
|
|
|
|
* and only if one of the 'X' bits is set.
|
|
|
|
*/
|
|
|
|
if ( (bits & I_TYPE) == I_DIRECTORY ||
|
|
|
|
bits & ((X_BIT << 6) | (X_BIT << 3) | X_BIT))
|
|
|
|
perm_bits = R_BIT | W_BIT | X_BIT;
|
|
|
|
else
|
|
|
|
perm_bits = R_BIT | W_BIT;
|
|
|
|
} else {
|
|
|
|
if (caller_uid == rip->i_uid) shift = 6; /* owner */
|
|
|
|
else if (caller_gid == rip->i_gid) shift = 3; /* group */
|
|
|
|
else if (in_group(rip->i_gid) == OK) shift = 3; /* other groups */
|
|
|
|
else shift = 0; /* other */
|
|
|
|
perm_bits = (bits >> shift) & (R_BIT | W_BIT | X_BIT);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If access desired is not a subset of what is allowed, it is refused. */
|
|
|
|
r = OK;
|
|
|
|
if ((perm_bits | access_desired) != perm_bits) r = EACCES;
|
|
|
|
|
|
|
|
/* Check to see if someone is trying to write on a file system that is
|
|
|
|
* mounted read-only.
|
|
|
|
*/
|
|
|
|
if (r == OK) {
|
|
|
|
if (access_desired & W_BIT) {
|
|
|
|
r = read_only(rip);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (rip != old_rip) put_inode(rip);
|
|
|
|
|
|
|
|
return(r);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*===========================================================================*
|
|
|
|
* in_group *
|
|
|
|
*===========================================================================*/
|
2012-03-25 20:25:53 +02:00
|
|
|
static int in_group(gid_t grp)
|
2010-08-02 13:44:45 +02:00
|
|
|
{
|
|
|
|
int i;
|
2011-09-08 14:23:03 +02:00
|
|
|
|
2011-09-08 17:09:41 +02:00
|
|
|
if (credentials.vu_ngroups > NGROUPS_MAX)
|
2011-09-08 14:23:03 +02:00
|
|
|
return(EINVAL);
|
|
|
|
|
|
|
|
for (i = 0; i < credentials.vu_ngroups; i++)
|
2010-08-02 13:44:45 +02:00
|
|
|
if (credentials.vu_sgroups[i] == grp)
|
|
|
|
return(OK);
|
|
|
|
|
|
|
|
return(EINVAL);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*===========================================================================*
|
|
|
|
* read_only *
|
|
|
|
*===========================================================================*/
|
2012-03-25 20:25:53 +02:00
|
|
|
int read_only(ip)
|
2010-08-02 13:44:45 +02:00
|
|
|
struct inode *ip; /* ptr to inode whose file sys is to be cked */
|
|
|
|
{
|
|
|
|
/* Check to see if the file system on which the inode 'ip' resides is mounted
|
|
|
|
* read only. If so, return EROFS, else return OK.
|
|
|
|
*/
|
|
|
|
|
|
|
|
register struct super_block *sp;
|
|
|
|
|
|
|
|
sp = ip->i_sp;
|
|
|
|
return(sp->s_rd_only ? EROFS : OK);
|
|
|
|
}
|