[BACK]Return to oxd.c CVS log [TXT][DIR] Up to [local] / OpenXM / src / kxx

Annotation of OpenXM/src/kxx/oxd.c, Revision 1.5

1.1       takayama    1: /*
1.5     ! takayama    2:  $OpenXM: OpenXM/src/kxx/oxd.c,v 1.4 2002/10/27 10:39:33 takayama Exp $
1.1       takayama    3: */
                      4:
                      5: #include <stdio.h>
                      6: #include <stdlib.h>
                      7: #include <fcntl.h>
                      8: #include <unistd.h>
                      9: #include <sys/types.h>
                     10: #include <sys/socket.h>
                     11: #include <sys/time.h>
                     12: #include <netinet/in.h>
                     13: #include <netdb.h>
                     14: #include <signal.h>
                     15: #include <setjmp.h>
                     16: /* -lnsl -lsocket /usr/ucblib/libucb.a */
                     17: #include "ox_kan.h"
                     18: #include "serversm.h"
1.4       takayama   19:
                     20: /* #define DEBUG */
1.1       takayama   21:
                     22: char *getTag(char *s);
                     23: char *getKeyValue(char *s,char *key);
                     24: char *getBody(char *s);
1.2       takayama   25: char *getOpenXMpath(void);
                     26: void *xtagMalloc(int n);
1.1       takayama   27:
                     28: int MyServerPid;
                     29: #define SERVERNAME_SIZE 4096
                     30: int Quiet = 0;
                     31: int Serial = 0;
                     32:
                     33: int LocalMode = 1;
                     34: static int findOxServer(char *server);
                     35: static void couldNotFind(char *s);
                     36: #if defined(__CYGWIN__)
                     37: int errno;
                     38: #endif
1.3       takayama   39: #define NOBODY 65534
1.1       takayama   40:
                     41: main(int argc, char *argv[]) {
                     42:   char sname[1024];
                     43:   int i;
                     44:   int fdControl = -1; int portControl = 8089;
                     45:   extern int OpenedSocket;
                     46:   extern int Serial;
                     47:   int result;
                     48:   int fd;
1.3       takayama   49:   int uid;
1.1       takayama   50:
                     51:   strcpy(sname,"localhost");
                     52:   i = 1;
                     53:   while (i<argc) {
1.2       takayama   54:     if (strcmp(argv[i],"--port")==0) {
1.1       takayama   55:       i++;
                     56:       if (i<argc) sscanf(argv[i],"%d",&portControl);
                     57:     }else if (strcmp(argv[i],"--insecure") == 0) {
                     58:       LocalMode = 0;
                     59:     }else {
                     60:       fprintf(stderr,"Unknown option %s.\n",argv[i]);
                     61:       oxdUsage(); exit(10);
                     62:     }
                     63:     i++;
                     64:   }
                     65:
1.3       takayama   66:   uid = getuid();
                     67:   if (uid == 0) {
                     68:        /* If I'm a super user, then change uid to nobody. */
                     69:        if (setuid(NOBODY) != 0) {
                     70:          oxdError("Failed to change uid to nobody (%d)\n",NOBODY);
                     71:        }
                     72:        fprintf(stderr,"uid is changed to nobody (%d).\n",NOBODY);
                     73:   }
1.1       takayama   74:
                     75:   if (LocalMode) {
                     76:     if (portControl != -1) {
                     77:       fdControl = socketOpen(sname,portControl);
1.2       takayama   78:          if (fdControl < 0) oxdError("Could not open a socket. \n\nPerhaps, oxd is already running on your machine.\nTo start oxd on a different port xyz, start oxd by oxd --port xyz");
1.1       takayama   79:       portControl = OpenedSocket;
                     80:          while (1) {
                     81:                /* fdControl : fd for the wait queue */
                     82:                /* fd : accepted fd */
                     83:                fprintf(stderr,"Waiting a connection... Serial=%d\n",Serial);
                     84:                fflush(NULL);
                     85:                fd = socketAcceptLocal2(fdControl);
                     86:                if (fd < 0) oxdError("Failed to accept.");
                     87:                fprintf(stderr,"\nConnected.\n");
                     88:                Serial++;
                     89:                MyServerPid = fork();
                     90:                if (MyServerPid > 0 ) {
                     91:                  /* parent */
                     92:                  close(fd);
                     93:                } else {
                     94:                  result = childServerMain(fd);
                     95:                  exit(10);
                     96:                }
                     97:          }
                     98:     }
                     99:   }else{
                    100:        oxdError("Non-localmode is not supported.");
                    101:   }
                    102: }
                    103:
                    104:
                    105: oxdUsage() {
                    106:   fprintf(stderr,"Usage: \n");
1.2       takayama  107:   fprintf(stderr,"  oxd [--port xyz]\n");
1.1       takayama  108:   fprintf(stderr,"\n");
                    109: }
                    110:
                    111: void exitServer(int n) {
                    112:   extern Serial;
                    113:   fprintf(stderr,"\nSerial=%d, Timeout. Exiting.\n",Serial);
                    114:   exit(0);
                    115: }
                    116: void exitServer2(FILE *fp,char *s) {
                    117:   extern Serial;
                    118:   fprintf(stderr,"\nSerial=%d: %s\n",Serial,s);
                    119:   fprintf(fp,"Error: %s\n",s);
                    120:   fprintf(fp,"Close connection.\n",s);
                    121:   fflush(NULL);
                    122:   exit(0);
                    123: }
                    124: childServerMain(int fd) {
                    125:   FILE *fp;
                    126: #define SIZE 1024
                    127:   char comm[SIZE+2];
                    128:   char *status;
                    129:   int id;
                    130:   char *tag;
                    131:   char *key;
                    132:   char *home;
                    133:   char *body;
                    134:   char fname[SIZE*2];
                    135:   char fnameBody[SIZE];
                    136:   char ccc[SIZE*3];
1.2       takayama  137:   extern int Serial;
                    138:   char *openxm;
1.5     ! takayama  139:   int resultCode;
1.1       takayama  140:   /* Starting oxd session */
                    141:   signal(SIGALRM,exitServer);
                    142:   alarm(60);
                    143:   fp = fdopen(fd,"w+");
                    144:   if (fp == NULL) oxdError("failed fdopen\n");
                    145:
                    146: #define GET_COMMAND {\
                    147:   fprintf(fp,"?"); fflush(fp); \
                    148:   status = fgets(comm,SIZE,fp); \
                    149:   if (status == NULL) { \
                    150:     fprintf(stderr,"End of Input.\n"); \
                    151:     exit(0); \
                    152:   } \
                    153:   /* fprintf(fp,"%s\n",comm); fflush(fp);*/ /* Just echo for debugging. */ \
                    154:   fprintf(stderr,"Serial=%d: command=%s\n",Serial,comm); }
                    155:
                    156:   /* Login  */
                    157:   GET_COMMAND ;
                    158:   tag = getTag(comm);
                    159:   if (tag == NULL) exitServer2(fp,"expecting <login method=\"file\"/>");
                    160:   if (strcmp(tag,"login") != 0) exitServer2(fp,"expecting <login method=\"file\"/>");
                    161:   key =getKeyValue(comm,"method");
                    162:   if (key == NULL || strcmp(key,"file") != 0) exitServer2(fp,"expecting <login method=\"file\"/>");
                    163:
                    164:   /* Out put a challenge. */
                    165:   home = getenv("HOME");
                    166:   if (home == NULL) oxdError("Set the HOME environmental variable.\n");
                    167:   id = Serial*1000+getpid();
                    168:   sprintf(fnameBody,".oxd%d",id);
                    169:   sprintf(fname,"%s/%s",home,fnameBody);
                    170:   fprintf(fp,"<challenge file=\"%s\"/>\n",fname); fflush(fp);
                    171:
                    172:   /* Wait <done/> */
                    173:   GET_COMMAND ;
                    174:   tag = getTag(comm);
                    175:   if ((tag == NULL) || (strcmp(tag,"done") != 0)) exitServer2(fp,"expecting <done>");
                    176:   if (access(fname,R_OK) == 0) {
                    177:        sprintf(ccc,"rm -f %s",fname);
                    178:        system(ccc);
                    179:   }else{
                    180:        exitServer2(fp,"Challenge file does not exist.");
                    181:   }
                    182:
                    183:   /* Expect <launch> */
                    184:   GET_COMMAND ;
                    185:   tag = getTag(comm);
                    186:   if ((tag == NULL) || (strcmp(tag,"launch")!=0))
                    187:        exitServer2(fp,"expecting <launch> ox -ox ox_asir -reverse -data dport -control cport </launch>");
                    188:
                    189:   body = getBody(comm);
                    190:   if (strlen(body) > SIZE*2) exitServer2(fp,"too big body.");
