capabilities: Some plugins don't actually require capabilities at runtime
[strongswan.git] / src / libcharon / plugins / dhcp / dhcp_plugin.c
1 /*
2 * Copyright (C) 2013 Tobias Brunner
3 * Hochschule fuer Technik Rapperswil
4 *
5 * Copyright (C) 2010 Martin Willi
6 * Copyright (C) 2010 revosec AG
7 *
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the
10 * Free Software Foundation; either version 2 of the License, or (at your
11 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
12 *
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 * for more details.
17 */
18
19 #include "dhcp_plugin.h"
20
21 #include <hydra.h>
22 #include <daemon.h>
23 #include <plugins/plugin_feature.h>
24
25 #include "dhcp_socket.h"
26 #include "dhcp_provider.h"
27
28 typedef struct private_dhcp_plugin_t private_dhcp_plugin_t;
29
30 /**
31 * private data of dhcp plugin
32 */
33 struct private_dhcp_plugin_t {
34
35 /**
36 * implements plugin interface
37 */
38 dhcp_plugin_t public;
39
40 /**
41 * DHCP communication socket
42 */
43 dhcp_socket_t *socket;
44
45 /**
46 * Attribute provider
47 */
48 dhcp_provider_t *provider;
49 };
50
51 METHOD(plugin_t, get_name, char*,
52 private_dhcp_plugin_t *this)
53 {
54 return "dhcp";
55 }
56
57 /**
58 * Register listener
59 */
60 static bool plugin_cb(private_dhcp_plugin_t *this,
61 plugin_feature_t *feature, bool reg, void *cb_data)
62 {
63 if (reg)
64 {
65 this->socket = dhcp_socket_create();
66
67 if (!this->socket)
68 {
69 return FALSE;
70 }
71 this->provider = dhcp_provider_create(this->socket);
72 hydra->attributes->add_provider(hydra->attributes,
73 &this->provider->provider);
74 }
75 else
76 {
77 hydra->attributes->remove_provider(hydra->attributes,
78 &this->provider->provider);
79 this->provider->destroy(this->provider);
80 this->socket->destroy(this->socket);
81 }
82 return TRUE;
83 }
84
85 METHOD(plugin_t, get_features, int,
86 private_dhcp_plugin_t *this, plugin_feature_t *features[])
87 {
88 static plugin_feature_t f[] = {
89 PLUGIN_CALLBACK((plugin_feature_callback_t)plugin_cb, NULL),
90 PLUGIN_PROVIDE(CUSTOM, "dhcp"),
91 PLUGIN_DEPENDS(RNG, RNG_WEAK),
92 };
93 *features = f;
94 return countof(f);
95 }
96
97 METHOD(plugin_t, destroy, void,
98 private_dhcp_plugin_t *this)
99 {
100 free(this);
101 }
102
103 /**
104 * Plugin constructor.
105 */
106 plugin_t *dhcp_plugin_create()
107 {
108 private_dhcp_plugin_t *this;
109
110 if (!lib->caps->check(lib->caps, CAP_NET_BIND_SERVICE))
111 { /* required to bind DHCP socket (port 68) */
112 DBG1(DBG_NET, "dhcp plugin requires CAP_NET_BIND_SERVICE capability");
113 return NULL;
114 }
115 else if (!lib->caps->keep(lib->caps, CAP_NET_RAW))
116 { /* required to open DHCP receive socket (AF_PACKET). according to
117 * capabilities(7) it is also required to use the socket */
118 DBG1(DBG_NET, "dhcp plugin requires CAP_NET_RAW capability");
119 return NULL;
120 }
121
122 INIT(this,
123 .public = {
124 .plugin = {
125 .get_name = _get_name,
126 .get_features = _get_features,
127 .destroy = _destroy,
128 },
129 },
130 );
131
132 return &this->public.plugin;
133 }