summaryrefslogtreecommitdiff
path: root/src/or/control.c
blob: 877342560ef89dba388e02d5059039a0c8d7e6fd (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
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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
/* Copyright 2004 Nick Mathewson */
/* See LICENSE for licensing information */
/* $Id$ */

#include "or.h"

#define CONTROL_CMD_ERROR        0x0000
#define CONTROL_CMD_DONE         0x0001
#define CONTROL_CMD_SETCONF      0x0002
#define CONTROL_CMD_GETCONF      0x0003
#define CONTROL_CMD_CONFVALUE    0x0004
#define CONTROL_CMD_SETEVENTS    0x0005
#define CONTROL_CMD_EVENT        0x0006
#define CONTROL_CMD_AUTHENTICATE 0x0007
#define _CONTROL_CMD_MAX_RECOGNIZED 0x0007

#define ERR_UNSPECIFIED             0x0000
#define ERR_UNRECOGNIZED_TYPE       0x0001
#define ERR_UNRECOGNIZED_CONFIG_KEY 0x0002
#define ERR_INVALID_CONFIG_VALUE    0x0003
#define ERR_UNRECOGNIZED_EVENT_CODE 0x0004
#define ERR_UNAUTHORIZED_USER       0x0005
#define ERR_FAILED_AUTHENTICATION   0x0006

#define _EVENT_MIN            0x0001
#define EVENT_CIRCUIT_STATUS  0x0001
#define EVENT_STREAM_STATUS   0x0002
#define EVENT_OR_CONN_STATUS  0x0003
#define EVENT_BANDWIDTH_USED  0x0004
#define EVENT_WARNING         0x0005
#define _EVENT_MAX            0x0005

#define EVENT_IS_INTERESTING(e) (global_event_mask & (1<<(e)))

static const char *CONTROL_COMMANDS[] = {
  "error",
  "done",
  "setconf",
  "getconf",
  "confvalue",
  "setevents",
  "events",
  "authenticate",
};

static uint32_t global_event_mask = 0;

static void update_global_event_mask(void);
static void send_control_message(connection_t *conn, uint16_t type,
                                 uint16_t len, const char *body);
static void send_control_done(connection_t *conn);
static void send_control_error(connection_t *conn, uint16_t error,
                               const char *message);
static void send_control_event(uint16_t event, uint16_t len, const char *body);
static int handle_control_setconf(connection_t *conn, uint16_t len,
                                  const char *body);
static int handle_control_getconf(connection_t *conn, uint16_t len,
                                  const char *body);
static int handle_control_setevents(connection_t *conn, uint16_t len,
                                    const char *body);
static int handle_control_authenticate(connection_t *conn, uint16_t len,
                                       const char *body);

static INLINE const char *
control_cmd_to_string(uint16_t cmd)
{
  return (cmd<=_CONTROL_CMD_MAX_RECOGNIZED) ? CONTROL_COMMANDS[cmd] : "Unknown";
}

static void update_global_event_mask(void)
{
  connection_t **conns;
  int n_conns, i;

  global_event_mask = 0;
  get_connection_array(&conns, &n_conns);
  for (i = 0; i < n_conns; ++i) {
    if (conns[i]->type == CONN_TYPE_CONTROL &&
        conns[i]->state == CONTROL_CONN_STATE_OPEN) {
      global_event_mask |= conns[i]->event_mask;
    }
  }
}

static void
send_control_message(connection_t *conn, uint16_t type, uint16_t len,
                     const char *body)
{
  char buf[4];
  tor_assert(conn);
  tor_assert(len || !body);
  tor_assert(type <= _CONTROL_CMD_MAX_RECOGNIZED);
  set_uint32(buf, htons(len));
  set_uint32(buf+2, htons(type));
  connection_write_to_buf(buf, 4, conn);
  if (len)
    connection_write_to_buf(body, len, conn);
}

static void
send_control_done(connection_t *conn)
{
  send_control_message(conn, CONTROL_CMD_DONE, 0, NULL);
}

static void
send_control_error(connection_t *conn, uint16_t error, const char *message)
{
  char buf[256];
  size_t len;
  set_uint16(buf, htons(error));
  len = strlen(message);
  tor_assert(len < (256-2));
  memcpy(buf+2, message, len);
  send_control_message(conn, CONTROL_CMD_ERROR, len+2, buf);
}

static void
send_control_event(uint16_t event, uint16_t len, const char *body)
{
  connection_t **conns;
  int n_conns, i;

  get_connection_array(&conns, &n_conns);
  for (i = 0; i < n_conns; ++i) {
    if (conns[i]->type == CONN_TYPE_CONTROL &&
        conns[i]->state == CONTROL_CONN_STATE_OPEN &&
        conns[i]->event_mask & (1<<event)) {
      send_control_message(conns[i], CONTROL_CMD_EVENT, len, body);
    }
  }
}

static int 
handle_control_setconf(connection_t *conn, uint16_t len,
                                  const char *body)
{
  /* XXXX009 NM */
  return 0;
}
static int handle_control_getconf(connection_t *conn, uint16_t len,
                                  const char *body)
{
  /* XXXX009 NM */
  return 0;
}
static int handle_control_setevents(connection_t *conn, uint16_t len,
                                    const char *body)
{
  uint16_t event_code;
  uint32_t event_mask = 0;
  if (len % 2) {
    send_control_error(conn, ERR_UNSPECIFIED,
                       "Odd number of bytes in setevents message");
    return 0;
  }

  for (; len; len -= 2, body += 2) {
    event_code = ntohs(get_uint16(body));
    if (event_code < _EVENT_MIN || event_code > _EVENT_MAX) {
      send_control_error(conn, ERR_UNRECOGNIZED_EVENT_CODE,
                         "Unrecognized event code");
      return 0;
    }
    event_mask |= (1 << event_code);
  }

  conn->event_mask = event_mask;

  update_global_event_mask();
  send_control_done(conn);
  return 0;
}
static int handle_control_authenticate(connection_t *conn, uint16_t len,
                                       const char *body)
{
  if (0/* XXXX009 NM */) {
    send_control_done(conn);
    conn->state = CONTROL_CONN_STATE_OPEN;
  } else {
    send_control_error(conn, ERR_FAILED_AUTHENTICATION,"Authentication failed");
  }
  return 0;
}

int connection_control_finished_flushing(connection_t *conn) {
  tor_assert(conn);
  tor_assert(conn->type == CONN_TYPE_CONTROL);

  connection_stop_writing(conn);
  return 0;
}

int connection_control_process_inbuf(connection_t *conn) {
  uint16_t body_len, command_type;
  char *body;

  tor_assert(conn);
  tor_assert(conn->type == CONN_TYPE_CONTROL);

 again:
  switch(fetch_from_buf_control(conn->inbuf, &body_len, &command_type, &body))
    {
    case -1:
      log_fn(LOG_WARN, "Error in control command. Failing.");
      return -1;
    case 0:
      /* Control command not all here yet. Wait. */
      return 0;
    case 1:
      /* We got a command. Process it. */
      break;
    default:
      tor_assert(0);
    }

  /* We got a command.  If we need authentication, only authentication
   * commands will be considered. */
  if (conn->state == CONTROL_CONN_STATE_NEEDAUTH &&
      command_type != CONTROL_CMD_AUTHENTICATE) {
    log_fn(LOG_WARN, "Rejecting '%s' command; authentication needed.",
           control_cmd_to_string(command_type));
    send_control_error(conn, ERR_UNAUTHORIZED_USER, "Authentication required");
    tor_free(body);
    goto again;
  }

  switch(command_type)
    {
    case CONTROL_CMD_SETCONF:
      if (handle_control_setconf(conn, body_len, body))
        return -1;
      break;
    case CONTROL_CMD_GETCONF:
      if (handle_control_getconf(conn, body_len, body))
        return -1;
      break;
    case CONTROL_CMD_SETEVENTS:
      if (handle_control_setevents(conn, body_len, body))
        return -1;
      break;
    case CONTROL_CMD_AUTHENTICATE:
      if (handle_control_authenticate(conn, body_len, body))
        return -1;
      break;
    case CONTROL_CMD_ERROR:
    case CONTROL_CMD_DONE:
    case CONTROL_CMD_CONFVALUE:
    case CONTROL_CMD_EVENT:
      log_fn(LOG_WARN, "Received client-only '%s' command; ignoring.",
             control_cmd_to_string(command_type));
      send_control_error(conn, ERR_UNRECOGNIZED_TYPE,
                         "Command type only valid from server to tor client");
      break;
    default:
      log_fn(LOG_WARN, "Received unrecognized command type %d; ignoring.",
             (int)command_type);
      send_control_error(conn, ERR_UNRECOGNIZED_TYPE,
                         "Unrecognized command type");
      break;
  }
  tor_free(body);
  goto again; /* There might be more data. */
}

int control_event_circuit_status(circuit_t *circ)
{
  if (!EVENT_IS_INTERESTING(EVENT_CIRCUIT_STATUS))
    return 0;

  /* XXXXX009 NM */

  return 0;
}

int control_event_stream_status(connection_t *conn)
{
  tor_assert(conn->type == CONN_TYPE_AP);

  if (!EVENT_IS_INTERESTING(EVENT_STREAM_STATUS))
    return 0;

  /* XXXXX009 NM */

  return 0;
}

int control_event_or_conn_status(connection_t *conn)
{
  tor_assert(conn->type == CONN_TYPE_OR);

  if (!EVENT_IS_INTERESTING(EVENT_OR_CONN_STATUS))
    return 0;

  /* XXXXX009 NM */

  return 0;
}

int control_event_bandwidth_used(uint32_t n_read, uint32_t n_written)
{
  char buf[8];

  if (!EVENT_IS_INTERESTING(EVENT_BANDWIDTH_USED))
    return 0;

  set_uint32(buf, htonl(n_read));
  set_uint32(buf+4, htonl(n_read));
  send_control_event(EVENT_BANDWIDTH_USED, 8, buf);

  return 0;
}

int control_event_warning(const char *msg)
{
  size_t len;
  if (!EVENT_IS_INTERESTING(EVENT_WARNING))
    return 0;

  len = strlen(msg);
  send_control_event(EVENT_WARNING, len+1, msg);

  return 0;
}


/*
  Local Variabls:
  mode:c
  indent-tabs-mode:nil
  c-basic-offset:2
  End:
*/