Sunday, December 14, 2008

chmod和sticky位

上次那了关于set-user-ID, 这一次一起说linux下两个特殊的位:
1. set-user-ID
2. sticky bit
set-user-ID的作用这就不说了(上次写了), 说一下sticky的作用:

Sticky Bit-The Sticky Bit is only valid on directories. When the Sticky Bit is set on a directory, users are only allowed to delete files they own. Without this setting, users are allowed to delete files owned by other users as long as they have the write (w) permission on the directory. Special permissions are stored as part of the regular mode of the file, but there is no room for three more permission bits for these permissions. Because of this, special permissions are actually included within the execute (x) bit of user, group and other permissions. Figure 3.13 and Figure 3.14 demonstrate how these permissions are displayed when the corresponding execute bit is set and not set, respectively.

* 意思就是有sticky位的文件只能被owner删除和rename.
* linux下不起作用.
* -_-! 写本文前, 不知道linux下是不行的. 无法往下写了! 下面就说chmod的使用吧

下面是linux下, 文件的permission的基本信息:









chmod有两种方式:
1. 使用字符串方式指定文件的权限
2. 使用数字方式指定文件的权限.

先提出两个常看权限问题:
1. 把文件的权限设置成owner可读写,group只读,other只读
1. 把文件的group权限修改为可读写

字符串的方式:
常用来修改文件其中的一组权限.(文件的权限分为三组)
数字的方式:
常用来一次设置文件的三组权限

使用字符串的方式去设置上面的第一个问题:
# chmod u=rw,g=r,o=r FileName
上面的方式中, 一次修改了文件的三组权限.
使用数据的方式去设置:
# chmod 0644 FileName

看起来, 都可以完成. 其实, 字符串的方式比较麻烦, 数字的方式比较精炼.

重点在第二个问题!对于第二个问题如下方法:
# chmod g+w FileName

这只能使用字符串的方式, 如可要使用数字的方式, 需要有逻辑介入

所为, 字符串的方式重点在于修改文件的一组权限.而字符串的方式重点在于设定文件的三组权限

字符串的方式还有一种用法:
1. -X(大写) 如果文件或目录指定的一组权限有x位, 就去掉, 没有就保留原状
2. +X(大写) 如果目录指定的一组权限没有x位,就增加, 有就保留原状, 对正规文件不起作用

下面是man手册中chmod使用八进制方式表达:
A numeric mode is from one to four octal digits (0-7), derived by
adding up the bits with values 4, 2, and 1. Omitted digits are assumed
to be leading zeros. The first digit selects the set user ID (4) and
set group ID (2) and restricted deletion or sticky (1) attributes. The
second digit selects permissions for the user who owns the file: read
(4), write (2), and execute (1); the third selects permissions for
other users in the file's group, with the same values; and the fourth
for other users not in the file's group, with the same values.

数字的方式没有其它的特点了吗? 也不是的, 至少sticky位只能使用数字的方式. 如下:
set-user-ID和sticky位的情况比较特殊:
* set-user-ID只有user和group两种情况, 没有other的情况
* 对于sticky位只有一种情况:有与没有.
那么, 就算这两种情况都出现, 刚好为 4+2+1=7

如: chmod 7755 file

珠海一日徒步

今天的大概步印如下图:
















最无语的是板樟山的隧道没有人行道, 两个无畏的年轻人决定爬山翻过山脉, 结果........ 太过有难度的山路在前面等着.... 又想起<<into the wild >>电影的悲惨结果..... 只好原路返回!!!!! 不过, 山上的景色很好..... 特别是山泉水.

Saturday, December 13, 2008

关于set-user-ID位

今天, 有人问我:为什么一般用户可以在X环境下, 可以把机器给关了, 但是在console下就要使用root权限.

其实, 这个问题很简单的. 一个很常看的例子就是sudo程序了. 看看吧:

jessinio@jessinio-laptop:/tmp$ ls -l $(which sudo)
-rwsr-xr-x 2 root root 115136 2008-09-01 21:17 /usr/bin/sudo

两个特点:
1. sudo的拥有者为root
2. 有一个"s"位

什么是"s"位, 它又有什么作用的呢?

"s"位就是"set-user-ID"的意思, 作用就是让其它用户运行有"s"位的程序时都可以等于程序的拥用者运行此程序一样.

就如sudo的例子, jessinio用户可以sudo时, 可以等于root(sudo程序的拥有者)运行一样.(这里不谈与sudo与sudoers文件的关系)

set-user-ID的得到是从chmod(system call)得到的.如:

jessinio@jessinio-laptop:/tmp$ sudo chmod u+s wrapper.o

这里有两个问题:
1. 对脚本文件不起作用(原理后面解释)
2. symbolic links文件不起作用

对于脚本的问题, 由于脚本是被解释器运行的, 如果解释器没有set-user-ID位的话, 脚本文件有set-user-ID位也没有意思

比如:readme.txt有set-user-ID, 使用vim编辑这个文件也不行使vim进程有root权限的

对于symbolic links的问题, 在chmod的man手册中就说清楚了:

