A Password Generator

一个简易的密码生成器,python 写的,遵循 2 句 BSD 许可证,应该很好懂,参数什么的可以自己调调。

PS:其实我写完了才发现有个叫 pwgen 的东西,pwgen -cns 16 似乎也不错。

参考:

  1. https://blog.delphij.net/2010/12/post-598.html
  2. phpmyadmin 生成密码的代码:https://github.com/Reen/phpmyadmin/blob/master/js/server_privileges.js#L80
#!/usr/bin/env python2.6
 
# Copyright 2011 Kou Zuyang. All rights reserved.
#
# Redistribution and use in source and binary forms, with or without modification, are
# permitted provided that the following conditions are met:
#
#    1. Redistributions of source code must retain the above copyright notice, this list of
#       conditions and the following disclaimer.
#
#    2. Redistributions in binary form must reproduce the above copyright notice, this list
#       of conditions and the following disclaimer in the documentation and/or other materials
#       provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY Kou Zuyang ``AS IS'' AND ANY EXPRESS OR IMPLIED
# WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
# FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> OR
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
# ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
# NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
# ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
# The views and conclusions contained in the software and documentation are those of the
# authors and should not be interpreted as representing official policies, either expressed
# or implied, of Kou Zuyang.
 
import random;
 
def generate_password():
    alphabet = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNPOPQRSTUVWXYZ'
    number = '1234567890'
    special = '!@#$%^&amp;*()-=_+[]{};:,./\~`?'
    charset = alphabet * 3 + number * 7 + special * 2
    print ''.join([random.choice(charset) for x in xrange(16)])
 
if __name__ == "__main__":
    generate_password()
Tags:,,

Posted in Information Technology | Tagged , , | Leave a comment

Get Nagios to Work with Nginx (Gentoo)

今天装 Nagios,开源监控软件。

照例第一步:

emerge net-analyzer/nagios

这个包是个 meta 包,里面似乎有一个内核和一些插件,以后再研究,先搭起来再说。

跑起 Nagios:

/etc/init.d/nagios start
rc-update add nagios default

安装完成之后它会告诉你,要是 Apache 或者 Lighttpd 的话我会给你配好,不是的话自己看着办吧,于是我就自己来了。:-(

首先安装 spawn-fcgi 和 fcgiwrap,第一个是管理 fcgi 进程的,第二个是把 CGI 包装成 fcgi 的。fcgiwrap 这兄弟目前被 ~x86 mask 了,先 unmask。

echo www-misc/fcgiwrap ~x86 >> /etc/portage/package.keywords

然后安装:

emerge www-servers/spawn-fcgi www-misc/fcgiwrap

跑起 spawn-fcgi:

sudo spawn-fcgi -a 127.0.0.1 -p 22222 -P /var/run/fcgiwrap.pid -u nagios -g nagios -- /usr/sbin/fcgiwrap

-a 是主机,-p 是端口(用 unix domain 也行),-P 是 pid 文件,-u 是用户,-g 是组,-- 后面是要运行的命令,用户名和组一定要对,主机和端口要跟 nginx 对上。这条命令是起 CGI 部分的,Nagios 也用了 PHP,PHP 请通过 fcgi 自行跑起来 :-) ,php-fpm 或者 spawn-fcgi 均可,参考 WordPress 之类就行。

下面是 nginx 的配置,主要是抄的别人的,主体参考这里,另外也参考这里。尤其是fastcgi_param AUTH_USER $remote_user; fastcgi_param REMOTE_USER $remote_user; 两句,前一篇没有,害惨我了,nagios 一直说认证不通过……

自己发挥改改就好撒。

location / {
        auth_basic "Restricted";
        auth_basic_user_file /PATH/TO/HTPASSWD;
 
        root /usr/share/nagios/htdocs;
 
        location ~ \.php$ {
                fastcgi_pass   YOUR_PHP_UPSTREAM;
                fastcgi_index  index.php;
                fastcgi_param  SCRIPT_FILENAME  /usr/share/nagios/htdocs$fastcgi_script_name;
                include fastcgi_params;
        }
 
        location /nagios/ {
                location /nagios/images/ {
                        gzip off;
                }
                expires 30d;
                alias /usr/share/nagios/htdocs/;
        }
 
        location /docs/ {
                root /usr/share/nagios/htdocs/;
                index index.html;
        }
 
        location ~ \.cgi {
                fastcgi_pass 127.0.0.1:22222;
                fastcgi_index index.cgi;
                fastcgi_param SCRIPT_FILENAME /usr/lib$fastcgi_script_name;
                include fastcgi_params;
                fastcgi_param  AUTH_USER          $remote_user;
                fastcgi_param  REMOTE_USER        $remote_user;
        }
}

关于里面提到的 htpasswd,是 basic auth 用的,咋生成看文档吧。

Restart Nginx & enjoy!

参考:

  1. http://yemaosheng.com/?p=899
  2. http://www.matejunkie.com/howto-let-nginx-serve-the-nagios-web-interface/
  3. http://wiki.nginx.org/HttpAuthBasicModule
Tags:,,,,,,

Posted in Information Technology | Tagged , , , , , , | 2 Comments

禁止 ssh 密码登陆

/etc/ssh/sshd_config 里面

PasswordAuthentication no
UsePAM no

