File: shim.c

package info (click to toggle)
xsp 4.2-2.1
  • links: PTS, VCS
  • area: main
  • in suites: buster, stretch
  • size: 5,168 kB
  • ctags: 2,422
  • sloc: cs: 13,500; sh: 12,082; xml: 8,279; makefile: 650; perl: 619; ansic: 299; php: 6
file content (233 lines) | stat: -rw-r--r-- 5,163 bytes parent folder | download | duplicates (3)
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
#include <assert.h>
#include <errno.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <stdarg.h>
#include <stdlib.h>
#include <time.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/un.h>

#define bool char
#define false 0
#define true 1

#define UNIX_PATH_MAX 108

#define BUFFER_SIZE 10

#define PREFIX "%5d    shim     [%s]        : "

bool debug = false;
bool verbose = false;

char time_buffer[50];

char * time_str (void)
{
    time_t now = time (0);
    strftime (time_buffer, 50, "%Y-%m-%d %H:%M:%S       ", localtime (&now));
    return time_buffer;
}

void print_prefix (void)
{
	if (verbose)
		printf (PREFIX, getpid (), time_str ());
}

void log1 (char * str)
{
    if(!debug) return;
	print_prefix ();
    printf("%s\n", str);
}

void log(char * fmt, ...)
{
    if(!debug) return;
    print_prefix ();
    va_list argptr;
    va_start(argptr, fmt);
    vprintf(fmt, argptr);
    va_end(argptr);
    printf ("\n");
}

ssize_t send_string (int fd, const char * value)
{
    return send (fd, value, strlen (value) + 1, 0);
}

ssize_t send_int (int fd, int value)
{
    int size = snprintf (NULL, 0, "%d", value);
    char buffer [size + 1];
    snprintf (buffer, size + 1, "%d", value);
    buffer [size] = 0;
    log("%d (%s) has size %d", value, buffer, size);
    return send_string (fd, buffer);
}

bool start_server (const char * path, int * socket_fd)
{
    assert (path != 0);
    assert (socket_fd != 0);
    *socket_fd = socket (AF_UNIX, SOCK_STREAM, 0);
    if (*socket_fd == -1) {
        perror ("socket");
        return false;
    }

    struct sockaddr_un local;
    local.sun_family = AF_UNIX;

    if (strlen (path) >= UNIX_PATH_MAX) {
        fprintf (stderr, "Shim: Path %s is too long!", path);
        return false;
    }
    strncpy (local.sun_path, path, UNIX_PATH_MAX - 1);
    local.sun_path [UNIX_PATH_MAX - 1] = 0;
    if (unlink (path) == -1 && errno != ENOENT) {
        perror ("unlink");
        return false;
    }

    socklen_t local_len = strlen (path) + sizeof (local.sun_family);
    if (bind (*socket_fd, (struct sockaddr *)&local, local_len) == -1) {
        perror ("bind");
        return false;
    }

    const int backlog = 5;
    if (listen (*socket_fd, backlog) == -1) {
        perror ("listen");
        return false;
    }

    chmod (path, 0660);

    return true;
}

pid_t spawn (int fd, char * params [])
{
    assert (params != 0);
    assert (*params != 0);
    log1 ("Spawning!");
    pid_t child = fork ();
    if (child)
        return 0;

    log1 ("Forked!");

    if (setsid () == -1) {
        perror ("setsid");
        exit(1);
    }

    child = fork ();
    if (child) {
        send_int (fd, child);
        log ("Sent pid %d", child);
        exit (0);
        return -1;
    }

    log1 ("Reforked!");

    log ("Running %s", *params);

    if (execv (*params, params) == -1) {
        perror ("execv");
        exit (1);
    }

    return -1;
}

bool run_connection (int fd, char * params [])
{
    assert (fd != 0);
    assert (params != 0);
    assert (*params != 0);
    for (;;) {
        char buffer [BUFFER_SIZE];
        ssize_t received = recv (fd, buffer, BUFFER_SIZE, 0);

        if (received < 0) {
            perror ("recv");
            return false;
        }

        if (received == 0)
            return true;

        if (strncmp (buffer, "SPAWN\n", 6) == 0) {
            pid_t spawned = spawn (fd, params);
            if (spawned != 0) {
                log1 ("Sending NOK");
                if (send_string (fd, "NOK\n") < 0) {
                    perror ("send");
                    return false;
                }
            }
        } else {
            log1 ("Sending NACK!");
            if (send_string (fd, "NACK!\n") < 0) {
                perror ("send");
                return false;
            }
        }
    }
}

int main (int argc, char * argv [], char *envp[])
{
    uid_t euid = geteuid ();
    setreuid (euid, euid);
    gid_t egid = getegid ();
    setregid (egid, egid);

    char * verbose_env = getenv("VERBOSE");
    char * debug_env = getenv("DEBUG");
    verbose = verbose_env && (verbose_env[0] == 'y' || verbose_env[0] == 'Y');
    debug = debug_env && (debug_env[0] == 'y' || debug_env[0] == 'Y');

    log1 ("Started.");
    log ("I'm uid %d euid %d gid %d egid %d", getuid (), geteuid (), getgid (), getegid ());

    if (argc <= 2) {
        fprintf (stderr, "Usage: %s <socket> <command>\n", argv [0]);
        return 1;
    }

    const char * path = argv [1];
	char ** params = argv + 2;

    log ("Will run %s", *params);

    int local_fd;
    if (!start_server (path, &local_fd))
        return 1;

    for (;;) {
        log1 ("Waiting for a connection...");
        struct sockaddr_un remote;
        socklen_t t = sizeof (remote);
        int remote_fd = accept (local_fd, (struct sockaddr *)&remote, &t);
        if (remote_fd == -1) {
            perror ("accept");
            return 1;
        }

        log1 ("Connected.");

        if (!run_connection (remote_fd, params))
            log1 ("Something went wrong while processing input");

        close (remote_fd);
    }
}