📄 mbus_config.c
字号:
abort(); } line = (char *) xmalloc(s.st_size+1); sscanf(buf, "%s", line); if (strcmp(line, "[MBUS]") != 0) { debug_msg("Invalid .mbus file\n"); abort(); } pos = strlen(line) + 1; while (pos < s.st_size) { /* get whole line and filter out spaces */ /* this is good enough for getting keys */ linepos=0; do { while((buf[pos+linepos]==' ')||(buf[pos+linepos]=='\n') ||(buf[pos+linepos]=='\t')) pos++; sscanf(buf+pos+linepos, "%s", line+linepos); linepos = strlen(line); } while((buf[pos+linepos]!='\n') && (s.st_size>(pos+linepos+1))); pos += linepos + 1; if (strncmp(line, id, strlen(id)) == 0) { key->algorithm = (char *) strdup(strtok(line+strlen(id), ",)")); if (strcmp(key->algorithm, "NOENCR") != 0) { key->key = (char *) strtok(NULL , ")"); ASSERT(key->key!=NULL); key->key_len = strlen(key->key); tmp = (char *) xmalloc(key->key_len); key->key_len = base64decode(key->key, key->key_len, tmp, key->key_len); key->key = tmp; } else { key->key = NULL; key->key_len = 0; } xfree(buf); xfree(line); return; } } debug_msg("Unable to read hashkey from config file\n"); xfree(buf); xfree(line);}#endifvoid mbus_get_encrkey(struct mbus_config *m, struct mbus_key *key){ /* This MUST be called while the config file is locked! */ int i, j, k;#ifdef WIN32 long status; DWORD type; char *buffer; int buflen = MBUS_BUF_SIZE; char *tmp; ASSERT(m->cfg_locked); /* Read the key from the registry... */ buffer = (char *) xmalloc(MBUS_BUF_SIZE); status = RegQueryValueEx(m->cfgKey, "ENCRYPTIONKEY", 0, &type, buffer, &buflen); if (status != ERROR_SUCCESS) { FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, NULL, status, 0, buffer, MBUS_BUF_SIZE, NULL); debug_msg("Unable to get encrkey: %s\n", buffer); abort(); } ASSERT(type == REG_SZ); ASSERT(buflen > 0); /* Parse the key... */ key->algorithm = strdup(strtok(buffer+1, ",)")); if (strcmp(key->algorithm, "NOENCR") != 0) { key->key = (char *) strtok(NULL , ")"); key->key_len = strlen(key->key); tmp = (char *) xmalloc(key->key_len); key->key_len = base64decode(key->key, key->key_len, tmp, key->key_len); key->key = tmp; } else { key->key = NULL; key->key_len = 0; } xfree(buffer);#else mbus_get_key(m, key, "ENCRYPTIONKEY=(");#endif if (strcmp(key->algorithm, "DES") == 0) { ASSERT(key->key != NULL); ASSERT(key->key_len == 8); /* check parity bits */ for (i = 0; i < 8; ++i) { k = key->key[i] & 0xfe; j = k; j ^= j >> 4; j ^= j >> 2; j ^= j >> 1; j = (j & 1) ^ 1; ASSERT((key->key[i] & 0x01) == j); } }}void mbus_get_hashkey(struct mbus_config *m, struct mbus_key *key){ /* This MUST be called while the config file is locked! */#ifdef WIN32 long status; DWORD type; char *buffer; int buflen = MBUS_BUF_SIZE; char *tmp; ASSERT(m->cfg_locked); /* Read the key from the registry... */ buffer = (char *) xmalloc(MBUS_BUF_SIZE); status = RegQueryValueEx(m->cfgKey, "HASHKEY", 0, &type, buffer, &buflen); if (status != ERROR_SUCCESS) { FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, NULL, status, 0, buffer, MBUS_BUF_SIZE, NULL); debug_msg("Unable to get encrkey: %s\n", buffer); abort(); } ASSERT(type == REG_SZ); ASSERT(buflen > 0); /* Parse the key... */ key->algorithm = strdup(strtok(buffer+1, ",")); key->key = strtok(NULL , ")"); key->key_len = strlen(key->key); /* Decode the key... */ tmp = (char *) xmalloc(key->key_len); key->key_len = base64decode(key->key, key->key_len, tmp, key->key_len); key->key = tmp; xfree(buffer);#else mbus_get_key(m, key, "HASHKEY=(");#endif}void mbus_get_net_addr(struct mbus_config *m, char *net_addr, uint16_t *net_port, int *net_scope){#ifdef WIN32 long status; DWORD type; char *buffer; int buflen = MBUS_BUF_SIZE; uint32_t port; ASSERT(m->cfg_locked); /* Read the key from the registry... */ buffer = (char *) xmalloc(MBUS_BUF_SIZE); buflen = MBUS_BUF_SIZE; status = RegQueryValueEx(m->cfgKey, "ADDRESS", 0, &type, buffer, &buflen); if (status != ERROR_SUCCESS) { FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, NULL, status, 0, buffer, MBUS_BUF_SIZE, NULL); debug_msg("Unable to get address: %s\n", buffer); strcpy(net_addr, MBUS_DEFAULT_NET_ADDR); } else { ASSERT(type == REG_SZ); ASSERT(buflen > 0); strncpy(net_addr, buffer, buflen); } buflen = sizeof(port); status = RegQueryValueEx(m->cfgKey, "PORT", 0, &type, (uint8_t *) &port, &buflen); if (status != ERROR_SUCCESS) { FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, NULL, status, 0, buffer, MBUS_BUF_SIZE, NULL); debug_msg("Unable to get port: %s\n", buffer); *net_port = MBUS_DEFAULT_NET_PORT; } else { ASSERT(type == REG_DWORD); ASSERT(buflen == 4); *net_port = (uint16_t) port; } buflen = MBUS_BUF_SIZE; status = RegQueryValueEx(m->cfgKey, "SCOPE", 0, &type, buffer, &buflen); if (status != ERROR_SUCCESS) { FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, NULL, status, 0, buffer, MBUS_BUF_SIZE, NULL); debug_msg("Unable to get scope: %s\n", buffer); *net_scope = MBUS_DEFAULT_SCOPE; } else { ASSERT(type == REG_SZ); ASSERT(buflen > 0); if (strncmp(buffer, SCOPE_HOSTLOCAL_NAME, strlen(SCOPE_HOSTLOCAL_NAME)) == 0) { *net_scope = SCOPE_HOSTLOCAL; } else if (strncmp(buffer, SCOPE_LINKLOCAL_NAME, strlen(SCOPE_LINKLOCAL_NAME)) == 0) { *net_scope = SCOPE_LINKLOCAL; } else { debug_msg("Unrecognized scope: %s\n", buffer); *net_scope = MBUS_DEFAULT_SCOPE; } } xfree(buffer);#else struct stat s; char *buf; char *line; int pos; int pos2; int linepos; int scope; char *addr; uint16_t port; ASSERT(m->cfg_locked); addr = (char *)xmalloc(20); memset(addr, 0, 20); port = 0; scope = -1; if (lseek(m->cfgfd, 0, SEEK_SET) == -1) { perror("Can't seek to start of config file"); abort(); } if (fstat(m->cfgfd, &s) != 0) { perror("Unable to stat config file\n"); abort(); } /* Read in the contents of the config file... */ buf = (char *) xmalloc(s.st_size+1); memset(buf, '\0', s.st_size+1); if (read(m->cfgfd, buf, s.st_size) != s.st_size) { perror("Unable to read config file\n"); abort(); } line = (char *) xmalloc(s.st_size+1); sscanf(buf, "%s", line); if (strcmp(line, "[MBUS]") != 0) { debug_msg("Invalid .mbus file\n"); abort(); } pos = strlen(line) + 1; while (pos < s.st_size) { /* get whole line and filter out spaces */ linepos=0; do { while((buf[pos+linepos]==' ')||(buf[pos+linepos]=='\n') ||(buf[pos+linepos]=='\t')) pos++; sscanf(buf+pos+linepos, "%s", line+linepos); linepos = strlen(line); } while((buf[pos+linepos]!='\n') && (s.st_size>(pos+linepos+1))); pos += linepos + 1; if (strncmp(line, "SCOPE", strlen("SCOPE")) == 0) { pos2 = strlen("SCOPE") + 1; if (strncmp(line+pos2, SCOPE_HOSTLOCAL_NAME, strlen(SCOPE_HOSTLOCAL_NAME)) == 0) { scope = SCOPE_HOSTLOCAL; } if (strncmp(line+pos2, SCOPE_LINKLOCAL_NAME, strlen(SCOPE_LINKLOCAL_NAME)) == 0) { scope = SCOPE_LINKLOCAL ; } } if (strncmp(line, "ADDRESS", strlen("ADDRESS")) == 0) { pos2 = strlen("ADDRESS") + 1; strncpy(addr, line+pos2, 16); } if (strncmp(line, "PORT", strlen("PORT")) == 0) { pos2 = strlen("PORT") + 1; port = atoi(line+pos2); } } /* If we did not find all values, fill in the default ones */ *net_port = (port>0)?port:MBUS_DEFAULT_NET_PORT; *net_scope = (scope!=-1)?scope:MBUS_DEFAULT_SCOPE; if (strlen(addr)>0) { strncpy(net_addr, addr, 16); } else { strcpy(net_addr, MBUS_DEFAULT_NET_ADDR); } debug_msg("using Addr:%s Port:%d Scope:%s for MBUS\n", net_addr, *net_port, (*net_scope==0)?SCOPE_HOSTLOCAL_NAME:SCOPE_LINKLOCAL_NAME); xfree(buf); xfree(line); xfree(addr);#endif}int mbus_get_version(struct mbus_config *m){#ifdef WIN32 long status; DWORD type; uint32_t cver; int verl; char buffer[MBUS_BUF_SIZE]; ASSERT(m->cfg_locked); /* Read the key from the registry... */ verl = sizeof(&cver); status = RegQueryValueEx(m->cfgKey, "CONFIG_VERSION", 0, &type, (uint8_t *) &cver, &verl); if (status != ERROR_SUCCESS) { FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, NULL, status, 0, buffer, MBUS_BUF_SIZE, NULL); debug_msg("Unable to get version: %s\n", buffer); return 0; } ASSERT(type == REG_DWORD); ASSERT(verl == 4); return cver;#else struct stat s; char *buf; char *line; int pos; int pos2; int linepos; int version = 0; ASSERT(m->cfg_locked); if (lseek(m->cfgfd, 0, SEEK_SET) == -1) { perror("Can't seek to start of config file"); abort(); } if (fstat(m->cfgfd, &s) != 0) { perror("Unable to stat config file\n"); abort(); } /* Read in the contents of the config file... */ buf = (char *) xmalloc(s.st_size+1); memset(buf, '\0', s.st_size+1); if (read(m->cfgfd, buf, s.st_size) != s.st_size) { perror("Unable to read config file\n"); abort(); } line = (char *) xmalloc(s.st_size+1); sscanf(buf, "%s", line); if (strcmp(line, "[MBUS]") != 0) { debug_msg("Invalid .mbus file\n"); abort(); } pos = strlen(line) + 1; while (pos < s.st_size) { /* get whole line and filter out spaces */ linepos=0; do { while((buf[pos+linepos]==' ')||(buf[pos+linepos]=='\n') ||(buf[pos+linepos]=='\t')) { pos++; } sscanf(buf+pos+linepos, "%s", line+linepos); linepos = strlen(line); } while((buf[pos+linepos]!='\n') && (s.st_size>(pos+linepos+1))); pos += linepos + 1; if (strncmp(line, "CONFIG_VERSION", strlen("CONFIG_VERSION")) == 0) { pos2 = strlen("CONFIG_VERSION") + 1; version = atoi(line+pos2); } } xfree(buf); xfree(line); return version;#endif}struct mbus_config *mbus_create_config(void){ return (struct mbus_config *) xmalloc(sizeof(struct mbus_config));}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -