Friday, April 25, 2008

How to patch your OpenBSD

Every OS needs to be patched, even for OpenBSD, either for security reasons, reliability ones, bug fixes or new functions.

To patch OpenBSD, you need first to know whether there are any patches released/applicable for your version of release. For OpenBSd, there are two ways you can check if there are any patches available. First, and recommended, is to check the errata (http://www.openbsd.org/errata.html) page. Second is to subscribe to "announce " and "security-announce" mailing lists. for more details on how, check OpenBSD web page or send a mail to majordomo@openbsd.org with subject "help".


In OpenBSD, there are 3 ways to patch your system with all the patches.
1. upgrade your system to -current branch, since all patches and fixes are incorporated into -current.

This is not suitable for most users because of the ever-changing code for -current.

2. upgrade your system to -stable branch of your your release.

By doing this, you'll need to fetch or update your source tree using the appropriate -stable branch, and recompile the kernel and userland files. While this is the easiest way and is OK for most users, it take quite a while to download source files and recompile the system, especially for these who has limited bandwidth to Internet.

3. Patch, compile and install individual impacted files.

This is what we will use for our example below. While this requires less bandwidth and typically less time than an entire cvs(1) checkout/update and source code compilation, this is sometimes the most difficult option, as there is no one universal set of instructions to follow. Sometimes you must patch, recompile and install one application, other times, you might have to recompile entire sections of the tree if the problem is in a library file.

Once you've identified the patch you need to apply to your system, here are the steps to follow:

++++++++++++++++++Following lines are from www.openbsd.org/faq/faq10.html:

Applying patches.

Patches for the OpenBSD Operating System are distributed as "Unified diffs", which are text files that hold differences to the original source code. They are NOT distributed in binary form. This means that to patch your system you must have the source code from the RELEASE version of OpenBSD readily available. In general, you should have the entire source tree available. If you are running a release from official CDROM, the source trees are available on disk 3, they are also available as files from the FTP servers. We will assume you have the entire tree checked out.

For our example here, we will look at patch 001 for OpenBSD 3.6 dealing with the st(4) driver, which handles tape drives. Without this patch, recovering data from backups is quite difficult. People using a tape drive need this patch, however those without a tape drive may have no particular need to install it. Let's look at the patch:

# more 001_st.patch
Apply by doing:
cd /usr/src
patch -p0 < 001_st.patch

Rebuild your kernel.

Index: sys/scsi/st.c
===================================================================
RCS file: /cvs/src/sys/scsi/st.c,v
retrieving revision 1.41
retrieving revision 1.41.2.1
diff -u -p -r1.41 -r1.41.2.1
--- sys/scsi/st.c 1 Aug 2004 23:01:06 -0000 1.41
+++ sys/scsi/st.c 2 Nov 2004 01:05:50 -0000 1.41.2.1
@@ -1815,7 +1815,7 @@ st_interpret_sense(xs)
u_int8_t skey = sense->flags & SSD_KEY;
int32_t info;

- if (((sense->flags & SDEV_OPEN) == 0) ||
+ if (((sc_link->flags & SDEV_OPEN) == 0) ||
(serr != 0x70 && serr != 0x71))
return (EJUSTRETURN); /* let the generic code handle it */

As you will note, the top of the patch includes brief instructions on applying it. We will assume you have put this patch into the /usr/src directory, in which case, the following steps are used:

# cd /usr/src
# patch -p0 < 001_st.patch
Hmm... Looks like a unified diff to me...
The text leading up to this was:
--------------------------
|Apply by doing:
| cd /usr/src
| patch -p0 < 001_st.patch
|
|Rebuild your kernel.
|
|Index: sys/scsi/st.c
|===================================================================
|RCS file: /cvs/src/sys/scsi/st.c,v
|retrieving revision 1.41
|retrieving revision 1.41.2.1
|diff -u -p -r1.41 -r1.41.2.1
|--- sys/scsi/st.c 1 Aug 2004 23:01:06 -0000 1.41
|+++ sys/scsi/st.c 2 Nov 2004 01:05:50 -0000 1.41.2.1
--------------------------
Patching file sys/scsi/st.c using Plan A...
Hunk #1 succeeded at 1815. <-- Look for this message!
done

Note the "Hunk #1 succeeded" message above. This indicates the patch was applied successfully. Many patches are more complex than this one, and will involve multiple hunks and multiple files, in which case, you should verify that all hunks succeeded on all files. If they did not, it normally means your source tree is not right, you didn't follow instructions carefully, or your patch was mangled. Patches are very sensitive to "white space" -- copying and pasting from your browser will often change tab characters into spaces or otherwise alter the white space of a file, making it not apply.

At this point, you can build the kernel as normal, install it and reboot the system.

Not all patches are for the kernel. In some cases, you will have to rebuild individual utilities. At other times, will require recompiling all utilities statically linked to a patched library. Follow the guidance in the header of the patch, and if uncertain, rebuild the entire system.

Patches that are irrelevant to your particular system need not be applied -- usually.

Sunday, April 13, 2008

use proxy authentication with perl

Here is a simple script to grab a URL using proxy that requires authentication and using different port for different service:

use strict;
use LWP::UserAgent;
use HTTP::Request;

my $Silverfox = LWP::UserAgent->new();
$Silverfox->proxy(http =>'http://username:pwd@myproxy.com:8080');
$Silverfox->proxy(ftp =>'username:pwd@http://myproxy.com:81');
$Silverfox->no_proxy(qw('127.0.0.1' local));

# initialize proxy settings from environment variables
#$Sliverfox->env_proxy;

my $req = HTTP::Request->new(GET =>'http://www.bsdplus.cn');
print $Silverfox->request($req)->as_string;

Monday, June 11, 2007

上海ADSL无法访问此blog

最近在家无法访问http://alan0203.blogspot.com,因此也无法更新。
目前不得不考虑购买付费空间。无论如何,如果您对BSD感兴趣,请继续关注http://alan.thechengs.name,它会自动转到相应的blog(目前是http://alan0203.blogspot.com).

谢谢!

Monday, June 04, 2007

VI:一旦拥有,别无所求(2)

学习如何在打开的文档中移动光标是非常重要的基础,尤其是对于后面的复制或删除操作。之后,我们就学习如何插入文本和编辑文档。

进入插入模式有以下六种常见的方式:
1. i:在光标所在字符的左侧添加文本/字符
2. I:在当前行的行首插入
3. o:在当前行的下方新建一行并开始插入
4. O:当当前行的上方新建一行并插入
5. a:在光标所在字符的右侧添加文本/字符
6. A:在当前行的行尾插入

除了这六种方式之外,还有一个比较复杂一点的:

7. c{motion}:选中motion动作覆盖的文本并进入插入模式,用新的文本替换选中的内容.这里的motion就是指前面提到的移动操作。比如c$就是选中从光标所在位置到当前行尾的文本并进入插入模式。


拷贝和剪切操作
d:剪切选中的文本,并将文本放到剪切本中。
y:复制选中的文本
c:同d,但是会使vi进入插入模式,见上面c{motion}

p:粘贴。这个恐怕是vi中最简单的命令了,只需将光标移动到目标位置,按p将复制的文本粘贴

Friday, June 01, 2007

VI: 一旦拥有,别无所求

"To me, vi is Zen.
To use vi is to practice zen.
Every command is a koan.
Profound to the user,
unintelligible to the uninitiated.
You discover truth every time you use it."
--reddy@lion.austin.com

"对我来说,vi就是禅。
用vi就是坐禅。
每一个命令就是一个心印
对皈依者意义深远
对不了解者不可捉摸
每一次使用都会有新的发现"

VI是所有*NIX类系统都内置的一个命令行文本编辑软件,功能强大,命令繁多。初学时很难上手,但是一旦你熟悉之后,相信你不会再想使用其他的编辑软件。

本篇假定你已经熟悉VI的一些基本命令,如:
vi httpd.conf #打开一个名为httpd.conf的文档,如果不存在,将在当前目录下创建此文档;
输入“:wq”:存盘推出命令。
输入“:q!”:不存盘,强制推出
ESC:在命令模式与输入模式之间切换
i,I或者o等:插入文本命令........

VI之一:光标移动篇

这里我们先学习一下如何在一篇打开的文档中进行光标移动。

1. 方向键h,j,k,l.
h: 将光标向移动一个字符
j: 将光标向移动一个字符
k: 将光标向移动一个字符
l: 将光标向移动一个字符

这四个键相当于方向键,另设它们的好处是:你的手指不必离开正常的输入位置

2. 按字符移动的命令
fx: 移动光标至当前行下一个x字符处;使用;来重复上一次的f移动
tx: 同fx,但是移动光标至x字符前,而不是x字符上
Fx:通fx,但反方向移动

w:前移光标至下一个单词第一字符
W:前移光标至下一个长单词第一字符
e: 前移光标至下一个单词最后字符
E: 前移光标至下一个长单词最后字符
b: 前一单词第一字符
B: 前一长单词第一字符
(vi 按空格或标点符号分隔单词,按空格或新行来分隔长单词)

(: 到句首
): 到句尾

0: 到行首
^: 到行首第一字符
$: 到行尾

{: 到段首
}: 到段尾

*: 读取光标当前所在位置的字符,并将向前移动至下一个同样的字符
#: 和*功能相同,但反方向移动光标

Ctrl+F: 向前滚屏
Ctrl+B: 向后滚屏
H,M,L: 移动光标至屏幕的顶端,中间,底端

G: 移动光标至文档最后一行
numG: 移动光标至第num行

/text: 从光标处开始,向前搜索"text"字串
?text: 同上,但搜索方向相反

同时也发现一个图片vi教程,讲的也很清晰:www.litrin.org/UserFiles/Image/vi_png.jpg

Tuesday, May 29, 2007

PCBSD内核编译/升级

目的:将PCBSD的内核由FreeBSD 6.1升级至 6.2

步骤:

1. 升级source file至6.2

首先编辑/usr/share/examples/cvsup/standard-supfile文件,修改其中的两个地方:
将其中的*default release=cvs tag=RELENG_6_1一行改为 RELENG_6_2,
将*default host=CHANGE_THIS.FreeBSD.org中的CHANGE_THIS.freebsd.org替换为cvsup.cn.freebsd.org

修改完成後,运行如下命令:
cvsup /usr/share/examples/cvsup/standard-supfile

这个命令将会更新系统源文件至6.2版。

2. 创建内核配置文件MYKERNEL(或任何其他你喜欢的名字)

请以root帐户运行如下的命令:

# cd /usr/src/sys/i386/conf
# mkdir /root/kernels
# cp /PCBSD/conf/PCBSDv1.3 /root/kernels/MYKERNEL
# ln -s /root/kernels/MYKERNEL

这里,你可以编辑MYKERNEL文件,将一些不需要的内核选项去掉,这样编译出的内核将会更简洁,效率更高。比如其中有cpu 486/cpu 586,如果你的CPU是PII以上的,你就可以删除这两行,或者通过在行首加一个#符号来注释掉他们,只保留cpu686就可。

但你一定要清楚每个选项的意思,否则请不要随意更改,不然很可能导致系统无法启动。


3. 编辑/etc/make.conf文件
如果下面的内容不在make.conf文件中,请添加:
CPUTYPE=i686 #i486, i586, i586/mmx, i686, p2, p3, p4, athlon,athlon-xp, athlon64, opteron
RELEASETAG="RELENG_6_2"
SUP_UPDATE=yes
SUP=/usr/local/bin/cvsup
SUPFLAGS=-g -L 2
SUPHOST=cvsup.cn.FreeBSD.org
SUPFILE=/usr/share/examples/cvsup/standard-supfile
PORTSSUPFILE=/usr/share/examples/cvsup/ports-supfile
#DOCSUPFILE=/usr/share/examples/cvsup/doc-supfile
KERNCONF=MYKERNEL

4.编译并安装内核

依次运行如下命令:
#cd /usr/src
#make buildkernel
#make installkernel
#reboot

重启後你的PCBSD系统就是6.2版的内核了。其中buildkernel和installkernel的过程需要很长的时间,尤其是你的CPU和内存不是很多的话。

5. 验证

重启完成後,可以运行如下命令来查看目前的内核版本:

# dmesg | grep RELEASE
FreeBSD 6.2-RELEASE #0: Fri Jan 12 10:40:27 UTC 2007

每个人的输出并不相同,但只要你看到6.2-RELEASE的字样,就表示你已经成功了~

ENJOY!

Thursday, May 24, 2007

命令行下使用代理服务器

在企业环境下经常需要PCBSD穿过代理服务器来访问外网,尤其是使用ports进行软件安装的时候。要设置HTTP代理服务器,请使用如下命令:

setenv HTTP_PROXY http://username:password@proxy_server_name:PortNumber

不需要验证的,就把username:password@部分去掉就行了。

没有测试过这个方案对微软的ISA server代理是否有效,ISA的验证是结合AD账户进行的,和普通的代理在密码验证协议上会有些差别。不过可以试试:

setenv HTTP_PROXY http://proxy_server_name:portNumber
setenv HTTP_PROXY_AUTH basic:*:username:password

用Windows引导启动WinXP+PCBSD

安装XP+PCBSD双启动以后,PCBSD boot manager启动的时候那声beep实在让我讨厌,尤其是在办公室打开电脑的时候。于是我决定改用Windows的启动菜单。

这次的修改需要两个工具:
1. 可启动的、带有fdisk的工具盘
2. PCBSD的启动记录(pcbsd.pbr)

第一个工具请自备,我讲一下如何获得第二个。

方法a: 启动到windows下,从PCBSD的安装盘上copy /boot/boot1 到你的Windows启动分区下,比如C:\,并将它重命名为"pcbsd.pbr"

方法b:启动到PCBSD下,以root身份运行如下命令:
dd if=/dev/XXX of=pcbsd.pbr bs=512 count=1

这里的 XXX 是你的PCBSD的盘符, 比如ad0s2a。
然后再将PCBSD.PBR拷贝到Windows的启动盘下,比如C:\。注意,虽然PCBSD下可以直接查看NTFS分区,但不能写入,你需要通过移动硬盘或者网络来完成这一步。

将pcbsd.pbr拷贝到windows的启动分区以后,你需要编辑windows的boot.ini文件,让它看起来如下如下:

[boot loader]
timeout=30
default=multi(0)disk(0)rdisk(0)partition(1)\WINDOWS
[operating systems]
multi(0)disk(0)rdisk(0)partition(1)\WINDOWS="Windows XP"
c:\pcbsd.pbr="My PCBSD 1.3"

这里要注意的是,boot.ini是一个默认的隐藏只读文件,你需要去掉它的的只读属性才能保存。

上面的步骤完成後,用你的启动盘重启系统,然后运行“fdisk /mbr”命令,然后从硬盘重启,你就不会再听到那讨厌的声响了,Windows的启动菜单里就会显示出“My PCBSD 1.3”的选项了。