⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 s5authlib.c

📁 ss5的socks5认扩展认证模块,实现了基本了认证功能,需要的话,可以进行功能增强扩展. 已在Linux下测试通过.
💻 C
📖 第 1 页 / 共 2 页
字号:
/* * *  File  : S5AuthLib.c *  Author: NieJun *  Mail  : yhniejun@163.com *  Date  : 2008/10/21 * */#include "S5AuthServer.h"#include "S5AuthLib.h"static pthread_t auth_server_thread_id = 0;AuthCode get_s5auth_conf(){    FILE *conf_fp;    char confString[256];    int  get_conf_sts = 0;    char logString[256] = {0};    memset(&S5AuthConf, 0, sizeof(S5AuthConf));    printf("Conf struct size [%d]\n", sizeof(S5AuthConf));    sprintf(S5AuthConf.conf_file, "/etc/s5auth/s5auth.conf");    printf("Conf file [%s]\n", S5AuthConf.conf_file);    if((conf_fp = fopen(S5AuthConf.conf_file, "r")) == NULL )    {        printf("Can not find configure file [%s]!\n", S5AuthConf.conf_file);        return AuthFalse;    }    while(fscanf(conf_fp, "%255s", confString)!= EOF)    {#if 1        if(confString[0] == '#')        {            while(fgetc(conf_fp) != '\n');        }        else if (STREQ(confString, "AuthAddr\0", sizeof("AuthAddr\0")))        {            get_conf_sts = fscanf(conf_fp, "%16s", S5AuthConf.auth_addr);            if (get_conf_sts <= 0)                sprintf(S5AuthConf.auth_addr, "%s", S5AuthAddr);        }        else if (STREQ(confString, "AuthPort\0", sizeof("AuthPort\0")))        {            get_conf_sts = fscanf(conf_fp, "%hd", &(S5AuthConf.auth_port));            if (get_conf_sts <= 0)                S5AuthConf.auth_port = S5AuthPort;        }        else if (STREQ(confString, "db_host\0", sizeof("db_host\0")))        {            get_conf_sts = fscanf(conf_fp, "%20s", S5AuthConf.db_host);            if (get_conf_sts <= 0)                return AuthFalse;                    }        else if (STREQ(confString, "db_user\0", sizeof("db_user\0")))        {            get_conf_sts = fscanf(conf_fp, "%32s", S5AuthConf.db_user);            if (get_conf_sts <= 0)                return AuthFalse;        }        else if (STREQ(confString, "db_passwd\0", sizeof("db_passwd\0")))        {            get_conf_sts = fscanf(conf_fp, "%32s", S5AuthConf.db_passwd);            if (get_conf_sts <= 0)                return AuthFalse;        }        else if (STREQ(confString, "db_name\0", sizeof("db_name\0")))        {            get_conf_sts = fscanf(conf_fp, "%32s", S5AuthConf.db_name);            if (get_conf_sts <= 0)                return AuthFalse;        }        else if (STREQ(confString, "db_port\0", sizeof("db_port\0")))        {            get_conf_sts = fscanf(conf_fp, "%hd", &(S5AuthConf.db_port));            if (get_conf_sts <= 0)                S5AuthConf.db_port = 3306;        }        else if (STREQ(confString, "AuthMode\0", sizeof("AuthMode\0")))        {            get_conf_sts = fscanf(conf_fp, "%hd", (BINT2 *)&(S5AuthConf.auth_mode));            if (get_conf_sts <= 0)                S5AuthConf.auth_mode = AUTH_DATABASE_MODE;        }        else if (STREQ(confString, "LogFile\0", sizeof("LogFile\0")))        {            get_conf_sts = fscanf(conf_fp, "%128s", S5AuthConf.log_file);            if (get_conf_sts <= 0)                sprintf(S5AuthConf.log_file, "%s", DEF_LOG_FILE);        }        /*        else        {            fclose(conf_fp);            return AuthFalse;        }        */#endif    }    //fclose(conf_fp);    //Check confguer value and set default value    if (S5AuthConf.auth_addr[0] == '\0')    {        sprintf(S5AuthConf.auth_addr, "%s", S5AuthAddr);    }    if (S5AuthConf.auth_port == 0)    {        S5AuthConf.auth_port = S5AuthPort;    }        if (S5AuthConf.db_host[0] == '\0')    {        sprintf(logString, "[db_host] not configure\n");#ifdef MAIN_DEBUG_INFO        printf("%s", logString);#endif        fclose(conf_fp);        return AuthFalse;    }    if (S5AuthConf.db_user[0] == '\0')    {        sprintf(logString, "[db_user] not configure\n");#ifdef MAIN_DEBUG_INFO        printf("%s", logString);#endif        fclose(conf_fp);        return AuthFalse;    }    if (S5AuthConf.db_passwd[0] == '\0')    {        sprintf(logString, "[db_password] not configure\n");#ifdef MAIN_DEBUG_INFO        printf("%s", logString);#endif        fclose(conf_fp);        return AuthFalse;    }        if (S5AuthConf.db_name[0] == '\0')    {        sprintf(logString, "[db_name] not configure\n");#ifdef MAIN_DEBUG_INFO        printf("%s", logString);#endif        fclose(conf_fp);        return AuthFalse;    }    if (S5AuthConf.db_port == 0)    {        S5AuthConf.db_port = 3306;    }    if (S5AuthConf.auth_mode == 0)    {        S5AuthConf.auth_mode = AUTH_DATABASE_MODE;    }    if (S5AuthConf.log_file[0] == '\0')    {        sprintf(S5AuthConf.log_file, "%s", DEF_LOG_FILE);    }    fclose(conf_fp);    #ifdef MAIN_DEBUG_INFO    printf("AuthAddr [%s], AuthPort [%d], Db host [%s], \    db_user [%s], db_passwd [%s], db_name [%s], \    db_port [%d], auth_mode [%d], log_file [%s]\n",        S5AuthConf.auth_addr, S5AuthConf.auth_port, S5AuthConf.db_host,         S5AuthConf.db_user, S5AuthConf.db_passwd, S5AuthConf.db_name,         S5AuthConf.db_port, S5AuthConf.auth_mode, S5AuthConf.log_file);#endif    return AuthTrue;} /* End of get_s5auth_conf */AuthCode MakeDaemon(void){    pid_t pid;    pid=fork();    /*    If father then exit    */    if( pid )        exit(0);    /*    If child then ontinue and become process group leader    */    else if( pid != -1 )        setsid(); 	    else        return AuthFalse;    pid=fork();  	    /*    No terminal association    */    if(pid)         exit(0);     else if( pid != -1 )    {        chdir("/");        umask(0);	    }    else        return AuthFalse;  return AuthTrue;} /* End of MakeDaemon() */AuthCode S5AuthServerMake(char *addr, unsigned int port){    int reuseAddrFlag = 1;    if ((S5AuthSocket = socket(AF_INET, SOCK_STREAM, 0)) == -1)    {        return AuthFalse;    }    memset((char *)&S5AuthSsin, 0, sizeof(struct sockaddr_in));    S5AuthSsin.sin_family = AF_INET;    if(port)        S5AuthSsin.sin_port = htons(port);    else        S5AuthSsin.sin_port = htons(S5AuthPort);    if(STREQ(addr, "0.0.0.0", sizeof("0.0.0.0")))        S5AuthSsin.sin_addr.s_addr = htonl(INADDR_ANY);    else        S5AuthSsin.sin_addr.s_addr = inet_addr(addr);    setsockopt(S5AuthSocket, SOL_SOCKET, SO_REUSEADDR, &reuseAddrFlag, sizeof(int));    if (bind(S5AuthSocket, (struct sockaddr *)&S5AuthSsin, sizeof(struct sockaddr_in)) == -1)    {        return AuthFalse;    }    if (listen(S5AuthSocket, 6) == -1)     {        return AuthFalse;    }    return AuthTrue;} /* End of S5AuthServerMake */BINT4 AuthNameHash(const char* str, BINT4 hash_size){    unsigned int hash = 0;    unsigned int x    = 0;    while (*str)    {        hash = (hash << 4) + (*str++);        if ((x = hash & 0xF0000000L) != 0)        {            hash ^= (x >> 24);            hash &= ~x;        }    }    hash = (hash & 0x7FFFFFFF)%hash_size;    return hash;    } /* End of AuthHash */AuthUser *add_auth_user(AuthUser *new_user){    AuthUser *tUser=NULL;    BINT4 index_key;    index_key = AuthNameHash(new_user->UserName, MAX_USER_TBL);    tUser = authUserTbl[index_key];    while (tUser)    {        if (STRCEQ(tUser->UserName, new_user->UserName))        {            //Update user info            memcpy(tUser->PassWord, new_user->PassWord, MAX_PASSWD_LEN);            return tUser;        }        tUser = tUser->next;    }    tUser = (AuthUser *)malloc(sizeof(AuthUser));    if (tUser == NULL)        return NULL;    memset(tUser, 0, sizeof(AuthUser));    memcpy(tUser->UserName, new_user->UserName, MAX_NAME_LEN);    memcpy(tUser->PassWord, new_user->PassWord, MAX_PASSWD_LEN);    if (authUserTbl[index_key])    {        authUserTbl[index_key]->prev = tUser;        tUser->next = authUserTbl[index_key];    }    authUserTbl[index_key] = tUser;    return tUser;        } /* End of add_auth_user */AuthCode db_initialization(){    MYSQL *mysql_sts;    if (mysql_init(&db_conn) == NULL)    {        return AuthFalse;    }    //mysql_sts = mysql_real_connect(&db_conn, db_host, db_user, db_passwd, db_name, MYSQL_PORT, NULL, 0);    mysql_sts = mysql_real_connect(&db_conn, S5AuthConf.db_host, S5AuthConf.db_user, S5AuthConf.db_passwd, S5AuthConf.db_name, S5AuthConf.db_port, NULL, 0);    if (!mysql_sts)        return AuthFalse;    return AuthTrue;    } /* End of db_init */MYSQL_RES *db_query(const char *sql){    MYSQL_RES *result;        if (!sql)        return NULL;    if (mysql_query(&db_conn, sql))        return NULL;    result = mysql_store_result(&db_conn);    if (result == NULL)        return NULL;    return result;    } /* End of db_query */MYSQL_ROW db_next(MYSQL_RES *result){    MYSQL_ROW row;    row = mysql_fetch_row(result);    if (row == NULL)    {        mysql_free_result(result);        result = NULL;        return NULL;    }    return row;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -