2014-04-09 13:17:20 +00:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include "../util.h"
|
|
|
|
|
2014-04-23 19:28:59 +00:00
|
|
|
mode_t
|
|
|
|
getumask(void)
|
|
|
|
{
|
2014-06-01 12:59:47 +00:00
|
|
|
mode_t mask = umask(0);
|
|
|
|
umask(mask);
|
|
|
|
return mask;
|
2014-04-23 19:28:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
mode_t
|
|
|
|
parsemode(const char *str, mode_t mode, mode_t mask)
|
2014-04-09 13:17:20 +00:00
|
|
|
{
|
|
|
|
char *end;
|
|
|
|
const char *p;
|
2014-04-23 19:28:59 +00:00
|
|
|
int octal, op = '+';
|
|
|
|
mode_t gmask = 0, m = 0;
|
2014-04-09 13:17:20 +00:00
|
|
|
|
|
|
|
octal = strtol(str, &end, 8);
|
|
|
|
if(*end == '\0') {
|
2014-04-23 19:28:59 +00:00
|
|
|
if(octal < 0 || octal > 07777) {
|
2014-04-09 13:17:20 +00:00
|
|
|
eprintf("%s: invalid mode\n", str);
|
2014-04-23 19:28:59 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
mode = 0;
|
|
|
|
if(octal & 04000) mode |= S_ISUID;
|
|
|
|
if(octal & 02000) mode |= S_ISGID;
|
|
|
|
if(octal & 01000) mode |= S_ISVTX;
|
|
|
|
if(octal & 00400) mode |= S_IRUSR;
|
|
|
|
if(octal & 00200) mode |= S_IWUSR;
|
|
|
|
if(octal & 00100) mode |= S_IXUSR;
|
|
|
|
if(octal & 00040) mode |= S_IRGRP;
|
|
|
|
if(octal & 00020) mode |= S_IWGRP;
|
|
|
|
if(octal & 00010) mode |= S_IXGRP;
|
|
|
|
if(octal & 00004) mode |= S_IROTH;
|
|
|
|
if(octal & 00002) mode |= S_IWOTH;
|
|
|
|
if(octal & 00001) mode |= S_IXOTH;
|
|
|
|
return mode;
|
2014-04-09 13:17:20 +00:00
|
|
|
}
|
2014-04-23 19:28:59 +00:00
|
|
|
for(p = str; *p; p++) {
|
2014-04-09 13:17:20 +00:00
|
|
|
switch(*p) {
|
|
|
|
/* masks */
|
|
|
|
case 'u':
|
2014-04-23 19:28:59 +00:00
|
|
|
gmask |= S_IRWXU;
|
2014-04-09 13:17:20 +00:00
|
|
|
break;
|
|
|
|
case 'g':
|
2014-04-23 19:28:59 +00:00
|
|
|
gmask |= S_IRWXG;
|
2014-04-09 13:17:20 +00:00
|
|
|
break;
|
|
|
|
case 'o':
|
2014-04-23 19:28:59 +00:00
|
|
|
gmask |= S_IRWXO;
|
2014-04-09 13:17:20 +00:00
|
|
|
break;
|
|
|
|
case 'a':
|
2014-04-23 19:28:59 +00:00
|
|
|
gmask |= S_IRWXU|S_IRWXG|S_IRWXO;
|
2014-04-09 13:17:20 +00:00
|
|
|
break;
|
|
|
|
/* opers */
|
2014-04-23 19:28:59 +00:00
|
|
|
case '=':
|
2014-04-09 13:17:20 +00:00
|
|
|
case '+':
|
|
|
|
case '-':
|
2014-04-23 19:28:59 +00:00
|
|
|
op = (int)*p;
|
2014-04-09 13:17:20 +00:00
|
|
|
break;
|
|
|
|
/* modes */
|
|
|
|
case 'r':
|
2014-04-23 19:28:59 +00:00
|
|
|
m |= S_IRUSR|S_IRGRP|S_IROTH;
|
2014-04-09 13:17:20 +00:00
|
|
|
break;
|
|
|
|
case 'w':
|
2014-04-23 19:28:59 +00:00
|
|
|
m |= S_IWUSR|S_IWGRP|S_IWOTH;
|
2014-04-09 13:17:20 +00:00
|
|
|
break;
|
|
|
|
case 'x':
|
2014-04-23 19:28:59 +00:00
|
|
|
m |= S_IXUSR|S_IXGRP|S_IXOTH;
|
2014-04-09 13:17:20 +00:00
|
|
|
break;
|
|
|
|
case 's':
|
2014-04-23 19:28:59 +00:00
|
|
|
m |= S_ISUID|S_ISGID;
|
2014-04-09 13:17:20 +00:00
|
|
|
break;
|
|
|
|
case 't':
|
2014-04-23 19:28:59 +00:00
|
|
|
m |= S_ISVTX;
|
2014-04-09 13:17:20 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
eprintf("%s: invalid mode\n", str);
|
2014-04-23 19:28:59 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
/* apply */
|
|
|
|
switch(op) {
|
|
|
|
case '+':
|
|
|
|
mode |= (m & ((~mask) & 0777));
|
|
|
|
break;
|
|
|
|
case '-':
|
|
|
|
mode &= (~(m & ((~mask) & 0777)));
|
|
|
|
break;
|
|
|
|
case '=':
|
|
|
|
mode = (m & ((~mask) & 0777));
|
|
|
|
break;
|
2014-04-09 13:17:20 +00:00
|
|
|
}
|
2014-04-23 19:28:59 +00:00
|
|
|
}
|
|
|
|
if(gmask && op != '=')
|
|
|
|
mode &= ~gmask;
|
|
|
|
return mode;
|
2014-04-09 13:17:20 +00:00
|
|
|
}
|