ProxyAuth
Linux PAM to authenticate device via Bluetooth device
pam_bt_pair.c
Go to the documentation of this file.
1 #include "pam_bt_pair.h"
2 
3 char *check_is_paired(GVariant *properties) {
4  char *addr = NULL;
5 
6  GVariantDict prop_dict;
7  GVariant *is_connected_gvariant;
8  GVariant *is_trusted_gvariant;
9  GVariant *addr_gvariant;
10 
11  g_variant_dict_init(&prop_dict, properties);
12 
13  if (!(is_connected_gvariant = g_variant_dict_lookup_value(&prop_dict, "Connected", G_VARIANT_TYPE("b")))) {
14  perror("No such key or Unexpected type");
15  return NULL;
16  }
17 
18  if (!(is_trusted_gvariant = g_variant_dict_lookup_value(&prop_dict, "Trusted", G_VARIANT_TYPE("b")))) {
19  perror("No such key or Unexpected type");
20  return NULL;
21  }
22 
23  if (g_variant_get_boolean(is_connected_gvariant) && g_variant_get_boolean(is_trusted_gvariant)) {
24  if (!(addr_gvariant = g_variant_dict_lookup_value(&prop_dict, "Address", G_VARIANT_TYPE("s")))) {
25  perror("No such key or Unexpected type");
26  return NULL;
27  }
28 
29  g_print("pika: %s\n", g_variant_get_string(addr_gvariant, NULL));
30 
31  if (!(addr = malloc(sizeof(char) * BT_MAC_LEN + 1))) {
32  perror("malloc");
33  return NULL;
34  }
35 
36  strncpy(addr, (char *)g_variant_get_string(addr_gvariant, NULL), BT_MAC_LEN);
37  addr[BT_MAC_LEN] = '\0';
38  }
39 
40  return addr;
41 }
42 
43 char **process_dbus_bt_list(GVariant *result, int *num_of_paired) {
44  if(!result) {
45  return NULL;
46  }
47 
48  char **paired_devices = NULL;
49  int num_of_paired_lc = 0;
50 
51  if (!(paired_devices = malloc(sizeof(char *) *BT_MAX_CONN))) {
52  perror("malloc");
53  return NULL;
54  }
55 
56  GVariantIter i;
57  const gchar *object_path;
58  GVariant *ifaces_and_properties;
59 
60  result = g_variant_get_child_value(result, 0);
61  g_variant_iter_init(&i, result);
62  while(g_variant_iter_next(&i, "{&o@a{sa{sv}}}", &object_path, &ifaces_and_properties)) {
63  const gchar *interface_name;
64  GVariant *properties;
65  GVariantIter ii;
66  g_variant_iter_init(&ii, ifaces_and_properties);
67  while(g_variant_iter_next(&ii, "{&s@a{sv}}", &interface_name, &properties)) {
68  gchar *interface_name_lower = g_ascii_strdown(interface_name, -1);
69  if(g_strstr_len(interface_name_lower, -1, "device")) {
70  char *addr;
71  if ((addr = check_is_paired(properties))) {
72  paired_devices[num_of_paired_lc] = addr;
73  num_of_paired_lc++;
74  }
75  }
76  g_free(interface_name_lower);
77  g_variant_unref(properties);
78  }
79  g_variant_unref(ifaces_and_properties);
80  }
81 
82  *num_of_paired = num_of_paired_lc;
83  return paired_devices;
84 }
85 
86 char **get_paired_devices(int *num_of_paired) {
87  GDBusConnection *conn;
88 
89  GVariant *result = NULL;
90  char **paired_devices = NULL;
91 
92  if(!(conn = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, NULL))) {
93  g_print("Not able to get connection to system bus\n");
94  return NULL;
95  }
96 
97  result = g_dbus_connection_call_sync(
98  conn, //connection
99  BLUEZ_DBUS_NAME, //bus_name
100  BLUEZ_DBUS_OBJ_PATH, //object_path
101  DBUS_INTERFACE_NAME, //interface_name
102  DBUS_METHOD_NAME, //method_name
103  NULL, //parameters
104  G_VARIANT_TYPE(DBUS_METHOD_RETURN_TYPE), //return type
105  G_DBUS_CALL_FLAGS_NONE, //flag
106  1000, //timeout_msec
107  NULL, //cancel error
108  NULL //error if parameter is not compatible with D-Bus protocol
109  );
110 
111  paired_devices = process_dbus_bt_list(result, num_of_paired);
112 
113  g_variant_unref(result);
114 
115  g_object_unref(conn);
116 
117  return paired_devices;
118 }
#define BT_MAX_CONN
Definition: pam_bt_misc.h:10
#define BT_MAC_LEN
Definition: pam_bt_misc.h:9
char ** get_paired_devices(int *num_of_paired)
Definition: pam_bt_pair.c:86
char ** process_dbus_bt_list(GVariant *result, int *num_of_paired)
Definition: pam_bt_pair.c:43
char * check_is_paired(GVariant *properties)
Definition: pam_bt_pair.c:3
#define BLUEZ_DBUS_NAME
Definition: pam_bt_pair.h:9
#define DBUS_METHOD_NAME
Definition: pam_bt_pair.h:12
#define DBUS_INTERFACE_NAME
Definition: pam_bt_pair.h:11
#define BLUEZ_DBUS_OBJ_PATH
Definition: pam_bt_pair.h:10
#define DBUS_METHOD_RETURN_TYPE
Definition: pam_bt_pair.h:13