为啥这样不要问我……去看看 /etc/ssh/sshd_config 里面的注释或者 man sshd 之类的吧,因为我也说不非常清楚,哈哈哈哈。

Tags:,

Posted in Information Technology | Tagged , | Leave a comment

解决 WordPress 自动升级中遇到的一个验证问题

WordPress 要升级 3.0.5,我懒,所以自动升级,点开自动升级的页面,输入验证方式,发现木有 sftp 的选项,印象中原来是有的,去查了一下,发现不是 WordPress 的问题,而是 PHP 没有装载 ssh 的模块,于是装上。

emerge dev-php5/pecl-ssh2

然后升级页面就神奇地出现了 SSH2 的选项,于是开心地填上用户名密码,一个回车,告诉你用户名或密码错误……我说不可能啊,于是去查系统日志,日志里竟然一个字都没写……明显就不是密码错了嘛。

啥问题呢,不清楚,咋办呢,查 WordPress 的源码吧。发现 wp-admin/includes/file.php 里有个函数叫 get_filesystem_method(),它的注释告诉我 WordPress 升级写文件有这么几种方式:Direct, SSH2, FTP PHP Extension, FTP Sockets,优先级依次递减,于是发现还有 Direct 这么一种神奇的方式,快速解决问题: chown -R nobody:nobody /path/to/WordPress。(我用 php-fpm 运行的,php 的 user 是 nobody)

不能把问题放着不管是不?去看看是啥问题,在 WordPress 源码里搜 "Username/Password incorrect for",发现几个文件里都有,分别对应几种连接方式:direct、ssh2、ftpext、ftpsockets。当然我们要看 SSH2 是不?于是这个文件是 wp-admin/includes/class-wp-filesystem-ssh2.php,里面那个 connect() 函数是我感兴趣的,里面有这么一句:

if ( !$this->keys ) {
  if ( ! @ssh2_auth_password($this->link, $this->options['username'], $this->options['password']) ) {
    $this->errors->add('auth', sprintf(__('Username/Password incorrect for %s'), $this->options['username']));
    return false;
}

也就是说没有使用 key 的时候,用密码验证,ssh2_auth_password() 失败的话就说是用户名或密码错。(PHP 达人告诉我前面的 @ 是啥?)

查这个函数的原型,人家只说失败的时候返回 false,谁说一定是密码错误了,你妹的……

不过在这个文件里还有新发现,在这个类的注释里,发现了一篇 pecl-ssh2 的安装指南,在里面发现了问题:On the serverside, 'PasswordAuthentication yes' must be enabled in the sshd_config. 一查我的配置,果然给弄成 no 了……

欣喜若狂地改成 yes,然后重启 sshd,/etc/init.d/sshd restart,竟然没法重启……尝试 reload,/etc/init.d/sshd reload,说 sshd 没运行(No /usr/sbin/sshd found running; none killed.),又开始扯淡了!!!

遵循不知道谁的教导,重新 emerge 一遍 openssh。

emerge net-misc/openssh

然后

/etc/init.d/sshd restart

就能重启了……

之后回到 WordPress 输上密码,发现能登录上了,变成 PHP 错误了……仔细一看,我的账户也没有改 WordPress 代码的权限啊……

折腾一圈白干了,专门弄个账户还不如改权限(chown -R nobody:nobody)呢。

不过过程中还发现了新的问题,sshd_config 里面 PasswordAuthentication no 之后还是能从别的主机用密码登陆(不过 php 这个 ssh2 的库登陆不了了),很神奇吧……以后再看是什么问题吧。

大概找到是啥问题了,下篇发出来吧。

Tags:,,,,,

Posted in Information Technology | Tagged , , , , , | 4 Comments

Gentoo/Nginx with IPv6 (Tunnel) Support

弄了一个 he.netIPv6 隧道,给服务器用。(你说你 Linode 为啥不提供原生 IPv6,为啥?)

一会就申请完了,你就获得 4 个 IP,服务器的 IPv4、IPv6,客户端的 IPv4、IPv6,你需要这些 IP 来做一些配置。

首先是配好 Gentoo,LInode 的 wiki 写的很清楚,注意 interface 的名字似乎不能太短,一开始我起的叫 he,出了奇怪的错误……改成 hetun 就好了……

注意 Linode 的 eth0 是用的 DHCP,别乱改。

接下来是 Nginx,因为编译的时候已经带上 USE="ipv6" 了,所以就不需要重新编译了,改一下 Listen 语句就行。我原先都是用的 Listen 0.0.0.0:80,要使 IPv6 有效的话要改成 [::]:80,于是尝试性地改了一个域名的配置。

重启 Nginx,报错,说 80 端口已经被 bind 了,我说不可能啊,lsof -i tcp:80 也是空的。想了想隐约知道是怎么回事了,于是把所有的配置文件都从 Listen 0.0.0.0:80 改成 Listen [::]:80,重启,OK 了。

想来是因为 Nginx 把 0.0.0.0:80 和 [::]:80 认成了两个端口,于是 bind 了两遍,这不就冲突了嘛……都改成 Listen [::]:80 就好了。

参考文献:

  1. http://kovyrin.net/2010/01/16/enabling-ipv6-support-in-nginx/
  2. tunnelbroker.net 给的配置命令
  3. http://www.linode.com/wiki/index.php/IPv6#Gentoo
Tags:,,,,

Posted in Information Technology | Tagged , , , , | Leave a comment