Files
mars_nwe_pcab/upstream-adapter/mars_trustee.inc

42 lines
3.1 KiB
C++

/* Native ftrustee extension: restricted direct assignments on configured data volumes. */
#include <stdint.h>
#include <limits.h>
#include <ctype.h>
#include <errno.h>
/* Every directory must be root-owned, non-writable by other users and not a symlink.
* This protects the original path-based trustee routines from unprivileged rename races. */
static int mt_path(const char *p,struct stat *st){
char part[PATH_MAX];size_t n=strlen(p),i;if(!n||n>=sizeof part||p[0]!='/')return -1;
memcpy(part,p,n+1);while(n>1&&part[n-1]=='/')part[--n]=0;
for(i=1;i<=n;i++)if(part[i]=='/'||part[i]==0){char c=part[i];part[i]=0;if(lstat(part,st)||!S_ISDIR(st->st_mode)||st->st_uid!=0||(st->st_mode&022))return -1;part[i]=c;}
return 0;
}
static int mt_relative(const char *p){if(!*p)return 1;const char *s=p;while(*s){size_t n=strcspn(s,"/");if(!n||n>120||strspn(s,"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_.-/")<n||(n==1&&s[0]=='.')||(n==2&&!strncmp(s,"..",2)))return 0;s+=n;if(*s=='/'&&!*++s)return 0;}return 1;}
static int mt_num(const char *s,long min,long max,long *v){char *end;errno=0;*v=strtol(s,&end,10);return !errno&&*s&&!*end&&*v>=min&&*v<=max;}
static int mt_admin(int argc,char **argv){
if(argc!=9||geteuid()!=0)return 2;
const char *op=argv[2],*vol=argv[3],*expectedRoot=argv[4],*rel=argv[5];long id,mask,previous;
if((strcmp(op,"get")&&strcmp(op,"set")&&strcmp(op,"remove"))||!strcmp(vol,"SYS")||!mt_num(argv[6],2,UINT32_MAX,&id)||!mt_num(argv[7],0,255,&mask)||!mt_num(argv[8],-1,1023,&previous)||!mt_relative(rel))return 2;
int v=nw_get_volume_number((uint8*)vol,strlen(vol));if(v<0||v>=used_nw_volumes)return 3;
NW_VOL *nv=nw_volumes+v;int opts=nv->options;
if(!(opts&VOL_OPTION_TRUSTEES)||(opts&(VOL_OPTION_IS_HOME|VOL_OPTION_IS_PIPE|VOL_OPTION_NO_INODES|VOL_OPTION_REMOUNT|VOL_OPTION_READONLY)))return 4;
char root[PATH_MAX],path[PATH_MAX],exp[PATH_MAX];struct stat rs,st;
if(!nv->unixname||snprintf(root,sizeof root,"%s",nv->unixname)>=(int)sizeof root||snprintf(exp,sizeof exp,"%s",expectedRoot)>=(int)sizeof exp)return 4;
size_t n=strlen(root),e=strlen(exp);while(n>1&&root[n-1]=='/')root[--n]=0;while(e>1&&exp[e-1]=='/')exp[--e]=0;
if(!strcmp(root,"/")||strcmp(root,exp)||mt_path(root,&rs))return 5;
if(snprintf(path,sizeof path,"%s/%s",root,rel)>=(int)sizeof path)return 5;
for(size_t i=n+1;path[i];i++)path[i]=(char)((opts&VOL_OPTION_DOWNSHIFT)?tolower((unsigned char)path[i]):toupper((unsigned char)path[i]));
if(mt_path(path,&st)||rs.st_dev!=st.st_dev||st.st_ino>UINT32_MAX||(uintmax_t)st.st_dev>UINT32_MAX)return 5;
act_obj_id=1;act_id_flags=1;act_uid=0;act_gid=0;
int old=tru_get_id_trustee(v,(uint8*)path,&st,(uint32)id),rc=0;if(old< -1)return 6;
if(strcmp(op,"get")){
if(old!=previous)return 7;
if(old>255)return 6;
if(!strcmp(op,"remove")){if(old!=-1)rc=tru_del_trustee(v,(uint8*)path,&st,(uint32)id);}
else if(old!=mask){NW_OIC entry;entry.id=(uint32)id;entry.trustee=(int)mask;rc=tru_add_trustee_set(v,(uint8*)path,&st,1,&entry);}
if(rc)return 8;
tru_free_cache(v);int now=tru_get_id_trustee(v,(uint8*)path,&st,(uint32)id);if(now!=(!strcmp(op,"remove")?-1:mask))return 9;old=now;
}
printf("\nMARS-TRUSTEE %d\n",old);fflush(stdout);return 0;
}