/mandos/trunk

To get this branch, use:
bzr branch /loggerhead/mandos/trunk
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
/*  -*- coding: utf-8 -*- */
/* 
 * iprouteadddel - Add or delete direct route to a local IP address
 * 
 * Copyright © 2015-2018 Teddy Hogeborn
 * Copyright © 2015-2018 Björn Påhlsson
 * 
 * This file is part of Mandos.
 * 
 * Mandos 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 of the License, or
 * (at your option) any later version.
 * 
 * Mandos 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 Mandos.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * Contact the authors at <mandos@recompile.se>.
 */

#define _GNU_SOURCE		/* program_invocation_short_name */
#include <stdbool.h>		/* bool, false, true */
#include <stdio.h>		/* fprintf(), stderr, FILE, vfprintf */
#include <errno.h>		/* program_invocation_short_name,
				   errno, perror(), EINVAL, ENOMEM */
#include <stdarg.h>		/* va_list, va_start */
#include <stdlib.h> 		/* EXIT_SUCCESS */
#include <argp.h>		/* struct argp_option, error_t, struct
				   argp_state, ARGP_KEY_ARG,
				   argp_usage(), ARGP_KEY_END,
				   ARGP_ERR_UNKNOWN, struct argp,
				   argp_parse() */
#include <sysexits.h>		/* EX_USAGE, EX_OSERR */
#include <netinet/ip.h>		/* sa_family_t, AF_INET6, AF_INET */
#include <inttypes.h>		/* PRIdMAX, intmax_t */

#include <netlink/netlink.h>	/* struct nl_addr, nl_addr_parse(),
				   nl_geterror(),
				   nl_addr_get_family(),
				   nl_addr_put() */
#include <netlink/route/route.h> /* struct rtnl_route,
				    struct rtnl_nexthop,
				    rtnl_route_alloc(),
				    rtnl_route_set_family(),
				    rtnl_route_set_protocol(),
				    RTPROT_BOOT,
				    rtnl_route_set_scope(),
				    RT_SCOPE_LINK,
				    rtnl_route_set_type(),
				    RTN_UNICAST,
				    rtnl_route_set_dst(),
				    rtnl_route_set_table(),
				    RT_TABLE_MAIN,
				    rtnl_route_nh_alloc(),
				    rtnl_route_nh_set_ifindex(),
				    rtnl_route_add_nexthop(),
				    rtnl_route_add(),
				    rtnl_route_delete(),
				    rtnl_route_put(),
				    rtnl_route_nh_free() */
#include <netlink/socket.h>	/* struct nl_sock, nl_socket_alloc(),
				   nl_connect(), nl_socket_free() */
#include <netlink/route/link.h>	/* rtnl_link_get_kernel(),
				   rtnl_link_get_ifindex(),
				   rtnl_link_put() */

bool debug = false;
const char *argp_program_version = "mandos-client-iprouteadddel " VERSION;
const char *argp_program_bug_address = "<mandos@recompile.se>";

/* Function to use when printing errors */
void perror_plus(const char *print_text){
  int e = errno;
  fprintf(stderr, "Mandos plugin helper %s: ",
	  program_invocation_short_name);
  errno = e;
  perror(print_text);
}

__attribute__((format (gnu_printf, 2, 3), nonnull))
int fprintf_plus(FILE *stream, const char *format, ...){
  va_list ap;
  va_start (ap, format);
  
  fprintf(stream, "Mandos plugin helper %s: ",
	  program_invocation_short_name);
  return vfprintf(stream, format, ap);
}

