19 lines
2.9 KiB
C++
19 lines
2.9 KiB
C++
#include <sys/wait.h>
|
|
#include <time.h>
|
|
static json_object *ma_acl(json_object *r,const char *op,NETOBJ *obj){
|
|
const char *vol=ma_s(r,"volume"),*root=ma_s(r,"root"),*rel=ma_s(r,"path");int write=strcmp(op,"trustee-get")!=0;
|
|
if(!ma_name(vol)||!strcmp(vol,"SYS")||!root||root[0]!='/'||strlen(root)>240||!rel||strlen(rel)>240||obj->id<2)return ma_err("Invalid trustee volume/path/object (SYS protected)",-1);
|
|
if(write){int i;for(i=0;i<max_connections;i++)if(connections[i].active)return ma_err("Disconnect all NetWare connections before changing trustees",-1);}
|
|
json_object *mv=ma_get(r,"mask"),*pv=ma_get(r,"previous");int mask=json_object_get_int(mv),prev=json_object_get_int(pv);
|
|
if(write&&(!json_object_is_type(pv,json_type_int)||prev< -1||prev>1023||(!strcmp(op,"trustee-set")&&(!json_object_is_type(mv,json_type_int)||mask<0||mask>255))))return ma_err("Invalid rights or revision",-1);
|
|
char ids[24],ms[16],ps[16];snprintf(ids,sizeof ids,"%u",(unsigned)obj->id);snprintf(ms,sizeof ms,"%d",mask);snprintf(ps,sizeof ps,"%d",write?prev:-1);
|
|
int fd[2];if(pipe2(fd,O_CLOEXEC|O_NONBLOCK))return ma_err("Trustee helper pipe failed",errno);
|
|
pid_t pid=fork();if(pid==0){close(fd[0]);dup2(fd[1],STDOUT_FILENO);close(fd[1]);execl("/usr/local/sbin/ftrustee","ftrustee","--mars-admin",!strcmp(op,"trustee-get")?"get":!strcmp(op,"trustee-set")?"set":"remove",vol,root,rel,ids,ms,ps,(char*)NULL);_exit(127);}close(fd[1]);if(pid<0){close(fd[0]);return ma_err("Trustee helper fork failed",errno);}
|
|
char buf[8192];size_t used=0;int status=0,done=0;struct timespec begin,now;clock_gettime(CLOCK_MONOTONIC,&begin);
|
|
while(!done){char tmp[1024];ssize_t n;while((n=read(fd[0],tmp,sizeof tmp))>0){size_t take=(size_t)n;if(take>sizeof buf-1-used)take=sizeof buf-1-used;memcpy(buf+used,tmp,take);used+=take;}pid_t w=waitpid(pid,&status,WNOHANG);if(w==pid){done=1;break;}if(w<0&&errno!=EINTR)break;clock_gettime(CLOCK_MONOTONIC,&now);if((now.tv_sec-begin.tv_sec)*1000+(now.tv_nsec-begin.tv_nsec)/1000000>=2000)break;struct pollfd p={fd[0],POLLIN,0};poll(&p,1,20);}
|
|
if(done){ssize_t n;while(used<sizeof buf-1&&(n=read(fd[0],buf+used,sizeof buf-1-used))>0)used+=(size_t)n;}close(fd[0]);if(!done){kill(pid,SIGKILL);waitpid(pid,NULL,0);return ma_err("Trustee helper timeout; check actual rights before retry",-1);}buf[used]=0;
|
|
if(!WIFEXITED(status)||WEXITSTATUS(status)){int code=WIFEXITED(status)?WEXITSTATUS(status):-1;return ma_err(code==7?"Trustee changed; reload":code==5?"Directory unsafe, root mismatch, symlink, ownership, device or 32-bit inode limitation":code==4?"Trustee flag t required; unsupported volume type":"Original trustee operation failed; inspect actual rights",code);}
|
|
char *p=strstr(buf,"MARS-TRUSTEE ");int result;if(!p||sscanf(p,"MARS-TRUSTEE %d",&result)!=1||result< -1||result>1023)return ma_err("Trustee result missing; inspect actual rights",-1);
|
|
json_object *o=json_object_new_object();ma_num(o,"mask",result);return o;
|
|
}
|