diff options
-rw-r--r-- | hcid/dbus.c | 5 | ||||
-rw-r--r-- | hcid/glib-ectomy.c | 153 | ||||
-rw-r--r-- | hcid/glib-ectomy.h | 120 | ||||
-rw-r--r-- | hcid/kword.h | 2 | ||||
-rw-r--r-- | hcid/lexer.l | 10 | ||||
-rw-r--r-- | hcid/lib.c | 14 | ||||
-rw-r--r-- | hcid/lib.h | 4 | ||||
-rw-r--r-- | hcid/main.c | 23 | ||||
-rw-r--r-- | hcid/security.c | 29 |
9 files changed, 172 insertions, 188 deletions
diff --git a/hcid/dbus.c b/hcid/dbus.c index 7ef7ac73..04e7c757 100644 --- a/hcid/dbus.c +++ b/hcid/dbus.c @@ -48,7 +48,7 @@ static DBusConnection *connection; -#define TIMEOUT (30 * 1000) // 30 seconds +#define TIMEOUT (30 * 1000) /* 30 seconds */ #define SERVICE_NAME "org.bluez.PinAgent" #define INTERFACE_NAME SERVICE_NAME @@ -57,8 +57,7 @@ static DBusConnection *connection; #define WRONG_ARGS_ERROR "org.bluez.Error.WrongArgs" -struct pin_request -{ +struct pin_request { int dev; bdaddr_t bda; }; diff --git a/hcid/glib-ectomy.c b/hcid/glib-ectomy.c index 8489cd73..bea7f47c 100644 --- a/hcid/glib-ectomy.c +++ b/hcid/glib-ectomy.c @@ -1,75 +1,81 @@ +#ifdef HAVE_CONFIG_H +#include <config.h> +#endif + #include <stdio.h> -#include <string.h> -#include <stdlib.h> -#include <unistd.h> #include <errno.h> #include <fcntl.h> +#include <unistd.h> +#include <stdlib.h> +#include <malloc.h> +#include <string.h> #include <limits.h> #include "glib-ectomy.h" -GIOError g_io_channel_read (GIOChannel *channel, - gchar *buf, - gsize count, - gsize *bytes_read) +GIOError g_io_channel_read(GIOChannel *channel, gchar *buf, gsize count, gsize *bytes_read) { int fd = channel->fd; gssize result; - - if (count > SSIZE_MAX) /* At least according to the Debian manpage for read */ - count = SSIZE_MAX; - - retry: - result = read (fd, buf, count); - - if (result < 0) - { - *bytes_read = 0; - - switch (errno) - { + + /* At least according to the Debian manpage for read */ + if (count > SSIZE_MAX) + count = SSIZE_MAX; + +retry: + result = read (fd, buf, count); + + if (result < 0) { + *bytes_read = 0; + + switch (errno) { #ifdef EINTR - case EINTR: - goto retry; + case EINTR: + goto retry; #endif #ifdef EAGAIN - case EAGAIN: - return G_IO_STATUS_AGAIN; + case EAGAIN: + return G_IO_STATUS_AGAIN; #endif - default: - return G_IO_STATUS_ERROR; - } - } + default: + return G_IO_STATUS_ERROR; + } + } - *bytes_read = result; + *bytes_read = result; - return (result > 0) ? G_IO_STATUS_NORMAL : G_IO_STATUS_EOF; + return (result > 0) ? G_IO_STATUS_NORMAL : G_IO_STATUS_EOF; } -void g_io_channel_close (GIOChannel *channel) +void g_io_channel_close(GIOChannel *channel) { - int fd = channel->fd; + if (!channel) + return; - close(fd); + close(channel->fd); memset(channel, 0, sizeof(channel)); free(channel); } -GIOChannel* g_io_channel_unix_new (int fd) +GIOChannel *g_io_channel_unix_new(int fd) { - GIOChannel *channel = malloc(sizeof(GIOChannel)); + GIOChannel *channel; + + channel = malloc(sizeof(GIOChannel)); + if (!channel) + return NULL; + channel->fd = fd; + return channel; } -gint g_io_channel_unix_get_fd (GIOChannel *channel) +gint g_io_channel_unix_get_fd(GIOChannel *channel) { return channel->fd; } - - struct watch { guint id; GIOChannel *channel; @@ -82,25 +88,19 @@ struct watch { static struct watch watch_head = { .id = 0, .next = 0 }; -void g_io_remove_watch (guint id) +void g_io_remove_watch(guint id) { - struct watch *w, *p; - - for (p = &watch_head, w = watch_head.next; w; w = w->next) - { - if (w->id == id) - { - p->next = w->next; - free (w); - return; - } - } + struct watch *w, *p; + + for (p = &watch_head, w = watch_head.next; w; w = w->next) + if (w->id == id) { + p->next = w->next; + free (w); + return; + } } -guint g_io_add_watch (GIOChannel *channel, - GIOCondition condition, - GIOFunc func, - gpointer user_data) +guint g_io_add_watch(GIOChannel *channel, GIOCondition condition, GIOFunc func, gpointer user_data) { struct watch *watch = malloc(sizeof(struct watch)); @@ -116,20 +116,27 @@ guint g_io_add_watch (GIOChannel *channel, return watch->id; } -GMainLoop *g_main_loop_new (GMainContext *context, - gboolean is_running) +GMainLoop *g_main_loop_new(GMainContext *context, gboolean is_running) { - GMainLoop *ml = malloc(sizeof(GMainLoop)); + GMainLoop *ml; + + ml = malloc(sizeof(GMainLoop)); + if (!ml) + return NULL; ml->bail = 0; return ml; } -void g_main_loop_run (GMainLoop *loop) +void g_main_loop_run(GMainLoop *loop) { int open_max = sysconf(_SC_OPEN_MAX); - struct pollfd *ufds = malloc(open_max * sizeof(struct pollfd)); + struct pollfd *ufds; + + ufds = malloc(open_max * sizeof(struct pollfd)); + if (!ufds) + return; while (!loop->bail) { int nfds, rc, i; @@ -142,43 +149,37 @@ void g_main_loop_run (GMainLoop *loop) ufds[nfds].revents = 0; nfds++; } - - rc = poll(ufds, nfds, -1); - if (rc < 0 && (errno == EINTR)) + rc = poll(ufds, nfds, -1); + if (rc < 0) continue; - if (rc < 0) { - perror("poll"); - continue; - } - p = &watch_head; w = watch_head.next; i = 0; + while (w) { if (ufds[i].revents) { gboolean keep = w->func(w->channel, ufds[i].revents, w->user_data); if (!keep) { - p->next = w->next; - memset(w, 0, sizeof(*w)); - w = p->next; - i++; - continue; + p->next = w->next; + memset(w, 0, sizeof(*w)); + w = p->next; + i++; + continue; } - } - + } + p = w; w = w->next; i++; } - } free(ufds); } -void g_main_loop_quit (GMainLoop *loop) +void g_main_loop_quit(GMainLoop *loop) { loop->bail = 1; } diff --git a/hcid/glib-ectomy.h b/hcid/glib-ectomy.h index 703ae54b..c507e3c7 100644 --- a/hcid/glib-ectomy.h +++ b/hcid/glib-ectomy.h @@ -1,34 +1,33 @@ -#ifndef _GLIB_ECTOMY_H_ -#define _GLIB_ECTOMY_H_ +#ifndef __GLIB_ECTOMY_H +#define __GLIB_ECTOMY_H #include <stdlib.h> #include <sys/poll.h> -typedef char gchar; -typedef short gshort; -typedef long glong; -typedef int gint; -typedef gint gboolean; +typedef char gchar; +typedef short gshort; +typedef long glong; +typedef int gint; +typedef gint gboolean; -typedef unsigned char guchar; -typedef unsigned short gushort; -typedef unsigned long gulong; -typedef unsigned int guint; +typedef unsigned char guchar; +typedef unsigned short gushort; +typedef unsigned long gulong; +typedef unsigned int guint; -typedef float gfloat; -typedef double gdouble; +typedef float gfloat; +typedef double gdouble; -typedef void* gpointer; -typedef const void *gconstpointer; +typedef void * gpointer; +typedef const void * gconstpointer; -typedef size_t gsize; -typedef ssize_t gssize; +typedef size_t gsize; +typedef ssize_t gssize; #ifndef SSIZE_MAX #define SSIZE_MAX INT_MAX #endif - typedef struct _GIOChannel { int fd; } GIOChannel; @@ -41,68 +40,53 @@ typedef struct _GMainLoop { int bail; } GMainLoop; -typedef enum -{ - G_IO_ERROR_NONE, - G_IO_ERROR_AGAIN, - G_IO_ERROR_INVAL, - G_IO_ERROR_UNKNOWN +typedef enum { + G_IO_ERROR_NONE, + G_IO_ERROR_AGAIN, + G_IO_ERROR_INVAL, + G_IO_ERROR_UNKNOWN } GIOError; -typedef enum -{ - G_IO_STATUS_ERROR = -1, - G_IO_STATUS_NORMAL = 0, - G_IO_STATUS_EOF = 1, - G_IO_STATUS_AGAIN = 2 +typedef enum { + G_IO_STATUS_ERROR = -1, + G_IO_STATUS_NORMAL = 0, + G_IO_STATUS_EOF = 1, + G_IO_STATUS_AGAIN = 2 } GIOStatus; -#ifndef FALSE -#define FALSE (0) +#ifndef FALSE +#define FALSE (0) #endif -#ifndef TRUE -#define TRUE (!FALSE) +#ifndef TRUE +#define TRUE (!FALSE) #endif - -typedef enum -{ - G_IO_IN = POLLIN, - G_IO_OUT = POLLOUT, - G_IO_PRI = POLLPRI, - G_IO_ERR = POLLERR, - G_IO_HUP = POLLHUP, - G_IO_NVAL = POLLNVAL +typedef enum { + G_IO_IN = POLLIN, + G_IO_OUT = POLLOUT, + G_IO_PRI = POLLPRI, + G_IO_ERR = POLLERR, + G_IO_HUP = POLLHUP, + G_IO_NVAL = POLLNVAL } GIOCondition; -typedef gboolean (*GIOFunc) (GIOChannel *source, - GIOCondition condition, - gpointer data); - -GIOError g_io_channel_read (GIOChannel *channel, - gchar *buf, - gsize count, - gsize *bytes_read); -void g_io_channel_close (GIOChannel *channel); +typedef gboolean (*GIOFunc) (GIOChannel *source, GIOCondition condition, gpointer data); -GIOChannel* g_io_channel_unix_new (int fd); -gint g_io_channel_unix_get_fd (GIOChannel *channel); -guint g_io_add_watch (GIOChannel *channel, - GIOCondition condition, - GIOFunc func, - gpointer user_data); -void g_io_remove_watch (guint id); +GIOError g_io_channel_read(GIOChannel *channel, gchar *buf, gsize count, gsize *bytes_read); +void g_io_channel_close(GIOChannel *channel); +GIOChannel *g_io_channel_unix_new(int fd); +gint g_io_channel_unix_get_fd(GIOChannel *channel); +guint g_io_add_watch(GIOChannel *channel, GIOCondition condition, GIOFunc func, gpointer user_data); +void g_io_remove_watch(guint id); +GMainLoop *g_main_loop_new(GMainContext *context, gboolean is_running); +void g_main_loop_run(GMainLoop *loop); +void g_main_loop_quit(GMainLoop *loop); -GMainLoop *g_main_loop_new (GMainContext *context, - gboolean is_running); -void g_main_loop_run (GMainLoop *loop); -void g_main_loop_quit (GMainLoop *loop); +#define g_main_new(is_running) g_main_loop_new(NULL, is_running); +#define g_main_run(loop) g_main_loop_run(loop) +#define g_main_quit(loop) g_main_loop_quit(loop) -#define g_main_new(is_running) g_main_loop_new (NULL, is_running); -#define g_main_run(loop) g_main_loop_run(loop) -#define g_main_quit(loop) g_main_loop_quit(loop) - -#endif +#endif /* __GLIB_ECTOMY_H */ diff --git a/hcid/kword.h b/hcid/kword.h index 88ceb8d1..2f6cae4a 100644 --- a/hcid/kword.h +++ b/hcid/kword.h @@ -34,7 +34,7 @@ struct kword { }; extern int lineno; -extern struct kword cfg_keyword[]; +extern struct kword cfg_keyword[]; extern struct kword sec_param[]; extern struct kword pair_param[]; diff --git a/hcid/lexer.l b/hcid/lexer.l index 7f10ffda..1031ea51 100644 --- a/hcid/lexer.l +++ b/hcid/lexer.l @@ -107,7 +107,7 @@ bdaddr {hextuple}:{hextuple}:{hextuple}:{hextuple}:{hextuple}:{hextuple} {kword} { int kw = find_keyword(cfg_keyword, yytext); - if( kw != -1 ) + if( kw != -1 ) return kw; yylval.str = yytext; @@ -120,13 +120,13 @@ bdaddr {hextuple}:{hextuple}:{hextuple}:{hextuple}:{hextuple}:{hextuple} } {string} { - if(yyleng > sizeof(str_buf)-1){ + if(yyleng > sizeof(str_buf) - 1){ yyerror("string too long"); return 0; } - strncpy(str_buf, yytext+1, yyleng-2); - str_buf[yyleng-2] = '\0'; + strncpy(str_buf, yytext + 1, yyleng - 2); + str_buf[yyleng - 2] = '\0'; yylval.str = str_buf; return STRING; @@ -148,7 +148,7 @@ bdaddr {hextuple}:{hextuple}:{hextuple}:{hextuple}:{hextuple}:{hextuple} %% -int yywrap(void) +int yywrap(void) { return 1; } @@ -51,7 +51,7 @@ volatile sig_atomic_t __io_canceled; /* * Device name expansion - * %d - device id + * %d - device id */ char *expand_name(char *dst, int size, char *str, int dev_id) { @@ -76,7 +76,7 @@ char *expand_name(char *dst, int size, char *str, int dev_id) case 'h': opt = hcid.host_name; break; - + case '%': dst[np++] = str[sp++]; /* fall through */ @@ -115,14 +115,14 @@ char *get_host_name(void) if (!gethostname(name, sizeof(name)-1)) { name[sizeof(name)-1] = 0; return strdup(name); - } + } return strdup("noname"); } /* Functions to manipulate program title */ extern char **environ; char *title_start; /* start of the proc title space */ -char *title_end; /* end of the proc title space */ +char *title_end; /* end of the proc title space */ int title_size; void init_title(int argc, char *argv[], char *envp[], const char *name) @@ -152,11 +152,11 @@ void init_title(int argc, char *argv[], char *envp[], const char *name) * Determine how much space we can use for set_title. * Use all contiguous argv and envp pointers starting at argv[0] */ - for (i=0; i<argc; i++) + for (i =0; i < argc; i++) if (!i || title_end == argv[i]) title_end = argv[i] + strlen(argv[i]) + 1; - for (i=0; envp[i]; i++) + for (i = 0; envp[i]; i++) if (title_end == envp[i]) title_end = envp[i] + strlen(envp[i]) + 1; @@ -170,7 +170,7 @@ void set_title(const char *fmt, ...) char buf[255]; va_list ap; - memset(title_start,0,title_size); + memset(title_start, 0, title_size); /* print the argument string */ va_start(ap, fmt); @@ -53,7 +53,7 @@ static inline void io_cancel(void) /* Read exactly len bytes (Signal safe)*/ static inline int read_n(int fd, void *buf, int len) { - register int t = 0, w; + register int w, t = 0; while (!__io_canceled && len > 0) { if ((w = read(fd, buf, len)) < 0) { @@ -74,7 +74,7 @@ static inline int read_n(int fd, void *buf, int len) /* Write exactly len bytes (Signal safe)*/ static inline int write_n(int fd, void *buf, int len) { - register int t = 0, w; + register int w, t = 0; while (!__io_canceled && len > 0) { if ((w = write(fd, buf, len)) < 0) { diff --git a/hcid/main.c b/hcid/main.c index fbc85a5c..abd7f14c 100644 --- a/hcid/main.c +++ b/hcid/main.c @@ -87,7 +87,7 @@ struct device_opts *alloc_device_opts(char *ref) device = malloc(sizeof(struct device_list)); if (!device) { - syslog(LOG_INFO, "Can't allocate devlist opts buffer. %s(%d)", + syslog(LOG_INFO, "Can't allocate devlist opts buffer. %s(%d)", strerror(errno), errno); exit(1); } @@ -190,7 +190,7 @@ static void configure_device(int hdev) /* Set scan mode */ dr.dev_opt = device_opts->scan; if (ioctl(s, HCISETSCAN, (unsigned long) &dr) < 0) { - syslog(LOG_ERR, "Can't set scan mode on hci%d. %s(%d)\n", + syslog(LOG_ERR, "Can't set scan mode on hci%d. %s(%d)\n", hdev, strerror(errno), errno); } @@ -201,7 +201,7 @@ static void configure_device(int hdev) dr.dev_opt = AUTH_DISABLED; if (ioctl(s, HCISETAUTH, (unsigned long) &dr) < 0) { - syslog(LOG_ERR, "Can't set auth on hci%d. %s(%d)\n", + syslog(LOG_ERR, "Can't set auth on hci%d. %s(%d)\n", hdev, strerror(errno), errno); } @@ -212,7 +212,7 @@ static void configure_device(int hdev) dr.dev_opt = ENCRYPT_DISABLED; if (ioctl(s, HCISETENCRYPT, (unsigned long) &dr) < 0) { - syslog(LOG_ERR, "Can't set encrypt on hci%d. %s(%d)\n", + syslog(LOG_ERR, "Can't set encrypt on hci%d. %s(%d)\n", hdev, strerror(errno), errno); } @@ -250,7 +250,7 @@ static void init_device(int hdev) case 0: break; case -1: - syslog(LOG_ERR, "Fork failed. Can't init device hci%d. %s(%d)\n", + syslog(LOG_ERR, "Fork failed. Can't init device hci%d. %s(%d)\n", hdev, strerror(errno), errno); default: return; @@ -259,7 +259,8 @@ static void init_device(int hdev) set_title("hci%d init", hdev); if ((s = hci_open_dev(hdev)) < 0) { - syslog(LOG_ERR, "Can't open device hci%d. %s(%d)\n", hdev, strerror(errno), errno); + syslog(LOG_ERR, "Can't open device hci%d. %s(%d)\n", + hdev, strerror(errno), errno); exit(1); } @@ -277,7 +278,7 @@ static void init_device(int hdev) if (device_opts->pkt_type) { dr.dev_opt = device_opts->pkt_type; if (ioctl(s, HCISETPTYPE, (unsigned long) &dr) < 0) { - syslog(LOG_ERR, "Can't set packet type on hci%d. %s(%d)\n", + syslog(LOG_ERR, "Can't set packet type on hci%d. %s(%d)\n", hdev, strerror(errno), errno); } } @@ -286,7 +287,7 @@ static void init_device(int hdev) if (device_opts->link_mode) { dr.dev_opt = device_opts->link_mode; if (ioctl(s, HCISETLINKMODE, (unsigned long) &dr) < 0) { - syslog(LOG_ERR, "Can't set link mode on hci%d. %s(%d)\n", + syslog(LOG_ERR, "Can't set link mode on hci%d. %s(%d)\n", hdev, strerror(errno), errno); } } @@ -295,7 +296,7 @@ static void init_device(int hdev) if (device_opts->link_policy) { dr.dev_opt = device_opts->link_policy; if (ioctl(s, HCISETLINKPOL, (unsigned long) &dr) < 0) { - syslog(LOG_ERR, "Can't set link policy on hci%d. %s(%d)\n", + syslog(LOG_ERR, "Can't set link policy on hci%d. %s(%d)\n", hdev, strerror(errno), errno); } } @@ -420,7 +421,7 @@ gboolean io_stack_event(GIOChannel *chan, GIOCondition cond, gpointer data) if (err == G_IO_ERROR_AGAIN) return TRUE; - syslog(LOG_ERR, "Read from control socket failed. %s(%d)", + syslog(LOG_ERR, "Read from control socket failed. %s(%d)", strerror(errno), errno); g_main_quit(event_loop); return FALSE; @@ -471,7 +472,7 @@ int main(int argc, char *argv[], char *env[]) hcid.key_file = strdup(HCID_KEY_FILE); init_defaults(); - + while ((opt = getopt(argc, argv, "f:n")) != EOF) { switch (opt) { case 'n': diff --git a/hcid/security.c b/hcid/security.c index fecafb93..7eb26258 100644 --- a/hcid/security.c +++ b/hcid/security.c @@ -81,7 +81,7 @@ static struct link_key *__get_link_key(int f, bdaddr_t *sba, bdaddr_t *dba) static struct link_key k; struct link_key *key = NULL; int r; - + while ((r = read_n(f, &k, sizeof(k)))) { if (r < 0) { syslog(LOG_ERR, "Link key database read failed. %s(%d)", @@ -191,7 +191,7 @@ static void link_key_notify(int dev, bdaddr_t *sba, void *ptr) bacpy(&key.dba, dba); key.type = evt->key_type; key.time = time(0); - + save_link_key(&key); } @@ -245,7 +245,7 @@ static void call_pin_helper(int dev, struct hci_conn_info *ci) case 0: break; case -1: - syslog(LOG_ERR, "Can't fork PIN helper. %s(%d)", + syslog(LOG_ERR, "Can't fork PIN helper. %s(%d)", strerror(errno), errno); default: return; @@ -261,9 +261,8 @@ static void call_pin_helper(int dev, struct hci_conn_info *ci) //hci_remote_name(dev, &ci->bdaddr, sizeof(name), name, 0); ba2str(&ci->bdaddr, addr); - sprintf(str, "%s %s %s \'%s\'", hcid.pin_helper, - ci->out ? "out" : "in", - addr, name); + sprintf(str, "%s %s %s \'%s\'", hcid.pin_helper, + ci->out ? "out" : "in", addr, name); setenv("PATH", "/bin:/usr/bin:/usr/local/bin", 1); @@ -276,7 +275,7 @@ static void call_pin_helper(int dev, struct hci_conn_info *ci) if (!pipe) { syslog(LOG_ERR, "Can't exec PIN helper. %s(%d)", strerror(errno), errno); goto reject; - } + } pin = fgets(str, sizeof(str), pipe); ret = pclose(pipe); @@ -291,7 +290,7 @@ static void call_pin_helper(int dev, struct hci_conn_info *ci) pin += 4; len = strlen(pin); - + memset(&pr, 0, sizeof(pr)); bacpy(&pr.bdaddr, &ci->bdaddr); memcpy(pr.pin_code, pin, len); @@ -364,14 +363,14 @@ static void pin_code_request(int dev, bdaddr_t *sba, bdaddr_t *dba) PIN_CODE_REPLY_CP_SIZE, &pr); } else { /* Outgoing connection */ - + /* Let PIN helper handle that */ request_pin(dev, ci); } } else { /* Let PIN helper handle that */ request_pin(dev, ci); - } + } free(cr); return; @@ -395,7 +394,7 @@ gboolean io_security_event(GIOChannel *chan, GIOCondition cond, gpointer data) free(data); return FALSE; } - + if ((err = g_io_channel_read(chan, buf, sizeof(buf), &len))) { if (err == G_IO_ERROR_AGAIN) return TRUE; @@ -469,20 +468,20 @@ void start_security_manager(int hdev) di = malloc(sizeof(*di)); if (!di) { - syslog(LOG_ERR, "Can't allocate device info buffer. %s(%d)", + syslog(LOG_ERR, "Can't allocate device info buffer. %s(%d)", strerror(errno), errno); close(dev); return; } - + di->dev_id = hdev; if (ioctl(dev, HCIGETDEVINFO, (void *)di)) { - syslog(LOG_ERR, "Can't get device info. %s(%d)", + syslog(LOG_ERR, "Can't get device info. %s(%d)", strerror(errno), errno); close(dev); return; } - + chan = g_io_channel_unix_new(dev); g_io_add_watch(chan, G_IO_IN | G_IO_NVAL | G_IO_HUP | G_IO_ERR, io_security_event, (void *) di); |