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

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

1.1       takayama    1: /*
1.4     ! takayama    2:  $OpenXM: OpenXM/src/kxx/oxd.c,v 1.3 2002/10/20 23:49:12 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.1       takayama  139:   /* Starting oxd session */
                    140:   signal(SIGALRM,exitServer);
                    141:   alarm(60);
                    142:   fp = fdopen(fd,"w+");
                    143:   if (fp == NULL) oxdError("failed fdopen\n");
                    144:
                    145: #define GET_COMMAND {\
                    146:   fprintf(fp,"?"); fflush(fp); \
                    147:   status = fgets(comm,SIZE,fp); \
                    148:   if (status == NULL) { \
                    149:     fprintf(stderr,"End of Input.\n"); \
                    150:     exit(0); \
                    151:   } \
                    152:   /* fprintf(fp,"%s\n",comm); fflush(fp);*/ /* Just echo for debugging. */ \
                    153:   fprintf(stderr,"Serial=%d: command=%s\n",Serial,comm); }
                    154:
                    155:   /* Login  */
                    156:   GET_COMMAND ;
                    157:   tag = getTag(comm);
                    158:   if (tag == NULL) exitServer2(fp,"expecting <login method=\"file\"/>");
                    159:   if (strcmp(tag,"login") != 0) exitServer2(fp,"expecting <login method=\"file\"/>");
                    160:   key =getKeyValue(comm,"method");
                    161:   if (key == NULL || strcmp(key,"file") != 0) exitServer2(fp,"expecting <login method=\"file\"/>");
                    162:
                    163:   /* Out put a challenge. */
                    164:   home = getenv("HOME");
                    165:   if (home == NULL) oxdError("Set the HOME environmental variable.\n");
                    166:   id = Serial*1000+getpid();
                    167:   sprintf(fnameBody,".oxd%d",id);
                    168:   sprintf(fname,"%s/%s",home,fnameBody);
                    169:   fprintf(fp,"<challenge file=\"%s\"/>\n",fname); fflush(fp);
                    170:
                    171:   /* Wait <done/> */
                    172:   GET_COMMAND ;
                    173:   tag = getTag(comm);
                    174:   if ((tag == NULL) || (strcmp(tag,"done") != 0)) exitServer2(fp,"expecting <done>");
                    175:   if (access(fname,R_OK) == 0) {
                    176:        sprintf(ccc,"rm -f %s",fname);
                    177:        system(ccc);
                    178:   }else{
                    179:        exitServer2(fp,"Challenge file does not exist.");
                    180:   }
                    181:
                    182:   /* Expect <launch> */
                    183:   GET_COMMAND ;
                    184:   tag = getTag(comm);
                    185:   if ((tag == NULL) || (strcmp(tag,"launch")!=0))
                    186:        exitServer2(fp,"expecting <launch> ox -ox ox_asir -reverse -data dport -control cport </launch>");
                    187:
                    188:   body = getBody(comm);
                    189:   if (strlen(body) > SIZE*2) exitServer2(fp,"too big body.");
1.2       takayama  190:   openxm = getOpenXMpath();
                    191:   sprintf(ccc,"%s %s",openxm,body);
                    192:   fprintf(stderr,"Serial=%d : Executing command=%s\n",Serial,ccc);
1.1       takayama  193:   fprintf(fp,"<bye/>\n"); fflush(NULL);
                    194:   fclose(fp); /* close the connection */
                    195:   system(ccc);
1.2       takayama  196:   fprintf(stderr,"Serial=%d : The following command is finished : %s\n",Serial,ccc);
1.1       takayama  197: }
                    198:
1.2       takayama  199: char *getOpenXMpath() {
                    200:   char *s;
                    201:   char *sss;
                    202:   s = getenv("OpenXM_HOME");
                    203:   if (s == NULL) {
                    204:        s = getenv("OPENXM_HOME");
                    205:   }
                    206:   if (s == NULL) sss="/usr/local/bin/openxm";
                    207:   else {
                    208:        sss = (char *) xtagMalloc(strlen(s)+20);
                    209:        sprintf(sss,"%s/bin/openxm",s);
                    210:   }
                    211:   if (access(sss,X_OK&R_OK) == 0) {
                    212:   }else{
                    213:        oxdError("The shell script openxm does not exists. It is usually generated under OpenXM/rc");
                    214:   }
                    215:   return sss;
                    216: }
1.1       takayama  217:
                    218: /* These are dummy.  It is defined in stackmachine.c */
                    219: unlockCtrlCForOx() { ; }
                    220: restoreLockCtrlCForOx() { ; }
                    221:
                    222: oxdError(char *s) {
                    223:   fprintf(stderr,"%s\n",s);
                    224:   exit(10);
                    225: }
                    226:
                    227:
                    228:

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