/src/license/init_env.c
C | 198 lines | 129 code | 18 blank | 51 comment | 40 complexity | 369b5c1e707fddb381256be34b333702 MD5 | raw file
- #include <stdio.h>
- #include <ctype.h>
- #include <stdlib.h>
- #include <string.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <fcntl.h>
- #include <errno.h>
- #include "get_mac.h"
- #include "get_md5.h"
- #include "get_time.h"
- #include "get_info.h"
- #include "shmem_pub.h"
- #include "openssl.h"
- #define INFOFILE "/etc/.license"
- #define FLAGFILE "/usr/etc/license"
- #define LEVEFILE "/usr/.type"
- #define LIC_ERR "0"
- #define LIC_TRY_TIME_INVALID "2"
- #define LIC_TRY_VALID "3"
- #define LIC_TEST_TIME_INVALID "4"
- #define LIC_TEST_VALID "5"
- #define LIC_END_VALID "1"
- #define LIC_END_VALID_H "6"
- #define LIC_END_VALID_M "7"
- #define LIC_END_VALID_L "8"
- /*
- char * get_serial(char * buf_c)
- {
- char mac[STR_LEN] = {0};
- static char buf[STR_LEN] = {0};
- char *buf2 = buf_c;
- char self[] ={0xCC,0xAB,0xB3,0xF5,0xD3,0xD0,0xB5,0xC0,'\0'};
- //"????????????????";
- char self2[] = {0xC9,0xF1,0xB0,0xAE,0xCA,0xC0,0xC8,0xCB,'\0'};
- //"????,????????????,??????,????,?????";
-
- get_info(mac);
- //printf("%s\n",mac);
- GetMd5String(mac,self,buf);
- //printf("%s\n%s",self,buf);
- StrToUpper(buf);
- 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",
- buf[0],buf[1],buf[2],buf[3],buf[4],buf[5],buf[6],buf[7],buf[8],buf[9],
- buf[10],buf[11],buf[12],buf[13],buf[14],buf[15],buf[16],buf[17],buf[18],buf[19],
- buf[20],buf[21],buf[22],buf[23],buf[24],buf[25],buf[26],buf[27],buf[28],buf[29],
- buf[30],buf[31]);
- GetMd5String(buf2,self2,buf);
- //printf("%s\n%s",self2,buf);
- StrToUpper(buf);
-
- 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",
- buf[0],buf[1],buf[2],buf[3],buf[4],buf[5],buf[6],buf[7],buf[8],buf[9],
- buf[10],buf[11],buf[12],buf[13],buf[14],buf[15],buf[16],buf[17],buf[18],buf[19],
- buf[20],buf[21],buf[22],buf[23],buf[24],buf[25],buf[26],buf[27],buf[28],buf[29],
- buf[30],buf[31]);
- return buf2;
- }
- */
- int write_file(char *filename, char * c)
- {
- FILE *f;
- if ((f = fopen(filename, "w+")) == NULL)
- {
- //printf("Open file error.\n");
- return 0;
- }
- if (!fprintf(f,"%s\n", c))
- {
- //printf("Can't write string.\n");
- fclose(f);
- return 0;
- }
- fclose(f);
- return 1;
- }
- int main()
- {
- char buf[STR_LEN] = {0};
- char out[STR_LEN] = {0};
- char buf2[STR_LEN] ={0};
- char type[STR_LEN] = {0};
- char value[STR_LEN] = {0};
- char level[STR_LEN] = {0};
- int current = 0;
- int now = 0;
- int i = 0;
- int timelen = 0;
- FILE *pf = NULL;
- if ((pf = fopen(INFOFILE, "r")) == NULL )
- {
- //fprintf(stderr, "Error open file!\n");
- return -1; // Error register!
- }
-
- if (fgets(out, STR_LEN, pf) == NULL)
- {
- //fprintf(stderr, "Error read file!\n");
- return -1; // Error read!
- }
- //printf("Read License From File:\n%s\n",out);
- Client((unsigned char *)out,(unsigned char *)buf);
- //printf("OUT:\n%s\n",buf);
- //get_serial(buf2);
- get_give_license_md5(buf2);
- //printf("Get Md5 String From String:\n%s\n",buf2);
-
- /*printf("BUF:%c\t BUF2:%c\n", buf[28],buf2[28]);
- printf("BUF:%c\t BUF2:%c\n", buf[31],buf2[31]);
- printf("BUF:%c\t BUF2:%c\n", buf[32],buf2[32]);
- printf("BUF:%c\t BUF2:%c\n", buf[33],buf2[33]);
- printf("BUF:%c\t BUF2:%c\n", buf[36],buf2[36]);*/
- if (!strncmp (buf, buf2, 27) &&
- buf[28] == buf2[28] &&
- buf[31] == buf2[31] &&
- buf[32] == buf2[32] &&
- buf[33] == buf2[33] &&
- buf[35] == buf2[35] )
- {
- if (buf[27] > 48 && buf[27] < 58){ // Test or Try User.
- //printf("T:%c, %d \n", buf[36],buf[36]);
- //printf("T:%c, %d \n", buf[37],buf[37]);
- timelen = buf[36] - 48 ; // time: the first bits
- if (buf[37] > 48 && buf[37] < 58){
- timelen = timelen + (buf[37] - 48 ) * 10;
- }
- current = timelen;
- now = get_now_quarter();
-
- memset(type, 0, STR_LEN);
- type[0] = buf[27];
- type[1] = '\0';
- i = atoi(type);
- if ((i % 2) == 0){
- if (current > now || current == now) { // OK
- strcpy(value,LIC_TRY_VALID);
- }
- else {
- strcpy(value,LIC_TRY_TIME_INVALID);
- }
- printf("LICENSE:\n TYPE:Try User.\n Time:%d Months.\n", (current - now) * 3);
- }
- if ((i % 2) != 0){
- if (current > now || current == now) { // OK
- strcpy(value,LIC_TEST_VALID);
- }
- else
- {
- strcpy(value,LIC_TEST_TIME_INVALID);
- }
- printf("LICENSE:\n TYPE:Test User.\n Time:%d Months.\n", (current - now) * 3);
- }
- }
-
- if (buf[27] > 64 && buf[27] < 91){
- strcpy(value,LIC_END_VALID);
- if (buf[27] > 84 && buf[27] < 91) {
- strcpy(value,LIC_END_VALID_H);
- printf("LICENSE:\n TYPE:Final User.\n Level:High\n");
- }
- if (buf[27] > 78 && buf[27] < 85) {
- strcpy(value,LIC_END_VALID_M);
- printf("LICENSE:\n TYPE:Final User.\n Level:Middle\n");
- }
- if (buf[27] > 64 && buf[27] < 79) {
- strcpy(value,LIC_END_VALID_M);
- printf("LICENSE:\n TYPE:Final User.\n Level:Low\n");
- }
- }
- // Create share mem.
- SetSHMData_Public(atoi(value));
- write_file(FLAGFILE,value);
- write_file(LEVEFILE,level);
- //printf("Check License OK!\n");
- return 0;
- }
- else
- {
- write_file(FLAGFILE,LIC_ERR);
- printf("Check License Error!\n");
- return -1;
- }
- }