这几天配置OpenVPN,使用了用户名密码的身份认证方式,借助已有的postfix邮箱帐号,省去了再为每个人设置用户名密码的麻烦。
原理很简单,OpenVPN服务器配置里有这样一句:
auth-user-pass-verify /etc/openvpn/auth-postfix-mailbox.py via-env
就是说要用/etc/openvpn/auth-postfix-mailbox.py这个脚本来验证用户名和密码。用户名和密码如何传递给它呢?via-env,环境变量。
脚本如下:
- #!/usr/bin/env python
-
- import os
- import sys
- from MySQLdb import *
- import md5crypt
-
- def auth(username, password):
- conn = connect (host = 'localhost',
- user = 'dbuser',
- passwd = 'dbpasswd',
- db = 'postfix')
- cursor = conn.cursor()
- cursor.execute("""
- select password from mailbox
- where username=%s
- and active=1
- """, (username))
- row = cursor.fetchone()
- if row == None:
- return 1
- crypt = md5crypt.md5crypt(password, row[0])
- cursor.execute("""
- select * from mailbox
- where username=%s
- and password=%s
- and active=1
- """, (username,crypt))
- row = cursor.fetchone()
- cursor.close()
- conn.close()
- if row == None:
- return 1
- return 0
-
- def main():
- status = 0
- try:
- username = os.environ['username']
- password = os.environ['password']
- status = auth(username, password)
- except:
- sys.exit(1)
-
- sys.exit(status)
-
- if __name__ == "__main__":
- main()
由于postfix使用md5认证,所以需要用md5crypt这个模块,从这里可以下载到。
Related posts
1. 使用MySQL数据库
1.1 使用UTF-8字符集
sqlobject.dburi="mysql://root:@localhost:3306/dbname?sqlobject_encoding=utf-8&read_default_file=my.cnf"
同时,my.cnf里在client一节加入 default-character-set = utf8
1.2 使用InnoDB类型的表
修改my.cnf,mysqld一节加入 default-storage-engine=INNODB
1.3 自身外键关联
使用InnoDB类型的表,tg-admin sql drop 时会遇到由于外键约束而无法删除的问题,可以改用MyISAM类型的表。tg-admin sql create时会有警告:
Warning: a circular reference was detected in the model. Unable to sort the classes by dependency: they will be treated in alphabetic order. This may or may not work depending on your database backend. The error was:
Found a circular reference: ......
2. SQLObject
2.1 MultipleJoin最好指定joinColumn
3. Kid模板
3.1 用defined(varname) 可以判断一个变量是否存在于模板中,value_of则可以指定变量不存在时的默认值
3.2 results.count()可以得到查询结果集的大小
3.3 根据条件输出不同的内容 ${condition and ‘a’ or ‘b’}
比如交替表格中行的背景色
<tr py:for="i, row in enumerate(rows)" class="${i%2==0 and 'odd' or 'even'}">
...
</tr>
在select的option标签里输入selected属性
...
<option value="..." selected="${condition and 'selected' or None}">... </option>
...
Related posts
最近用PyMaemo写一个N800上跑的程序时用到的:
- self.mote_pixbuf = gtk.gdk.pixbuf_new_from_xpm_data(MOTE_PIXMAP)
- self.mote_gray_pixbuf = self.mote_pixbuf.copy()
- self.mote_pixbuf.saturate_and_pixelate(self.mote_gray_pixbuf, 0.0, True)


Related posts
Recent Comments