00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00023 #include <glib.h>
00024 #include <alloca.h>
00025 #include <stdlib.h>
00026 #include <stdio.h>
00027 #include <unistd.h>
00028 #include <errno.h>
00029 #include <ctype.h>
00030 #include <string.h>
00031 #include <sys/wait.h>
00032
00033 #include "erreglog.h"
00034 #include "system.h"
00035
00036 #define MAC_LENGTH 12
00037 #define MAC_HEX_DECORATION 2
00038
00039 #define PROCESS_BUFFER_MAX (2*1024)
00040
00041 const char *get_sysset_macAddress(void)
00042 {
00043 FILE *POUT = 0;
00044 int nProcessRet = 0;
00045
00046 static char mac_address[MAC_LENGTH + 6] = "00-00-00-00-00-00";
00047
00048 POUT = popen("sysset -r -b -a 262 -l 6", "r");
00049 if (POUT == NULL)
00050 {
00051 ERREG_ERRORPRINTF("Could not read EUI64");
00052 }
00053 else
00054 {
00055 int nChar;
00056 int index = 0;
00057 char szBuffer[PROCESS_BUFFER_MAX + 1];
00058
00059 while (((nChar = getc(POUT)) != EOF) && (index < PROCESS_BUFFER_MAX))
00060 {
00061 szBuffer[index] = (char)nChar;
00062 index++;
00063 }
00064 szBuffer[index] = '\0';
00065 ERREG_LOGPRINTF("echoed %s", szBuffer);
00066
00067 nProcessRet = pclose(POUT);
00068 if (WIFEXITED(nProcessRet))
00069 {
00070 if (WEXITSTATUS(nProcessRet) == 0)
00071 {
00072 int nLen;
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082 ERREG_LOGPRINTF("pclose returned %d", WEXITSTATUS(nProcessRet));
00083 nLen = strlen(szBuffer);
00084 if (nLen != MAC_LENGTH + 2 + 1)
00085 {
00086 ERREG_ERRORPRINTF("Wrong length [%d] of MAC address [%s]", nLen, szBuffer);
00087 }
00088 else
00089 {
00090 char *cp1 = mac_address;
00091 char *cp2 = szBuffer + MAC_HEX_DECORATION;
00092 for (index = 0; index < MAC_LENGTH; index += 2)
00093 {
00094
00095 *cp1 = toupper(*cp2);
00096 cp1++;
00097 cp2++;
00098
00099
00100 *cp1 = toupper(*cp2);
00101 cp1++;
00102 cp2++;
00103
00104
00105 cp1++;
00106 }
00107 }
00108 }
00109 else
00110 {
00111 ERREG_ERRORPRINTF("pclose returned %d. Error retrieving MAC", WEXITSTATUS(nProcessRet));
00112 }
00113 }
00114 else
00115 {
00116 ERREG_ERRORPRINTF("pclose did not exit normally");
00117 }
00118 }
00119
00120 ERREG_LOGPRINTF("return [%s]", mac_address);
00121 return mac_address;
00122 }
00123
00124 const char *get_wired_macAddress(void)
00125 {
00126 FILE *POUT = 0;
00127 int nProcessRet = 0;
00128
00129 static char mac_address[MAC_LENGTH + 6] = "00-00-00-00-00-00";
00130
00131
00132 POUT = popen("/usr/bin/wired.sh mac | tail -1", "r");
00133 if (POUT == NULL)
00134 {
00135 ERREG_ERRORPRINTF("Could not read wired MAC address");
00136 }
00137 else
00138 {
00139 int nChar;
00140 int index = 0;
00141 char szBuffer[PROCESS_BUFFER_MAX + 1];
00142
00143 while (((nChar = getc(POUT)) != EOF) && (index < PROCESS_BUFFER_MAX))
00144 {
00145 szBuffer[index] = (char)nChar;
00146 index++;
00147 }
00148 szBuffer[index] = '\0';
00149 ERREG_LOGPRINTF("echoed %s", szBuffer);
00150
00151 nProcessRet = pclose(POUT);
00152 if (WIFEXITED(nProcessRet))
00153 {
00154 if (WEXITSTATUS(nProcessRet) == 0)
00155 {
00156 int nLen;
00157
00158
00159
00160
00161
00162 ERREG_LOGPRINTF("pclose returned %d", WEXITSTATUS(nProcessRet));
00163 nLen = strlen(szBuffer);
00164 if (nLen != MAC_LENGTH + 5 + 1)
00165 {
00166 ERREG_ERRORPRINTF("Wrong length [%d] of MAC address [%s]", nLen, szBuffer);
00167 }
00168 else
00169 {
00170 strncpy(mac_address, szBuffer, MAC_LENGTH + 5);
00171 }
00172 }
00173 else
00174 {
00175 ERREG_ERRORPRINTF("pclose returned %d. Error retrieving MAC", WEXITSTATUS(nProcessRet));
00176 }
00177 }
00178 else
00179 {
00180 ERREG_ERRORPRINTF("pclose did not exit normally");
00181 }
00182 }
00183
00184 ERREG_LOGPRINTF("return [%s]", mac_address);
00185 return mac_address;
00186 }
00187
00188
00189 const char *get_wireless_macAddress(void)
00190 {
00191 FILE *POUT = 0;
00192 int nProcessRet = 0;
00193
00194 static char mac_address[MAC_LENGTH + 6] = "00-00-00-00-00-00";
00195
00196
00197 POUT = popen("/usr/bin/wireless.sh mac | tail -1", "r");
00198 if (POUT == NULL)
00199 {
00200 ERREG_ERRORPRINTF("Could not read wireless MAC address");
00201 }
00202 else
00203 {
00204 int nChar;
00205 int index = 0;
00206 char szBuffer[PROCESS_BUFFER_MAX + 1];
00207
00208 while (((nChar = getc(POUT)) != EOF) && (index < PROCESS_BUFFER_MAX))
00209 {
00210 szBuffer[index] = (char)nChar;
00211 index++;
00212 }
00213 szBuffer[index] = '\0';
00214 ERREG_LOGPRINTF("echoed %s", szBuffer);
00215
00216 nProcessRet = pclose(POUT);
00217 if (WIFEXITED(nProcessRet))
00218 {
00219 if (WEXITSTATUS(nProcessRet) == 0)
00220 {
00221 int nLen;
00222
00223
00224
00225
00226
00227 ERREG_LOGPRINTF("pclose returned %d", WEXITSTATUS(nProcessRet));
00228 nLen = strlen(szBuffer);
00229 if (nLen != MAC_LENGTH + 5 + 1)
00230 {
00231 ERREG_ERRORPRINTF("Wrong length [%d] of MAC address [%s]", nLen, szBuffer);
00232 }
00233 else
00234 {
00235 strncpy(mac_address, szBuffer, MAC_LENGTH + 5);
00236 }
00237 }
00238 else
00239 {
00240 ERREG_ERRORPRINTF("pclose returned %d. Error retrieving MAC", WEXITSTATUS(nProcessRet));
00241 }
00242 }
00243 else
00244 {
00245 ERREG_ERRORPRINTF("pclose did not exit normally");
00246 }
00247 }
00248
00249 ERREG_LOGPRINTF("return [%s]", mac_address);
00250 return mac_address;
00251 }
00252
00253 #define WLAN_MAC_FILE "/mnt/free/wlan_mac.txt"
00254
00255 const char *get_wlanmactxt_macAddress(void)
00256 {
00257 static char mac_address[MAC_LENGTH + 6] = "00-00-00-00-00-00";
00258
00259 FILE *fd = fopen(WLAN_MAC_FILE, "r");
00260 if (fd)
00261 {
00262
00263 char* tmp = NULL;
00264 fseek(fd, 0, SEEK_END);
00265 int len = ftell(fd);
00266
00267 tmp = malloc(len+1);
00268 if (tmp)
00269 {
00270 fseek(fd, 0, SEEK_SET);
00271 fread(tmp, sizeof(char), len, fd);
00272 tmp[len] = '\0';
00273
00274
00275 char *new_line = strchr(tmp, '\n');
00276 if (new_line)
00277 {
00278 snprintf(mac_address, sizeof(mac_address), "%s", new_line+1);
00279 }
00280
00281 free(tmp);
00282 }
00283
00284 fclose(fd);
00285
00286 ERREG_LOGPRINTF("software mac_address [%s]", mac_address);
00287 }
00288
00289 return mac_address;
00290 }
00291
00292 #define VERSION_FILE "/etc/er_version"
00293 #define VERSION_COMMERCIAL_FILE "/etc/er_version_commercial"
00294
00295 #define VERSION_LENGTH 40
00296
00297 const char *get_software_version(void)
00298 {
00299 static char version[VERSION_LENGTH] = "";
00300
00301 FILE *fd = fopen(VERSION_FILE, "r");
00302 if (fd)
00303 {
00304 fgets(version, sizeof(version)-1, fd);
00305 fclose(fd);
00306
00307 char *new_line = strchr(version, '\n');
00308 if (new_line)
00309 {
00310 *new_line = '\0';
00311 }
00312
00313 ERREG_LOGPRINTF("software version [%s]", version);
00314 }
00315
00316 return version;
00317 }
00318
00319 const char *get_software_version_commercial(void)
00320 {
00321 static char version[VERSION_LENGTH] = "";
00322
00323 FILE *fd = fopen(VERSION_COMMERCIAL_FILE, "r");
00324 if (fd)
00325 {
00326 fgets(version, sizeof(version)-1, fd);
00327 fclose(fd);
00328
00329 char *new_line = strchr(version, '\n');
00330 if (new_line)
00331 {
00332 *new_line = '\0';
00333 }
00334
00335 ERREG_LOGPRINTF("software version [%s]", version);
00336 }
00337
00338 return version;
00339 }
00340
00341 #define PUBLIC_KEY_FILE "/proc/keystore/public"
00342
00343 #define MAX_PUBKEY_SIZE (1024)
00344
00345 static inline int is_pubkey_char(unsigned char c)
00346 {
00347 return (isalnum(c) || (c == '+') || (c == '/') || (c == '-') || (c == '\n') || (c == ' '));
00348 }
00349
00350 int get_public_key(char **pubkey)
00351 {
00352 int bytes = 0;
00353 int i;
00354 FILE *f = fopen(PUBLIC_KEY_FILE, "r");
00355 if (f == NULL)
00356 {
00357 return -1;
00358 }
00359
00360 *pubkey = (char *) malloc(MAX_PUBKEY_SIZE);
00361 if (*pubkey == NULL)
00362 {
00363 ERREG_ERRORPRINTF("Error: cannot allocate %d bytes for public key", MAX_PUBKEY_SIZE);
00364 return 1;
00365 }
00366
00367 if ((bytes = fread(*pubkey, 1, MAX_PUBKEY_SIZE, f)) == MAX_PUBKEY_SIZE)
00368 {
00369 ERREG_ERRORPRINTF("Error reading Public Key (too big)");
00370 free(*pubkey);
00371 *pubkey = NULL;
00372 return -2;
00373 }
00374
00375 if (!feof(f))
00376 {
00377 ERREG_ERRORPRINTF("Error reading Public Key (not EOF)");
00378 free(*pubkey);
00379 *pubkey = NULL;
00380 return -3;
00381 }
00382
00383 (*pubkey)[bytes] = '\0';
00384
00385
00386 for (i = 0; i < bytes; i++)
00387 {
00388 if (!is_pubkey_char((*pubkey)[i]))
00389 {
00390 ERREG_ERRORPRINTF("Error: this does not look like a public key!");
00391 free(*pubkey);
00392 *pubkey = NULL;
00393 return -5;
00394 }
00395 }
00396
00397 ERREG_LOGPRINTF("software publicKey [%s]", *pubkey);
00398
00399 fclose(f);
00400 return bytes;
00401 }
00402
00403
00404
00405
00406
00407
00408
00409
00410
00411
00412
00413
00414
00415
00416
00417
00418
00419
00420
00421
00422
00423
00424
00425
00426
00427
00428
00429