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

📄 logincbk.c

📁 基于网络服务控制芯片TF-320的嵌入式web server源代码
💻 C
字号:
/****************************************************************************
*
*	Name:			loginCbk.c
*
*	Description:	Call Back Function
*
*	Copyright:		(c) 2001-2002    Taifatech Inc.
*	                All rights reserved.
*
****************************************************************************/
#ifdef _MSC_VER
#include <Windows.h>
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <reg51.h>
#include "tf320Drv.h"
#include "tfTypes.h"
#include "fs.h"
#include "uip.h"
#include "httpd.h"
#include "html.h"
#include "login.h"
#include "errormsg.h"
#include "md5.h"

extern struct httpd_state *hs;
extern u8_t    xdata MY_IP[4];

u8_t httpd_validate_password(const char *hash);
char *itoah(u8_t bv);
void httpd_send_loginOk();
void httpd_send_loginError();
char ipbuf[16];

u8_t ssi_login(u8_t varid, value_t *vp)
{
    u8_t *ipa;
#ifdef _MSC_VER
    u32_t ipl;
#endif

    switch (varid)
    {
        case TGI_DEVICE_IP:
#ifdef _MSC_VER
            ipl = ntohl(*(u32_t *)MY_IP);
            ipl = htonl(ipl);
            ipa = (char *)&ipl;
            sprintf(ipbuf, "%hu.%hu.%hu.%hu", (u16_t)ipa[0], (u16_t)ipa[1], (u16_t)ipa[2], (u16_t)ipa[3]);
#else
            ipa = MY_IP;
            sprintf(ipbuf, "%bu.%bu.%bu.%bu", ipa[0], ipa[1], ipa[2], ipa[3]);
#endif
            vp->value.string = ipbuf;
            vp->type = string;
            break;
        case TGI_CHALLENGE_VALUE:
            vp->value.string = httpd_make_challenge();
            vp->type = string;
            break;
    }
    return OK;
}

u8_t tgi_login(u8_t cnt, void *vptr)
{
    u8_t i;
    u8_t *username=NULL, *passwd=NULL, *resp=NULL;
    variable_t *vp = vptr;

    /* get the challenge we've sent to the client */
    if (httpd_get_challenge() != OK)
    {
        /* no challenge found or challenge is too old */
        httpd_send_file(fs_open2("login.htm"));
        return OK;
    }

    for (i=0; i < cnt; i++, vp++)
    {
        if (!strcmp(vp->name, "Response"))
            resp = vp->value;
        else if(!strcmp(vp->name, "Username"))
            username = vp->value;
        else if(!strcmp(vp->name, "Password"))
            passwd = vp->value;
    }

    if (!username || !passwd || !resp)
        return ERROR;

    /* validate password of the user */
    if (strlen(username) == strlen(SUPER_USER_NAME)  && 
            !strcmp(username, SUPER_USER_NAME))
    {
        /* validate password */
        if (httpd_validate_password(resp) == OK)
        {
            httpd_send_loginOk();
        }
        else
            httpd_send_loginError();
    }
    else
    {
        httpd_send_loginError();
    }

    return OK;
}

/*
 * non-reentrant
 */
u8_t httpd_validate_password(const char *hash)
{
    u8_t i, k;
    u8_t hashbuf[2*MD5_DIGESTSIZE+1];
    static unsigned char md5_digest[MD5_DIGESTSIZE];
    char *passwd = user_table[0].passwd; 

    // initializing a context that will contain the encrypted string
    md5_init();

    md5_update("admin", sizeof("admin")-1);
    md5_update(passwd, strlen(passwd));
    md5_update(hs->challenge_value, strlen(hs->challenge_value));

    // finally, here is a final string encrypted in hex float format
    md5_final(md5_digest); 

    for (i=0, k=0; i < MD5_DIGESTSIZE; i++, k+=2)
    {
        memcpy(&hashbuf[k], itoah(md5_digest[i]), 2);
    }

    hashbuf[2*MD5_DIGESTSIZE] = '\0';

    /* compare with the challenge response in client's request */
    if (memcmp(hashbuf, hash, 2*MD5_DIGESTSIZE) == 0)
	{
		#ifdef DEBUG_MD5
		DEBUGF(DEBUG_MD5,("\r\n httpd_validate_password() (OK) => hashbuf: %s", hashbuf));
		DEBUGF(DEBUG_MD5,("\r\n httpd_validate_password() (OK) => hash: %s", hash));
		#endif
        return OK;
	}
    else
	{
		#ifdef DEBUG_MD5
		DEBUGF(DEBUG_MD5,("\r\n httpd_validate_password() (ERROR) => hashbuf: %s", hashbuf));
		DEBUGF(DEBUG_MD5,("\r\n httpd_validate_password() (ERROR) => hash: %s", hash));
		#endif
        return ERROR;
    }
}

/*
 * integer to ascii hex
 * Use small cases (abcdef).
 * non-reentrant
 */
char *itoah(u8_t bv)
{
    int ord = 4, i=0;
    unsigned char num;
    static char ch[2];
    while (ord >= 0)
    {
        num = (bv >> ord) & 0x0f;
        if (num <= 9)
            ch[i++] = '0' + num;
        else
            ch[i++] = 'a' + num - 10;
        ord -= 4;
    }

    return ch;
}

⌨️ 快捷键说明

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