summaryrefslogtreecommitdiff
path: root/inputmethod/skkserv/patches/patch-skkserv_skkserv.c
blob: c0fe2713481a2c7dfa9a04e918b69fb318863156 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
$NetBSD: patch-skkserv_skkserv.c,v 1.1 2013/03/10 18:53:32 hiramatsu Exp $

Add IPv6 support and binary search capability.

--- skkserv/skkserv.c.orig	2013-03-11 01:53:23.000000000 +0900
+++ skkserv/skkserv.c	2013-03-11 01:53:24.000000000 +0900
@@ -100,14 +100,16 @@
 #define SERVER_NOT_FOUND "4"
 #define SERVER_FULL	"9"
 
+#define warn(m)	\
+	{if (debug) fprintf(errout, "%s: %s\n", pgmnm, m);}
 #define err(m)	\
-	{if (debug) fprintf(errout, "%s: %s\n", pgmnm, m); exit(1);}
+	{warn(m); exit(1);}
 
 /*
  *	Global Variables
  */
 
-char	pgmver[] = "3.9.4 ";	/* version number */
+char	pgmver[] = "3.9.4nb7 (binary search) ";	/* version number */
 
 char	*pgmnm;		/* program name */
 char	*jname;		/* name of shared dictionary */
@@ -121,6 +123,9 @@
 int	jtab1[KANAMOJI]; /* index-table by 1st letter (1st part)*/
 int	jtab2[KANAMOJI]; /* index-table by 1st letter (2nd part)*/
 int	initsock;	/* socket for waiting requests */
+#ifdef PF_INET6
+int	initsock6;	/* IPv6 socket for waiting requests */
+#endif
 int	clientsock[MAXCLNT];	/* socket for each client */
 int	nclients;	/* max index for active clients */
 
@@ -130,8 +135,18 @@
   int	parg;
   int	setjisho = 0;
   int	ctlterm;	/* fildes for control terminal */
-  void	reread();
+  RETSIGTYPE 	req_reread();
+
+#ifdef SKKSERV_UID
+  gid_t	gid = SKKSERV_GID;
 
+  /* revoke privilege if any */
+  if (geteuid() == 0) {
+    setgroups(1, &gid);
+    setgid(SKKSERV_GID);
+    setuid(SKKSERV_UID);
+  }
+#endif
   pgmnm = argv[0];
   debug = 0;
   errout = stderr;
@@ -200,7 +215,10 @@
     fclose(stderr);
 
     /* detach child process from control terminal */
-#ifdef HAVE_TIOCNOTTY
+#ifdef HAVE_SETSID
+    setsid();
+#else
+#ifdef TIOCNOTTY
     if ((ctlterm = open("/dev/tty", 2)) >= 0) { 
       ioctl(ctlterm, TIOCNOTTY, 0);
       close(ctlterm);
@@ -215,6 +233,7 @@
     signal(SIGHUP, SIG_IGN);
     if (fork() != 0) exit(0);
 #endif
+#endif
   } else { /* debug mode */
     fprintf(errout, "SKK-JISYO is %s\n", jname);
     fflush(errout);
@@ -222,7 +241,7 @@
     if (errout != stderr) fclose(stderr);
   }
 
-  signal(SIGINT, reread);
+  signal(SIGINT, req_reread);
   /* 1993/6/5 by kam, re-read dictionary by "INT" signal */
 
   nclients = 0;
