123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380 |
- /*
- ============================================================================
- Name : ast_init
- Author : dy
- Version : v1.0
- Copyright : ZYCOO copyright
- Description : set init info to mysql and redis
- ============================================================================
- */
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <errno.h>
- #include <assert.h>
- #include <sys/time.h>
- #include <time.h>
- #include <ctype.h>
- #include <mysql/mysql.h>
- #include "hiredis/hiredis.h"
- MYSQL *g_conn; // mysql 连接
- MYSQL_RES *g_res; // mysql 记录集
- MYSQL_ROW g_row; // 字符串数组,mysql 记录行
- #define MAX_SIZE 2048
- #define MIDLE_SIZE 512
- #define MINI_SIZE 64
- #define KEYVALLEN 100
- #define MAX 65534
- #define VERSION "V1.0.1"
- #define RESET_FILE "/init/sql/.DS_Store"
- char uuid[64] = {0};
- long Timestamp;
- char g_host_name[MINI_SIZE];
- char g_user_name[MINI_SIZE] = "root";
- char g_password[MINI_SIZE];
- char g_db_name[MINI_SIZE] = "init_db";
- char sql_tmp[MIDLE_SIZE];
- const unsigned int g_db_port = 3306;
- void mytime(){
- struct timeval tv;
- gettimeofday(&tv, NULL);
- Timestamp = tv.tv_sec + 45*24*60*60;
- }
- void get_uuid(){
- FILE *fp;
- memset(uuid,'\0',sizeof(uuid));
- fp=popen("dmidecode -s system-uuid | sed '/^#/d' | head -n 1","r");
- fgets(uuid,sizeof(uuid),fp);
- if (uuid[strlen(uuid) - 1] == '\n')
- {
- uuid[strlen(uuid) - 1] = '\0';
- }
- pclose(fp);
- }
- /*初始化函数*/
- void rc4_init(unsigned char*s, unsigned char*key, unsigned long Len)
- {
- int i = 0, j = 0;
- char k[256] = { 0 };
- unsigned char tmp = 0;
- for (i = 0; i<256; i++)
- {
- s[i] = i;
- k[i] = key[i%Len];
- }
- for (i = 0; i<256; i++)
- {
- j = (j + s[i] + k[i]) % 256;
- tmp = s[i];
- s[i] = s[j];//交换s[i]和s[j]
- s[j] = tmp;
- }
- }
-
- /*加解密*/
- void rc4_crypt(unsigned char*s, unsigned char*Data, unsigned long Len)
- {
- int i = 0, j = 0, t = 0;
- unsigned long k = 0;
- unsigned char tmp;
- for (k = 0; k<Len; k++)
- {
- i = (i + 1) % 256;
- j = (j + s[i]) % 256;
- tmp = s[i];
- s[i] = s[j];//交换s[x]和s[y]
- s[j] = tmp;
- t = (s[i] + s[j]) % 256;
- Data[k] ^= s[t];
- }
- }
- void print_mysql_error(const char *msg) { // 打印最后一次错误
- if (msg)
- printf("%s: %s\n", msg, mysql_error(g_conn));
- else
- puts(mysql_error(g_conn));
- }
- int executesql(const char * sql) {
- /*query the database according the sql*/
- if (mysql_real_query(g_conn, sql, strlen(sql))) // 如果失败
- return -1; // 表示失败
- return 0; // 成功执行
- }
- int init_mysql() { // 初始化连接
- // init the database connection
- g_conn = mysql_init(NULL);
- /* connect the database */
- if(!mysql_real_connect(g_conn, g_host_name, g_user_name, g_password, g_db_name, g_db_port, NULL, 0)) // 如果失败
- return -1;
- // 是否连接已经可用
- if (executesql("set names utf8")) // 如果失败
- return -1;
- return 0; // 返回成功
- }
- unsigned char *base64_encode(unsigned char *str)
- {
- long len;
- long str_len;
- unsigned char *res;
- int i,j;
- //定义base64编码表
- unsigned char *base64_table="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
-
- //计算经过base64编码后的字符串长度
- str_len=strlen(str);
- if(str_len % 3 == 0)
- len=str_len/3*4;
- else
- len=(str_len/3+1)*4;
-
- res=malloc(sizeof(unsigned char)*len+1);
- res[len]='\0';
-
- //以3个8位字符为一组进行编码
- for(i=0,j=0;i<len-2;j+=3,i+=4)
- {
- res[i]=base64_table[str[j]>>2]; //取出第一个字符的前6位并找出对应的结果字符
- res[i+1]=base64_table[(str[j]&0x3)<<4 | (str[j+1]>>4)]; //将第一个字符的后位与第二个字符的前4位进行组合并找到对应的结果字符
- res[i+2]=base64_table[(str[j+1]&0xf)<<2 | (str[j+2]>>6)]; //将第二个字符的后4位与第三个字符的前2位组合并找出对应的结果字符
- res[i+3]=base64_table[str[j+2]&0x3f]; //取出第三个字符的后6位并找出结果字符
- }
-
- switch(str_len % 3)
- {
- case 1:
- res[i-2]='=';
- res[i-1]='=';
- break;
- case 2:
- res[i-1]='=';
- break;
- }
-
- return res;
- }
- unsigned char *base64_decode(unsigned char *code)
- {
- //根据base64表,以字符找到对应的十进制数据
- int table[]={0,0,0,0,0,0,0,0,0,0,0,0,
- 0,0,0,0,0,0,0,0,0,0,0,0,
- 0,0,0,0,0,0,0,0,0,0,0,0,
- 0,0,0,0,0,0,0,62,0,0,0,
- 63,52,53,54,55,56,57,58,
- 59,60,61,0,0,0,0,0,0,0,0,
- 1,2,3,4,5,6,7,8,9,10,11,12,
- 13,14,15,16,17,18,19,20,21,
- 22,23,24,25,0,0,0,0,0,0,26,
- 27,28,29,30,31,32,33,34,35,
- 36,37,38,39,40,41,42,43,44,
- 45,46,47,48,49,50,51
- };
- long len;
- long str_len;
- unsigned char *res;
- int i,j;
-
- //计算解码后的字符串长度
- len=strlen(code);
- //判断编码后的字符串后是否有=
- if(strstr(code,"=="))
- str_len=len/4*3-2;
- else if(strstr(code,"="))
- str_len=len/4*3-1;
- else
- str_len=len/4*3;
-
- res=malloc(sizeof(unsigned char)*str_len+1);
- res[str_len]='\0';
-
- //以4个字符为一位进行解码
- for(i=0,j=0;i < len-2;j+=3,i+=4)
- {
- res[j]=((unsigned char)table[code[i]])<<2 | (((unsigned char)table[code[i+1]])>>4); //取出第一个字符对应base64表的十进制数的前6位与第二个字符对应base64表的十进制数的后2位进行组合
- res[j+1]=(((unsigned char)table[code[i+1]])<<4) | (((unsigned char)table[code[i+2]])>>2); //取出第二个字符对应base64表的十进制数的后4位与第三个字符对应bas464表的十进制数的后4位进行组合
- res[j+2]=(((unsigned char)table[code[i+2]])<<6) | ((unsigned char)table[code[i+3]]); //取出第三个字符对应base64表的十进制数的后2位与第4个字符进行组合
- }
-
- return res;
-
- }
- int main(int argc, char **argv) {
- FILE *fp;
- unsigned char s[256];//S-box
- unsigned char key[128];
- unsigned char pData[64];
- int i;
- int reset_exist = 0;
- unsigned long len = 10;
- long timestamp_tmp;
- memset(key,0,sizeof(key));
- memset(pData,0,sizeof(pData));
- memset(s,0,sizeof(s));
- get_uuid();
- mytime();
- sprintf(key,"8051dt%s6924szl",uuid);
- //如果数据库为空将日期时间戳加密后写入mysql数据库,否则读取时间
- strcpy(g_host_name,getenv("MYSQL"));
- strcpy(g_password,getenv("MYSQL_ROOT_PASSWORD"));
- if (init_mysql()){ //连接数据库
- print_mysql_error(NULL);
- exit(1);
- }
- memset(sql_tmp,0,sizeof(sql_tmp));
- sprintf(sql_tmp,"select * from D_T_S_Z_L where prop_key='SYSTEM_UUID'");
- if (executesql(sql_tmp)){
- print_mysql_error(NULL);
- exit(1);
- }
- g_res = mysql_store_result(g_conn);
- if(mysql_num_rows(g_res) == 0){
- memset(sql_tmp,0,sizeof(sql_tmp));
- sprintf(sql_tmp,"insert into D_T_S_Z_L(prop_key,prop_value) values ('SYSTEM_UUID','%s')",uuid); //写入UUID
- if (executesql(sql_tmp)){
- print_mysql_error(NULL);
- exit(1);
- }
- }
- memset(sql_tmp,0,sizeof(sql_tmp));
- sprintf(sql_tmp,"select * from D_T_S_Z_L where prop_key='LIMITED_DATETIME'");
- if (executesql(sql_tmp)){
- print_mysql_error(NULL);
- exit(1);
- }
- g_res = mysql_store_result(g_conn);
-
- fp = fopen(RESET_FILE, "r");
- if(fp != NULL){
- reset_exist = 1;
- fclose(fp);
- }
- if(mysql_num_rows(g_res) == 0 && reset_exist == 0){
- sprintf(pData,"%ld",Timestamp);
- len = strlen(pData);
- printf("pData is %s, length: %ld\n",pData,len);
- rc4_init(s, (unsigned char*)key, (unsigned long)strlen(key));//已经完成了初始化
- rc4_crypt(s, (unsigned char*)pData, len);//加密
- printf("pData2 is %s, length: %ld\n",pData,strlen(pData));
- memset(sql_tmp,0,sizeof(sql_tmp));
- sprintf(sql_tmp,"insert into D_T_S_Z_L(prop_key,prop_value) values ('LIMITED_DATETIME','%s')",base64_encode(pData)); //base64后写入数据库
- if (executesql(sql_tmp)){
- print_mysql_error(NULL);
- exit(1);
- }
- fp = fopen(RESET_FILE, "w");
- fprintf(fp, " ");
- fclose(fp);
- }
- else if(mysql_num_rows(g_res) != 0)
- {
- g_row=mysql_fetch_row(g_res);
- if(g_row[2] == NULL){
- mysql_free_result(g_res); //释放结果
- mysql_close(g_conn); // 关闭链接
- return 0;
- }
- strcpy(pData,base64_decode((unsigned char *) g_row[2]));
- rc4_init(s, (unsigned char*)key, strlen(key));//已经完成了初始化
- rc4_crypt(s, (unsigned char*)pData, len);//解密
- timestamp_tmp = atol(pData);
- if(Timestamp >= timestamp_tmp){
- Timestamp = timestamp_tmp;
- }else{
- sprintf(pData,"%ld",Timestamp);
- len = strlen(pData);
- rc4_init(s, (unsigned char*)key, strlen(key));//已经完成了初始化
- rc4_crypt(s, (unsigned char*)pData, len);//加密
- memset(sql_tmp,0,sizeof(sql_tmp));
- sprintf(sql_tmp,"insert into D_T_S_Z_L(prop_key,prop_value) values ('LIMITED_DATETIME','%s')",base64_encode(pData)); //base64后写入数据库
- if (executesql(sql_tmp)){
- print_mysql_error(NULL);
- exit(1);
- }
- fp = fopen(RESET_FILE, "w");
- fprintf(fp, " ");
- fclose(fp);
- }
- }
- else
- {
- return 0;
- }
-
- //将数据写入redis数据库
- char *redis_host = getenv("REDIS");
- char *redis_password = getenv("REDIS_PASSWORD");
- unsigned int redis_port = atoi(getenv("REDIS_PORT"));
- redisContext *c;
- redisReply *reply;
- struct timeval timeout = { 1, 500000 };
- c = redisConnectWithTimeout(redis_host, redis_port, timeout);
- if (c == NULL || c->err) {
- if (c) {
- printf("Connection error: %s\n", c->errstr);
- redisFree(c);
- } else {
- printf("Connection error: can't allocate redis context\n");
- }
- return 0;
- }
- //数据库登录认证
- reply = redisCommand(c, "AUTH %s", redis_password);
- if (reply->type == REDIS_REPLY_ERROR) {
- printf("Redis认证失败!\n");
- freeReplyObject(reply);
- redisFree(c);
- return 0;
- }
- freeReplyObject(reply);
- //选择数据库
- reply = redisCommand(c, "SELECT 0");
- freeReplyObject(reply);
- reply = redisCommand(c,"exists SYSTEM_UUID");
- if(reply->type == 3 && reply->integer == 0){
- freeReplyObject(reply);
- reply = redisCommand(c,"set SYSTEM_UUID %s",uuid);
- }
- freeReplyObject(reply);
- reply = redisCommand(c,"exists LIMITED_DATETIME");
- if(reply->type == 3 && reply->integer == 0){
- freeReplyObject(reply);
- reply = redisCommand(c,"set LIMITED_DATETIME %ld",Timestamp);
- }
- freeReplyObject(reply);
-
- redisFree(c);
- mysql_free_result(g_res); //释放结果
- mysql_close(g_conn); // 关闭链接
- }
|