Beispiel #1
0
int database::get_device_id_and_user(const char GUID[SUPLA_GUID_SIZE],
                                     int *UserID) {
  if (_mysql == NULL) return false;

  MYSQL_BIND pbind[1];
  memset(pbind, 0, sizeof(pbind));

  char GUIDHEX[SUPLA_GUID_HEXSIZE];
  st_guid2hex(GUIDHEX, GUID);

  pbind[0].buffer_type = MYSQL_TYPE_STRING;
  pbind[0].buffer = (char *)GUIDHEX;
  pbind[0].buffer_length = SUPLA_GUID_SIZE * 2;

  MYSQL_STMT *stmt;
  int dev_id;
  if (stmt_get_int(
          (void **)&stmt, &dev_id, UserID, NULL, NULL,
          "SELECT id, user_id FROM supla_iodevice WHERE guid = unhex(?)", pbind,
          1)) {
    return dev_id;
  }

  return 0;
}
Beispiel #2
0
int database::get_client_id(int UserID, const char GUID[SUPLA_GUID_SIZE]) {
  MYSQL_STMT *stmt;
  int Result = 0;

  char GUIDHEX[SUPLA_GUID_HEXSIZE];
  st_guid2hex(GUIDHEX, GUID);

  MYSQL_BIND pbind[2];
  memset(pbind, 0, sizeof(pbind));

  pbind[0].buffer_type = MYSQL_TYPE_LONG;
  pbind[0].buffer = (char *)&UserID;

  pbind[1].buffer_type = MYSQL_TYPE_STRING;
  pbind[1].buffer = (char *)GUIDHEX;
  pbind[1].buffer_length = SUPLA_GUID_SIZE * 2;

  if (!stmt_get_int(
          (void **)&stmt, &Result, NULL, NULL, NULL,
          "SELECT id FROM supla_client WHERE user_id = ? AND guid = unhex(?)",
          pbind, 2))
    return 0;

  return Result;
}
Beispiel #3
0
int database::add_client(int AccessID, const char *GUID, const char *AuthKey,
                         const char *Name, unsigned int ipv4,
                         const char *softver, int proto_version, int UserID) {
  int ClientID = 0;

  char NameHEX[SUPLA_DEVICE_NAMEHEX_MAXSIZE];
  st_str2hex(NameHEX, Name, SUPLA_DEVICE_NAME_MAXSIZE);

  char *AuthKeyHashHEX = NULL;

  MYSQL_BIND pbind[9];
  memset(pbind, 0, sizeof(pbind));

  char GUIDHEX[SUPLA_GUID_HEXSIZE];
  st_guid2hex(GUIDHEX, GUID);

  if (AccessID == 0) {
    pbind[0].buffer_type = MYSQL_TYPE_NULL;
  } else {
    pbind[0].buffer_type = MYSQL_TYPE_LONG;
    pbind[0].buffer = (char *)&AccessID;
  }

  pbind[1].buffer_type = MYSQL_TYPE_STRING;
  pbind[1].buffer = (char *)GUIDHEX;
  pbind[1].buffer_length = (SUPLA_GUID_SIZE * 2);

  pbind[2].buffer_type = MYSQL_TYPE_STRING;
  pbind[2].buffer = (char *)NameHEX;
  pbind[2].buffer_length = strnlen(NameHEX, 256);

  pbind[3].buffer_type = MYSQL_TYPE_LONG;
  pbind[3].buffer = (char *)&ipv4;
  pbind[3].is_unsigned = true;

  pbind[4].buffer_type = MYSQL_TYPE_LONG;
  pbind[4].buffer = (char *)&ipv4;
  pbind[4].is_unsigned = true;

  pbind[5].buffer_type = MYSQL_TYPE_STRING;
  pbind[5].buffer = (char *)softver;
  pbind[5].buffer_length = strnlen(softver, 32);

  pbind[6].buffer_type = MYSQL_TYPE_LONG;
  pbind[6].buffer = (char *)&proto_version;

  pbind[7].buffer_type = MYSQL_TYPE_LONG;
  pbind[7].buffer = (char *)&UserID;

  if (AuthKey == NULL) {
    pbind[8].buffer_type = MYSQL_TYPE_NULL;
  } else {
    AuthKeyHashHEX = st_get_authkey_hash_hex(AuthKey);

    if (AuthKeyHashHEX == NULL) return 0;

    pbind[8].buffer_type = MYSQL_TYPE_STRING;
    pbind[8].buffer = (char *)AuthKeyHashHEX;
    pbind[8].buffer_length = strnlen(AuthKeyHashHEX, BCRYPT_HASH_MAXSIZE * 2);
  }

  const char sql[] =
      "INSERT INTO `supla_client`(`access_id`, `guid`, `name`, `enabled`, "
      "`reg_ipv4`, `reg_date`, `last_access_ipv4`, `last_access_date`, "
      "`software_version`, `protocol_version`, `user_id`, `auth_key`) VALUES "
      "(?,unhex(?),unhex(?),1,?,UTC_TIMESTAMP(),?,UTC_TIMESTAMP(),?,?,?,unhex(?"
      "))";

  MYSQL_STMT *stmt;
  if (stmt_execute((void **)&stmt, sql, pbind, 9, false)) {
    ClientID = get_last_insert_id();
  }

  if (stmt != NULL) mysql_stmt_close(stmt);

  if (AuthKeyHashHEX) {
    free(AuthKeyHashHEX);
    AuthKeyHashHEX = NULL;
  }

  return ClientID;
}
Beispiel #4
0
unsigned char clientcfg_init(int argc, char* argv[]) {

    int a;
    struct passwd *pw;
    char *buffer;
    char GUIDHEX[SUPLA_GUID_HEXSIZE+1];

    for(a=0; a<argc; a++) {
        if ( strcmp("-i", argv[a]) == 0 && a<argc-1 ) {
            cfg_id_file = strdup(argv[a+1]);
            a++;
        } else if ( strcmp("-h", argv[a]) == 0 && a<argc-1 ) {
            cfg_host = strdup(argv[a+1]);
            a++;
        } else if ( strcmp("-tcp", argv[a]) == 0  ) {
            cfg_ssl_enabled = 0;
        } else if ( strcmp("-p", argv[a]) == 0 && a<argc-1 ) {
            cfg_port = atoi(argv[a+1]);
            a++;
        } else if ( strcmp("-aid", argv[a]) == 0 && a<argc-1 ) {
            cfg_aid = atoi(argv[a+1]);
            a++;
        } else if ( strcmp("-pwd", argv[a]) == 0 && a<argc-1 ) {
            cfg_pwd = strdup(argv[a+1]);
            a++;
        };
    }

    if ( cfg_port == 0 )
        cfg_port = cfg_ssl_enabled == 1 ? 2016 : 2015;

    if ( cfg_host == NULL )
        cfg_host = strdup("127.0.0.1");

    if ( cfg_id_file == NULL ) {

        pw = getpwuid(getuid());
        a = strlen(pw->pw_dir)+50;

        buffer = malloc(a);

        if ( snprintf(buffer, a, "%s/.supla-client", pw->pw_dir) < 1 ) {
            free(buffer);
            return 0;
        }


        if ( st_file_exists(buffer) == 0 ) {
            if ( mkdir(buffer, 0700) == -1 ) {
                free(buffer);
                return 0;
            }
        }

        if ( snprintf(buffer, a, "%s/.supla-client/id", pw->pw_dir) < 1 ) {
            free(buffer);
            return 0;
        }

        cfg_id_file = strdup(buffer);
        free(buffer);
    }

    if ( cfg_id_file == NULL || strlen(cfg_id_file) < 1 ) {
        supla_log(LOG_ERR, "Unknown id file!");
        return 0;
    }

    if ( st_read_guid_from_file(cfg_id_file, cfg_client_GUID, 1) == 0 ) {
        return 0;
    }


    st_guid2hex(GUIDHEX, cfg_client_GUID);
    GUIDHEX[SUPLA_GUID_HEXSIZE] = 0;
    supla_log(LOG_INFO, "Client GUID: %s", GUIDHEX);


    return 1;
}