Debian Codegraph @

ax25/axspawn.c

raw
1 /*
2 * axspawn.c - run a program from ax25d.
3 *
4 * Copyright (c) 1996 Joerg Reuter DL1BKE ([email protected])
5 *
6 * This program is a hack.
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * It might even kill your cat... ;-)
19 *
20 * Status: alpha (still...)
21 *
22 * usage: change the "default" lines in your /usr/local/etc/ax25d.conf:
23 *
24 * default * * * * * 1 root /usr/local/sbin/axspawn axspawn
25 *
26 * a line like this would wait for an incoming info frame first.
27 *
28 * default * * * * * 1 root /usr/local/sbin/axspawn axspawn --wait
29 *
30 * The program will check if the peer is an AX.25 socket, the
31 * callsign is a valid amateur radio callsign, strip the SSID,
32 * check if UID/GID are valid, allow a password-less login if the
33 * password-entry in /etc/passwd is "+" or empty; in every other case
34 * login will prompt for a password.
35 *
36 * Still on my TODO list: a TheNet compatible or MD5 based
37 * authentication scheme... Won't help you if you changed the "+"-entry
38 * in /etc/passwd to a valid passord and login with telnet, though.
39 * A better solution could be a small program called from .profile.
40 *
41 * Axspawn can create user accounts automatically. You may specify
42 * the user shell, first and maximum user id, group ID in the config
43 * file and (unlike WAMPES) create a file "/usr/local/etc/ax25.profile"
44 * which will be copied to ~/.profile.
45 *
46 * This is an example for the config file:
47 *
48 * # this is /usr/local/etc/axspawn.conf
49 * #
50 * # allow automatic creation of user accounts
51 * create yes
52 *
53 * # allow empty password field (so user may login via telnet, or pop3 their
54 * # mail) [default no]
55 * create_empty_password no
56 *
57 * # pwcheck method: password or call or group [default: password]
58 * # "password" means, that passwords with '+' force a login without
59 * # prompting for a password (old behaviour; backward compatibility).
60 * # "call" means, that ham calls via ax25/netrom/rose/.. should be able
61 * # to login without password, even if it's set (for e.g. to secure
62 * # from abuse of inet connections)
63 * # "group" means, that if the gid of the user matches the configured
64 * # default user_gid, then the login is granted without password.
65 * #pwcheck call
66 * #pwcheck group
67 * #pwcheck password
68 *
69 * # create with system utility useradd(8)? [default no]
70 * create_with_useradd no
71 *
72 * # guest user if above is 'no' or everything else fails. Disable with "no"
73 * guest ax25
74 *
75 * # group id or name for autoaccount
76 * group ax25
77 *
78 * # first user id to use
79 * first_uid 400
80 *
81 * # maximum user id
82 * max_uid 2000
83 *
84 * # where to add the home directory for the new user
85 * home /home/hams
86 *
87 * # secure homedirectories (g-rwx)
88 * #secure_home yes
89 *
90 * # user's shell
91 * shell /bin/bash
92 *
93 * # bind user id to callsign for outgoing connects.
94 * associate yes
95 *
96 * SECURITY:
97 *
98 * Umm... auto accounting is a security problem by definition. Unlike
99 * WAMPES, which creates an empty password field, Axspawn adds an
100 * "impossible" ('+') password to /etc/passwd. Login gets called with
101 * the "-f" option, thus new users have the chance to login without
102 * a password. (I guess this won't work with the shadow password system).
103 * News: there are good reasons to use empty password fields. For e.g.
104 * at our mailbox-system db0tud where we have no inet access, so
105 * every user is a ham, not a hacker. We need the empty password
106 * so new may use ax25 for automatical account generation, and then
107 * could login via telnet or poll their mail with pop3.
108 * But i acknowledge, that this package will mostly used by hams who
109 * use their computer for both, inet and ampr. So the new option
110 * create_empty_password is not the default, but a feature
111 * needed because we won't patch this code after a new release.
112 * the useradd method and compression implementation is the the work
113 * of thomas <dg1rtf>, the finetune is by me. GPL
114 * the compression code initialy was for tnt by dl4ybg (GPL).
115 * - dl9sau for db0tud team.
116 *
117 * The "associate" option has to be used with great care. If a user
118 * logs on it removes any existing callsign from the translation table
119 * for this userid and replaces it with the callsign and SSID of the
120 * user. This will happen with multiple connects (same callsign,
121 * different SSIDs), too. Unless you want your users to be able
122 * to call out from your machine disable "associate".
123 *
124 * Of course Axspawn does callsign checking: Only letters and numbers
125 * are allowed, the callsign must be longer than 4 characters and
126 * shorter than 6 characters (without SSID). There must be at least
127 * one digit, and max. two digits within the call. The SSID must
128 * be within the range of 0 and 15. Please drop me a note if you
129 * know a valid Amateur Radio (sic!) callsign that does not fit this
130 * pattern _and_ can be represented correctly in AX.25.
131 */
132
133 /* removed -h <protocol> from login command as it was causing hostname lookups
134 with new login/libc - Terry, vk2ktj. */
135
136
137 /*#define QUEUE_DELAY 400 / * 400 usec */
138 #define QUEUE_DELAY 100000 /* 10 msec */
139 #define USERPROFILE ".profile"
140 #define PASSWDFILE "/etc/passwd"
141
142 #include <stdio.h>
143 #include <stdlib.h>
144 #include <fcntl.h>
145 #include <string.h>
146 #include <ctype.h>
147 #include <termios.h>
148 #include <unistd.h>
149 #include <signal.h>
150 #include <time.h>
151 #include <pwd.h>
152 #include <grp.h>
153 #include <pty.h>
154 #include <utmp.h>
155 #include <paths.h>
156 #include <errno.h>
157 #include <syslog.h>
158 #include <sys/time.h>
159 #include <sys/types.h>
160 #include <sys/stat.h>
161 #include <sys/ioctl.h>
162 #include <sys/file.h>
163 #include <sys/wait.h>
164
165 #include <config.h>
166
167 #include <sys/socket.h>
168
169 #include <netax25/ax25.h>
170 #include <netrose/rose.h>
171
172 #include <netax25/axlib.h>
173
174 #include "../pathnames.h"
175 #include "axspawn.h"
176 #include "access.h"
177
178 #define MAXLEN strlen("DB0PRA-15")
179 #define MINLEN strlen("KA9Q")
180
181 #define AX_PACLEN 256
182 #define NETROM_PACLEN 236
183 #define ROSE_PACLEN 128
184
185 #define IS_DIGIT(x) ( (x >= '0') && (x <= '9') )
186 #define IS_LETTER(x) ( (x >= 'A') && (x <= 'Z') )
187
188 #define MSG_NOCALL "Sorry, you are not allowed to connect.\n"
189 #define MSG_CANNOTFORK "Sorry, system is overloaded.\n"
190 #define MSG_NOPTY "Sorry, all channels in use.\n"
191 #define MSG_NOTINDBF "Sorry, you are not in my database.\n"
192
193 #define write_ax25_static_line(s) { \
194 char *msg = strdup(s); \
195 if (msg) { \
196 write_ax25(msg, strlen(msg), 1); \
197 free(msg); \
198 } \
199 }
200
201 #define EXITDELAY 10
202
203 #define USERADD_CONF "/etc/default/useradd"
204
205 struct huffencodtab {
206 unsigned short code;
207 unsigned short len;
208 };
209
210 struct huffdecodtab {
211 unsigned short node1;
212 unsigned short node2;
213 };
214
215
216
217 /* huffman encoding table */
218 static struct huffencodtab huffencodtab[] = {
219 {0xab2c, 15}, {0xaa84, 15}, {0x9fc4, 15}, {0xab3c, 15},
220 {0xab1c, 15}, {0xaafc, 15}, {0xaaec, 15}, {0xaad4, 15},
221 {0xaab4, 15}, {0xf340, 10}, {0xaaa4, 15}, {0x7d64, 15},
222 {0xaadc, 15}, {0xf400, 7}, {0xaa94, 15}, {0x9ff4, 15},
223 {0x9fd4, 15}, {0x7d74, 15}, {0xab44, 15}, {0xab34, 15},
224 {0xab24, 15}, {0xab14, 15}, {0xab04, 15}, {0xaaf4, 15},
225 {0xaae4, 15}, {0xab60, 14}, {0xab0c, 15}, {0xaacc, 15},
226 {0xaabc, 15}, {0xaaac, 15}, {0xaa9c, 15}, {0xaa8c, 15},
227 {0xc000, 3}, {0x3a80, 9}, {0xabc0, 10}, {0x0060, 11},
228 {0x7d40, 12}, {0xab5c, 14}, {0x0000, 12}, {0xab58, 14},
229 {0x7c00, 9}, {0x3c80, 9}, {0x7d00, 11}, {0x0010, 12},
230 {0x1200, 7}, {0x7a00, 7}, {0xb800, 6}, {0x3200, 7},
231 {0x2200, 7}, {0xf600, 8}, {0x3d00, 8}, {0x9e00, 9},
232 {0xbd80, 9}, {0x7c80, 9}, {0x0080, 9}, {0xaa00, 9},
233 {0xbd00, 9}, {0x9f00, 9}, {0x0300, 8}, {0xab78, 13},
234 {0xab68, 13}, {0x3c00, 9}, {0x3000, 9}, {0x0020, 11},
235 {0x7d50, 12}, {0x3800, 7}, {0x7800, 7}, {0x9c00, 7},
236 {0xfe00, 7}, {0x2400, 6}, {0xbc00, 8}, {0x0200, 8},
237 {0x0100, 8}, {0xf100, 8}, {0x0040, 11}, {0x3100, 8},
238 {0xf200, 8}, {0x3400, 7}, {0x1c00, 7}, {0x1e00, 7},
239 {0xbe00, 7}, {0xaba0, 11}, {0x3e00, 7}, {0x1400, 6},
240 {0x3600, 7}, {0xf380, 9}, {0xf080, 9}, {0x2000, 8},
241 {0xfc00, 8}, {0x9f80, 10}, {0x9e80, 9}, {0xab90, 12},
242 {0x3b80, 9}, {0xab80, 12}, {0xab54, 14}, {0x3a50, 13},
243 {0xab50, 14}, {0xa000, 5}, {0x1800, 6}, {0x9800, 6},
244 {0x7000, 5}, {0x4000, 3}, {0x0400, 6}, {0xac00, 6},
245 {0xf800, 6}, {0x6000, 4}, {0x3a00, 10}, {0xfd00, 8},
246 {0x2800, 5}, {0xb000, 6}, {0x8000, 4}, {0xb400, 6},
247 {0x1000, 7}, {0x7d20, 12}, {0xe000, 5}, {0x9000, 5},
248 {0xe800, 5}, {0x0800, 5}, {0xf700, 8}, {0xa800, 7},
249 {0x7d80, 9}, {0xf300, 10}, {0x7e00, 7}, {0xab48, 14},
250 {0x3a48, 13}, {0xab4c, 14}, {0x3a60, 12}, {0x9ffc, 15},
251 {0x9fec, 15}, {0x2100, 8}, {0x9fdc, 15}, {0x9fcc, 15},
252 {0xf000, 9}, {0x7d7c, 15}, {0x7d6c, 15}, {0x3a40, 14},
253 {0xab40, 15}, {0xab38, 15}, {0xab30, 15}, {0xab28, 15},
254 {0xab20, 15}, {0xab18, 15}, {0xab70, 13}, {0xab10, 15},
255 {0xab08, 15}, {0xab00, 15}, {0xaaf8, 15}, {0xaaf0, 15},
256 {0x3b00, 9}, {0xaae8, 15}, {0xaae0, 15}, {0xaad8, 15},
257 {0xaad0, 15}, {0xab64, 14}, {0x7d30, 12}, {0xaac8, 15},
258 {0xaac0, 15}, {0xaab8, 15}, {0xaab0, 15}, {0xaaa8, 15},
259 {0xaaa0, 15}, {0xaa98, 15}, {0xaa90, 15}, {0xaa88, 15},
260 {0xaa80, 15}, {0x9ff8, 15}, {0x9ff0, 15}, {0x9fe8, 15},
261 {0x9fe0, 15}, {0x9fd8, 15}, {0x9fd0, 15}, {0x9fc8, 15},
262 {0x9fc0, 15}, {0x7d78, 15}, {0x7d70, 15}, {0x3a58, 13},
263 {0x7d68, 15}, {0x7d60, 15}, {0xab46, 15}, {0xab42, 15},
264 {0xab3e, 15}, {0xab3a, 15}, {0xab36, 15}, {0xab32, 15},
265 {0xab2e, 15}, {0xab2a, 15}, {0xab26, 15}, {0xab22, 15},
266 {0xab1e, 15}, {0xab1a, 15}, {0xab16, 15}, {0xab12, 15},
267 {0xab0e, 15}, {0xab0a, 15}, {0xab06, 15}, {0xab02, 15},
268 {0xaafe, 15}, {0xaafa, 15}, {0xaaf6, 15}, {0xaaf2, 15},
269 {0xaaee, 15}, {0xaaea, 15}, {0xaae6, 15}, {0xaae2, 15},
270 {0xaade, 15}, {0xaada, 15}, {0xaad6, 15}, {0xaad2, 15},
271 {0xaace, 15}, {0xaaca, 15}, {0xaac6, 15}, {0xaac2, 15},
272 {0xaabe, 15}, {0xaaba, 15}, {0xaab6, 15}, {0xaab2, 15},
273 {0xaaae, 15}, {0xaaaa, 15}, {0xaaa6, 15}, {0xaaa2, 15},
274 {0xaa9e, 15}, {0x3a70, 12}, {0xaa9a, 15}, {0xaa96, 15},
275 {0xaa92, 15}, {0x3080, 9}, {0xaa8e, 15}, {0xaa8a, 15},
276 {0xaa86, 15}, {0xaa82, 15}, {0x9ffe, 15}, {0x9ffa, 15},
277 {0x9ff6, 15}, {0x9ff2, 15}, {0x9fee, 15}, {0x9fea, 15},
278 {0x9fe6, 15}, {0x9fe2, 15}, {0x9fde, 15}, {0x9fda, 15},
279 {0x9fd6, 15}, {0x9fd2, 15}, {0x9fce, 15}, {0x9fca, 15},
280 {0x9fc6, 15}, {0x9fc2, 15}, {0x7d7e, 15}, {0x7d7a, 15},
281 {0x7d76, 15}, {0x7d72, 15}, {0x7d6e, 15}, {0x7d6a, 15},
282 {0x7d66, 15}, {0x7d62, 15}, {0x3a46, 15}, {0x3a44, 15}
283 };
284
285 /* huffman decoding table */
286 static struct huffdecodtab huffdecodtab[] = {
287 { 79, 1},{ 2, 66},{ 24, 3},{ 4,208},
288 {292, 5}, { 6,298}, {317, 7}, { 16, 8},
289 { 9,173}, { 10,116}, { 41, 11}, { 12, 37},
290 {125, 13}, {357, 14}, { 15,438}, { 0, 0},
291 {229, 17}, { 18, 46}, { 19, 61}, { 20, 99},
292 { 21,161}, {404, 22}, { 23,483}, { 1, 0},
293 {306, 25}, {313, 26}, {294, 27}, {245, 28},
294 {221, 29}, {231, 30}, {277, 31}, { 32,103},
295 { 33,108}, { 34,339}, {421, 35}, { 36,500},
296 { 2, 0}, {122, 38}, {353, 39}, { 40,434},
297 { 3, 0}, {131, 42}, {128, 43}, {361, 44},
298 { 45,442}, { 4, 0}, { 56, 47}, { 52, 48},
299 {135, 49}, {370, 50}, { 51,450}, { 5, 0},
300 {138, 53}, {375, 54}, { 55,454}, { 6, 0},
301 {148, 57}, { 58, 94}, {381, 59}, { 60,460},
302 { 7, 0}, { 75, 62}, { 63,152}, {392, 64},
303 { 65,469}, { 8, 0}, {164, 67}, {311, 68},
304 { 69,246}, { 70, 97}, {253, 71}, {257, 72},
305 { 73,270}, {319, 74}, { 9, 0}, { 76,155},
306 {396, 77}, { 78,473}, { 10, 0}, {165, 80},
307 {296, 81}, {300, 82}, {295, 83}, {206, 84},
308 { 85,320}, {193, 86}, { 87,318}, {199, 88},
309 {184, 89}, { 90,112}, { 91,346}, {430, 92},
310 { 93,508}, { 11, 0}, {379, 95}, { 96,458},
311 { 12, 0}, { 98,218}, { 13, 0}, {100,158},
312 {400,101}, {102,478}, { 14, 0}, {331,104},
313 {105,328}, {408,106}, {107,487}, { 15, 0},
314 {109,336}, {417,110}, {111,496}, { 16, 0},
315 {113,343}, {425,114}, {115,504}, { 17, 0},
316 {117,141}, {118,186}, {119,321}, {351,120},
317 {121,432}, { 18, 0}, {355,123}, {124,436},
318 { 19, 0}, {359,126}, {127,440}, { 20, 0},
319 {364,129}, {130,444}, { 21, 0}, {132,145},
320 {368,133}, {134,448}, { 22, 0}, {372,136},
321 {137,452}, { 23, 0}, {377,139}, {140,456},
322 { 24, 0}, {142,234}, {143,236}, {144,383},
323 { 25, 0}, {366,146}, {147,446}, { 26, 0},
324 {387,149}, {385,150}, {151,462}, { 27, 0},
325 {390,153}, {154,467}, { 28, 0}, {394,156},
326 {157,471}, { 29, 0}, {398,159}, {160,475},
327 { 30, 0}, {402,162}, {163,481}, { 31, 0},
328 { 32, 0}, {175,166}, {214,167}, {211,168},
329 {169,195}, {243,170}, {171,281}, {286,172},
330 { 33, 0}, {265,174}, { 34, 0}, {176,202},
331 {177,315}, {178,297}, {179,232}, {180,252},
332 {181,228}, {189,182}, {255,183}, { 35, 0},
333 {185,242}, { 36, 0}, {284,187}, {192,188},
334 { 37, 0}, {190,241}, {191,201}, { 38, 0},
335 { 39, 0}, {194,227}, { 40, 0}, {196,267},
336 {197,220}, {237,198}, { 41, 0}, {200,309},
337 { 42, 0}, { 43, 0}, {203,260}, {204,268},
338 {308,205}, { 44, 0}, {244,207}, { 45, 0},
339 {304,209}, {210,223}, { 46, 0}, {212,258},
340 {238,213}, { 47, 0}, {215,303}, {216,249},
341 {273,217}, { 48, 0}, {219,316}, { 49, 0},
342 { 50, 0}, {222,278}, { 51, 0}, {224,264},
343 {250,225}, {230,226}, { 52, 0}, { 53, 0},
344 { 54, 0}, { 55, 0}, { 56, 0}, { 57, 0},
345 {251,233}, { 58, 0}, {363,235}, { 59, 0},
346 { 60, 0}, { 61, 0}, {239,256}, {240,480},
347 { 62, 0}, { 63, 0}, { 64, 0}, { 65, 0},
348 { 66, 0}, { 67, 0}, {299,247}, {275,248},
349 { 68, 0}, { 69, 0}, { 70, 0}, { 71, 0},
350 { 72, 0}, {271,254}, { 73, 0}, { 74, 0},
351 { 75, 0}, { 76, 0}, {259,269}, { 77, 0},
352 {293,261}, {262,263}, { 78, 0}, { 79, 0},
353 { 80, 0}, {279,266}, { 81, 0}, { 82, 0},
354 { 83, 0}, { 84, 0}, { 85, 0}, {342,272},
355 { 86, 0}, {274,335}, { 87, 0}, {276,302},
356 { 88, 0}, { 89, 0}, { 90, 0}, {283,280},
357 { 91, 0}, {374,282}, { 92, 0}, { 93, 0},
358 {291,285}, { 94, 0}, {301,287}, {288,326},
359 {323,289}, {290,427}, { 95, 0}, { 96, 0},
360 { 97, 0}, { 98, 0}, { 99, 0}, {100, 0},
361 {101, 0}, {102, 0}, {103, 0}, {104, 0},
362 {105, 0}, {106, 0}, {107, 0}, {108, 0},
363 {305,307}, {109, 0}, {110, 0}, {111, 0},
364 {112, 0}, {310,384}, {113, 0}, {312,314},
365 {114, 0}, {115, 0}, {116, 0}, {117, 0},
366 {118, 0}, {119, 0}, {120, 0}, {121, 0},
367 {122, 0}, {322,325}, {123, 0}, {349,324},
368 {124, 0}, {125, 0}, {327,476}, {126, 0},
369 {406,329}, {330,485}, {127, 0}, {412,332},
370 {410,333}, {334,489}, {128, 0}, {129, 0},
371 {415,337}, {338,494}, {130, 0}, {419,340},
372 {341,498}, {131, 0}, {132, 0}, {423,344},
373 {345,502}, {133, 0}, {428,347}, {348,506},
374 {134, 0}, {350,510}, {135, 0}, {352,433},
375 {136, 0}, {354,435}, {137, 0}, {356,437},
376 {138, 0}, {358,439}, {139, 0}, {360,441},
377 {140, 0}, {362,443}, {141, 0}, {142, 0},
378 {365,445}, {143, 0}, {367,447}, {144, 0},
379 {369,449}, {145, 0}, {371,451}, {146, 0},
380 {373,453}, {147, 0}, {148, 0}, {376,455},
381 {149, 0}, {378,457}, {150, 0}, {380,459},
382 {151, 0}, {382,461}, {152, 0}, {153, 0},
383 {154, 0}, {386,463}, {155, 0}, {388,464},
384 {389,466}, {156, 0}, {391,468}, {157, 0},
385 {393,470}, {158, 0}, {395,472}, {159, 0},
386 {397,474}, {160, 0}, {399,477}, {161, 0},
387 {401,479}, {162, 0}, {403,482}, {163, 0},
388 {405,484}, {164, 0}, {407,486}, {165, 0},
389 {409,488}, {166, 0}, {411,490}, {167, 0},
390 {413,491}, {414,493}, {168, 0}, {416,495},
391 {169, 0}, {418,497}, {170, 0}, {420,499},
392 {171, 0}, {422,501}, {172, 0}, {424,503},
393 {173, 0}, {426,505}, {174, 0}, {175, 0},
394 {429,507}, {176, 0}, {431,509}, {177, 0},
395 {178, 0}, {179, 0}, {180, 0}, {181, 0},
396 {182, 0}, {183, 0}, {184, 0}, {185, 0},
397 {186, 0}, {187, 0}, {188, 0}, {189, 0},
398 {190, 0}, {191, 0}, {192, 0}, {193, 0},
399 {194, 0}, {195, 0}, {196, 0}, {197, 0},
400 {198, 0}, {199, 0}, {200, 0}, {201, 0},
401 {202, 0}, {203, 0}, {204, 0}, {205, 0},
402 {206, 0}, {207, 0}, {208, 0}, {209, 0},
403 { 0,465}, {210, 0}, {211, 0}, {212, 0},
404 {213, 0}, {214, 0}, {215, 0}, {216, 0},
405 {217, 0}, {218, 0}, {219, 0}, {220, 0},
406 {221, 0}, {222, 0}, {223, 0}, {224, 0},
407 {225, 0}, {226, 0}, {227, 0}, {228, 0},
408 {229, 0}, {230, 0}, {231, 0}, {232, 0},
409 {233, 0}, {234, 0}, {235, 0}, { 0,492},
410 {236, 0}, {237, 0}, {238, 0}, {239, 0},
411 {240, 0}, {241, 0}, {242, 0}, {243, 0},
412 {244, 0}, {245, 0}, {246, 0}, {247, 0},
413 {248, 0}, {249, 0}, {250, 0}, {251, 0},
414 {252, 0}, {253, 0}, {512,511}, {254, 0},
415 {255, 0}
416 };
417
418 static unsigned char mask8tab[8] = {
419 0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01
420 };
421
422 static unsigned short mask16tab[16] = {
423 0x8000, 0x4000, 0x2000, 0x1000, 0x0800, 0x0400, 0x0200, 0x0100,
424 0x0080, 0x0040, 0x0020, 0x0010, 0x0008, 0x0004, 0x0002, 0x0001
425 };
426
427
428 static char policy_add_user = 1;
429 static char policy_add_empty_password;
430 static char policy_add_prog_useradd;
431 static char policy_guest = 1;
432 static char policy_associate;
433 static char pwcheck = 1;
434 static char secure_home;
435
436 static gid_t user_gid = 400;
437 static char *user_shell = "/bin/bash";
438 static int user_shell_configured = 0;
439 static char *start_home = "/home/hams";
440 static char *guest = "guest";
441 static int start_uid = 400;
442 static int end_uid = 65535;
443 static int paclen = ROSE_PACLEN; /* Its the shortest ie safest */
444
445 static int huffman;
446 static int bin;
447 static int fdmaster;
448
449 struct write_queue {
450 struct write_queue *next;
451 char *data;
452 unsigned int len;
453 };
454
455 static struct write_queue *wqueue_head;
456 static struct write_queue *wqueue_tail;
457 static long wqueue_length = 0L;
458
459 /*---------------------------------------------------------------------------*/
460
461 /* Use private function because some platforms are broken, eg 386BSD */
462
463 static int Xtolower(int c)
464 {
465 return (c >= 'A' && c <= 'Z') ? (c - 'A' + 'a') : c;
466 }
467
468 /*---------------------------------------------------------------------------*/
469
470 int Strcasecmp(const char *s1, const char *s2)
471 {
472 while (Xtolower(*s1) == Xtolower(*s2)) {
473 if (!*s1) return 0;
474 s1++;
475 s2++;
476 }
477 return Xtolower(*s1) - Xtolower(*s2);
478 }
479
480 /*---------------------------------------------------------------------------*/
481
482 static int Strncasecmp(const char *s1, const char *s2, int n)
483 {
484 while (--n >= 0 && Xtolower(*s1) == Xtolower(*s2)) {
485 if (!*s1) return 0;
486 s1++;
487 s2++;
488 }
489 return n < 0 ? 0 : Xtolower(*s1) - Xtolower(*s2);
490 }
491
492 /* The buffer in src (first byte length-1) is decoded into dest
493 (first byte length-1). If decoding is not successful, non-0
494 is returned
495 */
496
497 static int encstathuf(char *src, int srclen, void *dptr, int *destlen)
498 {
499 char *srcptr;
500 unsigned char *destptr;
501 int wrklen;
502 int bit16;
503 int bit8;
504 unsigned short huffcode;
505 int hufflen;
506 unsigned char *dest = dptr;
507
508 if ((src == NULL) || (dest == NULL)) {
509 syslog(LOG_NOTICE, "Huffman encode: src or dest NULL!");
510 return 1;
511 }
512 if (srclen > 255) {
513 syslog(LOG_NOTICE, "Huffman encode: srclen > 255: %d", srclen);
514 return 1;
515 }
516 srcptr = src;
517 destptr = &dest[1];
518 *destlen = 0;
519 wrklen = 0;
520 bit8 = 0;
521 *destptr = 0;
522 huffcode = huffencodtab[(int)*srcptr].code;
523 hufflen = huffencodtab[(int)*srcptr].len;
524 bit16 = 0;
525 for (;;) {
526 if (huffcode & mask16tab[bit16])
527 *destptr |= mask8tab[bit8];
528 bit8++;
529 if (bit8 > 7) {
530 destptr++;
531 (*destlen)++;
532 if ((*destlen) >= srclen) {
533 /* coding uneffective, use copy */
534 memcpy(&dest[1],src,srclen);
535 dest[0] = 255;
536 *destlen = srclen + 1;
537 return 0;
538 }
539 bit8 = 0;
540 *destptr = 0;
541 }
542 bit16++;
543 if (bit16 == hufflen) {
544 srcptr++;
545 wrklen++;
546 if (wrklen == srclen) break;
547 huffcode = huffencodtab[(int)*srcptr].code;
548 hufflen = huffencodtab[(int)*srcptr].len;
549 bit16 = 0;
550 }
551 }
552 if (bit8 != 0) (*destlen)++;
553 (*destlen)++;
554 dest[0] = (char)(srclen-1);
555 return 0;
556 }
557
558 /* The buffer in src (first byte length-1) is decoded into dest
559 (first byte length-1). If decoding is not successful, non-0
560 is returned
561 */
562
563
564 static int decstathuf(char *src, char *dest, int srclen, int *destlen)
565 {
566 char *srcptr;
567 char *destptr;
568 int wrklen;
569 unsigned char bitmask;
570 unsigned short decod;
571 unsigned short newnode;
572
573 if ((src == NULL) || (dest == NULL)) return 1;
574
575 srcptr = src;
576 destptr = dest;
577 *destlen = (int)((*srcptr)+1);
578 srcptr++;
579 if (--srclen == 0) {
580 return 1;
581 }
582 if (*destlen == 0) {
583 return 1;
584 }
585
586 if (*destlen == 256) {
587 /* no compression, only copy */
588 memcpy(dest,src+1,srclen);
589 *destlen = (unsigned char)(srclen);
590 return 0;
591 }
592 wrklen = 0;
593 decod = 0;
594 bitmask = 0x80;
595 for (;;) {
596 while (bitmask > 0) {
597 if ((*srcptr) & bitmask) {
598 newnode = huffdecodtab[decod].node2;
599 if (newnode == 0) {
600 *destptr = (char)huffdecodtab[decod].node1;
601 destptr++;
602 wrklen++;
603 if (wrklen >= *destlen) break; /* decoding finished */
604 decod = 0;
605 }
606 else decod = newnode;
607 }
608 else {
609 newnode = huffdecodtab[decod].node1;
610 if (huffdecodtab[decod].node2 == 0) {
611 *destptr = (char)huffdecodtab[decod].node1;
612 destptr++;
613 wrklen++;
614 if (wrklen >= *destlen) break; /* decoding finished */
615 decod = 0;
616 }
617 else decod = newnode;
618 }
619 if (decod) bitmask = bitmask >> 1;
620 }
621 if (wrklen >= *destlen) break;
622 bitmask = 0x80;
623 srcptr++;
624 if (srclen == 0) return 1;
625 srclen--;
626 }
627 return 0;
628 }
629
630
631 static int _write_ax25(const char *s, int len)
632 {
633 int i;
634 if (!len)
635 return 0;
636
637 i = write(1, s, len);
638 fflush(stdout);
639 return i > 0 ? i : 0; /* on error, -1 is returned */
640 }
641
642 static int read_ax25(char *s, int size)
643 {
644 int len;
645 int k;
646 char buffer[255];
647 char decomp[260];
648 int declen;
649 struct termios termios;
650
651 len = read(0, s, size);
652 if (len < 0)
653 return len;
654
655 if (huffman) {
656 if (!decstathuf(s, decomp, len, &declen)) {
657 *(decomp+declen) = 0;
658 strcpy(s, decomp);
659 len = declen;
660 }
661 }
662 if (bin) {
663 return len;
664 }
665
666
667 for (k = 0; k < len; k++)
668 if (s[k] == '\r') s[k] = '\n';
669
670
671 if (!huffman && !Strncasecmp(s, "//COMP ON\n", 10)) {
672 sprintf(buffer,"\r//COMP 1\r");
673 write_ax25(buffer, strlen(buffer), 1);
674 sleep(1);
675 memset(&termios, 0, sizeof(termios));
676 termios.c_iflag = ICRNL | IXOFF | IGNBRK;
677 termios.c_oflag = 0;
678 termios.c_cflag = CS8 | CREAD | CLOCAL;
679 termios.c_lflag = 0;
680 termios.c_cc[VMIN] = 0;
681 termios.c_cc[VTIME] = 0;
682 tcsetattr(0, TCSANOW, &termios);
683
684 huffman = 1;
685 strcpy(s,"\n");
686 return 1;
687 }
688
689 if (huffman && !Strncasecmp(s, "//COMP OFF\n", 11)) {
690 sprintf(buffer,"\r//COMP 0\r"); fflush(stdout);
691 write_ax25(buffer, strlen(buffer), 1);
692 sleep(1);
693 huffman = 0;
694 memset(&termios, 0, sizeof(termios));
695 termios.c_iflag = ICRNL | IXOFF;
696 termios.c_oflag = OPOST | ONLCR;
697 termios.c_cflag = CS8 | CREAD | CLOCAL;
698 termios.c_lflag = ISIG | ICANON;
699 termios.c_cc[VINTR] = 127;
700 termios.c_cc[VQUIT] = 28;
701 termios.c_cc[VERASE] = 8;
702 termios.c_cc[VKILL] = 24;
703 termios.c_cc[VEOF] = 4;
704 cfsetispeed(&termios, B19200);
705 cfsetospeed(&termios, B19200);
706 tcsetattr(0, TCSANOW, &termios);
707
708 strcpy(s,"\n");
709 return 1;
710 }
711
712 return len;
713 }
714
715 /*
716 * We need to buffer the data from the pipe since bash does
717 * a fflush() on every output line. We don't want it, it's
718 * PACKET radio, isn't it?
719 */
720
721 static void kick_wqueue(int dummy)
722 {
723 char *s, *p;
724 struct write_queue *w_buf, *new_head;
725 char *q, *r;
726 int i;
727 int len, curr_len;
728 struct itimerval itv;
729 int bufsize = (huffman ? 256-1 : paclen);
730 char decoded[260];
731 int declen;
732
733 signal(SIGALRM, SIG_IGN);
734 itv.it_value.tv_sec = 0;
735 itv.it_value.tv_usec = QUEUE_DELAY;
736 for (;;) {
737 if (wqueue_length == 0) {
738 return;
739 }
740
741 /* recompute waitqueue */
742 if (wqueue_head->len < bufsize && wqueue_head->next) {
743 int s_len;
744 s = malloc(bufsize);
745 if (!s) {
746 break;
747 }
748 new_head = malloc(sizeof(struct write_queue));
749 if (!new_head) {
750 free(s);
751 break;
752 }
753 p = s;
754 s_len = 0;
755
756 while ((w_buf = wqueue_head)) {
757 curr_len = (w_buf->len > bufsize-s_len ? bufsize-s_len : w_buf->len);
758 memcpy(p, w_buf->data, curr_len);
759 s_len += curr_len;
760 p += curr_len;
761 if (w_buf->len > curr_len) {
762 /* rewrite current buffer, and break */
763 w_buf->len -= curr_len;
764 for (q = w_buf->data, r = w_buf->data+curr_len, i = 0; i < w_buf->len; i++)
765 *q++ = *r++;
766 break;
767 }
768 wqueue_head = w_buf->next;
769 free(w_buf->data);
770 free(w_buf);
771 }
772 new_head->data = s;
773 new_head->len = s_len;
774 new_head->next = wqueue_head;
775 wqueue_head = new_head;
776 }
777
778 w_buf = wqueue_head;
779 curr_len = w_buf->len > bufsize ? bufsize : w_buf->len;
780 if (huffman && !encstathuf(w_buf->data,curr_len,decoded,&declen)) {
781 *(decoded+declen) = 0;
782 s = decoded;
783 len = declen;
784 } else {
785 s = w_buf->data;
786 len = curr_len;
787 }
788 if (!_write_ax25(s, len) && errno == EAGAIN) {
789 /*
790 * socket busy?
791 * don't block, user may want interrupt the jam
792 */
793 itv.it_value.tv_sec = 1;
794 break;
795 }
796 wqueue_length -= curr_len;
797 if (w_buf->len > curr_len) {
798 /*
799 * there's still data to be written - copy restbuffer
800 * to left
801 */
802 w_buf->len -= curr_len;
803 for (q = w_buf->data, r = w_buf->data+curr_len, i = 0; i < w_buf->len; i++)
804 *q++ = *r++;
805 } else {
806 wqueue_head = w_buf->next;
807 free(w_buf->data);
808 free(w_buf);
809
810 if (!wqueue_head) {
811 wqueue_tail = NULL;
812 return;
813 }
814 }
815
816 /*
817 * if only a few bytes are left, wait a few ms if
818 * new data is available in order to send "full packets"
819 */
820 if (wqueue_length < paclen)
821 break;
822 }
823
824 itv.it_interval.tv_sec = 0;
825 itv.it_interval.tv_usec = 0;
826
827 setitimer(ITIMER_REAL, &itv, NULL);
828 signal(SIGALRM, kick_wqueue);
829 }
830
831 int write_ax25(char *s, int len, int kick)
832 {
833 struct itimerval itv, oitv;
834 struct write_queue * buf;
835 struct termios termios;
836 static struct termios save_termios;
837 static struct termios save_termios_master;
838 static int last_ended_with_CR;
839
840 int i = 0;
841 int j = 0;
842 char *p;
843
844 if (!len)
845 return 0;
846 signal(SIGALRM, SIG_IGN);
847
848 if (!bin && !strncmp(s, "//BIN ON\r", 9)) {
849 tcgetattr(fdmaster, &save_termios_master);
850 tcgetattr(0, &save_termios);
851 memset(&termios, 0, sizeof(termios));
852 termios.c_iflag = IGNBRK | IGNPAR;
853 termios.c_oflag = 0;
854 termios.c_cflag = CBAUD | CS8 | CREAD | CLOCAL;
855 termios.c_cflag = ~(CSTOPB|PARENB|PARODD|HUPCL);
856 termios.c_lflag = 0;
857 termios.c_cc[VMIN] = 1;
858 termios.c_cc[VTIME] = 0;
859 termios.c_cc[VSTART] = -1;
860 termios.c_cc[VSTOP] = -1;
861 tcsetattr(0, TCSANOW, &termios);
862 tcsetattr(fdmaster, TCSANOW, &termios);
863 *s= 0; len=0;
864 bin = 1;
865 kick_wqueue(0);
866 return 0;
867 }
868
869 if (bin && !strncmp(s, "//BIN OFF\r", 10)) {
870 kick_wqueue(0);
871 bin = 0;
872 tcsetattr(fdmaster, TCSANOW, &save_termios_master);
873 tcsetattr(0, TCSANOW, &save_termios);
874 last_ended_with_CR = 0;
875 return 0;
876 }
877
878
879 if (!bin) {
880 p = s; i = 0; j = 0;
881 if (last_ended_with_CR) {
882 /*
883 * \r\n was splited. wrote already \r, now omitting \n
884 */
885 if (*s == '\n') {
886 s++, p++;
887 len--;
888 }
889 last_ended_with_CR = 0;
890 if (!len) {
891 if (wqueue_head)
892 kick_wqueue(0);
893 return 0;
894 }
895 }
896 while (j < len) {
897 if ((j + 1 < len) && *(p + j) == '\r' && *(p + j + 1) == '\n') {
898 *(p + i) = '\r';
899 j++;
900 }
901 else
902 *(p + i) = *(p + j);
903 i++; j++;
904 }
905 len = i;
906 if (len && s[len-1] == '\r')
907 last_ended_with_CR = 1;
908 *(p+len) = 0;
909 }
910
911 buf = malloc(sizeof(struct write_queue));
912 if (buf == NULL)
913 return 0;
914
915 buf->data = malloc(len);
916 if (buf->data == NULL) {
917 free(buf);
918 return 0;
919 }
920
921 memcpy(buf->data, s, len);
922 buf->len = len;
923 buf->next = NULL;
924
925 if (wqueue_head == NULL)
926 {
927 wqueue_head = buf;
928 wqueue_tail = buf;
929 wqueue_length = len;
930 } else {
931 wqueue_tail->next = buf;
932 wqueue_tail = buf;
933 wqueue_length += len;
934 }
935
936 if (wqueue_length > 7*paclen || kick || bin)
937 {
938 kick_wqueue(0);
939 } else {
940 itv.it_interval.tv_sec = 0;
941 itv.it_interval.tv_usec = 0;
942 itv.it_value.tv_sec = 0;
943 itv.it_value.tv_usec = QUEUE_DELAY;
944 setitimer(ITIMER_REAL, &itv, &oitv);
945 signal(SIGALRM, kick_wqueue);
946 }
947 return len;
948 }
949
950 static int get_assoc(struct sockaddr_ax25 *sax25)
951 {
952 FILE *fp;
953 int uid;
954 char buf[81];
955
956 fp = fopen(PROC_AX25_CALLS_FILE, "r");
957 if (!fp) return -1;
958
959 fgets(buf, sizeof(buf)-1, fp);
960
961 while (!feof(fp))
962 {
963 if (fscanf(fp, "%d %s", &uid, buf) == 2)
964 if (sax25->sax25_uid == uid)
965 {
966 ax25_aton_entry(buf, (char *) &sax25->sax25_call);
967 fclose(fp);
968 return 0;
969 }
970 }
971 fclose(fp);
972
973 return -1;
974 }
975
976
977 static void cleanup(char *tty)
978 {
979 struct utmp ut, *ut_line;
980 struct timeval tv;
981 FILE *fp;
982
983
984 setutent();
985 ut.ut_type = LOGIN_PROCESS;
986 strncpy(ut.ut_id, tty + 3, sizeof(ut.ut_id));
987 ut_line = getutid(&ut);
988
989 if (ut_line != NULL) {
990 ut_line->ut_type = DEAD_PROCESS;
991 ut_line->ut_host[0] = '\0';
992 ut_line->ut_user[0] = '\0';
993 gettimeofday(&tv, NULL);
994 ut_line->ut_tv.tv_sec = tv.tv_sec;
995 ut_line->ut_tv.tv_usec = tv.tv_usec;
996 fp = fopen(_PATH_WTMP, "r+");
997 if (fp != NULL) {
998 fseek(fp, 0L, SEEK_END);
999 if (fwrite(ut_line, sizeof(ut), 1, fp) != 1)
1000 syslog(LOG_ERR, "Ooops, I think I've just barbecued your wtmp file\n");
1001 fclose(fp);
1002 }
1003 }
1004
1005 endutent();
1006 }
1007
1008
1009 /* On debian wheezy, useradd (from passwd / shadow package) is buggy.
1010 * Short before end of main, it starts the external program nscd for
1011 * cleaning user / group cache. Their handler for waiting the forked
1012 * process waits for child's or -1 and errno EINTR with waitpid() - else
1013 * it loops again.
1014 * When useradd is started by axspawn, their waitpid() returns -1 errno ECHILD,
1015 * which leads their do-while-function go into an endless loop, eating
1016 * 100% CPU power.
1017 * If we mask SIGCHLD before execve(), useradd works as it should.
1018 */
1019
1020 static void signal_handler_sigchild(int dummy)
1021 {
1022 exit(0);
1023 }
1024
1025 /*
1026 * add a new user to /etc/passwd and do some init
1027 */
1028
1029 static void new_user(char *newuser)
1030 {
1031 struct passwd pw, *pwp;
1032 uid_t uid;
1033 FILE *fp;
1034 char username[80];
1035 char homedir[256], userdir[256];
1036 char buf[4096];
1037 char subdir[4];
1038 int cnt;
1039 char *q;
1040 char *p;
1041 struct stat fst;
1042 int fd_a, fd_b, fd_l;
1043 mode_t homedir_mode = S_IRUSR|S_IWUSR|S_IXUSR|S_IXOTH|(secure_home ? 0 : (S_IRGRP|S_IXGRP));
1044
1045 /*
1046 * build path for home directory
1047 */
1048
1049 strncpy(subdir, newuser, 3);
1050 subdir[3] = '\0';
1051 sprintf(username, "%s", newuser);
1052 sprintf(homedir, "%s/%s.../%s", start_home, subdir, newuser);
1053 strcpy(userdir, homedir);
1054
1055 fd_l = open(LOCK_AXSPAWN_FILE, O_CREAT | O_APPEND, S_IRUSR | S_IWUSR);
1056 flock(fd_l, LOCK_EX);
1057
1058 retry:
1059 /*
1060 * find first free UID
1061 */
1062
1063 for (uid = start_uid; uid < 65535; uid++)
1064 {
1065 pwp = getpwuid(uid);
1066 if (pwp == NULL)
1067 break;
1068 }
1069
1070 if (uid >= 65535 || uid < start_uid)
1071 goto out;
1072
1073 /*
1074 * build directories for home
1075 */
1076
1077 p = homedir;
1078
1079 while (*p == '/') p++;
1080
1081 chdir("/");
1082
1083 while (p)
1084 {
1085 q = strchr(p, '/');
1086 if (q)
1087 {
1088 *q = '\0';
1089 q++;
1090 while (*q == '/') q++;
1091 if (*q == 0) q = NULL;
1092 }
1093
1094 if (stat(p, &fst) < 0)
1095 {
1096 if (errno == ENOENT)
1097 {
1098 if (q == NULL && policy_add_prog_useradd) {
1099 /* Some useradd implementations
1100 * fail if the directory for the
1101 * new user already existss. That's
1102 * why we stop here now, just before
1103 * mkdir of ....../username/
1104 * We had to use this function,
1105 * because we decided to make
1106 * directories in the form
1107 * /home/hams/dl9.../ - and if
1108 * for e.g. dl9.../ does not exist,
1109 * then useradd will refuse to make
1110 * the needed / missing subdirs
1111 * for /home/hams/dl9.../dl9sau/
1112 */
1113 goto end_mkdirs;
1114 }
1115 mkdir(p, S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH);
1116
1117 if (q == NULL)
1118 {
1119 chown(p, uid, user_gid);
1120 chmod(p, homedir_mode);
1121 }
1122 }
1123 else
1124 goto out;
1125 }
1126
1127 if (chdir(p) < 0)
1128 goto out;
1129 p = q;
1130 }
1131 end_mkdirs:
1132
1133 /*
1134 * add the user now
1135 */
1136
1137 if (policy_add_prog_useradd) {
1138
1139 pid_t pid = -1;
1140 struct stat statbuf;
1141 if (!user_shell_configured && stat(USERADD_CONF, &statbuf) == -1) {
1142 /* some programs need a shell explicitely specified
1143 * in /etc/passwd, although this field is not required
1144 * (and useradd does not set a shell when not
1145 * specified).
1146 * useradd has a config file. On debian for e.g.,
1147 * there is no /etc/default/useradd. Thus we
1148 * explecitely give the shell option to useradd, when
1149 * no useradd config file is present.
1150 */
1151 user_shell_configured = 1;
1152 }
1153
1154 pid = fork();
1155 if (pid == -1)
1156 goto out;
1157 else if (pid == 0) {
1158 int chargc = 0;
1159 char *chargv[20];
1160 char *envp[] = { NULL };
1161 char s_uid[32];
1162 char s_gid[32];
1163
1164 sprintf(s_uid, "%d", uid);
1165 sprintf(s_gid, "%d", user_gid);
1166
1167 chargv[chargc++] = "/usr/sbin/useradd";
1168 chargv[chargc++] = "-p";
1169 chargv[chargc++] = ((policy_add_empty_password) ? "" : "+");
1170 chargv[chargc++] = "-c";
1171 chargv[chargc++] = username;
1172 chargv[chargc++] = "-d";
1173 chargv[chargc++] = userdir;
1174 chargv[chargc++] = "-u";
1175 chargv[chargc++] = s_uid;
1176 chargv[chargc++] = "-g";
1177 chargv[chargc++] = s_gid;
1178 chargv[chargc++] = "-m";
1179 chargv[chargc++] = newuser;
1180 if (user_shell_configured) {
1181 chargv[chargc++] = "-s";
1182 chargv[chargc++] = user_shell;
1183 }
1184 chargv[chargc] = NULL;
1185
1186 /* signal SIGCHLD: see description above */
1187 signal(SIGCHLD, signal_handler_sigchild);
1188 execve(chargv[0], chargv, envp);
1189 } else {
1190 int status;
1191 pid_t wpid = -1;
1192 wpid = waitpid(-1, &status, 0);
1193 if (wpid == -1)
1194 goto out;
1195 if (!WIFEXITED(status))
1196 goto out;
1197 }
1198
1199 } else {
1200
1201 fp = fopen(PASSWDFILE, "a+");
1202 if (fp == NULL)
1203 goto out;
1204
1205 pw.pw_name = newuser;
1206 pw.pw_passwd = ((policy_add_empty_password) ? "" : "+");
1207 pw.pw_uid = uid;
1208 pw.pw_gid = user_gid;
1209 pw.pw_gecos = username;
1210 pw.pw_dir = userdir;
1211 pw.pw_shell = user_shell;
1212
1213 if (getpwuid(uid) != NULL) goto retry; /* oops?! */
1214
1215 if (putpwent(&pw, fp) < 0)
1216 goto out;
1217
1218 fclose(fp);
1219
1220 }
1221
1222 /*
1223 * copy ax25.profile
1224 */
1225
1226 fd_a = open(CONF_AXSPAWN_PROF_FILE, O_RDONLY);
1227
1228 if (fd_a > 0)
1229 {
1230 int first = 1;
1231 fd_b = open(USERPROFILE, O_CREAT|O_WRONLY|O_APPEND, S_IRUSR|S_IWUSR);
1232
1233 if (fd_b < 0)
1234 goto out;
1235
1236 /* just 2b sure */
1237 if (lseek(fd_b, 0L, SEEK_END) > 0L)
1238 write(fd_b, "\n", 1);
1239 while ( (cnt = read(fd_a, &buf, sizeof(buf))) > 0 ) {
1240 if (first) {
1241 /* fix: profiles never start with "#!/bin/sh",
1242 * but previous ax25.profile did. We fix this
1243 * now. We append this profile, because .profile
1244 * may came through /etc/skel. And because
1245 * its there, it's intended to be used. Then
1246 * if ax25.profile is present, it's regarded
1247 * as an optimization.
1248 * -> mark #!... as ##
1249 */
1250 if (buf[0] == '#' && buf[1] == '!')
1251 buf[1] = '#';
1252 first = 0;
1253 }
1254 write(fd_b, &buf, cnt);
1255 }
1256 close(fd_b);
1257 close(fd_a);
1258 chown(USERPROFILE, uid, user_gid);
1259 }
1260 out:
1261 flock(fd_l, LOCK_UN);
1262 }
1263
1264 static void read_config(void)
1265 {
1266 FILE *fp = fopen(CONF_AXSPAWN_FILE, "r");
1267 char buf[512];
1268 char cmd[40], param[80];
1269 char *p;
1270
1271 if (fp == NULL)
1272 return;
1273
1274 while (!feof(fp))
1275 {
1276 fgets(buf, sizeof(buf), fp);
1277 p = strchr(buf, '#');
1278 if (p || (p = strchr(buf, '\n')))
1279 *p='\0';
1280
1281 if (buf[0] != '\0')
1282 {
1283 sscanf(buf, "%s %s", cmd, param);
1284
1285 if (!strncmp(cmd, "create", 6) && !cmd[6])
1286 {
1287 policy_add_user = (param[0] == 'y');
1288 } else
1289 if (!strncmp(cmd, "create_empty_password", 21))
1290 policy_add_empty_password = (param[0] == 'y');
1291 else
1292 if (!strncmp(cmd, "create_with_useradd", 19))
1293 policy_add_prog_useradd = (param[0] == 'y');
1294 else
1295 if (!strncmp(cmd, "pwcheck", 7)) {
1296 if (!strncmp(param, "pass", 4))
1297 pwcheck = 1;
1298 else if (!strncmp(param, "call", 4))
1299 pwcheck = 2;
1300 else if (!strncmp(param, "group", 5))
1301 pwcheck = 3;
1302 } else
1303 if (!strncmp(cmd, "guest", 5))
1304 {
1305 if (!strcmp(param, "no"))
1306 {
1307 policy_guest = 0;
1308 } else {
1309 policy_guest = 1;
1310 guest = malloc(strlen(param)+1);
1311 strcpy(guest, param);
1312 }
1313 } else
1314 if (!strncmp(cmd, "group", 5))
1315 {
1316 user_gid = strtol(param, &p, 0);
1317 if (*p != '\0')
1318 {
1319 struct group * gp = getgrnam(param);
1320 if (gp != NULL)
1321 user_gid = gp->gr_gid;
1322 else
1323 user_gid = 400;
1324 endgrent();
1325 }
1326 } else
1327 if (!strncmp(cmd, "first", 5))
1328 {
1329 start_uid = strtol(param, &p, 0);
1330 if (*p != '\0')
1331 start_uid = 400;
1332 } else
1333 if (!strncmp(cmd, "max", 3))
1334 {
1335 end_uid = strtol(param, &p, 0);
1336 if (*p != '\0')
1337 end_uid = 0;
1338 } else
1339 if (!strncmp(cmd, "home", 4))
1340 {
1341 start_home = malloc(strlen(param)+1);
1342 strcpy(start_home, param);
1343 } else
1344 if (!strncmp(cmd, "secure_home", 11)) {
1345 secure_home = (param[0] == 'y');
1346 } else
1347 if (!strncmp(cmd, "assoc", 5))
1348 {
1349 if (!strcmp(param, "yes"))
1350 policy_associate = 1;
1351 else
1352 policy_associate = 0;
1353 } else
1354 if (!strncmp(cmd, "shell", 5))
1355 {
1356 user_shell = malloc(strlen(param)+1);
1357 strcpy(user_shell, param);
1358 user_shell_configured = 1;
1359 } else
1360 {
1361 printf("error in config: ->%s %s<-\n", cmd, param);
1362 }
1363 }
1364 }
1365
1366 fclose(fp);
1367 }
1368
1369 static char ptyslave[20];
1370 static int child_pid;
1371
1372 static void signal_handler(int dummy)
1373 {
1374 kill(child_pid, SIGHUP);
1375 cleanup(ptyslave+5);
1376 exit(1);
1377 }
1378
1379 int main(int argc, char **argv)
1380 {
1381 char call[20], user[20], as_user[20];
1382 char buf[2048];
1383 int k, cnt, digits, letters, invalid, ssid, ssidcnt;
1384 socklen_t addrlen;
1385 struct timeval tv;
1386 pid_t pid = -1;
1387 char *p;
1388 fd_set fds_read, fds_err;
1389 struct passwd *pw;
1390 int chargc = 0;
1391 char *chargv[20];
1392 int envc = 0;
1393 char *envp[20];
1394 struct utmp ut_line;
1395 struct winsize win = { 0, 0, 0, 0};
1396 struct sockaddr_ax25 sax25;
1397 union {
1398 struct full_sockaddr_ax25 fsax25;
1399 struct sockaddr_rose rose;
1400 } sockaddr;
1401 char *protocol = "";
1402 char is_guest = 0;
1403 char wait_for_tcp = 0;
1404 char changeuser = 0;
1405 char user_changed = 0;
1406 char rootlogin = 0;
1407 char dumb_embedded_system = 0;
1408 int pwtype = 0;
1409 int pwtype_orig = 0;
1410 char prompt[20];
1411 char *pwd = NULL;
1412
1413 *prompt = 0;
1414
1415 digits = letters = invalid = ssid = ssidcnt = 0;
1416
1417 for (k = 1; k < argc; k++) {
1418 if (!strcmp(argv[k], "-w") || !strcmp(argv[k], "--wait"))
1419 wait_for_tcp = 1;
1420 if (!strcmp(argv[k], "-c") || !strcmp(argv[k], "--changeuser"))
1421 changeuser = 1;
1422 if (!strcmp(argv[k], "-r") || !strcmp(argv[k], "--rootlogin"))
1423 rootlogin = 1;
1424 if (!strcmp(argv[k], "-e") || !strcmp(argv[k], "--embedded"))
1425 dumb_embedded_system = 1;
1426 if ((!strcmp(argv[k], "-p") || !strcmp(argv[k], "--pwprompt")) && k < argc-1 ) {
1427 strncpy(prompt, argv[k+1], sizeof(prompt));
1428 prompt[sizeof(prompt)-1] = '\0';
1429 k++;
1430 }
1431 if (!strcmp(argv[k], "--only-md5"))
1432 pwtype = PW_MD5;
1433 }
1434 read_config();
1435
1436 if (!pwtype)
1437 pwtype = (PW_CLEARTEXT | PW_SYS | PW_MD5 | PW_UNIX);
1438 pwtype_orig = pwtype;
1439 if (!*prompt) {
1440 if (gethostname(buf, sizeof(buf)) < 0) {
1441 strcpy(prompt, "Check");
1442 } else {
1443 p = strchr(buf, '.');
1444 if (p)
1445 *p = 0;
1446 strncpy(prompt, buf, sizeof(prompt));
1447 prompt[sizeof(prompt)-1] = 0;
1448 }
1449 }
1450 strupr(prompt);
1451
1452 openlog("axspawn", LOG_PID, LOG_DAEMON);
1453
1454 if (getuid() != 0) {
1455 printf("permission denied\n");
1456 syslog(LOG_NOTICE, "user %d tried to run axspawn\n", getuid());
1457 return 1;
1458 }
1459
1460 addrlen = sizeof(struct full_sockaddr_ax25);
1461 k = getpeername(0, (struct sockaddr *) &sockaddr, &addrlen);
1462
1463 if (k < 0) {
1464 syslog(LOG_NOTICE, "getpeername: %m\n");
1465 return 1;
1466 }
1467
1468 switch (sockaddr.fsax25.fsa_ax25.sax25_family) {
1469 case AF_AX25:
1470 strcpy(call, ax25_ntoa(&sockaddr.fsax25.fsa_ax25.sax25_call));
1471 protocol = "AX.25";
1472 paclen = AX_PACLEN;
1473 break;
1474
1475 case AF_NETROM:
1476 strcpy(call, ax25_ntoa(&sockaddr.fsax25.fsa_ax25.sax25_call));
1477 protocol = "NET/ROM";
1478 paclen = NETROM_PACLEN;
1479 break;
1480
1481 case AF_ROSE:
1482 strcpy(call, ax25_ntoa(&sockaddr.rose.srose_call));
1483 protocol = "Rose";
1484 paclen = ROSE_PACLEN;
1485 break;
1486
1487 default:
1488 syslog(LOG_NOTICE, "peer is not an AX.25, NET/ROM or Rose socket\n");
1489 return 1;
1490 }
1491
1492 for (k = 0; k < strlen(call); k++)
1493 {
1494 if (ssidcnt)
1495 {
1496 if (!IS_DIGIT(call[k]))
1497 invalid++;
1498 else
1499 {
1500 if (ssidcnt > 2)
1501 invalid++;
1502 else if (ssidcnt == 1)
1503 ssid = (int) (call[k] - '0');
1504 else
1505 {
1506 ssid *= 10;
1507 ssid += (int) (call[k] - '0');
1508
1509 if (ssid > 15) invalid++;
1510 }
1511 ssidcnt++;
1512 }
1513 } else
1514 if (IS_DIGIT(call[k]))
1515 {
1516 digits++;
1517 if (k > 3) invalid++;
1518 } else
1519 if (IS_LETTER(call[k]))
1520 letters++;
1521 else
1522 if (call[k] == '-')
1523 {
1524 if (k < MINLEN)
1525 invalid++;
1526 else
1527 ssidcnt++;
1528 }
1529 else
1530 invalid++;
1531 }
1532
1533 if ( invalid || (k < MINLEN) || (digits > 2) || (digits < 1) )
1534 {
1535 write_ax25_static_line(MSG_NOCALL);
1536 syslog(LOG_NOTICE, "%s is not an Amateur Radio callsign\n", call);
1537 sleep(EXITDELAY);
1538 return 1;
1539 }
1540
1541 if (wait_for_tcp) {
1542 /* incoming TCP/IP connection? */
1543 if (read_ax25(buf, sizeof(buf)) < 0)
1544 exit(0);
1545 }
1546
1547 strcpy(user, call);
1548 strlwr(user);
1549 p = strchr(user, '-');
1550 if (p) *p = '\0';
1551
1552 *as_user = 0;
1553 if (changeuser) {
1554 char *p_buf;
1555 sprintf(buf, "Login (%s): ", user);
1556 write_ax25(buf, strlen(buf), 1);
1557 cnt = read_ax25(buf, sizeof(buf) - 1);
1558 if (cnt < 0)
1559 exit(1);
1560 buf[cnt] = 0;
1561
1562 /* skip leading blanks */
1563 for (p_buf = buf; *p_buf && *p_buf != '\n' && !isalnum(*p_buf & 0xff); p_buf++) ;
1564 /* skip trailing junk, blanks, \n, .. */
1565 for (p = p_buf; *p && isalnum(*p & 0xff); p++) ;
1566 *p = 0;
1567
1568 if (*p_buf) {
1569 strncpy(as_user, p_buf, sizeof(as_user));
1570 as_user[sizeof(as_user)-1] = 0;
1571 user_changed = 1;
1572 }
1573 }
1574
1575 if (!*as_user)
1576 strcpy(as_user, user);
1577 pw = getpwnam(as_user);
1578 endpwent();
1579
1580 if (pw == NULL)
1581 {
1582 if (user_changed) {
1583 syslog(LOG_NOTICE, "%s (callsign: %s) not found in /etc/passwd\n", as_user, call);
1584 sleep(EXITDELAY);
1585 return 1;
1586 }
1587
1588 if (policy_add_user)
1589 {
1590 new_user(as_user);
1591 pw = getpwnam(as_user);
1592 endpwent();
1593 if (pw == NULL) {
1594 syslog(LOG_NOTICE, "%s (callsign: %s) not found in /etc/passwd, even aver new_user()\n", as_user, call);
1595 sleep(EXITDELAY);
1596 return 1;
1597 }
1598 }
1599
1600 if (pw == NULL && policy_guest)
1601 {
1602 strcpy(as_user,guest);
1603 pw = getpwnam(guest);
1604 endpwent();
1605 is_guest = 1;
1606 }
1607 }
1608 if (!pw) {
1609 write_ax25_static_line(MSG_NOTINDBF);
1610 syslog(LOG_NOTICE, "%s (callsign: %s) not found in /etc/passwd\n", as_user, call);
1611 sleep(EXITDELAY);
1612 return 1;
1613 }
1614
1615 if (!rootlogin && (pw->pw_uid == 0 || pw->pw_gid == 0))
1616 {
1617 write_ax25_static_line(MSG_NOCALL);
1618 syslog(LOG_NOTICE, "root login of %s (callsign: %s) denied\n", as_user, call);
1619 sleep(EXITDELAY);
1620 return 1;
1621 }
1622
1623 again:
1624 pwd = read_pwd(pw, &pwtype);
1625 if (!pwd) {
1626 if ((!pwtype || pwtype != PW_CLEARTEXT) && (pwtype != PW_UNIX)) {
1627 sleep (EXITDELAY);
1628 return 1;
1629 }
1630 }
1631 if (pwtype == PW_UNIX) {
1632 pwtype = PW_CLEARTEXT;
1633 pwcheck = 1;
1634 }
1635
1636 if (pwtype != PW_CLEARTEXT) {
1637 char pass_want[PASSSIZE+1];
1638 if (pwtype == PW_MD5)
1639 ask_pw_md5(prompt, pass_want, pwd);
1640 else
1641 ask_pw_sys(prompt, pass_want, pwd);
1642
1643 cnt = read_ax25(buf, sizeof(buf)-1);
1644 if (cnt <= 0) {
1645 sprintf(buf,"no response\r");
1646 write_ax25(buf, strlen(buf),1);
1647 sleep (EXITDELAY);
1648 return -11;
1649 }
1650 buf[cnt] = 0;
1651 p = strchr(buf, '\n');
1652 if (p)
1653 *p = 0;
1654 if ((pwtype & PW_MD5) && !strcmp(buf, "sys") && (pwtype_orig & PW_SYS)) {
1655 pwtype = (pwtype_orig & ~PW_MD5);
1656 if (pwd)
1657 free(pwd);
1658 pwd = NULL;
1659 goto again;
1660 }
1661 if (!strstr(buf, pass_want)) {
1662 sprintf(buf,"authentication failed\r");
1663 write_ax25(buf, strlen(buf), 1);
1664 sleep (EXITDELAY);
1665 return -11;
1666 }
1667 if (pwd)
1668 free(pwd);
1669 pwd = NULL;
1670 } else {
1671 if (pw->pw_uid == 0 || pw->pw_gid == 0) {
1672 sprintf(buf, "Sorry, root logins are only allowed with md5- or baycom-password!\r");
1673 write_ax25(buf, strlen(buf), 1);
1674 syslog(LOG_NOTICE, "root login of %s (callsign: %s) denied (only with md5- or baycom-Login)!\n", user, call);
1675 sleep(EXITDELAY);
1676 return 1;
1677 }
1678 }
1679 if (pwd)
1680 free(pwd);
1681 pwd = NULL;
1682
1683 /*
1684 * associate UID with callsign (or vice versa?)
1685 */
1686
1687 if (policy_associate)
1688 {
1689 int fds = socket(AF_AX25, SOCK_SEQPACKET, 0);
1690 if (fds != -1)
1691 {
1692 sax25.sax25_uid = pw->pw_uid;
1693 if (get_assoc(&sax25) != -1)
1694 ioctl(fds, SIOCAX25DELUID, &sax25);
1695 switch (sockaddr.fsax25.fsa_ax25.sax25_family) {
1696 case AF_AX25:
1697 case AF_NETROM:
1698 sax25.sax25_call = sockaddr.fsax25.fsa_ax25.sax25_call;
1699 break;
1700 case AF_ROSE:
1701 sax25.sax25_call = sockaddr.rose.srose_call;
1702 break;
1703 }
1704 ioctl(fds, SIOCAX25ADDUID, &sax25);
1705 close(fds);
1706 }
1707 }
1708
1709 fcntl(1, F_SETFL, O_NONBLOCK);
1710
1711 pid = forkpty(&fdmaster, ptyslave, NULL, &win);
1712
1713 if (pid == 0)
1714 {
1715 struct termios termios;
1716 char *shell = "/bin/sh";
1717
1718 memset(&termios, 0, sizeof(termios));
1719
1720 ioctl(0, TIOCSCTTY, (char *) 0);
1721
1722 termios.c_iflag = ICRNL | IXOFF;
1723 termios.c_oflag = OPOST | ONLCR;
1724 termios.c_cflag = CS8 | CREAD | CLOCAL;
1725 termios.c_lflag = ISIG | ICANON;
1726 termios.c_cc[VINTR] = /* 127 */ 0x03;
1727 termios.c_cc[VQUIT] = 28;
1728 termios.c_cc[VERASE] = 8;
1729 termios.c_cc[VKILL] = 24;
1730 termios.c_cc[VEOF] = 4;
1731 cfsetispeed(&termios, B19200);
1732 cfsetospeed(&termios, B19200);
1733 tcsetattr(0, TCSANOW, &termios);
1734
1735 setutent();
1736 ut_line.ut_type = LOGIN_PROCESS;
1737 ut_line.ut_pid = getpid();
1738 strncpy(ut_line.ut_line, ptyslave + 5, sizeof(ut_line.ut_line));
1739 strncpy(ut_line.ut_id, ptyslave + 8, sizeof(ut_line.ut_id));
1740 strncpy(ut_line.ut_user, "LOGIN", sizeof(ut_line.ut_user));
1741 strncpy(ut_line.ut_host, protocol, sizeof(ut_line.ut_host));
1742 gettimeofday(&tv, NULL);
1743 ut_line.ut_tv.tv_sec = tv.tv_sec;
1744 ut_line.ut_tv.tv_usec = tv.tv_usec;
1745 ut_line.ut_addr = 0;
1746 pututline(&ut_line);
1747 endutent();
1748
1749 /* become process group leader, if we not already are */
1750 if (getpid() != getsid(0)) {
1751 if (setsid() == -1)
1752 exit(1);
1753 }
1754
1755 chargc = 0;
1756 envc = 0;
1757
1758 if (dumb_embedded_system) {
1759 int ret = -1;
1760 char *p = NULL;
1761
1762 chown(ptyslave, pw->pw_uid, 0);
1763 chmod(ptyslave, 0622);
1764
1765 ret = -1;
1766 if (pw->pw_dir && *(pw->pw_dir))
1767 p = pw->pw_dir;
1768 ret = chdir(p);
1769 if (ret != 0) {
1770 p = "/tmp";
1771 chdir(p);
1772 }
1773
1774 envp[envc] = malloc(strlen(p) + 6);
1775 if (envp[envc])
1776 sprintf(envp[envc++], "HOME=%s", p);
1777 envp[envc] = malloc(strlen(pw->pw_name) + 6);
1778 if (envp[envc])
1779 sprintf(envp[envc++], "USER=%s", pw->pw_name);
1780 envp[envc] = malloc(strlen(pw->pw_name) + 9);
1781 if (envp[envc])
1782 sprintf(envp[envc++], "LOGNAME=%s", pw->pw_name);
1783
1784 if (pw->pw_shell && *(pw->pw_shell)) {
1785 shell = pw->pw_shell;
1786 } else {
1787 shell = "/bin/sh";
1788 }
1789 p = strrchr(shell, '/');
1790 if (p) {
1791 if (p[1]) {
1792 p = strdup(p);
1793 if (p)
1794 *p = '-';
1795
1796 } else p = NULL;
1797 }
1798 if (!p)
1799 p = shell;
1800 chargv[chargc++] = p;
1801 envp[envc] = malloc(strlen(shell) + 7);
1802 if (envp[envc])
1803 sprintf(envp[envc++], "SHELL=%s", shell);
1804
1805 if (pw->pw_uid == 0)
1806 p = "/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin";
1807 else
1808 p = "/bin:/usr/bin:/usr/local/bin";
1809 envp[envc] = malloc(strlen(p) + 6);
1810 if (envp[envc])
1811 sprintf(envp[envc++], "PATH=%s", p);
1812
1813 } else {
1814
1815 chargv[chargc++] = "/bin/login";
1816 chargv[chargc++] = "-p";
1817 /* there exist several conectps:
1818 * Historically, the special character '+' in the password
1819 * field indicated that users may login via ax25, netrom, rose,
1820 * etc.. - but not via other protocols like telnet.
1821 * This secures the digipeater from abuse by inet access of
1822 * non-hams.
1823 * On the other hand, this leads to the problem that telent
1824 * via HF, pop3, etc.. do not work.
1825 * The "pwcheck == 2 method means, that the password is used on
1826 * every login mechanism other than this axspawn program;
1827 * here we do not rely on the password - the ax25 call of
1828 * the ham is enough. We have already checked above, that
1829 * the call of the user is valid (and not root, httpd, etc..);
1830 * thus this method is safe here.
1831 * Another mechanism (pwcheck == 3) is to check if the gid
1832 * equals to user_gid preference. I prefer this way, because
1833 * this approach gives the chance to temporary lock a user
1834 * out (abuse, ..) by changing his gid in passwd to for e.g.
1835 * 65534 (nogroup).
1836 */
1837 chargv[chargc++] = "-h";
1838 chargv[chargc++] = protocol;
1839 if (pwtype != PW_CLEARTEXT /* PW_SYS or PW_MD5 are already authenticated */
1840 || pwcheck == 2 || (pwcheck == 3 && (pw->pw_gid == user_gid || is_guest)) || !strcmp(pw->pw_passwd, "+"))
1841 chargv[chargc++] = "-f";
1842 chargv[chargc++] = as_user;
1843 }
1844 chargv[chargc] = NULL;
1845
1846 envp[envc] = malloc(30);
1847 if (envp[envc])
1848 sprintf(envp[envc++], "AXCALL=%s", call);
1849 envp[envc] = malloc(30);
1850 if (envp[envc])
1851 sprintf(envp[envc++], "CALL=%s", user);
1852 envp[envc] = malloc(30);
1853 if (envp[envc])
1854 sprintf(envp[envc++], "PROTOCOL=%s", protocol);
1855 envp[envc] = malloc(30);
1856 if (envp[envc])
1857 sprintf(envp[envc++], "TERM=dumb"); /* SuSE bug (dump - tsts) */
1858 /* other useful defaults */
1859 envp[envc] = malloc(30);
1860 if (envp[envc])
1861 sprintf(envp[envc++], "EDITOR=/usr/bin/ex");
1862 envp[envc] = malloc(30);
1863 if (envp[envc])
1864 sprintf(envp[envc++], "LESS=-d -E -F");
1865 envp[envc] = NULL;
1866
1867 if (dumb_embedded_system) {
1868 if (setgid(pw->pw_gid) == -1)
1869 exit(1);
1870 if (setuid(pw->pw_uid) == -1)
1871 exit(1);
1872 execve(shell, chargv, envp);
1873 /* point of no return */
1874 exit(1);
1875 }
1876
1877 execve(chargv[0], chargv, envp);
1878 /* point of no return */
1879 exit(1);
1880 }
1881 else if (pid > 0)
1882 {
1883 child_pid = 0;
1884 signal(SIGHUP, signal_handler);
1885 signal(SIGTERM, signal_handler);
1886 signal(SIGINT, signal_handler);
1887 signal(SIGQUIT, signal_handler);
1888
1889 while (1)
1890 {
1891 FD_ZERO(&fds_read);
1892 FD_ZERO(&fds_err);
1893 FD_SET(0, &fds_read);
1894 FD_SET(0, &fds_err);
1895 if (wqueue_length <= paclen*7)
1896 FD_SET(fdmaster, &fds_read);
1897 FD_SET(fdmaster, &fds_err);
1898
1899 k = select(fdmaster+1, &fds_read, NULL, &fds_err, NULL);
1900
1901 if (k > 0)
1902 {
1903 if (FD_ISSET(0, &fds_err))
1904 {
1905 if (huffman) {
1906 sprintf(buf,"\r//COMP 0\r");
1907 write_ax25(buf, strlen(buf), 1);
1908 sleep(EXITDELAY);
1909 }
1910 kill(pid, SIGHUP);
1911 cleanup(ptyslave+5);
1912 return 1;
1913 }
1914
1915 if (FD_ISSET(fdmaster, &fds_err))
1916 {
1917 /* give the last packet in the timer controlled sendqueue a chance.. */
1918 if (wqueue_length) {
1919 continue;
1920 }
1921 if (huffman) {
1922 sprintf(buf,"\r//COMP 0\r");
1923 write_ax25(buf, strlen(buf), 1);
1924 sleep(EXITDELAY);
1925 }
1926 cleanup(ptyslave+5);
1927 return 1;
1928 }
1929
1930 if (FD_ISSET(0, &fds_read))
1931 {
1932 cnt = read_ax25(buf, sizeof(buf));
1933 if (cnt < 0) /* Connection died */
1934 {
1935 kill(pid, SIGHUP);
1936 cleanup(ptyslave+5);
1937 return 1;
1938 } else
1939 write(fdmaster, buf, cnt);
1940 }
1941
1942 if (FD_ISSET(fdmaster, &fds_read))
1943 {
1944 cnt = read(fdmaster, buf, (huffman ? 254 : sizeof(buf)));
1945 if (cnt < 0)
1946 {
1947 /* give the last packet in the timer controlled sendqueue a chance.. */
1948 if (wqueue_length) {
1949 continue;
1950 }
1951 if (huffman) {
1952 sprintf(buf,"\r//COMP 0\r");
1953 write_ax25(buf, strlen(buf), 1);
1954 sleep(EXITDELAY);
1955 }
1956 cleanup(ptyslave+5);
1957 return 1; /* Child died */
1958 }
1959 write_ax25(buf, cnt, 0);
1960 }
1961 } else
1962 if (k < 0 && errno != EINTR)
1963 {
1964 if (huffman) {
1965 sprintf(buf,"\r//COMP 0\r");
1966 write_ax25(buf, strlen(buf), 1);
1967 sleep(EXITDELAY);
1968 }
1969
1970 kill(pid, SIGHUP); /* just in case... */
1971 cleanup(ptyslave+5);
1972 return 0;
1973 }
1974 }
1975 }
1976 else
1977 {
1978 write_ax25_static_line(MSG_CANNOTFORK);
1979 syslog(LOG_ERR, "cannot fork %m, closing connection to %s\n", call);
1980 sleep(EXITDELAY);
1981 return 1;
1982 }
1983
1984 sleep(EXITDELAY);
1985
1986 return 0;
1987 }