Annotation of OpenXM/src/kxx/oxd.c, Revision 1.7
1.1 takayama 1: /*
1.7 ! takayama 2: $OpenXM: OpenXM/src/kxx/oxd.c,v 1.6 2002/10/28 08:57:28 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);
1.3 takayama 36: #define NOBODY 65534
1.1 takayama 37:
38: main(int argc, char *argv[]) {
39: char sname[1024];
40: int i;
41: int fdControl = -1; int portControl = 8089;
42: extern int OpenedSocket;
43: extern int Serial;
44: int result;
45: int fd;
1.3 takayama 46: int uid;
1.1 takayama 47:
48: strcpy(sname,"localhost");
49: i = 1;
50: while (i<argc) {
1.2 takayama 51: if (strcmp(argv[i],"--port")==0) {
1.1 takayama 52: i++;
53: if (i<argc) sscanf(argv[i],"%d",&portControl);
54: }else if (strcmp(argv[i],"--insecure") == 0) {
55: LocalMode = 0;
56: }else {
57: fprintf(stderr,"Unknown option %s.\n",argv[i]);
58: oxdUsage(); exit(10);
59: }
60: i++;
61: }
62:
1.3 takayama 63: uid = getuid();
64: if (uid == 0) {
65: /* If I'm a super user, then change uid to nobody. */
66: if (setuid(NOBODY) != 0) {
67: oxdError("Failed to change uid to nobody (%d)\n",NOBODY);
68: }
69: fprintf(stderr,"uid is changed to nobody (%d).\n",NOBODY);
70: }
1.1 takayama 71:
72: if (LocalMode) {
73: if (portControl != -1) {
74: fdControl = socketOpen(sname,portControl);
1.2 takayama 75: 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 76: portControl = OpenedSocket;
77: while (1) {
78: /* fdControl : fd for the wait queue */
79: /* fd : accepted fd */
80: fprintf(stderr,"Waiting a connection... Serial=%d\n",Serial);
81: fflush(NULL);
82: fd = socketAcceptLocal2(fdControl);
83: if (fd < 0) oxdError("Failed to accept.");
84: fprintf(stderr,"\nConnected.\n");
85: Serial++;
86: MyServerPid = fork();
87: if (MyServerPid > 0 ) {
88: /* parent */
89: close(fd);
90: } else {
91: result = childServerMain(fd);
92: exit(10);
93: }
94: }
95: }
96: }else{
97: oxdError("Non-localmode is not supported.");
98: }
99: }
100:
101:
102: oxdUsage() {
103: fprintf(stderr,"Usage: \n");
1.2 takayama 104: fprintf(stderr," oxd [--port xyz]\n");
1.1 takayama 105: fprintf(stderr,"\n");
106: }
107:
108: void exitServer(int n) {
109: extern Serial;
110: fprintf(stderr,"\nSerial=%d, Timeout. Exiting.\n",Serial);
111: exit(0);
112: }
113: void exitServer2(FILE *fp,char *s) {
114: extern Serial;
115: fprintf(stderr,"\nSerial=%d: %s\n",Serial,s);
116: fprintf(fp,"Error: %s\n",s);
117: fprintf(fp,"Close connection.\n",s);
118: fflush(NULL);
119: exit(0);
120: }
121: childServerMain(int fd) {
122: FILE *fp;
123: #define SIZE 1024
124: char comm[SIZE+2];
125: char *status;
126: int id;
127: char *tag;
128: char *key;
129: char *home;
130: char *body;
131: char fname[SIZE*2];
132: char fnameBody[SIZE];
133: char ccc[SIZE*3];
1.2 takayama 134: extern int Serial;
135: char *openxm;
1.5 takayama 136: int resultCode;
1.6 takayama 137: int st;
138:
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.5 takayama 193: /* Old code.
1.1 takayama 194: fprintf(fp,"<bye/>\n"); fflush(NULL);
1.5 takayama 195: fclose(fp);
1.1 takayama 196: system(ccc);
1.2 takayama 197: fprintf(stderr,"Serial=%d : The following command is finished : %s\n",Serial,ccc);
1.5 takayama 198: */
199: /* New code. It requires ox with -finish option. */
200: resultCode = system(ccc);
201: fprintf(stderr,"Serial=%d : The following command is finished : %s, resultCode=%d\n",Serial,ccc,resultCode);
202: if (resultCode == 0) {
203: fprintf(fp,"<suceeded/>\n");
204: }else{
205: fprintf(fp,"<failed code=\"%d\"/>\n",resultCode);
206: }
207:
208: GET_COMMAND /* expect <login/> */
209:
210: fclose(fp); /* close the connection */
1.6 takayama 211: fprintf(stderr,"Waiting the termination of the child process (ox server).\n");
212: fflush(NULL);
213: wait(&st);
214: fprintf(stderr,"%d: The child process is terminated.\n",Serial);
1.5 takayama 215: exit(0);
1.1 takayama 216: }
217:
1.2 takayama 218: char *getOpenXMpath() {
219: char *s;
220: char *sss;
221: s = getenv("OpenXM_HOME");
222: if (s == NULL) {
223: s = getenv("OPENXM_HOME");
224: }
225: if (s == NULL) sss="/usr/local/bin/openxm";
226: else {
227: sss = (char *) xtagMalloc(strlen(s)+20);
228: sprintf(sss,"%s/bin/openxm",s);
229: }
230: if (access(sss,X_OK&R_OK) == 0) {
231: }else{
232: oxdError("The shell script openxm does not exists. It is usually generated under OpenXM/rc");
233: }
234: return sss;
235: }
1.1 takayama 236:
237: /* These are dummy. It is defined in stackmachine.c */
238: unlockCtrlCForOx() { ; }
239: restoreLockCtrlCForOx() { ; }
240:
241: oxdError(char *s) {
242: fprintf(stderr,"%s\n",s);
243: exit(10);
244: }
245:
246:
247:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>