我想做一個suid應用程序,它只會執行位於受限文件夾中的ruby腳本。我試圖用realpath(3)來做到這一點,但它只返回路徑的第一部分。下面是我的代碼...如何將相對路徑轉換爲C中的絕對路徑?
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
#include <string.h>
#define SUEXEC_STR_LEN 2048
#define RUBY_APP "/usr/bin/ruby"
#define DIRECTORY_SEPARATOR "/"
static void safepath(const char *path_in, char * path_out, int outlen) {
realpath(path_in, path_out);
}
int main (int argc, char *argv[])
{
char cmd[SUEXEC_STR_LEN];
char path_out[SUEXEC_STR_LEN];
char path_in[SUEXEC_STR_LEN];
char *cp = &cmd[0];
strncpy(cp, RUBY_APP, SUEXEC_STR_LEN - 1);
strncpy(path_in, DIRECTORY_SEPARATOR, SUEXEC_STR_LEN - 1);
strncat(path_in,argv[1],SUEXEC_STR_LEN - 1);
safepath(path_in,path_out,SUEXEC_STR_LEN - 1);
printf("path_in=%s path_out=%s\n",path_in,path_out);
setuid(0);
// system(cmd);
return 0;
}
這是我得到
[email protected]:/root/src# ./a.out foo/bar/../test
path_in=/foo/bar/../test path_out=/foo
這一結果的一個例子的結果,我想
[email protected]:/root/src# ./a.out foo/bar/../test
path_in=/foo/bar/../test path_out=/foo/test
我覺得這裏的問題是,'真實路徑()'預計的路徑實際存在,因爲它也通告它解析符號鏈接,它不可能做,如果它只是操縱字符串。 – Emmet
@Emmet,你是對的。那麼我應該使用什麼呢? –
http://stackoverflow.com/questions/11034002/how-to-get-absolute-path-of-file-or-directory-that-does-not-exist自己執行字符串操作。 – timrau