ユーザーを指定して起動するには?(→setuid,setgidを使う)

    • ふとそんなことが気になった21時。
    • apacheは設定ファイルで指定されたUserで起動される。*1その仕組みはどうやっているのか?
$ wget http://ftp.riken.jp/Linux/centos/5.5/os/SRPMS/httpd-2.2.3-43.el5.centos.src.rpm
$ rpm2cpio httpd-2.2.3-43.el5.centos.src.rpm | cpio -id
$ tar xvfz httpd-2.2.3.tar.gz
$ grep User httpd-2.2.3/ -r
..多くて絞れないので設定ファイルのUserを無効なものにして起動、エラーさせて絞ってみる
..そして出たのが以下。/etc/passwd から取得しようとしてエラー
# less /var/log/httpd/error_log
[Sat Feb 12 12:43:35 2011] [alert] getpwuid: couldn't determine user name from uid 4294967295, you probably need to modify the User directive
    • そうかsetgid,setuidか。
$ cat > setuid.c
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>

int main(void)
{
    int rc = setuid(0); # rootになれなければエラーするはず
    printf("rc=%d\n", rc);
    printf("Real UID\t= %d\n", getuid());
    printf("Effective UID\t= %d\n", geteuid());
    printf("Real GID\t= %d\n", getgid());
    printf("Effective GID\t= %d\n", getegid());

    return EXIT_SUCCESS;
}

$ gcc -Wall setuid.c
$ ./a.out
rc=-1
Real UID        = 500
Effective UID   = 500
Real GID        = 100
Effective GID   = 100
$ su -c !!
su -c ./a.out
Password:
rc=0
Real UID        = 0
Effective UID   = 0
Real GID        = 0
Effective GID   = 0