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

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

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

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