summaryrefslogtreecommitdiff
path: root/anthy/src/shim_posix.c
blob: 327501c9675e2d7c6b53155a5148c20ad75f568f (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
/*
 * shim_posix.c — the few POSIX entry points anthy calls that WASI does not provide.
 *
 * Measured on the wasi-sdk-25 sysroot: libc.a defines neither getuid, getpwuid nor
 * gethostname, while anthy uses getpwuid(getuid()) in conf.c and gethostname() when
 * it builds the session id. Linking without these fails with undefined symbols.
 *
 * Nothing here is security relevant: the module runs inside the embedder's own
 * process, HOME only decides where anthy looks for the private dictionary, and the
 * session id only has to be unique within the process.
 */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include "pwd.h"

uid_t getuid(void)
{
    return 0;
}

uid_t geteuid(void)
{
    return 0;
}

static struct passwd fake_pw;
static char home_dir[512];

struct passwd *getpwuid(uid_t uid)
{
    const char *h;
    (void)uid;

    h = getenv("HOME");
    snprintf(home_dir, sizeof(home_dir), "%s", (h && *h) ? h : "/work");

    fake_pw.pw_name = (char *)"wasm";
    fake_pw.pw_passwd = (char *)"";
    fake_pw.pw_uid = 0;
    fake_pw.pw_gid = 0;
    fake_pw.pw_gecos = (char *)"";
    fake_pw.pw_dir = home_dir;
    fake_pw.pw_shell = (char *)"/";
    return &fake_pw;
}

struct passwd *getpwnam(const char *name)
{
    (void)name;
    return getpwuid(0);
}

/* declared in <unistd.h>, but the definition is missing from libc.a */
int gethostname(char *name, size_t len)
{
    if (!name || len == 0)
        return -1;
    snprintf(name, len, "wasm");
    return 0;
}