防止用户重复登录pptpd以及相关源代码

39 篇文章 4 订阅
问了pptpd的作者,他说在pptpd里并没有提供这个功能,pppd里有方法可以做到。
pppd在完成用户认证以后会执行/etc/ppp/auth-up脚本,并且传一些参数给它,为了查看这些参数,我在这个脚本里写以下的代码:
  1. #!/bin/sh
  2. echo $* > arg.txt
复制代码


一个用户拨入VPN服务以后,生成arg.txt文件,查看这个文件内容:
  1. ppp1 st root /dev/pts/2 115200
复制代码


查看pppd的源代码:
在pppd/auth.c的 network_phase()函数里:
  1.    /*
  2.     * If the peer had to authenticate, run the auth-up script now.
  3.     */
  4.    if (go->neg_chap || go->neg_upap || go->neg_eap) {
  5.        notify(auth_up_notifier, 0);
  6.        auth_state = s_up;
  7.        if (auth_script_state == s_down && auth_script_pid == 0) {
  8.            auth_script_state = s_up;
  9.            auth_script(_PATH_AUTHUP);   /* 调用auth-up 脚本 */
  10.        }
  11.    }
复制代码


在auth.c文件的auth_script()函数里
  1.    argv[0] = script;
  2.    argv[1] = ifname;
  3.    argv[2] = peer_authname;
  4.    argv[3] = user_name;
  5.    argv[4] = devnam;
  6.    argv[5] = strspeed;
  7.    argv[6] = NULL;

  8.    auth_script_pid = run_program(script, argv, 0, auth_script_done, NULL);
  9. }
复制代码


这里可以看到传递给auth-up/auth-down脚本的参数。

所以,可以利用auth-up脚本来防止用户的重复登录。

根据作者提供的脚本样例,我写了auth-up这个脚本:
  1. #!/bin/bash
  2. # get the username from the parameters
  3. USER=$2
  4. # if there is a session already for this user, terminate the old one
  5. if [ -f /var/run/pptpd-users/$USER ]; then
  6.    kill -HUP `cat /var/run/pptpd-users/$USER`
  7.    rm /var/run/pptpd-users/$USER
  8. fi
  9. # remember the pid of the pppd process
  10. PPID=`awk '/PPid/ { print $2; }' /proc/$$/status`
  11. echo $PPID > /var/run/pptpd-users/$USER
复制代码


有了这个脚本以后,用户登录并建立VPN连接以后,再以同样的用户名在其他地方登录成功以后,前一次的连接会被服务端强制断开。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值