1114879Sjulian%{ 2330449Seadler/*- 3114879Sjulian * parser.y 4114879Sjulian * 5330449Seadler * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 6330449Seadler * 7114879Sjulian * Copyright (c) 2001-2002 Maksim Yevmenkin <m_evmenkin@yahoo.com> 8114879Sjulian * All rights reserved. 9114879Sjulian * 10114879Sjulian * Redistribution and use in source and binary forms, with or without 11114879Sjulian * modification, are permitted provided that the following conditions 12114879Sjulian * are met: 13114879Sjulian * 1. Redistributions of source code must retain the above copyright 14114879Sjulian * notice, this list of conditions and the following disclaimer. 15114879Sjulian * 2. Redistributions in binary form must reproduce the above copyright 16114879Sjulian * notice, this list of conditions and the following disclaimer in the 17114879Sjulian * documentation and/or other materials provided with the distribution. 18114879Sjulian * 19114879Sjulian * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 20114879Sjulian * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21114879Sjulian * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22114879Sjulian * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 23114879Sjulian * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24114879Sjulian * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25114879Sjulian * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26114879Sjulian * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27114879Sjulian * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28114879Sjulian * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29114879Sjulian * SUCH DAMAGE. 30114879Sjulian * 31121054Semax * $Id: parser.y,v 1.5 2003/06/07 21:22:30 max Exp $ 32114879Sjulian * $FreeBSD: stable/11/usr.sbin/bluetooth/hcsecd/parser.y 330449 2018-03-05 07:26:05Z eadler $ 33114879Sjulian */ 34114879Sjulian 35121054Semax#include <sys/fcntl.h> 36114879Sjulian#include <sys/queue.h> 37281210Stakawata#define L2CAP_SOCKET_CHECKED 38121054Semax#include <bluetooth.h> 39114879Sjulian#include <errno.h> 40121054Semax#include <limits.h> 41114879Sjulian#include <stdio.h> 42235789Sbapt#include <stdlib.h> 43114879Sjulian#include <stdarg.h> 44114879Sjulian#include <string.h> 45114879Sjulian#include <syslog.h> 46135245Semax#include <unistd.h> 47114879Sjulian#include "hcsecd.h" 48114879Sjulian 49114879Sjulian int yyparse (void); 50114879Sjulian int yylex (void); 51114879Sjulian 52114879Sjulianstatic void free_key (link_key_p key); 53114879Sjulianstatic int hexa2int4(char *a); 54114879Sjulianstatic int hexa2int8(char *a); 55114879Sjulian 56114879Sjulianextern int yylineno; 57114879Sjulianstatic LIST_HEAD(, link_key) link_keys; 58121054Semax char *config_file = "/etc/bluetooth/hcsecd.conf"; 59114879Sjulian 60114879Sjulianstatic link_key_p key = NULL; 61114879Sjulian%} 62114879Sjulian 63114879Sjulian%union { 64114879Sjulian char *string; 65114879Sjulian} 66114879Sjulian 67114879Sjulian%token <string> T_BDADDRSTRING T_HEXSTRING T_STRING 68114879Sjulian%token T_DEVICE T_BDADDR T_NAME T_KEY T_PIN T_NOKEY T_NOPIN T_JUNK 69114879Sjulian 70114879Sjulian%% 71114879Sjulian 72114879Sjulianconfig: line 73114879Sjulian | config line 74114879Sjulian ; 75114879Sjulian 76114879Sjulianline: T_DEVICE 77114879Sjulian { 78114879Sjulian key = (link_key_p) malloc(sizeof(*key)); 79114879Sjulian if (key == NULL) { 80114879Sjulian syslog(LOG_ERR, "Could not allocate new " \ 81114879Sjulian "config entry"); 82114879Sjulian exit(1); 83114879Sjulian } 84114879Sjulian 85114879Sjulian memset(key, 0, sizeof(*key)); 86114879Sjulian } 87114879Sjulian '{' options '}' 88114879Sjulian { 89114879Sjulian if (get_key(&key->bdaddr, 1) != NULL) { 90114879Sjulian syslog(LOG_ERR, "Ignoring duplicated entry " \ 91121054Semax "for bdaddr %s", 92121054Semax bt_ntoa(&key->bdaddr, NULL)); 93114879Sjulian free_key(key); 94114879Sjulian } else 95114879Sjulian LIST_INSERT_HEAD(&link_keys, key, next); 96114879Sjulian 97114879Sjulian key = NULL; 98114879Sjulian } 99114879Sjulian ; 100114879Sjulian 101114879Sjulianoptions: option ';' 102114879Sjulian | options option ';' 103114879Sjulian ; 104114879Sjulian 105114879Sjulianoption: bdaddr 106114879Sjulian | name 107114879Sjulian | key 108114879Sjulian | pin 109114879Sjulian ; 110114879Sjulian 111114879Sjulianbdaddr: T_BDADDR T_BDADDRSTRING 112114879Sjulian { 113121054Semax if (!bt_aton($2, &key->bdaddr)) { 114133178Semax syslog(LOG_ERR, "Cound not parse BD_ADDR " \ 115114879Sjulian "'%s'", $2); 116114879Sjulian exit(1); 117114879Sjulian } 118114879Sjulian } 119114879Sjulian ; 120114879Sjulian 121114879Sjulianname: T_NAME T_STRING 122114879Sjulian { 123114879Sjulian if (key->name != NULL) 124114879Sjulian free(key->name); 125114879Sjulian 126114879Sjulian key->name = strdup($2); 127114879Sjulian if (key->name == NULL) { 128114879Sjulian syslog(LOG_ERR, "Could not allocate new " \ 129114879Sjulian "device name"); 130114879Sjulian exit(1); 131114879Sjulian } 132114879Sjulian } 133114879Sjulian ; 134114879Sjulian 135114879Sjuliankey: T_KEY T_HEXSTRING 136114879Sjulian { 137114879Sjulian int i, len; 138114879Sjulian 139114879Sjulian if (key->key != NULL) 140114879Sjulian free(key->key); 141114879Sjulian 142133178Semax key->key = (uint8_t *) malloc(NG_HCI_KEY_SIZE); 143114879Sjulian if (key->key == NULL) { 144114879Sjulian syslog(LOG_ERR, "Could not allocate new " \ 145114879Sjulian "link key"); 146114879Sjulian exit(1); 147114879Sjulian } 148114879Sjulian 149114879Sjulian memset(key->key, 0, NG_HCI_KEY_SIZE); 150114879Sjulian 151114879Sjulian len = strlen($2) / 2; 152114879Sjulian if (len > NG_HCI_KEY_SIZE) 153114879Sjulian len = NG_HCI_KEY_SIZE; 154114879Sjulian 155114879Sjulian for (i = 0; i < len; i ++) 156114879Sjulian key->key[i] = hexa2int8((char *)($2) + 2*i); 157114879Sjulian } 158114879Sjulian | T_KEY T_NOKEY 159114879Sjulian { 160114879Sjulian if (key->key != NULL) 161114879Sjulian free(key->key); 162114879Sjulian 163114879Sjulian key->key = NULL; 164114879Sjulian } 165114879Sjulian ; 166114879Sjulian 167114879Sjulianpin: T_PIN T_STRING 168114879Sjulian { 169114879Sjulian if (key->pin != NULL) 170114879Sjulian free(key->pin); 171114879Sjulian 172114879Sjulian key->pin = strdup($2); 173114879Sjulian if (key->pin == NULL) { 174114879Sjulian syslog(LOG_ERR, "Could not allocate new " \ 175114879Sjulian "PIN code"); 176114879Sjulian exit(1); 177114879Sjulian } 178114879Sjulian } 179114879Sjulian | T_PIN T_NOPIN 180114879Sjulian { 181114879Sjulian if (key->pin != NULL) 182114879Sjulian free(key->pin); 183114879Sjulian 184114879Sjulian key->pin = NULL; 185114879Sjulian } 186114879Sjulian ; 187114879Sjulian 188114879Sjulian%% 189114879Sjulian 190114879Sjulian/* Display parser error message */ 191114879Sjulianvoid 192114879Sjulianyyerror(char const *message) 193114879Sjulian{ 194114879Sjulian syslog(LOG_ERR, "%s in line %d", message, yylineno); 195114879Sjulian} 196114879Sjulian 197114879Sjulian/* Re-read config file */ 198114879Sjulianvoid 199121054Semaxread_config_file(void) 200114879Sjulian{ 201114879Sjulian extern FILE *yyin; 202114879Sjulian 203114879Sjulian if (config_file == NULL) { 204114879Sjulian syslog(LOG_ERR, "Unknown config file name!"); 205114879Sjulian exit(1); 206114879Sjulian } 207114879Sjulian 208114879Sjulian if ((yyin = fopen(config_file, "r")) == NULL) { 209114879Sjulian syslog(LOG_ERR, "Could not open config file '%s'. %s (%d)", 210114879Sjulian config_file, strerror(errno), errno); 211114879Sjulian exit(1); 212114879Sjulian } 213114879Sjulian 214114879Sjulian clean_config(); 215114879Sjulian if (yyparse() < 0) { 216114879Sjulian syslog(LOG_ERR, "Could not parse config file '%s'",config_file); 217114879Sjulian exit(1); 218114879Sjulian } 219114879Sjulian 220114879Sjulian fclose(yyin); 221114879Sjulian yyin = NULL; 222114879Sjulian 223114879Sjulian#if __config_debug__ 224114879Sjulian dump_config(); 225114879Sjulian#endif 226114879Sjulian} 227114879Sjulian 228114879Sjulian/* Clean config */ 229114879Sjulianvoid 230114879Sjulianclean_config(void) 231114879Sjulian{ 232114879Sjulian link_key_p key = NULL; 233114879Sjulian 234114879Sjulian while ((key = LIST_FIRST(&link_keys)) != NULL) { 235114879Sjulian LIST_REMOVE(key, next); 236114879Sjulian free_key(key); 237114879Sjulian } 238114879Sjulian} 239114879Sjulian 240114879Sjulian/* Find link key entry in the list. Return exact or default match */ 241114879Sjulianlink_key_p 242114879Sjulianget_key(bdaddr_p bdaddr, int exact_match) 243114879Sjulian{ 244114879Sjulian link_key_p key = NULL, defkey = NULL; 245114879Sjulian 246114879Sjulian LIST_FOREACH(key, &link_keys, next) { 247114879Sjulian if (memcmp(bdaddr, &key->bdaddr, sizeof(key->bdaddr)) == 0) 248114879Sjulian break; 249114879Sjulian 250114879Sjulian if (!exact_match) 251114879Sjulian if (memcmp(NG_HCI_BDADDR_ANY, &key->bdaddr, 252114879Sjulian sizeof(key->bdaddr)) == 0) 253114879Sjulian defkey = key; 254114879Sjulian } 255114879Sjulian 256114879Sjulian return ((key != NULL)? key : defkey); 257114879Sjulian} 258114879Sjulian 259114879Sjulian#if __config_debug__ 260114879Sjulian/* Dump config */ 261114879Sjulianvoid 262114879Sjuliandump_config(void) 263114879Sjulian{ 264114879Sjulian link_key_p key = NULL; 265114879Sjulian char buffer[64]; 266114879Sjulian 267114879Sjulian LIST_FOREACH(key, &link_keys, next) { 268114879Sjulian if (key->key != NULL) 269114879Sjulian snprintf(buffer, sizeof(buffer), 270114879Sjulian"0x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x", 271114879Sjulian key->key[0], key->key[1], key->key[2], 272114879Sjulian key->key[3], key->key[4], key->key[5], 273114879Sjulian key->key[6], key->key[7], key->key[8], 274114879Sjulian key->key[9], key->key[10], key->key[11], 275114879Sjulian key->key[12], key->key[13], key->key[14], 276114879Sjulian key->key[15]); 277114879Sjulian 278114879Sjulian syslog(LOG_DEBUG, 279114879Sjulian"device %s " \ 280121054Semax"bdaddr %s " \ 281114879Sjulian"pin %s " \ 282114879Sjulian"key %s", 283114879Sjulian (key->name != NULL)? key->name : "noname", 284121054Semax bt_ntoa(&key->bdaddr, NULL), 285114879Sjulian (key->pin != NULL)? key->pin : "nopin", 286114879Sjulian (key->key != NULL)? buffer : "nokey"); 287114879Sjulian } 288114879Sjulian} 289114879Sjulian#endif 290114879Sjulian 291121054Semax/* Read keys file */ 292121054Semaxint 293121054Semaxread_keys_file(void) 294121054Semax{ 295121054Semax FILE *f = NULL; 296121054Semax link_key_t *key = NULL; 297121054Semax char buf[HCSECD_BUFFER_SIZE], *p = NULL, *cp = NULL; 298121054Semax bdaddr_t bdaddr; 299121054Semax int i, len; 300121054Semax 301121054Semax if ((f = fopen(HCSECD_KEYSFILE, "r")) == NULL) { 302121054Semax if (errno == ENOENT) 303121054Semax return (0); 304121054Semax 305121054Semax syslog(LOG_ERR, "Could not open keys file %s. %s (%d)\n", 306121054Semax HCSECD_KEYSFILE, strerror(errno), errno); 307121054Semax 308121054Semax return (-1); 309121054Semax } 310121054Semax 311121054Semax while ((p = fgets(buf, sizeof(buf), f)) != NULL) { 312121054Semax if (*p == '#') 313121054Semax continue; 314121054Semax if ((cp = strpbrk(p, " ")) == NULL) 315121054Semax continue; 316121054Semax 317121054Semax *cp++ = '\0'; 318121054Semax 319121054Semax if (!bt_aton(p, &bdaddr)) 320121054Semax continue; 321121054Semax 322121054Semax if ((key = get_key(&bdaddr, 1)) == NULL) 323121054Semax continue; 324121054Semax 325121054Semax if (key->key == NULL) { 326133178Semax key->key = (uint8_t *) malloc(NG_HCI_KEY_SIZE); 327121054Semax if (key->key == NULL) { 328121054Semax syslog(LOG_ERR, "Could not allocate link key"); 329121054Semax exit(1); 330121054Semax } 331121054Semax } 332121054Semax 333121054Semax memset(key->key, 0, NG_HCI_KEY_SIZE); 334121054Semax 335121054Semax len = strlen(cp) / 2; 336121054Semax if (len > NG_HCI_KEY_SIZE) 337121054Semax len = NG_HCI_KEY_SIZE; 338121054Semax 339121054Semax for (i = 0; i < len; i ++) 340121054Semax key->key[i] = hexa2int8(cp + 2*i); 341121054Semax 342121054Semax syslog(LOG_DEBUG, "Restored link key for the entry, " \ 343121054Semax "remote bdaddr %s, name '%s'", 344121054Semax bt_ntoa(&key->bdaddr, NULL), 345121054Semax (key->name != NULL)? key->name : "No name"); 346121054Semax } 347121054Semax 348121054Semax fclose(f); 349121054Semax 350121054Semax return (0); 351121054Semax} 352121054Semax 353121054Semax/* Dump keys file */ 354121054Semaxint 355121054Semaxdump_keys_file(void) 356121054Semax{ 357121054Semax link_key_p key = NULL; 358121054Semax char tmp[PATH_MAX], buf[HCSECD_BUFFER_SIZE]; 359121054Semax int f; 360121054Semax 361121054Semax snprintf(tmp, sizeof(tmp), "%s.tmp", HCSECD_KEYSFILE); 362121054Semax if ((f = open(tmp, O_RDWR|O_CREAT|O_TRUNC|O_EXCL, 0600)) < 0) { 363121054Semax syslog(LOG_ERR, "Could not create temp keys file %s. %s (%d)\n", 364121054Semax tmp, strerror(errno), errno); 365121054Semax return (-1); 366121054Semax } 367121054Semax 368121054Semax LIST_FOREACH(key, &link_keys, next) { 369121054Semax if (key->key == NULL) 370121054Semax continue; 371121054Semax 372121054Semax snprintf(buf, sizeof(buf), 373121054Semax"%s %02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x\n", 374121054Semax bt_ntoa(&key->bdaddr, NULL), 375121054Semax key->key[0], key->key[1], key->key[2], key->key[3], 376121054Semax key->key[4], key->key[5], key->key[6], key->key[7], 377121054Semax key->key[8], key->key[9], key->key[10], key->key[11], 378121054Semax key->key[12], key->key[13], key->key[14], key->key[15]); 379121054Semax 380121054Semax if (write(f, buf, strlen(buf)) < 0) { 381121054Semax syslog(LOG_ERR, "Could not write temp keys file. " \ 382121054Semax "%s (%d)\n", strerror(errno), errno); 383121054Semax break; 384121054Semax } 385121054Semax } 386121054Semax 387121054Semax close(f); 388121054Semax 389121054Semax if (rename(tmp, HCSECD_KEYSFILE) < 0) { 390121054Semax syslog(LOG_ERR, "Could not rename(%s, %s). %s (%d)\n", 391121054Semax tmp, HCSECD_KEYSFILE, strerror(errno), errno); 392121054Semax unlink(tmp); 393121054Semax return (-1); 394121054Semax } 395121054Semax 396121054Semax return (0); 397121054Semax} 398121054Semax 399114879Sjulian/* Free key entry */ 400114879Sjulianstatic void 401114879Sjulianfree_key(link_key_p key) 402114879Sjulian{ 403114879Sjulian if (key->name != NULL) 404114879Sjulian free(key->name); 405114879Sjulian if (key->key != NULL) 406114879Sjulian free(key->key); 407114879Sjulian if (key->pin != NULL) 408114879Sjulian free(key->pin); 409114879Sjulian 410114879Sjulian memset(key, 0, sizeof(*key)); 411114879Sjulian free(key); 412114879Sjulian} 413114879Sjulian 414114879Sjulian/* Convert hex ASCII to int4 */ 415114879Sjulianstatic int 416114879Sjulianhexa2int4(char *a) 417114879Sjulian{ 418114879Sjulian if ('0' <= *a && *a <= '9') 419114879Sjulian return (*a - '0'); 420114879Sjulian 421114879Sjulian if ('A' <= *a && *a <= 'F') 422114879Sjulian return (*a - 'A' + 0xa); 423114879Sjulian 424114879Sjulian if ('a' <= *a && *a <= 'f') 425114879Sjulian return (*a - 'a' + 0xa); 426114879Sjulian 427114879Sjulian syslog(LOG_ERR, "Invalid hex character: '%c' (%#x)", *a, *a); 428114879Sjulian exit(1); 429114879Sjulian} 430114879Sjulian 431114879Sjulian/* Convert hex ASCII to int8 */ 432114879Sjulianstatic int 433114879Sjulianhexa2int8(char *a) 434114879Sjulian{ 435114879Sjulian return ((hexa2int4(a) << 4) | hexa2int4(a + 1)); 436114879Sjulian} 437114879Sjulian 438