chmod never changes the permissions of symbolic links; the chmod system call cannot
change their permissions. This is not a problem since the permissions of symbolic links
are never used. However, for each symbolic link listed on the command line, chmod
changes the permissions of the pointed-to file. In contrast, chmod ignores symbolic
links encountered during recursive directory traversals.

上面的是linux的chmod的man手册.

但是在FreeBSD中, chmod是可以使用"-h"参数去令chmod程序去修改文件的权限(当然有set-user-ID), 但是, 还是不起作用的, 因为对于unix/linux系统来说, symbolic links文件的permissions是被忽略的

问题又来了, 对于写脚本的人来说, 怎么利用上set-user-ID呢?

答案是使用编译型语言写wrapper

简单的例子如下:

#include
int main (int argc, char ** argv) {

int err;
char *newenv[] = { NULL };

if ((err = execle("./runfile.py", "pauseme", NULL, newenv)) < 0 ) {
exit(err);
}

return 0; // never reached!
}

其中, runfile.py就是这个目标wraper程序要去调用的脚本程序. 这样, 当wrapper程序有set-user-ID位的情况下, runfile.py脚本就会被set-user-ID用户去调用, 就是等于有set-user-ID位了^_^


上面的wrapper.c文件的处理方法:

jessinio@jessinio-laptop:/tmp$ gcc wrapper.c -o wrapper.o
jessinio@jessinio-laptop:/tmp$ sudo chmod root:root wrapper.o
jessinio@jessinio-laptop:/tmp$ sudo chmod u+s wrapper.o

注意: 每次使用chmod设置permissions后, set-user-ID都会被去掉的.可以重新设置它

Thursday, December 11, 2008

FreeBSD下的mysql配置

FreeBSD下的mysql在install后, 在/etc/rc.conf中增加:

mysql_enable="YES"

启动mysql_server

ps auxwww|grep mysql

可以看到两个重要参数:
1. my.ini文件的位置
2. db文件的位置

在linux( centOS )下只可以看到db文件的位置. 因为my.ini默认去/etc/my.ini找.

一般需要做如下设置:
1. 指定mysql的编码(my.cnf)
2. 把db文件指到其它地方

实际上, my.cnf是放在db文件夹内的.(可以从rc脚本中看到)


1. 配置/etc/rc.conf

查看rc脚本中的变量, 可以在/etc/rc.conf加入一个参数:
mysql_dbdir="/path/to/database"

使用utf-8编码:

在my.ini中的[mysqld一节中增加:
character_set_server=utf8

urxvt的配置

常有人问: 有了gnome-terminal, konsole, 为什么还使用古董级的rxvt呢?

其实, rxvt满足了我一个要求: 使用screen时可以使用滚动条(mouse和touchpad的)

我的rxvt没有什么配置, 只是之前在gnome-terminal的theme

!real transparent
URxvt.background:rgba:0000/0000/0000/E000
URxvt.depth:32

URxvt.colorBD:yellow
URxvt.colorUL:green
URxvt.multichar_encoding:utf-8
URxvt.scrollBar:Fault
URxvt.scrollBar_right:True
URxvt.scrollBar_floating: True
URxvt.scrollstyle: next
URxvt.saveLines:10000

!color scheme
URxvt.foreground:#27CF38
URxvt.color0:black
URxvt.color1:#AA0000
URxvt.color2:#6C93F2
URxvt.color3:#AA5500
URxvt.color4:#DED03A
URxvt.color5:#AA00AA
URxvt.color6:#00AAAA
URxvt.color7:#AAAAAA
URxvt.color8:#555555
URxvt.color9:#FF5555
URxvt.color10:#55FF55
URxvt.color11:#FFFF55
URxvt.color12:#5555FF
URxvt.color13:#FF55FF
URxvt.color14:#55FFFF
URxvt.color15:#FFFFFF

!fonts setting
URxvt.font:xft:Monospace:size=16

由newsyslog引发的问题之一: apache的reload

一直很喜欢nginx的reload, 实际上就是sighup, 如:

kill -HUP $(cat /var/run/nginx.pid)

在apache的rc脚本中, 是使用这种方法:

httpd -DNOHTTPACCEPT -k graceful

但是, 本文都是newsyslog引发的问题, newsyslog要求什么呢?

newsyslog需要使用signal去使其它程序(如apache等)reload, 上apache的rc脚本中, 看不到signal.

google了一把, 找到以下好信息

* reload - same as 'apachectl graceful' (sends USR1 signal)
* restart - same as 'apachectl restart' (sends HUP signal)
* reload-modules - same as stop + start

Monday, December 1, 2008

网络分析工具的工作原理

有些好怪网络公司的网站访问量统计分析工具的基本原理

在一时想不明的情况下, 注册了google公司的分析工具:

https://www.google.com/analytics/

在注册的过程看到以下一段文字:
Copy the following code block into every webpage you want to track immediately before the tag

哈哈, 还没有看google提供的javascript脚本, 已经知道基本原理了.

在自己的页面上增加一段google的analytics文件, 这就可以让analytics知道不少基本信息了, 如果有空再看一下google analytics提供的javascipt脚本吧