@@ -244,8 +263,11 @@
 mksock()
 {
   struct sockaddr_in	sin;
+#ifdef PF_INET6
+  struct sockaddr_in6	sin6;
+#endif
   struct servent	*sp;
-  int	optbuf = 1; /* enable socket REUSEADDR */
+  int	optbuf;		/* enable socket REUSEADDR */
   
   bzero((char*)&sin, sizeof(sin));
   sin.sin_family = AF_INET;
@@ -262,16 +284,54 @@
   sin.sin_port = htons(portnum); 
 
   if ((initsock = socket(PF_INET, SOCK_STREAM, 0)) < 0)
-    err("socket error; socket cannot be created");
-  if (setsockopt(initsock, SOL_SOCKET, SO_REUSEADDR, 
+    warn("socket error; socket cannot be created");
+#ifdef PF_INET6
+  bzero((char*)&sin6, sizeof(sin6));
+  sin6.sin6_family = AF_INET6;
+  /* sin6.sin6_addr.s6_addr = IN6ADDR_ANY_INIT;  ... all-zero pattern */
+  sin6.sin6_port = htons(portnum); 
+  if ((initsock6 = socket(PF_INET6, SOCK_STREAM, 0)) < 0)
+    warn("socket error; IPv6 socket cannot be created");
+#endif
+  optbuf = 1;
+  if (initsock >= 0 && setsockopt(initsock, SOL_SOCKET, SO_REUSEADDR, 
+		 &optbuf, sizeof(optbuf)) < 0)
+    warn("socket error; cannot set socket option");
+#ifdef PF_INET6
+  optbuf = 1;
+  if (initsock6 >= 0 && setsockopt(initsock6, SOL_SOCKET, SO_REUSEADDR, 
 		 &optbuf, sizeof(optbuf)) < 0)
-    err("socket error; cannot set socket option");
-  if (bind(initsock, (struct sockaddr *)&sin, sizeof(sin))< 0) 
-    err("bind error; the socket is already used");
-  if (listen(initsock, MAXQUE) < 0) 
+    warn("socket error; cannot set socket option");
+#endif
+  if (initsock >= 0 && bind(initsock, (struct sockaddr *)&sin, sizeof(sin))< 0)  {
+    warn("bind error; the socket is already used");
+    close(initsock);
+    initsock = -1;
+  }
+#ifdef PF_INET6
+  if (initsock6 >= 0 && bind(initsock6, (struct sockaddr *)&sin6, sizeof(sin6))< 0) {
+    warn("bind error; the socket is already used");
+    close(initsock6);
+    initsock6 = -1;
+  }
+#endif
+  if (initsock < 0
+#ifdef PF_INET6
+      && initsock6 < 0
+#endif
+	)
+    exit(1);
+  if (initsock >= 0 && listen(initsock, MAXQUE) < 0) 
     err("listen error; something wrong happened with the socket");
+#ifdef PF_INET6
+  if (initsock6 >= 0 && listen(initsock6, MAXQUE) < 0) 
+    err("listen error; something wrong happened with the IPv6 socket");
+#endif
   if (debug) {
     fprintf(errout, "file descriptor for initsock is %d\n", initsock);
+#ifdef PF_INET6
+    fprintf(errout, "file descriptor for initsock6 is %d\n", initsock6);
+#endif
     fflush(errout);
   }
 }
@@ -303,8 +363,19 @@
     mkoldjtab(buf);
 }
 
+/* using stdio in signal handler is unsafe */
+#ifdef __STDC__
+volatile
+#endif
+int reread_requested;
+
+RETSIGTYPE req_reread()
+{
+  reread_requested = 1;
+}
+
 /* 1993/6/5 by kam, re-read dictionary by "INT" signal */
-RETSIGTYPE reread()
+void reread()
 {
   if (fclose(jisho) < 0) {
     fprintf(stderr, 
@@ -382,6 +453,10 @@
   code = KANA_END;
 
   while ((c = fgetc(jisho)) != EOF) {
+    if (c == '>') {
+      fgets(buf, BUFSIZE, jisho);
+      continue;
+    }
     target = ((c & 0xff)<< 8) | (fgetc(jisho) & 0xff);
     if (target == STRMARK) {
       fgets(buf, BUFSIZE, jisho);
@@ -455,15 +530,22 @@
 main_loop()
 {
   fd_set 		readfds, writefds, exceptfds;
-  fd_set 		getrfds();
+  void	 		getrfds();
   struct sockaddr_in	from;
+#ifdef PF_INET6
+  struct sockaddr_in6	from6;
+#endif
   int			len;
   register int		i;
 
   FD_ZERO(&writefds);
   FD_ZERO(&exceptfds);
   for(;;) {	/* infinite loop; waiting for client's request */
-    readfds = getrfds();
+    if (reread_requested) {
+      reread();
+      reread_requested = 0;
+    }
+    getrfds(&readfds);
     if (select(MAXDTAB, &readfds, &writefds, &exceptfds, NULL) < 0) {
       if (errno == EINTR) /* if signal happens */
 	continue;
@@ -474,16 +556,32 @@
       fflush(errout);
     }
 
-    if (FD_ISSET(initsock, &readfds)) {
+    if (initsock >= 0 && FD_ISSET(initsock, &readfds)) {
       len = sizeof(from);
       if ((clientsock[nclients ++] = accept(initsock, &from, &len)) < 0) {
 	err("accept error; something wrong happened with the socket");
       }
-      if (nclients >= MAXDTAB - 3 - debug * 2) {
+      if (nclients >= MAXDTAB - 3
+#ifdef PF_INET6
+		-1
+#endif
+		- debug * 2) {
 	write(clientsock[--nclients], SERVER_FULL, 1);
 	close(clientsock[nclients]);
       }
     }
+#ifdef PF_INET6
+    if (initsock6 >= 0 && FD_ISSET(initsock6, &readfds)) {
+      len = sizeof(from6);
+      if ((clientsock[nclients ++] = accept(initsock6, &from6, &len)) < 0) {
+	err("accept error; something wrong happened with the socket");
+      }
+      if (nclients >= MAXDTAB - 4 - debug * 2) {
+	write(clientsock[--nclients], SERVER_FULL, 1);
+	close(clientsock[nclients]);
+      }
+    }
+#endif
 
     /*	naiive scheduling */
     for (i = 0; i < nclients; i ++)
@@ -511,20 +609,24 @@
  *	get bit pattern of read file descriptor
  */
 
-fd_set getrfds()
+void getrfds(rfds)
+fd_set	*rfds;
 {
-  fd_set		rfds;
   register int		i;
 
-  FD_ZERO(&rfds);
-  FD_SET(initsock, &rfds);
+  FD_ZERO(rfds);
+  if (initsock >= 0)
+    FD_SET(initsock, rfds);
+#ifdef PF_INET6
+  if (initsock6 >= 0)
+    FD_SET(initsock6, rfds);
+#endif
   for (i = 0; i < nclients; i ++)
-    FD_SET(clientsock[i], &rfds);
-  return (rfds);
+    FD_SET(clientsock[i], rfds);
 }
 
 /*
- *	reply to client: linear search
+ *	reply to client: binary search
  */
 
 search(commsock)
@@ -537,6 +639,7 @@
   int		n; 	 	 /* number of characters from client */
   int		sttpnt; 	 /* start point of searching */
   int		endpnt; 	 /* end point of searching */
+  int		curpnt;		 /* current point of searching */
   int		errcod = 0; 	 /* error flag */
   int		sstyle;		 /* search style */
 
@@ -645,18 +748,36 @@
       endpnt = jtab1[KANA_END - code + 1];
     }
   }
-  fseek(jisho, sttpnt, 0);
   if (debug)
-    fprintf(stderr, "from %d to %d\n", sttpnt, endpnt);
+    fprintf(errout, "from %d to %d\n", sttpnt, endpnt);
   
+  for (;;) {
+    if ((sstyle & 0x4) == 0) {	/* binary search? */
+      curpnt = (sttpnt + endpnt) / 2;
+      fseek(jisho, curpnt, 0);
   while ((c = fgetc(jisho)) != EOF) {
+	curpnt++;
+	if (c == EOL) break;
+      }
+      if (c == EOF) break;
+      if (curpnt >= endpnt) {
+	fseek(jisho, sttpnt, 0);
+	sstyle |= 0x4;	/* linear search */
+      }
+    }
+
+    if (debug) {fprintf(errout, "%d:%d\t%d\t%d\t", sstyle, sttpnt, curpnt, endpnt);}
+    c = fgetc(jisho);
     pbuf = &combuf[1]; /* ' ' is end-symbol */
     while (c == *pbuf && c != ' ' && c != EOL) {
-      if (debug) {fprintf(errout, "1:%d:%d:%d:%d:\n", c, *pbuf, ' ', EOL);}
+/*    if (debug) {fprintf(errout, "1:%d:%d:%d:%d:", c, *pbuf, ' ', EOL);}*/
+      if (debug) {fprintf(errout, "%c", c);}
       c = fgetc(jisho); pbuf++;
     } 
-    if (debug) {fprintf(errout, "1:%d:%d:%d:%d:\n", c, *pbuf, ' ', EOL);}
+/*    if (debug) {fprintf(errout, "1:%d:%d:%d:%d:", c, *pbuf, ' ', EOL);}*/
+    if (debug) {fprintf(errout, "%c", c);}
     if (c == ' ' && (*pbuf == ' ' || *pbuf == '\n')) { /* found */
+      if (debug) {fprintf(errout, "found\n");}
       if ((errcod = write(commsock, SERVER_FOUND, 1)) >= 0)
 	while ((c = fgetc(jisho)) != EOF) {
 	  *pbuf = c;
@@ -672,7 +793,18 @@
       }
       return(0);
     }
-    if (comp(*pbuf, c, sstyle)) {
+    if (debug) {
+      int ch;
+
+      if (c != ' ')
+	do {
+	  ch = fgetc(jisho);
+	  fprintf(errout, "%c", ch);
+	} while (ch != ' ' && ch != EOL);
+      fprintf(errout, "unmatched\n");
+    }
+    if (sstyle & 0x4) {
+      if (comp(*pbuf, c, sstyle&~0x4)) {
       if (debug) {
 	fprintf(stderr, "comp break %d \n", ftell(jisho));
       }
@@ -684,6 +816,12 @@
       if (c == EOL) break;
     }
     if (ftell(jisho) >= endpnt) break;
+    } else {
+      if (comp(*pbuf, c, sstyle&~0x4))
+	endpnt = curpnt;
+      else
+	sttpnt = curpnt;
+    }
   }
 
   if ((errcod = write(commsock, SERVER_NOT_FOUND, 1)) >= 0) {
@@ -724,6 +862,9 @@
     exit(1);
   }
   hentry = gethostbyname(hname);
+  if (hentry == NULL) {
+    fprintf(errout, "%s: WARNING: gethostbyname(%s) failed\n", pgmnm, hname);
+  } else {
 #ifdef NO_ADDR_LIST
   strcat(hname, ":");
   strcat(hname, hentry->h_addr);
@@ -736,6 +877,7 @@
     strcat(hname, inet_ntoa(*(struct in_addr *)*p++));
   }
 #endif
+  }
   strcat(hname, ": ");
 }