|
|
1.1 ! root 1: # ! 2: #define ERROR 0 ! 3: #define GOOD 1 ! 4: ! 5: /* ! 6: * fdemon tiu txt msiz [trace] ! 7: * file store demon to handle commands from (tiu). ! 8: * the txt arg is not used. ! 9: * The maximum message size sent to the user is (msiz). ! 10: * If (trace) is present the demon will trace on the specified file. ! 11: * the current output is used if '-' is specified. ! 12: */ ! 13: #include <stdio.h> ! 14: #include "udemon.h" ! 15: ! 16: ! 17: char unixid[128]; ! 18: char *uidp &unixid; ! 19: char nbuf[NSIZ] { ! 20: }; ! 21: char *mkdir "/bin/mkdir"; ! 22: char *rmdir "/bin/rmdir"; ! 23: char *minishell "/bin/sh"; ! 24: char *netpwd "/usr/lib/spidernet"; ! 25: int zero[2] { ! 26: 0,0}; ! 27: ! 28: ! 29: ! 30: ! 31: int *tp; ! 32: int parent; ! 33: int pid; ! 34: char *uniqs "prXXXXXX"; ! 35: int kk[50]; ! 36: #define CHILD 0 ! 37: #define INPUT 0 ! 38: #define OUTPUT 1 ! 39: #define RPIPE 0 ! 40: #define WPIPE 1 ! 41: main(argc,argv) ! 42: int argc; ! 43: char *argv[]; ! 44: { ! 45: int code; /*status to/from tiu*/ ! 46: int fil; /*file code for active file*/ ! 47: int mode; ! 48: int rpfil[2], wpfil[2]; /*pipes for shell execution*/ ! 49: int op; /*op code*/ ! 50: char *cp, *ap; ! 51: int k, n, s, t; ! 52: register i; ! 53: int rlength,rsum; ! 54: int nchar; ! 55: char *rbp; ! 56: char *p; ! 57: ! 58: ! 59: signal(1,1); ! 60: signal(2,1); ! 61: signal(3,1); ! 62: if (argc == 4) ! 63: trace = 0; ! 64: else ! 65: if (argc == 5){ ! 66: trace = 1; ! 67: if (*argv[4] != '-'){ ! 68: close(OUTPUT); ! 69: close(INPUT); ! 70: close(2); ! 71: if (creat(argv[4],0666) < 0) { ! 72: mesg("debug creat failed"); ! 73: exit(EGOOD); ! 74: } ! 75: dup(INPUT); ! 76: dup(INPUT); ! 77: } ! 78: }else { ! 79: mesg("Incorrect number of arguments"); ! 80: exit(EGOOD); ! 81: } ! 82: msiz = decimal(argv[3]); ! 83: prt("msiz = %d\n",msiz); ! 84: parent = 1; ! 85: rstart: ! 86: prt("got to res"); ! 87: panic = 0; ! 88: if ((fn = open(tiu = argv[1],2)) < 0) { ! 89: mesg("Can't open tiu channel"); ! 90: exit(EGOOD); ! 91: } ! 92: snstat(fn,&chan,2); ! 93: utmp.u_tty = chan + 'A'; ! 94: for (i=0; i<OPENS; i++) ! 95: openf[i] = 0; ! 96: rp = resp; ! 97: logout(); ! 98: prt("logout done"); ! 99: trouble: ! 100: snstat(fn,&code,3); ! 101: prt("check trob code"); ! 102: if (code) ! 103: prt("trouble %d\n",code); ! 104: closeall(); ! 105: setexit(); ! 106: nextmes: ! 107: prt("nextmes"); ! 108: if (panic){ ! 109: prt("nextmes panic\n"); ! 110: closeall(); ! 111: close(fn); ! 112: if(!parent) ! 113: exit(EBAD); ! 114: goto rstart; ! 115: ! 116: } ! 117: prt("start recv"); ! 118: if ((n=recv(mbuf,&code)) < 0) ! 119: goto trouble; ! 120: prt("mesg received"); ! 121: if (code != 3) ! 122: error(ESIG,code); ! 123: if ((login == 0) && (filstat("/tmp/ulock",&statbuf) >= 0)) ! 124: error(EUNAV,1); ! 125: mlast = &mbuf[n]; ! 126: lp = mlink; ! 127: mp = mbuf; ! 128: sp = stack; ! 129: rp = resp; ! 130: ig = 0; ! 131: (sp++)->s_type = NULL; ! 132: prt("sp = %o %o\n",stack,sp); ! 133: if ((*mp <= '~') && (*mp >= '!')) ! 134: goto escape; ! 135: ! 136: nextcmd: ! 137: prt("next sp %o %o\n",sp,sp->s_type); ! 138: if ((lp == mlink) && (mp >= mlast)){ ! 139: if (mp == mlast) ! 140: goto stop; ! 141: error(EEOM,1); ! 142: } ! 143: if (*mp > 0) ! 144: goto data; ! 145: ! 146: if ((op = *mp++ & 0377) == 0) ! 147: goto nextcmd; ! 148: prt("op = %o\n",op); ! 149: ! 150: if (ig){ ! 151: if (op == EOM) ! 152: error(EEOM,2); ! 153: if ((op == FI) & (ig < 0)) ig = 0; ! 154: goto nextcmd; ! 155: ! 156: } ! 157: op =& 0177; ! 158: if (op >= OPNUM) ! 159: error(EOP,1); ! 160: if ((login == 0) && (op != (LOGIN-128)) && (argspec[op][0] != 0)) ! 161: error(ELOG,1); ! 162: asp = sp; ! 163: prt("for sp = %o %o\n",sp,sp->s_type); ! 164: for (i=0; i<3; i++){ ! 165: s = argspec[op][i]; ! 166: if (s==0) ! 167: break; ! 168: la: ! 169: t = asp->s_type; ! 170: prt("la %o %o %o %o\n",s,t,asp,sp); ! 171: if (t==s) ! 172: goto cont; ! 173: ! 174: if ((t==NTYPE) || (t==WTYPE)){ ! 175: deref(asp); ! 176: prt("deref %o\n",asp); ! 177: goto la; ! 178: ! 179: } ! 180: if (s < 0) ! 181: goto cont; ! 182: ! 183: prt("swit s = %o\n",s); ! 184: switch (s){ ! 185: case XTYPE: ! 186: if (t == IITYPE) ! 187: goto cont; ! 188: ! 189: case IITYPE: ! 190: tp = zero; ! 191: break; ! 192: case RTYPE: ! 193: if (wstore[1].s_type != RTYPE) ! 194: error(EARGT,2); ! 195: tp = &wstore[1].s_val; ! 196: break; ! 197: case DTYPE: ! 198: if (wstore[0].s_type != DTYPE) ! 199: error(EARGT,8); ! 200: tp = &wstore[0].s_val; ! 201: break; ! 202: default: ! 203: error(EARGT,1); ! 204: } ! 205: argpush(&asp); ! 206: prt("argpush %o %o\n",asp,sp); ! 207: asp->s_type = s; ! 208: asp->s_val = *tp++; ! 209: asp->s_val1 = *tp; ! 210: prt("arg %o %o %o %o %o\n",asp->s_val,asp->s_val1,tp,zero); ! 211: cont: ! 212: asp--; ! 213: } ! 214: switch(op+128){ ! 215: case UNLESS: ! 216: if (dstat(ncopy(sp),&entry) >= 0) ! 217: ig = -1; ! 218: case FI: ! 219: prt("FI"); ! 220: goto nextcmd; ! 221: ! 222: case IF: ! 223: prt("IF %o %o\n",sp,sp->s_type); ! 224: if (dstat(ncopy(sp),&entry) < 0){ ! 225: ig = -1; ! 226: prt("IF ig = -1\n"); ! 227: } ! 228: prt("end IF %o\n",sp->s_type); ! 229: goto nextcmd; ! 230: ! 231: case SKIP: ! 232: ig = ((sp--)->s_val1&0377) | 0400; ! 233: mp = lp->l_ms; ! 234: goto nextcmd; ! 235: ! 236: case EOM: ! 237: stop: ! 238: if (respond(OK)) { ! 239: prt("respond = 1\n"); ! 240: if(!parent){ ! 241: exit(EBAD); ! 242: } ! 243: goto trouble; ! 244: } ! 245: goto nextmes; ! 246: ! 247: case ASSIGN: ! 248: t = (sp--)->s_val1; ! 249: if ((t<0) || (t>=VARNO)) ! 250: error(EWADDR,1); ! 251: assign: ! 252: asp = &wstore[t]; ! 253: asp->s_type = sp->s_type; ! 254: asp->s_val = sp->s_val; ! 255: asp->s_val1 = sp->s_val1; ! 256: goto nextcmd; ! 257: ! 258: case FCREATE: ! 259: cp = ncopy(sp--); ! 260: asp = sp--; ! 261: if (asp->s_type == XTYPE) { ! 262: asp->s_val1 = 0666; ! 263: prt("made it 0666\n"); ! 264: } ! 265: prt("creat mode %o\n",asp->s_val1); ! 266: fil = creat(cp, asp->s_val1); ! 267: goto opn; ! 268: ! 269: case DCREATE: ! 270: cp = ncopy(sp--); ! 271: asp = sp--; ! 272: tp = mkdir; ! 273: if (dstat(cp,&entry) >= 0) ! 274: error(EEXIST,1); ! 275: execute: ! 276: callsys(cp); ! 277: goto nextcmd; ! 278: ! 279: case REDEFINE: ! 280: t = 0; ! 281: goto df; ! 282: ! 283: case DEFINE: ! 284: t = 1; ! 285: df: ! 286: if (nodef[sp->s_type]==0) ! 287: error(EARGT,3); ! 288: asp = sp--; ! 289: if (sp->s_type != NTYPE) ! 290: error(EARGT,4); ! 291: ncopy(asp); ! 292: cp = ncopy(sp); ! 293: if (asp->s_type != FTYPE) ! 294: unixid[1].d_type = asp->s_type | 0200; ! 295: if (dstat(cp, &entry) >= 0){ ! 296: if (t) ! 297: error(EEXIST,2); ! 298: unlink(cp); ! 299: } ! 300: if (mklink(unixid,cp) < 0) ! 301: error(0,1); ! 302: goto nextcmd; ! 303: ! 304: case DELETE: ! 305: i = dcheck(sp); ! 306: prt("delete dcheck finished\n"); ! 307: if (i == DTYPE) { ! 308: tp = rmdir; ! 309: cp = ncopy(sp); ! 310: goto execute; ! 311: } ! 312: if (i == ATYPE) ! 313: error(EISACC,1); ! 314: prt("delete copy %s\n",ncopy(sp)); ! 315: cp = ncopy(sp--); ! 316: if (unlink(cp) < 0) ! 317: error(EUNDEF,1); ! 318: prt("delete unlink\n"); ! 319: goto nextcmd; ! 320: ! 321: case OPEN: ! 322: prt("OP %o %o \n",*mp,READ); ! 323: if (sp->s_type == XTYPE) sp->s_val1 = 02; ! 324: asp = sp--; ! 325: prt("OPEN %o \n",asp->s_val1); ! 326: if((*mp&0377) == READ){ ! 327: asp->s_val1 = 0; ! 328: }else ! 329: if((*mp&0377) == WRITE){ ! 330: asp->s_val1 = 1; ! 331: }else ! 332: asp->s_val1 = 2; ! 333: fil = open(ncopy(sp),asp->s_val1); ! 334: opn: ! 335: if (fil < 0) ! 336: error(0,2); ! 337: sp++; ! 338: sp->s_type = RTYPE; ! 339: sp->s_val = 0; ! 340: sp->s_val1 = fil; ! 341: openf[fil] = 1; ! 342: t = 1; ! 343: goto assign; ! 344: ! 345: case CLOSE: ! 346: t = (sp--)->s_val1; ! 347: if (openf[t] == 0) ! 348: error(EFREF,1); ! 349: close(t); ! 350: goto nextcmd; ! 351: ! 352: /* ! 353: case SEEK: ! 354: asp = sp--; ! 355: t = sp->s_val1; ! 356: if (openf[t] == 0) ! 357: error(EFREF,2); ! 358: seek(t,asp->s_val1,0); ! 359: seek(t,asp->s_val,7); ! 360: goto nextcmd; ! 361: ! 362: case APPEND: ! 363: t = sp->s_val1; ! 364: if (openf[t] == 0) ! 365: error(EFREF,7); ! 366: seek(t,0,2); ! 367: goto nextcmd; ! 368: ! 369: case TRUNCATE: ! 370: asp = sp--; ! 371: t = sp->s_val1; ! 372: if (openf[t] == 0) ! 373: error(EFREF,3); ! 374: if (asp->s_type != XTYPE){ ! 375: seek(t,asp->s_val1,0); ! 376: seek(t,asp->s_val,7); ! 377: } ! 378: trunc(t); ! 379: goto nextcmd; ! 380: ! 381: */ ! 382: case READ: ! 383: asp = sp--; ! 384: fil = sp->s_val1; ! 385: if (openf[fil] == 0) ! 386: error(EFREF,4); ! 387: k = (asp->s_type ==XTYPE) ? 0 : 1; ! 388: cp = asp->s_val1; ! 389: if (rcopy(fil,k,cp)) ! 390: goto trouble; ! 391: goto nextcmd; ! 392: ! 393: case WRITE: ! 394: asp = sp--; ! 395: fil = sp->s_val1; ! 396: if (openf[fil] == 0) ! 397: error(EFREF,5); ! 398: if (respond(WRITE)) ! 399: goto trouble; ! 400: ! 401: k = (asp->s_type == XTYPE) ? 0 : 1; ! 402: cp = asp->s_val1; ! 403: length = 0; ! 404: sum = 0; ! 405: i = 0; ! 406: for (;;){ ! 407: if ((n=recv(buf,&code)) < 0) ! 408: goto trouble; ! 409: ! 410: length =+ n; ! 411: sum =+ addup(buf,n); ! 412: if (code==3) ! 413: error(ESIG,code); ! 414: if (code > 4) ! 415: error(ESIG,code); ! 416: if ((k) && (n>cp)) n=cp; ! 417: if ((n!=0) && (i == 0)) ! 418: if (write(fil,buf,n) < 0) ! 419: i = 1; ! 420: if (code > 1) ! 421: break; ! 422: cp =- n; ! 423: } ! 424: if((nchar = recv(buf,&code)) < 0) ! 425: prt("bad read\n"); ! 426: if(code != 3) ! 427: prt("Bad cheksum message\n"); ! 428: prt("snstat after %o\n",code); ! 429: prt("code %o %o %o %o\n",sum,length,code,buf[0]); ! 430: rbp = buf; ! 431: rbp++; ! 432: rsum.hibyte = *rbp++; ! 433: rsum.lobyte = *rbp++; ! 434: rlength.hibyte = *rbp++; ! 435: rlength.lobyte = *rbp++; ! 436: prt("received %o %o %o %o\n",buf[1],buf[2],buf[3],buf[4]); ! 437: prt("%o %o\n",rsum,rlength); ! 438: if(sum != rsum){ ! 439: prt("Checksum error\n"); ! 440: } ! 441: if(length != rlength){ ! 442: prt("Byte count error\n"); ! 443: } ! 444: if (i) ! 445: error(0,3); ! 446: goto nextcmd; ! 447: ! 448: case XMT: ! 449: goto nextcmd; ! 450: ! 451: case CMODE: ! 452: k = (sp--)->s_val1; ! 453: t = (sp--)->s_val1; ! 454: cp = ncopy(sp); ! 455: if (filstat(cp,&statbuf) < 0) ! 456: error(EUNDEF,4); ! 457: k = (k & t) | (statbuf->mode & (~t)); ! 458: if (chmod(cp,k) < 0) ! 459: error(0,10); ! 460: goto nextcmd; ! 461: ! 462: case STATUS: ! 463: prt("STAT %o %d\n",sp,sp->s_type); ! 464: switch(sp->s_type){ ! 465: case RTYPE: ! 466: fil = sp->s_val1; ! 467: if (openf[fil] == 0) ! 468: error(EFREF,6); ! 469: if (desstat(fil,&statbuf) < 0) ! 470: error(0,7); ! 471: goto stdun; ! 472: ! 473: case NTYPE: ! 474: case FTYPE: ! 475: case DTYPE: ! 476: case ATYPE: ! 477: if (filstat(ncopy(sp),&statbuf) < 0) ! 478: error(EUNDEF,3); ! 479: stdun: ! 480: *rp++ = STYPE; ! 481: *rp++ = sp->s_type; ! 482: prt("stat rp = %o\n",rp); ! 483: rp =+ copy(rp,&statbuf,sizeof(us)); ! 484: prt("rp +34. = %o\n",rp); ! 485: prt("STATUS to be sent %d\n",sp->s_type); ! 486: goto nextcmd; ! 487: ! 488: default: ! 489: error(EARGT,7); ! 490: } ! 491: case LIST: ! 492: asp = sp--; ! 493: prt("list %o %o\n",asp->s_type,unixid); ! 494: prt("list %s %s %o\n",asp->s_val1,ncopy(asp),ncopy(asp)); ! 495: if ((fil = open(ncopy(asp),0)) < 0) ! 496: error(0,8); ! 497: n = rcopy(fil,2,16); ! 498: close(fil); ! 499: if (n) ! 500: goto trouble; ! 501: ! 502: goto nextcmd; ! 503: ! 504: case POP: ! 505: sp--; ! 506: goto nextcmd; ! 507: ! 508: case CDIR: ! 509: if (chdir(ncopy(sp)) < 0) ! 510: error(0,4); ! 511: wstore[0].s_type = sp->s_type; ! 512: wstore[0].s_val = sp->s_val; ! 513: wstore[0].s_val1 = (sp--)->s_val1; ! 514: goto nextcmd; ! 515: ! 516: case LOGIN: ! 517: closeall(); ! 518: if (getpwentry(sp->s_val1,buf)) ! 519: error(EPWD,1); ! 520: login = 1; ! 521: if(parent){ ! 522: parent = 0; ! 523: if((pid = fork())!= -1){ ! 524: if(pid != CHILD){ ! 525: parent = 1; ! 526: while(pid != wait(&status)); ! 527: if(((status>>8)&0377) == EBAD) ! 528: prt("Daemon error\n"); ! 529: logout(); ! 530: goto nextmes; ! 531: }else{ ! 532: setuid(uid); ! 533: } ! 534: ! 535: }else{ ! 536: prt("Can't fork\n"); ! 537: } ! 538: } ! 539: goto stop; ! 540: ! 541: case QUIT: ! 542: closeall(); ! 543: if(respond(OK)) ! 544: if(!parent){ ! 545: prt("going to exit\n"); ! 546: exit(EBAD); ! 547: } ! 548: if(!parent){ ! 549: prt("going to exit - good\n"); ! 550: exit(EGOOD); ! 551: } ! 552: ! 553: logout(); ! 554: goto nextmes; ! 555: case UNIQUE: ! 556: prt("asp = %o\n",asp); ! 557: /* ! 558: */ ! 559: prt("asp = %o %o\n",asp,sp); ! 560: up = uniqb; ! 561: cp = ncopy(sp--); ! 562: asp = sp--; ! 563: if(asp->s_type == XTYPE){ ! 564: asp->s_val1 = 0666; ! 565: } ! 566: prt("saved %s\n",cp); ! 567: up =+ copy(uniqb,cp,size(cp)); ! 568: prt("copied %s\n",uniqb); ! 569: *up++ = '/'; ! 570: copy(up,uniqs,size(uniqs)); ! 571: prt("unique name %s\n",uniqb); ! 572: up = mktemp(uniqb); ! 573: prt("mktmp name %s\n",up); ! 574: *rp++ = UNIQUE; ! 575: rp =+ copy(rp,uniqb,size(uniqb)); ! 576: *rp++ = '\0'; ! 577: prt("resp name %s\n",resp); ! 578: close(creat(up,asp->s_val1)); ! 579: fil = open(up,2); ! 580: goto opn; ! 581: default: ! 582: error(EOP,2); ! 583: } ! 584: data: ! 585: prt("data %d\n",ig); ! 586: if (ig){ ! 587: switch (*mp++){ ! 588: case LTYPE: ! 589: if (((*mp++ & 0377) + 0400) == ig) ! 590: ig = 0; ! 591: goto nextcmd; ! 592: ! 593: case NTYPE: ! 594: while (*mp++); ! 595: goto nextcmd; ! 596: ! 597: default: ! 598: mp =+ 2; ! 599: case ITYPE: ! 600: case WTYPE: ! 601: case RTYPE: ! 602: mp =+ 2; ! 603: goto nextcmd; ! 604: ! 605: } ! 606: } ! 607: sp++; ! 608: sp->s_type = *mp++; ! 609: prt("data2 %o %o %o\n",sp,sp->s_type,*mp); ! 610: switch (sp->s_type){ ! 611: case NTYPE: ! 612: sp->s_val1 = mp; ! 613: if (*mp == 0) ! 614: error(EARGT,9); ! 615: while (*mp++); ! 616: prt("NTYPE %o\n",sp->s_type); ! 617: goto nextcmd; ! 618: ! 619: case ITYPE: ! 620: sp->s_type = IITYPE; ! 621: case WTYPE: ! 622: prt("mp = %o\n",mp); ! 623: mp =+ copy(&sp->s_val1,mp,2); ! 624: prt("mp = %o\n",mp); ! 625: sp->s_val = (sp->s_val1 < 0) ? -1 : 0; ! 626: goto nextcmd; ! 627: ! 628: case RTYPE: ! 629: error(EARGT,5); ! 630: case LTYPE: ! 631: sp--; ! 632: mp++; ! 633: goto nextcmd; ! 634: ! 635: case ETYPE: ! 636: sp--; ! 637: mp =+ 2; ! 638: error(*mp++, *mp++); ! 639: } ! 640: mp =+ copy(&sp->s_val,mp,4); ! 641: goto nextcmd; ! 642: ! 643: escape: ! 644: if (login == 0) ! 645: error(ELOG, 2); ! 646: if ((pipe(rpfil) < 0) || (pipe(wpfil) < 0)) ! 647: error(0,11); ! 648: mbuf[n++] = '\n'; ! 649: write(rpfil[WPIPE],mbuf,n); ! 650: close(rpfil[WPIPE]); ! 651: while((i = fork()) == -1) ! 652: sleep(1); ! 653: if (i == CHILD){ ! 654: if (rpfil[RPIPE] != 0) { ! 655: close(INPUT); ! 656: dup(rpfil[RPIPE]); ! 657: } ! 658: if (wpfil[WPIPE] != 1) { ! 659: close(OUTPUT); ! 660: dup(wpfil[WPIPE]); ! 661: } ! 662: for (i=2; i<15; i++) ! 663: close(i); ! 664: dup(OUTPUT); ! 665: execl(minishell,minishell,"-t",0); ! 666: mesg("Can't execute shell"); ! 667: exit(EBAD); ! 668: } ! 669: close(wpfil[WPIPE]); ! 670: close(rpfil[RPIPE]); ! 671: /* ! 672: if(desstat(wpfil[RPIPE],kk) < 0){ ! 673: prt("stat failed of pipe file\n"); ! 674: }else{ ! 675: prt("stat %o %o %o\n",kk[0],kk[1],kk[5]); ! 676: } ! 677: */ ! 678: if (n = rcopy(wpfil[RPIPE],-1,0)){ ! 679: kill(i,9); ! 680: } ! 681: while(wait(&status) != i); ! 682: close(wpfil[RPIPE]); ! 683: if (n) ! 684: goto trouble; ! 685: else ! 686: goto stop; ! 687: ! 688: } ! 689: mklink(p,q) ! 690: char *p,*q; ! 691: { ! 692: return(link(p,q)); ! 693: } ! 694: callsys(cp) ! 695: char *cp; ! 696: { ! 697: register i; ! 698: ! 699: while ((i=fork()) == -1) ! 700: sleep(1); ! 701: if (i==CHILD){ ! 702: for (i=0; i<15; i++) close(i); ! 703: execl(tp,tp,cp,0); ! 704: execl(&tp[4],&tp[4],cp,0); ! 705: mesg("Can't exec"); ! 706: mesg(cp); ! 707: exit(EBAD); ! 708: } ! 709: while (i != wait(&pstat)); ! 710: if (pstat.w_stat == EBAD) ! 711: error(EFAIL,1); ! 712: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.