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
|
/*
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation;
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS.
* IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) AND AUTHOR(S) BE LIABLE FOR ANY
* CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*
* ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS,
* COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS
* SOFTWARE IS DISCLAIMED.
*
*/
#define DBUS_API_SUBJECT_TO_CHANGE
#include <dbus/dbus.h>
#include <stdbool.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <getopt.h>
#include <stdarg.h>
#include <string.h>
#include <errno.h>
#include <signal.h>
#include <time.h>
char* progname = NULL;
bool verbose = false;
void syntax()
{
printf("Syntax: %s <options>\n", progname);
printf(" -u upcmd --upcmd=upcmd\n");
printf(" -d downcmd --downcmd=downcmd\n");
printf(" -b aa:bb:cc:dd:ee:ff --btid=aa:bb:cc:dd:ee:ff\n");
printf(" -v --verbose\n");
printf(" -V --version\n");
printf(" -p --protect\n");
printf(" -h --help\n");
exit(1);
}
void message(bool verb, char* msg, ...)
{
if (!verbose && verb) return;
va_list vargs;
va_start(vargs, msg);
vprintf(msg, vargs);
va_end(vargs);
}
#define assert(a, b) if(!(a)) real_assert(b)
void real_assert (char* msg)
{
if (0 != errno) {
char* errstr = strerror(errno);
message(false, "[Assert] failed with errno %d/%s: %s\n", errno, errstr, msg);
} else
message(false, "[Assert] failed: %s\n", msg);
#ifdef DEBUG
exit(1);
#endif
}
void fork_and_run(char* cmd)
{
pid_t pid;
pid = fork();
assert(0 <= pid, "Could not fork");
if (0 == pid) {
system(cmd);
exit(0);
}
}
uint32_t getConnectionUnixUser(const char* service, DBusConnection* conn)
{
DBusMessage* msg;
DBusMessageIter args;
DBusPendingCall* pending;
uint32_t uid;
char* errmsg;
if (NULL == service) return -1;
msg = dbus_message_new_method_call("org.freedesktop.DBus", // target for the method call
"/org/freedesktop/DBus", // object to call on
"org.freedesktop.DBus", // interface to call on
"GetConnectionUnixUser"); // method name
if (NULL == msg) {
message(false, "Message Null\n");
exit(1);
}
// append arguments
dbus_message_iter_init_append(msg, &args);
if (!dbus_message_iter_append_basic(&args, DBUS_TYPE_STRING, &service)) {
message(false, "Out Of Memory!\n");
exit(1);
}
// send message and get a handle for a reply
if (!dbus_connection_send_with_reply (conn, msg, &pending, -1)) { // -1 is default timeout
message(false, "Out Of Memory!\n");
exit(1);
}
if (NULL == pending) {
message(false, "Pending Call Null\n");
exit(1);
}
dbus_connection_flush(conn);
// free message
dbus_message_unref(msg);
// block until we recieve a reply
dbus_pending_call_block(pending);
// get the reply message
msg = dbus_pending_call_steal_reply(pending);
if (NULL == msg) {
message(false, "Reply Null\n");
exit(1);
}
// free the pending message handle
dbus_pending_call_unref(pending);
if (DBUS_MESSAGE_TYPE_ERROR == dbus_message_get_type(msg)) {
message(false, "Failed to check owner\n");
}
// read the parameters
else if (!dbus_message_iter_init(msg, &args))
message(false, "Message has no arguments!\n");
else if (DBUS_TYPE_UINT32 != dbus_message_iter_get_arg_type(&args))
message(false, "Argument is not uint32 (is %c)!\n", dbus_message_iter_get_arg_type(&args));
else {
dbus_message_iter_get_basic(&args, &uid);
dbus_message_unref(msg);
return uid;
}
dbus_message_unref(msg);
return -1;
}
DBusConnection* dbusConnect()
{
DBusConnection* conn;
DBusError err;
int ret;
// initialise the errors
dbus_error_init(&err);
// connect to the bus and check for errors
conn = dbus_bus_get(DBUS_BUS_SYSTEM, &err);
if (dbus_error_is_set(&err)) {
message(false, "Connection Error (%s)\n", err.message);
dbus_error_free(&err);
}
if (NULL == conn) {
exit(1);
}
// add a rule for which messages we want to see
dbus_bus_add_match(conn, "type='signal',interface='cx.ath.matthew.bluemon.ProximitySignal'", &err); // see signals from the given interface
dbus_connection_flush(conn);
if (dbus_error_is_set(&err)) {
message(false, "Match Error (%s)\n", err.message);
exit(1);
}
return conn;
}
void spurn_children()
{
struct sigaction act;
act.sa_flags = SA_NOCLDWAIT;
act.sa_handler = SIG_IGN;
sigaction(SIGCHLD, &act, NULL);
}
int main(int argc, char ** argv)
{
DBusMessage* msg;
DBusMessageIter args;
DBusConnection* conn;
char* sigvalue;
char* upcmd = NULL;
char* downcmd = NULL;
char* btid = NULL;
char* servicename = NULL;
char s[30];
time_t t;
bool protect = false;
uint32_t uid;
spurn_children();
progname = argv[0];
static struct option long_opts[] = {
{"help", 0, 0, 'h'},
{"verbose", 0, 0, 'v'},
{"version", 0, 0, 'V'},
{"upcmd", 0, 0, 'u'},
{"downcmd", 0, 0, 'd'},
{"btid", 0, 0, 'b'},
{"protect", 0, 0, 'p'},
};
int c;
int idx = 0;
while (-1 != (c = getopt_long(argc, argv, "-u:d:b:hVvp",long_opts, &idx))) {
switch (c) {
case 'u':
upcmd = optarg;
break;
case 'd':
downcmd = optarg;
break;
case 'b':
btid = optarg;
break;
case 'p':
protect = true;
break;
case 'v':
verbose = true;
break;
case 'V':
printf("Bluemon client version %s.\n", VERSION);
break;
case 'h':
case ':':
case '?':
case 1:
syntax();
break;
}
}
if (NULL == upcmd || NULL == downcmd || btid == NULL) syntax();
conn = dbusConnect();
// loop listening for signals being emmitted
while (true) {
// non blocking read of the next available message
dbus_connection_read_write(conn, -1);
while (NULL != (msg = dbus_connection_pop_message(conn))) {
message(true, "Got Message: %s.%s\n", dbus_message_get_interface(msg),
dbus_message_get_member(msg));
// check if the message is a signal from the correct interface and with the correct name
// and from the correct source on the bus
if (dbus_message_is_signal(msg, "cx.ath.matthew.bluemon.ProximitySignal", "Connect")) {
if (!protect || 0 == (uid = getConnectionUnixUser(dbus_message_get_sender(msg), conn))) {
// read the parameters
if (!dbus_message_iter_init(msg, &args))
message(true, "Message Has No Parameters\n");
else if (DBUS_TYPE_STRING != dbus_message_iter_get_arg_type(&args))
message(true, "Argument is not string!\n");
else {
dbus_message_iter_get_basic(&args, &sigvalue);
time(&t);
strftime(s, 30, "%H:%M", localtime(&t));
message(true, "[%s] Got Connect Signal for %s\n", s, sigvalue);
if (0 == strncasecmp(btid, sigvalue, strlen(btid)))
fork_and_run(upcmd);
}
}
else
message(true, "Got signal from %d, not root\n", uid);
}
else if (dbus_message_is_signal(msg, "cx.ath.matthew.bluemon.ProximitySignal", "Disconnect")) {
if (!protect || 0 == (uid = getConnectionUnixUser(dbus_message_get_sender(msg), conn))) {
// read the parameters
if (!dbus_message_iter_init(msg, &args))
message(true, "Message Has No Parameters\n");
else if (DBUS_TYPE_STRING != dbus_message_iter_get_arg_type(&args))
message(true, "Argument is not string!\n");
else {
dbus_message_iter_get_basic(&args, &sigvalue);
time(&t);
strftime(s, 30, "%H:%M", localtime(&t));
message(true, "[%s] Got Disconnect Signal for %s\n", s, sigvalue);
if (0 == strncasecmp(btid, sigvalue, strlen(btid)))
fork_and_run(downcmd);
}
}
else
message(true, "Got signal from %d, not root\n", uid);
}
else
message(true, "Spurious message from %s\n", dbus_message_get_sender(msg));
// free the message
dbus_message_unref(msg);
}
}
// close the connection
dbus_connection_close(conn);
}
|