#define _POSIX_C_SOURCE 200809L #include "server.h" #include "fragger.h" #include #include #include #include static void put(unsigned char *p,uint32_t v){for(int i=0;i<4;i++)p[i]=(unsigned char)(v>>(i*8));} static uint32_t get(const unsigned char *p){return p[0]|(uint32_t)p[1]<<8|(uint32_t)p[2]<<16|(uint32_t)p[3]<<24;} static unsigned char in[512],out[512],copy[512],req[512];static size_t used; static int call(unsigned task,unsigned seq,size_t n,size_t cap){return mars_nds_request(task,seq,in,n,out,cap,&used);} static size_t name(const char *s){memset(req,0,sizeof req);size_t l=(strlen(s)+1)*2;put(req+12,l);for(size_t i=0;s[i];i++)req[16+i*2]=s[i];return 16+((l+3)&~3u)+8;} static void first(size_t len,unsigned verb,unsigned wire){memset(in,0,sizeof in);in[0]=2;put(in+1,NDS_END);put(in+5,wire);put(in+9,len+12);put(in+17,verb);put(in+21,128);} int main(int argc,char **argv){ (void)argv; if(argc==4&&!strcmp(argv[1],"resource")){ size_t n=name(argv[2]);first(n,1,512);memcpy(in+25,req,n); assert(!call(1,1,n+25,sizeof out)&&used==24&&get(out+8)==0); uint32_t id=(uint32_t)out[16]<<24|(uint32_t)out[17]<<16|(uint32_t)out[18]<<8|out[19]; assert(id==(uint32_t)strtoul(argv[3],NULL,10)); /* Documented NCP 104/06, not NDAP verb 6. */ in[0]=6;put(in+1,0xffffffff);assert(!call(1,2,5,sizeof out)&&used==24); assert(get(out)==0xe7&&get(out+4)==0&&get(out+8)==1&&get(out+12)==1&&get(out+16)==n&&get(out+20)==12); put(in+1,0x20);assert(!call(1,3,5,sizeof out)&&used==4&&get(out)==1); put(in+1,0x100);assert(call(1,4,5,sizeof out)==0xfe&&used==0); put(in+1,0x101);assert(!call(1,5,5,sizeof out)&&used==4&&get(out)==1); put(in+1,0xe7);assert(call(1,6,5,23)==0xfe&&used==0); assert(call(1,7,4,sizeof out)==0xfe&&used==0); in[0]=7;assert(call(1,8,1,sizeof out)==0xfb&&used==0); /* no unauthenticated reset */ puts("PASS NCP 104/06 statistics, flag negotiation, bounds, reset denied"); /* Unknown wire format and authentication continue to fail closed. */ for(unsigned v=2;v<=6;v++){first(0,v,512);assert(call(1,v,25,sizeof out)==0xfb&&used==0);} for(unsigned v=54;v<=61;v++){first(0,v,512);assert(call(1,v,25,sizeof out)==0xfb&&used==0);} puts("PASS resource object NCP Resolve, unsupported NDAP/auth verbs denied");return 0; } if(argc>1&&!strcmp(argv[1],"database")){ in[0]=1;assert(!call(1,1,4,sizeof out)&&!strcmp((char*)out+8,"MARS_TREE")); size_t n=name("CN=ALICE.OU=USERS.O=HOME");first(n,1,512);memcpy(in+25,req,n); assert(!call(1,2,n+25,sizeof out)); if(argc==3){assert(used==24&&get(out+8)==0);uint32_t id=(uint32_t)out[16]<<24|(uint32_t)out[17]<<16|(uint32_t)out[18]<<8|out[19];assert(id==(uint32_t)strtoul(argv[2],NULL,10));} else assert(used==12&&(int32_t)get(out+8)==-601); first(0,57,512);assert(call(1,3,25,sizeof out)==0xfb&&used==0); puts("PASS database-backed NCP Resolve Name and fail-closed login");return 0; } if(argc>1){in[0]=1;assert(call(1,1,4,sizeof out)==0xfb&&used==0);puts("PASS disabled or invalid model fails closed");return 0;} assert(!setenv("MARS_NDS_LAB","1",1));assert(!setenv("MARS_NDS_LAB_CONFIG","{\"tree\":\"MARS_TREE\",\"organization\":\"HOME\",\"units\":[\"USERS\",\"USERS/RETRO\",\"GROUPS\"]}",1)); in[0]=1;assert(!call(1,1,4,sizeof out)&&used==18&&get(out+4)==10&&!strcmp((char*)out+8,"MARS_TREE"));assert(call(1,1,4,8)==0xfe&&used==0);in[1]=1;assert(call(1,2,4,sizeof out)==0xfe);in[1]=0; in[0]=4;assert(!call(1,3,1,sizeof out)&&used==18&&get(out)==14&&!memcmp(out+4,"O\0=\0H\0O\0M\0E\0\0",14)); size_t n=name(".ou=retro.ou=users.o=home");first(n,1,512);memcpy(in+25,req,13);assert(!call(1,4,38,sizeof out)&&used==8);uint32_t h=get(out+4);memcpy(copy,out,used);assert(!call(1,4,38,sizeof out)&&!memcmp(copy,out,used)); in[0]=2;put(in+1,h);memcpy(in+5,req+13,n-13);assert(call(2,5,n-8,sizeof out)==0xfe);assert(!call(1,5,n-8,sizeof out)&&used==24&&get(out+4)==NDS_END&&get(out+8)==0&&get(out+12)==1&&get(out+20)==0);memcpy(copy,out,used);assert(!call(1,5,n-8,sizeof out)&&!memcmp(copy,out,used)); n=name("RETRO.USERS.HOME");first(n,1,512);memcpy(in+25,req,n);assert(!call(1,6,n+25,sizeof out)&&used==24&&!memcmp(out+16,copy+16,4)); n=name("CN=SECRET.OU=USERS.O=HOME");first(n,1,512);memcpy(in+25,req,n);assert(!call(1,7,n+25,sizeof out)&&used==12&&(int32_t)get(out+8)==-601); n=name("HOME");put(req+4,1);first(n,1,512);memcpy(in+25,req,n);assert(!call(1,8,n+25,sizeof out)&&(int32_t)get(out+8)==-672); first(0,57,512);assert(call(1,9,25,sizeof out)==0xfb&&used==0); n=name("HOME");first(n,1,512);memcpy(in+25,req,2);assert(!call(1,10,27,sizeof out));h=get(out+4);in[0]=3;put(in+1,h);assert(call(2,11,5,sizeof out)==0xfe);assert(!call(1,11,5,sizeof out)&&used==0);assert(call(1,12,5,sizeof out)==0xfe); n=name("HOME");put(req+12,0xffffffff);first(n,1,512);memcpy(in+25,req,n);assert(!call(1,13,n+25,sizeof out)&&(int32_t)get(out+8)==-641); for(unsigned i=0;i<50000;i++){size_t k=rand()%sizeof in;for(size_t j=0;j