-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcall_uucico.c
289 lines (239 loc) · 8 KB
/
call_uucico.c
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
/* Rhizo-uuardop: Tools to integrate Ardop to UUCP
* Copyright (C) 2019 Rhizomatica
* Author: Rafael Diniz <rafael@riseup.net>
*
* This is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3, or (at your option)
* any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this software; see the file COPYING. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street,
* Boston, MA 02110-1301, USA.
*
* Routines to call uucico when receiving a call
*/
/**
* @file call_uucico.c
* @author Rafael Diniz
* @date 26 Jul 2019
* @brief Routines to call uucico when receiving a call
*
* Code to call uucico
*
*/
#include <sys/ioctl.h>
#include <stdint.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <unistd.h>
#include <signal.h>
#include <poll.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>
#include <sys/inotify.h>
#include <libgen.h>
#include <errno.h>
#include <fcntl.h>
#include <sys/wait.h>
#include <grp.h>
#include "call_uucico.h"
bool call_uucico(rhizo_conn *connector){
fprintf(stderr, "call_uucico: Sending signal to start uucico!\n");
if (connector->uucico_active == true)
fprintf(stderr, "Warning: trying to activate already active uucico!\n");
else
connector->uucico_active = true;
return true;
}
void *uucico_thread(void *conn){
rhizo_conn *connector = conn;
pid_t pid;
int st;
// set some file descriptors as in in.uucpd...
// use 2 pipe() to create the fds for I/O
// fork()
// in the parent, call wait to wait for uucico (in a new thread?)
// in the child, remap the fds to 0, 1 (and 2?)
// in the child, call execl() (or execlp() (uucico -l)
while (connector->shutdown == false)
{
while (connector->uucico_active == false)
usleep(100000); // 0.1s
fprintf(stderr, "uucico_thread: session started!\n");
// parent write to child
pipe(connector->pipefd1); // pipe[0] is read, pipe[1] is write
// child write to parent
pipe(connector->pipefd2);
pthread_t tid1;
pthread_create(&tid1, NULL, uucico_read_thread, (void *) connector);
pthread_t tid2;
pthread_create(&tid2, NULL, uucico_write_thread, (void *) connector);
// parent
if ((pid = fork()) != 0)
{
if (pid < 0) {
fprintf(stderr, "fork() error.\n");
return NULL;
}
close(connector->pipefd1[0]);
close(connector->pipefd2[1]);
// pthread_create the two threads which does the job of reading / writing from/to buffers and fds...
while(wait(&st) != pid);
if ( WIFEXITED(st) ){
fprintf(stderr, "uucico child exec exited with status = %d\n", WEXITSTATUS(st));
// uucico ended!
// we should disconnect here!
}
close(connector->pipefd1[1]);
close(connector->pipefd2[0]);
// fprintf(stderr, "uucico before join\n");
pthread_join(tid1, NULL);
pthread_join(tid2, NULL);
// fprintf(stderr, "uucico after join\n");
// usleep(2000000); // 2s for the system to cool down
connector->clean_buffers = true;
connector->send_break = true;
connector->uucico_active = false;
fprintf(stderr, "uucico_thread: session ended!\n");
continue;
}
// this is the child (uucico)
close(0);
close(1);
close(2);
dup2(connector->pipefd1[0], 0);
dup2(connector->pipefd2[1], 1);
dup2(connector->pipefd2[1], 2); // is this correct?
close(connector->pipefd1[0]);
close(connector->pipefd2[1]);
close(connector->pipefd1[1]); // closing write pipefd1 (child reads from parent)
close(connector->pipefd2[0]); // closing read pipefd2 (child writes to parent)
#if 0 // lets run all as root
char pwd[] = "/var/spool/uucp"; // uucp home
if (chdir(pwd) != 0) {
perror(pwd);
exit(1);
}
gid_t gid = 10; // uucp gid
if (setgid(gid) != 0) {
perror("setgid");
exit(1);
}
char user[] = "uucp";
if (initgroups(user, gid) < 0) {
perror("initgroups");
exit(1);
}
uid_t uid = 10; // uucp uid
if (setuid(uid) != 0) {
perror("setuid");
exit(1);
}
#endif
char shell[] = "/usr/sbin/uucico";
// setenv("LOGNAME", user, 1);
// setenv("USER", user, 1);
setenv("SHELL", shell, 1);
setenv("TERM", "dumb", 1);
if (connector->ask_login == true)
execl(shell, shell, "-l", NULL);
else
execl(shell, shell, NULL);
perror(shell);
_exit(EXIT_SUCCESS);
}
return NULL;
}
void *uucico_read_thread(void *conn)
{
bool running = true;
rhizo_conn *connector = (rhizo_conn *) conn;
int num_read = 0;
int bytes_pipe = 0;
uint8_t buffer[BUFFER_SIZE];
while(running)
{
ioctl(connector->pipefd2[0], FIONREAD, &bytes_pipe);
// fprintf(stderr, "trying to read from uucico %d bytes!\n", bytes_pipe);
if (bytes_pipe > BUFFER_SIZE)
bytes_pipe = BUFFER_SIZE;
if (bytes_pipe <= 0)
bytes_pipe = 1; // so we block in read() in case of no data to read
// workaround to make protocol 'y' work better
while (circular_buf_size(connector->in_buffer) > BUFFER_SIZE/2)
{
bytes_pipe = 1; // slow down...
usleep(100000); // 0.1s
}
num_read = read(connector->pipefd2[0], buffer, bytes_pipe);
if (num_read > 0)
{
while (circular_buf_free_size(connector->in_buffer) < num_read)
usleep(20000);
circular_buf_put_range(connector->in_buffer, buffer, num_read);
}
if (num_read == 0)
{
fprintf(stderr, "uucico_read_thread: read == 0\n");
running = false;
}
if (num_read == -1)
{
fprintf(stderr, "uucico_read_thread: read() error! error no: %d\n",errno);
running = false;
}
}
connector->session_counter_read++;
return NULL;
}
void *uucico_write_thread(void *conn) {
bool running = true;
rhizo_conn *connector = (rhizo_conn *) conn;
int bytes_to_read = 0;
int num_written = 0;
uint8_t buffer[BUFFER_SIZE];
while (running)
{
bytes_to_read = circular_buf_size(connector->out_buffer);
if (bytes_to_read == 0)
{ // we spinlock here
usleep(100000); // 0.1s
if (connector->session_counter_read > connector->session_counter_write)
running = false;
continue;
}
if (bytes_to_read > BUFFER_SIZE)
bytes_to_read = BUFFER_SIZE;
circular_buf_get_range(connector->out_buffer, buffer, bytes_to_read);
num_written = write(connector->pipefd1[1], buffer, bytes_to_read);
if (num_written == 0)
{
fprintf(stderr, "pipe_write_thread: write == 0\n");
running = false;
}
if (num_written == -1)
{
running = false;
if (errno == EPIPE)
{
fprintf(stderr, "uucico_write_thread: write() EPIPE!\n");
}
else
{
fprintf(stderr, "uucico_write_thread: write() error no: %d\n", errno);
}
}
}
connector->session_counter_write++;
return NULL;
}