荔园在线

荔园之美,在春之萌芽,在夏之绽放,在秋之收获,在冬之沉淀

[回到开始] [上一篇][下一篇]


发信人: georgehill (清风浮云 人生), 信区: Linux
标  题: 一个简单的telnetd防护程序(2)(转寄)
发信站: BBS 荔园晨风站 (Wed Oct 25 18:14:41 2000), 站内信件

【 以下文字转载自 georgehill 的信箱 】
【 原文由 georgehill.bbs@smth.org 所发表 】
发信人: lisuke (smart), 信区: Security
标  题: 一个简单的telnetd防护程序(2)(转寄)
发信站: BBS 水木清华站 (Wed Oct 25 00:27:02 2000)

防火墙模块:ipauth.c
/************************************************************************/
/* ipauth.c, by digger                                                  */
/* ipauth read the file that include all IPs that authorized to access  */
/* some services of localhost, the format is just like:                 */
/************************************************************************/
/* # this is one comments line begin with "#"                           */
/* 172.18.85.0          # allow subnet
/* 172.18.86.146                                                        */
/* 172.18.86.145                                                        */
/* ...                                                                  */
/************************************************************************/
/* function InitAuthIP read the authorized IP into memory array, and    */
/* function IPIsAuthed check if the given IP is authorized              */
/************************************************************************/
# include       <stdio.h>
# include       <sys/types.h>
# include       <string.h>
# include       <sys/socket.h>
# include       <netinet/in.h>
# include       <arpa/inet.h>
# define        MAXHOSTS        32
# define        TRUE            0
# define        FALSE           -1
u_long  AuthedIP[MAXHOSTS];     /* authorized IPs */
int     AuthedIPNum;            /* number of authorized IPs */
void InitAuthIP(char *file)     /* read IP from file into memory array */
{
        FILE    *fp;
        char    sBuf[64];
        char    *tmp;
        char    *s;
        u_long  IP;
        if ((fp  = fopen(file,"r")) == NULL) {
                fprintf(stderr, "fopen %s error, terminated\n", file);
         exit(-1);
        }
        AuthedIPNum = 0;
        while (AuthedIPNum < MAXHOSTS && !feof(fp) && fgets(sBuf, 64, fp)) {
                tmp = sBuf;
                s = strtok(tmp, " \t\r\n");
                if (s == NULL) continue;        /* ignore empty line */
                if (s[0] == '#') continue;      /* ignore commits line */
                if ((IP = inet_addr(s)) != -1) {
                        AuthedIP[AuthedIPNum ++] = IP;
                }
        }
        if (AuthedIPNum == 0) {                 /* default Authorized IP */
                AuthedIP[0] = inet_addr("127.0.0.1");
                AuthedIPNum ++ ;
        }
        fclose(fp);
}
int IPIsAuthed(u_long IP)
                AuthedIP[0] = inet_addr("127.0.0.1");
                AuthedIPNum ++ ;
    }
        fclose(fp);
}
{nt IPIsAuthed(u_long IP)
        int     i;
        for (i = 0;i < AuthedIPNum;i ++) {
                if ((AuthedIP[i] & (u_long)255) == 0) { /* subnet */
                        if ((AuthedIP[i] & IP) == AuthedIP[i])
                                break;
                } else if (AuthedIP[i] == IP) {         /* ip */
                        break;
                }
        }
        if (i == AuthedIPNum) return FALSE;
        else return TRUE;
}

--

※ 来源:·BBS 水木清华站 smth.org·[FROM: 162.105.30.203]
--
※ 转载:·BBS 荔园晨风站 bbs.szu.edu.cn·[FROM: 192.168.1.116]


[回到开始] [上一篇][下一篇]

荔园在线首页 友情链接:深圳大学 深大招生 荔园晨风BBS S-Term软件 网络书店