int main(int argc, char *argv[]){
  int ret;
  int exitcode = EXIT_SUCCESS;
  struct arguments {
    bool add;			/* true: add, false: delete */
    char *address;		/* IP address as string */
    struct nl_addr *nl_addr;	/* Netlink IP address */
    char *interface;		/* interface name */
  } arguments = { .add = true, .address = NULL, .interface = NULL };
  struct argp_option options[] = {
    { .name = "debug", .key = 128,
      .doc = "Debug mode" },
    { .name = NULL }
  };
  struct rtnl_route *route = NULL;
  struct rtnl_nexthop *nexthop = NULL;
  struct nl_sock *sk = NULL;
  
  error_t parse_opt(int key, char *arg, struct argp_state *state){
    int lret;
    errno = 0;
    switch(key){
    case 128:			/* --debug */
      debug = true;
      break;
    case ARGP_KEY_ARG:
      switch(state->arg_num){
      case 0:
	if(strcasecmp(arg, "add") == 0){
	  ((struct arguments *)(state->input))->add = true;
	} else if(strcasecmp(arg, "delete") == 0){
	  ((struct arguments *)(state->input))->add = false;
	} else {
	  fprintf_plus(stderr, "Unrecognized command: %s\n", arg);
	  argp_usage(state);
	}
	break;
      case 1:
	((struct arguments *)(state->input))->address = arg;
	lret = nl_addr_parse(arg, AF_UNSPEC, &(((struct arguments *)
						(state->input))
					       ->nl_addr));
	if(lret != 0){
	  fprintf_plus(stderr, "Failed to parse address %s: %s\n",
		       arg, nl_geterror(lret));
	  argp_usage(state);
	}
	break;
      case 2:
	((struct arguments *)(state->input))->interface = arg;
	break;
      default:
	argp_usage(state);
      }
      break;
    case ARGP_KEY_END:
      if(state->arg_num < 3){
	argp_usage(state);
      }
      break;
    default:
      return ARGP_ERR_UNKNOWN;
    }
    return errno;
  }
  
  struct argp argp = { .options = options, .parser = parse_opt,
		       .args_doc = "[ add | delete ] ADDRESS INTERFACE",
		       .doc = "Mandos client helper -- Add or delete"
		       " local route to IP address on interface" };
  
  ret = argp_parse(&argp, argc, argv, ARGP_IN_ORDER, 0, &arguments);
  switch(ret){
  case 0:
    break;
  case EINVAL:
    exit(EX_USAGE);
  case ENOMEM:
  default:
    errno = ret;
    perror_plus("argp_parse");
    exitcode = EX_OSERR;
    goto end;
  }
  /* Get netlink socket */
  sk = nl_socket_alloc();
  if(sk == NULL){
    fprintf_plus(stderr, "Failed to allocate netlink socket: %s\n",
		 nl_geterror(ret));
    exitcode = EX_OSERR;
    goto end;
  }
  /* Connect socket to netlink */
  ret = nl_connect(sk, NETLINK_ROUTE);
  if(ret < 0){
    fprintf_plus(stderr, "Failed to connect socket to netlink: %s\n",
		 nl_geterror(ret));
    exitcode = EX_OSERR;
    goto end;
  }
  /* Get link object of specified interface */
  struct rtnl_link *link = NULL;
  ret = rtnl_link_get_kernel(sk, 0, arguments.interface, &link);
  if(ret < 0){
    fprintf_plus(stderr, "Failed to use interface %s: %s\n",
		 arguments.interface, nl_geterror(ret));
    exitcode = EX_OSERR;
    goto end;
  }
  /* Get netlink route object */
  route = rtnl_route_alloc();
  if(route == NULL){
    fprintf_plus(stderr, "Failed to get netlink route:\n");
    exitcode = EX_OSERR;
    goto end;
  }
  /* Get address family of specified address */
  sa_family_t af = (sa_family_t)nl_addr_get_family(arguments.nl_addr);
  if(debug){
    fprintf_plus(stderr, "Address family of %s is %s (%" PRIdMAX
		 ")\n", arguments.address,
		 af == AF_INET6 ? "AF_INET6" :
		 ( af == AF_INET ? "AF_INET" : "UNKNOWN"),
		 (intmax_t)af);
  }
  /* Set route parameters: */
  rtnl_route_set_family(route, (uint8_t)af);   /* Address family */
  rtnl_route_set_protocol(route, RTPROT_BOOT); /* protocol - see
						  ip-route(8) */
  rtnl_route_set_scope(route, RT_SCOPE_LINK); /* link scope */
  rtnl_route_set_type(route, RTN_UNICAST);    /* normal unicast
						 address route */
  rtnl_route_set_dst(route, arguments.nl_addr); /* Destination
						   address */
  rtnl_route_set_table(route, RT_TABLE_MAIN); /* "main" routing
						 table */
  /* Create nexthop */
  nexthop = rtnl_route_nh_alloc();
  if(nexthop == NULL){
    fprintf_plus(stderr, "Failed to get netlink route nexthop\n");
    exitcode = EX_OSERR;
    goto end;
  }
  /* Get index number of specified interface */
  int ifindex = rtnl_link_get_ifindex(link);
  if(debug){
    fprintf_plus(stderr, "ifindex of %s is %d\n", arguments.interface,
		 ifindex);
  }
  /* Set interface index number on nexthop object */
  rtnl_route_nh_set_ifindex(nexthop, ifindex);
  /* Set route to use nexthop object */
  rtnl_route_add_nexthop(route, nexthop);
  /* Add or delete route? */
  if(arguments.add){
    ret = rtnl_route_add(sk, route, NLM_F_EXCL);
  } else {
    ret = rtnl_route_delete(sk, route, 0);
  }
  if(ret < 0){
     fprintf_plus(stderr, "Failed to %s route: %s\n",
		  arguments.add ? "add" : "delete",
		  nl_geterror(ret));
    exitcode = EX_OSERR;
    goto end;
  }
 end:
  /* Deallocate route */
  if(route){
    rtnl_route_put(route);
  } else if(nexthop) {
    /* Deallocate route nexthop */
    rtnl_route_nh_free(nexthop);
  }
  /* Deallocate parsed address */
  if(arguments.nl_addr){
    nl_addr_put(arguments.nl_addr);
  }
  /* Deallocate link struct */
  if(link){
    rtnl_link_put(link);
  }
  /* Deallocate netlink socket struct */
   if(sk){
    nl_socket_free(sk);
  }
  return exitcode;
}