Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/nss-rainbow.c
blob: c108799b1cd7afde82cab1075103fec863afcdf5 (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
#define _GNU_SOURCE
#include <nss.h>
#include <stdio.h>
#include <pwd.h>
#include <stdint.h>
#include <stdlib.h>
#include <stdbool.h>
#include <stdarg.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>
#include <unistd.h>
#include <string.h>
#include <limits.h>
#include <syslog.h>

/* code generators */
#define STATIC_ASSERT(expr)  extern char __static_assertion_failed [(expr) ? 1 : -1]
#define SAVE_ERR(EXPR) {int __errno_save = errno; EXPR; errno = __errno_save;}
#define __XSTRING(X) __STRING(X)
#define PERROR(msg) {syslog(LOG_ERR, "%s|%d| %s: %s\nError %d: %s", __FILE__, __LINE__, __func__, msg, errno, strerror(errno));}
#define CHK(EXPR, MSG, ERR_LABEL) {if(EXPR) { PERROR(MSG); goto ERR_LABEL;}}
#define LET(LETEXPR, CONDEXPR, MSG, ERR_LABEL) LETEXPR; if (CONDEXPR) { PERROR(MSG); goto ERR_LABEL;}

/* constants */
#define SHELL "/sbin/nologin"

static bool g_done;

/* implementation */
enum nss_status
_nss_rainbow_endpwent(void) {
  g_done = false;
  return NSS_STATUS_SUCCESS;
}

enum nss_status
_nss_rainbow_setpwent(void) {
  return NSS_STATUS_SUCCESS;
  g_done = false;
}

int write_buf(char** buf, size_t * buflen, const char* val)
{
  /* write_buf will not write outside of (*buf)[0..(*buflen-1)]
   * if *buflen > strlen(val), then write_buf will ensure that *buf points to a null-terminated string.
   * on success, write_buf will advance *buf to the next free char,
   *                            set *buflen to indicate the quantity of remaining space,
   *                            and return 1.
   * on error, write_buf will set errno, return 0, and will not modify *buf or *buflen.
   * *buf and val should not overlap.
   */

  LET(size_t copy_amt = strlen(val) + 1, copy_amt == 0,
      "Integer overflow.", out_err_overflow);

  if (*buflen < copy_amt)
    goto out_err_range;

  memcpy(*buf, val, copy_amt);

  *buf += copy_amt;
  *buflen -= copy_amt;
  return 1;

out_err_overflow:
  errno = EOVERFLOW;
  return 0;

out_err_range:
  errno = ERANGE;
  return 0;
}

int format_buf(char** buf, size_t* buflen, const char* fmt, ...)
{
  /* format_buf will not write outside of (*buf)[0..(*buflen-1)]
   * if *buflen > 0, format_buf will ensure that *buf points to a null-terminated string.
   * on success, format_buf will advance *buf to the next free char,
   *                             set *buflen to indicate the quantity of remaining space,
   *                             and return 1.
   * on error, format_buf will set errno, return 0, and will not modify *buf or *buflen.
   * *buf should know overlap with fmt or any optional arguments.
   */
  if (*buflen < 1)
  {
    errno = ERANGE;
    return 0;
  }

  size_t safe_buflen = *buflen - 1;

  va_list ap;
  va_start(ap, fmt);
  int status = vsnprintf(*buf, safe_buflen, fmt, ap);
  SAVE_ERR(va_end(ap));

  if (status < 0)
    goto out_err;

  size_t written = (size_t) status;
  if (safe_buflen < written) {
    errno = ERANGE;
    goto out_err;
  }

  *buf += written+1;
  *buflen -= written+1;
  return 1;

out_err:
  (*buf)[safe_buflen] = '\0';
  return 0;
}

enum nss_status
_nss_rainbow_getpwent_r(struct passwd *result, char* buf, size_t buflen, int *errnop) {
  /* I'm going to interpret the reentrancy requirement on this function to mean
   * that the sequence of all results returned by all calls to this function
   * between calls to setpwent() should contain contain every entry exactly
   * once. It is undefined whether writes to the underlying storage that
   * interleave with calls to getpwent() will be visible to callers. */

  openlog("nss-rainbow", LOG_PID, LOG_LOCAL0);

  result->pw_uid = getuid();
  result->pw_gid = getgid();

  LET(char* home = getenv("HOME"), home == NULL,
      "Unable to retrieve $HOME", out_error_unavail);

  LET(char * user = getenv("USER"), user == NULL,
      "Unable to retrieve $USER.", out_error_unavail);

  result->pw_dir = buf;
  CHK(write_buf(&buf, &buflen, home) == 0,
      "Not enough buffer for $HOME.", out_error_again);

  result->pw_gecos = result->pw_name = result->pw_passwd = buf;
  CHK(write_buf(&buf, &buflen, user) == 0,
      "Not enough buffer for $USER.", out_error_again);

  result->pw_shell = buf;
  CHK(write_buf(&buf, &buflen, SHELL) == 0,
      "Shell string constant too long.", out_error_again);

  syslog(LOG_ERR, "%s success: %s (%d, %d) %s %s %s", __func__,
         result->pw_name, result->pw_uid, result->pw_gid, result->pw_dir,
         result->pw_shell, result->pw_passwd);

  if (g_done)
    return NSS_STATUS_NOTFOUND;

  g_done = true;
  return NSS_STATUS_SUCCESS;

out_error_unavail:
  return NSS_STATUS_UNAVAIL;

out_error_again:
  *errnop = errno;
  return NSS_STATUS_TRYAGAIN;
}

enum nss_status _nss_rainbow_getpwuid_r(uid_t uid, struct passwd *result, char* buf, size_t buflen, int *errnop) {
  openlog("nss-rainbow", LOG_PID, LOG_LOCAL0);

  if (uid < 10000)
      return NSS_STATUS_NOTFOUND;

  result->pw_dir = buf;
  CHK(format_buf(&buf, &buflen, "/home/olpc/isolation/1/uid_to_home_dir/%d", uid) == 0,
      "Unable to calculate home dir.", out_error_errno);

  struct stat st;
  CHK(stat(result->pw_dir, &st) == -1,
      "Stat failed for homebuf.", out_error_errno);
  result->pw_uid = uid;
  result->pw_gid = st.st_gid;

  result->pw_gecos = result->pw_name = result->pw_passwd = buf;
  CHK(format_buf(&buf, &buflen, "%d", uid) == 0,
      "Unable to calculate user name.", out_error_errno);

  result->pw_shell = buf;
  CHK(write_buf(&buf, &buflen, SHELL) == 0,
      "Shell string constant too long.", out_error_errno);

  syslog(LOG_ERR, "%s success in uid: %s (%d, %d) %s %s %s", __func__, result->pw_name, result->pw_uid, result->pw_gid, result->pw_dir, result->pw_shell, result->pw_passwd);

  return NSS_STATUS_SUCCESS;

out_error_errno:
  *errnop = errno;
  return NSS_STATUS_TRYAGAIN;
}


enum nss_status _nss_rainbow_getpwnam_r(const char * name, struct passwd *result, char* buf, size_t buflen, int *errnop) {
  openlog("nss-rainbow", LOG_PID, LOG_LOCAL0);

  char * endptr = (char*) (uintptr_t) (name + strlen(name));

  errno = 0;    /* To distinguish success/failure after call */
  long val = strtol(name, &endptr, 10);

  if ((errno == ERANGE && (val == LONG_MAX || val == LONG_MIN))
   || (errno != 0 && val == 0)
   || (name == endptr)) {
      PERROR("Unable to parse name into a uid.");
      *errnop = (errno = 0) ? EINVAL : errno;
      return NSS_STATUS_NOTFOUND;
  }

  return _nss_rainbow_getpwuid_r((uid_t)val, result, buf, buflen, errnop);
}