1.2       takayama  191:   openxm = getOpenXMpath();
                    192:   sprintf(ccc,"%s %s",openxm,body);
                    193:   fprintf(stderr,"Serial=%d : Executing command=%s\n",Serial,ccc);
1.5     ! takayama  194:   /*  Old code.
1.1       takayama  195:   fprintf(fp,"<bye/>\n"); fflush(NULL);
1.5     ! takayama  196:   fclose(fp);
1.1       takayama  197:   system(ccc);
1.2       takayama  198:   fprintf(stderr,"Serial=%d : The following command is finished : %s\n",Serial,ccc);
1.5     ! takayama  199:    */
        !           200:   /* New code. It requires  ox with -finish option. */
        !           201:   resultCode = system(ccc);
        !           202:   fprintf(stderr,"Serial=%d : The following command is finished : %s, resultCode=%d\n",Serial,ccc,resultCode);
        !           203:   if (resultCode == 0) {
        !           204:        fprintf(fp,"<suceeded/>\n");
        !           205:   }else{
        !           206:        fprintf(fp,"<failed code=\"%d\"/>\n",resultCode);
        !           207:   }
        !           208:
        !           209:   GET_COMMAND   /* expect <login/> */
        !           210:
        !           211:   fclose(fp); /* close the connection */
        !           212:   exit(0);
1.1       takayama  213: }
                    214:
1.2       takayama  215: char *getOpenXMpath() {
                    216:   char *s;
                    217:   char *sss;
                    218:   s = getenv("OpenXM_HOME");
                    219:   if (s == NULL) {
                    220:        s = getenv("OPENXM_HOME");
                    221:   }
                    222:   if (s == NULL) sss="/usr/local/bin/openxm";
                    223:   else {
                    224:        sss = (char *) xtagMalloc(strlen(s)+20);
                    225:        sprintf(sss,"%s/bin/openxm",s);
                    226:   }
                    227:   if (access(sss,X_OK&R_OK) == 0) {
                    228:   }else{
                    229:        oxdError("The shell script openxm does not exists. It is usually generated under OpenXM/rc");
                    230:   }
                    231:   return sss;
                    232: }
1.1       takayama  233:
                    234: /* These are dummy.  It is defined in stackmachine.c */
                    235: unlockCtrlCForOx() { ; }
                    236: restoreLockCtrlCForOx() { ; }
                    237:
                    238: oxdError(char *s) {
                    239:   fprintf(stderr,"%s\n",s);
                    240:   exit(10);
                    241: }
                    242:
                    243:
                    244:

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>