8000 minix/minix/servers/vfs/path.c at R3.3.0 · devinblood/minix · GitHub
[go: up one dir, main page]

Skip to content

Latest commit

 

History

History
878 lines (750 loc) · 24.9 KB

path.c

File metadata and controls

878 lines (750 loc) · 24.9 KB
}
/* Fill in response fields */
result_node->inode_nr = res.inode_nr;
result_node->fmode = res.fmode;
result_node->fsize = res.fsize;
result_node->dev = res.dev;
result_node->fs_e = res.fs_e;
result_node->uid = res.uid;
result_node->gid = res.gid;
return(r);
}
/*===========================================================================*
* lookup_init *
*===========================================================================*/
void lookup_init(resolve, path, flags, vmp, vp)
struct lookup *resolve;
char *path;
int flags;
struct vmnt **vmp;
struct vnode **vp;
{
assert(vmp != NULL);
assert(vp != NULL);
resolve->l_path = path;
resolve->l_flags = flags;
resolve->l_vmp = vmp;
resolve->l_vnode = vp;
resolve->l_vmnt_lock = TLL_NONE;
resolve->l_vnode_lock = TLL_NONE;
*vmp = NULL; /* Initialize lookup result to NULL */
*vp = NULL;
}
/*===========================================================================*
* get_name *
*===========================================================================*/
int get_name(dirp, entry, ename)
struct vnode *dirp;
struct vnode *entry;
char ename[NAME_MAX + 1];
{
#define DIR_ENTRIES 8
#define DIR_ENTRY_SIZE (sizeof(struct dirent) + NAME_MAX)
off_t pos, new_pos;
int r, consumed, totalbytes, name_len;
char buf[DIR_ENTRY_SIZE * DIR_ENTRIES];
struct dirent *cur;
pos = 0;
if (!S_ISDIR(dirp->v_mode)) return(EBADF);
do {
r = req_getdents(dirp->v_fs_e, dirp->v_inode_nr, pos, (vir_bytes)buf,
sizeof(buf), &new_pos, 1);
if (r == 0) {
return(ENOENT); /* end of entries -- matching inode !found */
} else if (r < 0) {
return(r); /* error */
}
consumed = 0; /* bytes consumed */
totalbytes = r; /* number of bytes to consume */
do {
cur = (struct dirent *) (buf + consumed);
name_len = cur->d_reclen - offsetof(struct dirent, d_name) - 1;
if(cur->d_name + name_len+1 > &buf[sizeof(buf)])
return(EINVAL); /* Rubbish in dir entry */
if (entry->v_inode_nr == cur->d_fileno) {
/* found the entry we were looking for */
int copylen = MIN(name_len + 1, NAME_MAX + 1);
if (strlcpy(ename, cur->d_name, copylen) >= copylen) {
return(ENAMETOOLONG);
}
ename[NAME_MAX] = '\0';
return(OK);
}
/* not a match -- move on to the next dirent */
consumed += cur->d_reclen;
} while (consumed < totalbytes);
pos = new_pos;
} while (1);
}
/*===========================================================================*
* canonical_path *
*===========================================================================*/
int canonical_path(orig_path, rfp)
char orig_path[PATH_MAX];
struct fproc *rfp;
{
/* Find canonical path of a given path */
int len = 0;
int r, symloop = 0;
struct vnode *dir_vp, *parent_dir;
struct vmnt *dir_vmp, *parent_vmp;
char component[NAME_MAX+1]; /* NAME_MAX does /not/ include '\0' */
char temp_path[PATH_MAX];
struct lookup resolve;
parent_dir = dir_vp = NULL;
parent_vmp = dir_vmp = NULL;
strlcpy(temp_path, orig_path, PATH_MAX);
temp_path[PATH_MAX - 1] = '\0';
/* First resolve path to the last directory holding the file */
do {
if (dir_vp) {
unlock_vnode(dir_vp);
unlock_vmnt(dir_vmp);
put_vnode(dir_vp);
}
lookup_init(&resolve, temp_path, PATH_NOFLAGS, &dir_vmp, &dir_vp);
resolve.l_vmnt_lock = VMNT_READ;
resolve.l_vnode_lock = VNODE_READ;
if ((dir_vp = last_dir(&resolve, rfp)) == NULL) return(err_code);
/* dir_vp points to dir and resolve path now contains only the
* filename.
*/
strlcpy(orig_path, temp_path, NAME_MAX+1); /* Store file name */
/* If we're just crossing a mount point, our name has changed to '.' */
if (!strcmp(orig_path, ".")) orig_path[0] = '\0';
/* check if the file is a symlink, if so resolve it */
r = rdlink_direct(orig_path, temp_path, rfp);
if (r <= 0)
break;
/* encountered a symlink -- loop again */
strlcpy(orig_path, temp_path, PATH_MAX);
symloop++;
} while (symloop < _POSIX_SYMLOOP_MAX);
if (symloop >= _POSIX_SYMLOOP_MAX) {
if (dir_vp) {
unlock_vnode(dir_vp);
unlock_vmnt(dir_vmp);
put_vnode(dir_vp);
}
return(ELOOP);
}
/* We've got the filename and the actual directory holding the file. From
* here we start building up the canonical path by climbing up the tree */
while (dir_vp != rfp->fp_rd) {
strlcpy(temp_path, "..", NAME_MAX+1);
/* check if we're at the root node of the file system */
if (dir_vp->v_vmnt->m_root_node == dir_vp) {
if (dir_vp->v_vmnt->m_mounted_on == NULL) {
/* Bail out, we can't go any higher */
break;
}
unlock_vnode(dir_vp);
unlock_vmnt(dir_vmp);
put_vnode(dir_vp);
dir_vp = dir_vp->v_vmnt->m_mounted_on;
dir_vmp = dir_vp->v_vmnt;
if (lock_vmnt(dir_vmp, VMNT_READ) != OK)
panic("failed to lock vmnt");
if (lock_vnode(dir_vp, VNODE_READ) != OK)
panic("failed to lock vnode");
dup_vnode(dir_vp);
}
lookup_init(&resolve, temp_path, PATH_NOFLAGS, &parent_vmp,
&parent_dir);
resolve.l_vmnt_lock = VMNT_READ;
resolve.l_vnode_lock = VNODE_READ;
if ((parent_dir = advance(dir_vp, &resolve, rfp)) == NULL) {
unlock_vnode(dir_vp);
unlock_vmnt(dir_vmp);
put_vnode(dir_vp);
return(err_code);
}
/* now we have to retrieve the name of the parent directory */
if ((r = get_name(parent_dir, dir_vp, component)) != OK) {
unlock_vnode(parent_dir);
unlock_vmnt(parent_vmp);
unlock_vnode(dir_vp);
unlock_vmnt(dir_vmp);
put_vnode(parent_dir);
put_vnode(dir_vp);
return(r);
}
len += strlen(component) + 1;
if (len >= PATH_MAX) {
/* adding the component to orig_path would exceed PATH_MAX */
unlock_vnode(parent_dir);
unlock_vmnt(parent_vmp);
unlock_vnode(dir_vp);
unlock_vmnt(dir_vmp);
put_vnode(parent_dir);
put_vnode(dir_vp);
return(ENOMEM);
}
/* Store result of component in orig_path. First make space by moving
* the contents of orig_path to the right. Move strlen + 1 bytes to
* include the terminating '\0'. Move to strlen + 1 bytes to reserve
* space for the slash.
*/
memmove(orig_path+strlen(component)+1, orig_path, strlen(orig_path)+1);
/* Copy component into canon_path */
memmove(orig_path, component, strlen(component));
/* Put slash into place */
orig_path[strlen(component)] = '/';
/* Store parent_dir result, and continue the loop once more */
unlock_vnode(dir_vp);
unlock_vmnt(dir_vmp);
put_vnode(dir_vp);
dir_vp = parent_dir;
dir_vmp = parent_vmp;
parent_vmp = NULL;
}
unlock_vmnt(dir_vmp);
unlock_vnode(dir_vp);
put_vnode(dir_vp);
/* add the leading slash */
len = strlen(orig_path);
if (strlen(orig_path) >= PATH_MAX) return(ENAMETOOLONG);
memmove(orig_path+1, orig_path, len + 1 /* include terminating nul */);
orig_path[0] = '/';
/* remove trailing slash if there is any */
if (len > 1 && orig_path[len] == '/') orig_path[len] = '\0';
return(OK);
}
/*===========================================================================*
* check_perms *
*===========================================================================*/
static int check_perms(ep, io_gr, pathlen)
endpoint_t ep;
cp_grant_id_t io_gr;
size_t pathlen;
{
int r, slot;
struct vnode *vp;
60F8
struct vmnt *vmp;
struct fproc *rfp;
char canon_path[PATH_MAX];
struct lookup resolve;
struct sockaddr_un sun;
if (isokendpt(ep, &slot) != OK) return(EINVAL);
if (pathlen < sizeof(sun.sun_path) || pathlen >= PATH_MAX) return(EINVAL);
rfp = &(fproc[slot]);
r = sys_safecopyfrom(who_e, io_gr, (vir_bytes) 0, (vir_bytes) canon_path,
pathlen);
if (r != OK) return(r);
canon_path[pathlen] = '\0';
/* Turn path into canonical path to the socket file */
if ((r = canonical_path(canon_path, rfp)) != OK) return(r);
if (strlen(canon_path) >= pathlen) return(ENAMETOOLONG);
/* copy canon_path back to the caller */
r = sys_safecopyto(who_e, (cp_grant_id_t) io_gr, (vir_bytes) 0,
(vir_bytes) canon_path, pathlen);
if (r != OK) return(r);
/* Now do permissions checking */
lookup_init(&resolve, canon_path, PATH_NOFLAGS, &vmp, &vp);
resolve.l_vmnt_lock = VMNT_READ;
resolve.l_vnode_lock = VNODE_READ;
if ((vp = eat_path(&resolve, rfp)) == NULL) return(err_code);
/* check permissions */
r = forbidden(rfp, vp, (R_BIT | W_BIT));
unlock_vnode(vp);
unlock_vmnt(vmp);
put_vnode(vp);
return(r);
}
/*===========================================================================*
* do_checkperms *
*===========================================================================*/
int do_checkperms(void)
{
/* This should be replaced by an ACL check. */
if (!super_user) return EPERM;
return check_perms(job_m_in.m_lsys_vfs_checkperms.endpt,
job_m_in.m_lsys_vfs_checkperms.grant,
job_m_in.m_lsys_vfs_checkperms.count);
}
0