PageRenderTime 42ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/src/license/init_env.c

http://windowsfw.googlecode.com/
C | 198 lines | 129 code | 18 blank | 51 comment | 40 complexity | 369b5c1e707fddb381256be34b333702 MD5 | raw file
  1. #include <stdio.h>
  2. #include <ctype.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <sys/types.h>
  6. #include <sys/stat.h>
  7. #include <fcntl.h>
  8. #include <errno.h>
  9. #include "get_mac.h"
  10. #include "get_md5.h"
  11. #include "get_time.h"
  12. #include "get_info.h"
  13. #include "shmem_pub.h"
  14. #include "openssl.h"
  15. #define INFOFILE "/etc/.license"
  16. #define FLAGFILE "/usr/etc/license"
  17. #define LEVEFILE "/usr/.type"
  18. #define LIC_ERR "0"
  19. #define LIC_TRY_TIME_INVALID "2"
  20. #define LIC_TRY_VALID "3"
  21. #define LIC_TEST_TIME_INVALID "4"
  22. #define LIC_TEST_VALID "5"
  23. #define LIC_END_VALID "1"
  24. #define LIC_END_VALID_H "6"
  25. #define LIC_END_VALID_M "7"
  26. #define LIC_END_VALID_L "8"
  27. /*
  28. char * get_serial(char * buf_c)
  29. {
  30. char mac[STR_LEN] = {0};
  31. static char buf[STR_LEN] = {0};
  32. char *buf2 = buf_c;
  33. char self[] ={0xCC,0xAB,0xB3,0xF5,0xD3,0xD0,0xB5,0xC0,'\0'};
  34. //"????????????????";
  35. char self2[] = {0xC9,0xF1,0xB0,0xAE,0xCA,0xC0,0xC8,0xCB,'\0'};
  36. //"????,????????????,??????,????,?????";
  37. get_info(mac);
  38. //printf("%s\n",mac);
  39. GetMd5String(mac,self,buf);
  40. //printf("%s\n%s",self,buf);
  41. StrToUpper(buf);
  42. sprintf(buf2,"%c%c%c%c-%c%c%c%c-%c%c%c%c-%c%c%c%c-%c%c%c%c-%c%c%c%c-%c%c%c%c-%c%c%c%c",
  43. buf[0],buf[1],buf[2],buf[3],buf[4],buf[5],buf[6],buf[7],buf[8],buf[9],
  44. buf[10],buf[11],buf[12],buf[13],buf[14],buf[15],buf[16],buf[17],buf[18],buf[19],
  45. buf[20],buf[21],buf[22],buf[23],buf[24],buf[25],buf[26],buf[27],buf[28],buf[29],
  46. buf[30],buf[31]);
  47. GetMd5String(buf2,self2,buf);
  48. //printf("%s\n%s",self2,buf);
  49. StrToUpper(buf);
  50. sprintf(buf2,"%c%c%c%c-%c%c%c%c-%c%c%c%c-%c%c%c%c-%c%c%c%c-%c%c%c%c-%c%c%c%c-%c%c%c%c",
  51. buf[0],buf[1],buf[2],buf[3],buf[4],buf[5],buf[6],buf[7],buf[8],buf[9],
  52. buf[10],buf[11],buf[12],buf[13],buf[14],buf[15],buf[16],buf[17],buf[18],buf[19],
  53. buf[20],buf[21],buf[22],buf[23],buf[24],buf[25],buf[26],buf[27],buf[28],buf[29],
  54. buf[30],buf[31]);
  55. return buf2;
  56. }
  57. */
  58. int write_file(char *filename, char * c)
  59. {
  60. FILE *f;
  61. if ((f = fopen(filename, "w+")) == NULL)
  62. {
  63. //printf("Open file error.\n");
  64. return 0;
  65. }
  66. if (!fprintf(f,"%s\n", c))
  67. {
  68. //printf("Can't write string.\n");
  69. fclose(f);
  70. return 0;
  71. }
  72. fclose(f);
  73. return 1;
  74. }
  75. int main()
  76. {
  77. char buf[STR_LEN] = {0};
  78. char out[STR_LEN] = {0};
  79. char buf2[STR_LEN] ={0};
  80. char type[STR_LEN] = {0};
  81. char value[STR_LEN] = {0};
  82. char level[STR_LEN] = {0};
  83. int current = 0;
  84. int now = 0;
  85. int i = 0;
  86. int timelen = 0;
  87. FILE *pf = NULL;
  88. if ((pf = fopen(INFOFILE, "r")) == NULL )
  89. {
  90. //fprintf(stderr, "Error open file!\n");
  91. return -1; // Error register!
  92. }
  93. if (fgets(out, STR_LEN, pf) == NULL)
  94. {
  95. //fprintf(stderr, "Error read file!\n");
  96. return -1; // Error read!
  97. }
  98. //printf("Read License From File:\n%s\n",out);
  99. Client((unsigned char *)out,(unsigned char *)buf);
  100. //printf("OUT:\n%s\n",buf);
  101. //get_serial(buf2);
  102. get_give_license_md5(buf2);
  103. //printf("Get Md5 String From String:\n%s\n",buf2);
  104. /*printf("BUF:%c\t BUF2:%c\n", buf[28],buf2[28]);
  105. printf("BUF:%c\t BUF2:%c\n", buf[31],buf2[31]);
  106. printf("BUF:%c\t BUF2:%c\n", buf[32],buf2[32]);
  107. printf("BUF:%c\t BUF2:%c\n", buf[33],buf2[33]);
  108. printf("BUF:%c\t BUF2:%c\n", buf[36],buf2[36]);*/
  109. if (!strncmp (buf, buf2, 27) &&
  110. buf[28] == buf2[28] &&
  111. buf[31] == buf2[31] &&
  112. buf[32] == buf2[32] &&
  113. buf[33] == buf2[33] &&
  114. buf[35] == buf2[35] )
  115. {
  116. if (buf[27] > 48 && buf[27] < 58){ // Test or Try User.
  117. //printf("T:%c, %d \n", buf[36],buf[36]);
  118. //printf("T:%c, %d \n", buf[37],buf[37]);
  119. timelen = buf[36] - 48 ; // time: the first bits
  120. if (buf[37] > 48 && buf[37] < 58){
  121. timelen = timelen + (buf[37] - 48 ) * 10;
  122. }
  123. current = timelen;
  124. now = get_now_quarter();
  125. memset(type, 0, STR_LEN);
  126. type[0] = buf[27];
  127. type[1] = '\0';
  128. i = atoi(type);
  129. if ((i % 2) == 0){
  130. if (current > now || current == now) { // OK
  131. strcpy(value,LIC_TRY_VALID);
  132. }
  133. else {
  134. strcpy(value,LIC_TRY_TIME_INVALID);
  135. }
  136. printf("LICENSE:\n TYPE:Try User.\n Time:%d Months.\n", (current - now) * 3);
  137. }
  138. if ((i % 2) != 0){
  139. if (current > now || current == now) { // OK
  140. strcpy(value,LIC_TEST_VALID);
  141. }
  142. else
  143. {
  144. strcpy(value,LIC_TEST_TIME_INVALID);
  145. }
  146. printf("LICENSE:\n TYPE:Test User.\n Time:%d Months.\n", (current - now) * 3);
  147. }
  148. }
  149. if (buf[27] > 64 && buf[27] < 91){
  150. strcpy(value,LIC_END_VALID);
  151. if (buf[27] > 84 && buf[27] < 91) {
  152. strcpy(value,LIC_END_VALID_H);
  153. printf("LICENSE:\n TYPE:Final User.\n Level:High\n");
  154. }
  155. if (buf[27] > 78 && buf[27] < 85) {
  156. strcpy(value,LIC_END_VALID_M);
  157. printf("LICENSE:\n TYPE:Final User.\n Level:Middle\n");
  158. }
  159. if (buf[27] > 64 && buf[27] < 79) {
  160. strcpy(value,LIC_END_VALID_M);
  161. printf("LICENSE:\n TYPE:Final User.\n Level:Low\n");
  162. }
  163. }
  164. // Create share mem.
  165. SetSHMData_Public(atoi(value));
  166. write_file(FLAGFILE,value);
  167. write_file(LEVEFILE,level);
  168. //printf("Check License OK!\n");
  169. return 0;
  170. }
  171. else
  172. {
  173. write_file(FLAGFILE,LIC_ERR);
  174. printf("Check License Error!\n");
  175. return -1;
  176. }
  177. }