#include <config.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/param.h>
#include <stdio.h>
#ifdef HAVE_STRING_H
# include <string.h>
#endif
#ifdef HAVE_STRINGS_H
# include <strings.h>
#endif
#ifdef HAVE_UNISTD_H
# include <unistd.h>
#endif
#include <errno.h>
#include "missing.h"
#include "secure_path.h"
int
sudo_secure_path(path, type, uid, gid, sbp)
const char *path;
int type;
uid_t uid;
gid_t gid;
struct stat *sbp;
{
struct stat sb;
int rval = SUDO_PATH_MISSING;
if (path != NULL && stat(path, &sb) == 0) {
if ((sb.st_mode & _S_IFMT) != type) {
rval = SUDO_PATH_BAD_TYPE;
} else if (uid != (uid_t)-1 && sb.st_uid != uid) {
rval = SUDO_PATH_WRONG_OWNER;
} else if (sb.st_mode & S_IWOTH) {
rval = SUDO_PATH_WORLD_WRITABLE;
} else if (ISSET(sb.st_mode, S_IWGRP) &&
(gid == (gid_t)-1 || sb.st_gid != gid)) {
rval = SUDO_PATH_GROUP_WRITABLE;
} else {
rval = SUDO_PATH_SECURE;
}
if (sbp)
(void) memcpy(sbp, &sb, sizeof(struct stat));
}
return rval;
}
int
sudo_secure_file(path, uid, gid, sbp)
const char *path;
uid_t uid;
gid_t gid;
struct stat *sbp;
{
return sudo_secure_path(path, _S_IFREG, uid, gid, sbp);
}
int
sudo_secure_dir(path, uid, gid, sbp)
const char *path;
uid_t uid;
gid_t gid;
struct stat *sbp;
{
return sudo_secure_path(path, _S_IFDIR, uid, gid, sbp);
}