如何使用 C、CGI、HTML 为特定用户创建目录?
How to make directories for an specific user using C, CGI, HTML?
我有一个问题,我正在使用 C、CGI 和 HTML 创建目录,我从 2 个源代码开始,一个是这样的:
/*
* newdir.c - Create a directory
*/
#include <unistd.h>
#include <fcntl.h>
#include <stdlib.h>
#include <stdio.h>
int main(int argc, char *argv[])
{
printf("\n#Argumentos: %d\n", argc);
if(mkdir(argv[1], 0777)) {
perror("mkdir");
exit(EXIT_FAILURE);
}
exit(EXIT_SUCCESS);
}
我还有另一个:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// Everything here is the same as sampleprogram.c except that
// there are differences when we have to read in the string
int main(void)
{
char *data;
char* s = malloc(100 * sizeof(char));
printf("%s%c%c\n",
"Content-Type:text/html;charset=iso-8859-1",13,10);
int length = atoi(getenv("CONTENT_LENGTH"));
int j =0;
char c;
c = getchar();
data = malloc(sizeof(char) * (length + 1) );
while ( c != EOF && j < length ) {
//read in one character
data[j] = c;
c = getchar();
j++;
}
data[j] = '[=12=]';
printf("<TITLE>Make directorys</TITLE>");
printf("<H3> Make new directory \n <br> %s ");
if(data == NULL) {
printf("<P>Error! Error in passing data from form to script.");
return 0;
}
char delimiters[] = "&";
char* str = strtok( data, delimiters);
sscanf(str, "s=%s", s);
int i;
for (i=0; i < strlen(s); i++ ) {
if (s[i] == '+')
s[i] = ' ';
}
str = strtok(NULL, delimiters);
//s is the name collected from the text area in the html file
if(mkdir(s, 0777)) {
perror("mkdir");
exit(EXIT_FAILURE);
}
exit(EXIT_SUCCESS);
return 0;
}
所以我所做的是将第一个代码添加到第二个代码中,另一个代码从 html 的文本字段中收集信息,并由 CGI 脚本处理。
它工作完美,但该目录是为用户“daemon”创建的,如果我不从控制台再次将权限设置为 777,我将无法在其中添加任何内容,为什么会发生这种情况?有什么不对吗?
PD:如果我 运行 来自控制台的第一个代码工作完美,我就是用户 :(
你没有办法真正解决这个问题,因为执行scripts/programs的用户是daemon
用户。这是您设置 Web 服务器的方式,Web 服务器本身是 daemon
用户的 运行,这意味着每个 script/program 调用Web 服务器也将由同一用户 运行。
此外,c programs you posted are really poorly coded, and it doesn't seem like you need to write them in the c 语言。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(void)
{
const char *content_length;
char *data;
char s[100];
content_length = getenv("CONTENT_LENGTH");
if (content_length != NULL)
{
int length;
int j;
char c;
printf("Content-Type: text/html;charset=iso-8859-1\r\n\n");
length = atoi(content_length);
c = getchar();
data = malloc(length + 1);
if (data != NULL)
{
const char *delimiters;
char *token;
delimiters = "&";
while ((c != EOF) && (j < length))
{
data[j] = c;
c = getchar();
j++;
}
data[j] = '[=10=]';
printf("<TITLE>Make directorys</TITLE>");
printf("<H3> Make new directory \n <br> %s ");
token = strtok(data, delimiters);
if ((token != NULL) && (sscanf(str, "s=%99s", s) == 1))
{
int i;
for (i = 0 ; s[i] != '[=10=]' ; i++ )
{
if (s[i] == '+')
s[i] = ' ';
}
if (mkdir(s, 0777))
{
perror("mkdir");
exit(EXIT_FAILURE);
}
}
free(data);
}
}
return 0;
}
我有一个问题,我正在使用 C、CGI 和 HTML 创建目录,我从 2 个源代码开始,一个是这样的:
/*
* newdir.c - Create a directory
*/
#include <unistd.h>
#include <fcntl.h>
#include <stdlib.h>
#include <stdio.h>
int main(int argc, char *argv[])
{
printf("\n#Argumentos: %d\n", argc);
if(mkdir(argv[1], 0777)) {
perror("mkdir");
exit(EXIT_FAILURE);
}
exit(EXIT_SUCCESS);
}
我还有另一个:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// Everything here is the same as sampleprogram.c except that
// there are differences when we have to read in the string
int main(void)
{
char *data;
char* s = malloc(100 * sizeof(char));
printf("%s%c%c\n",
"Content-Type:text/html;charset=iso-8859-1",13,10);
int length = atoi(getenv("CONTENT_LENGTH"));
int j =0;
char c;
c = getchar();
data = malloc(sizeof(char) * (length + 1) );
while ( c != EOF && j < length ) {
//read in one character
data[j] = c;
c = getchar();
j++;
}
data[j] = '[=12=]';
printf("<TITLE>Make directorys</TITLE>");
printf("<H3> Make new directory \n <br> %s ");
if(data == NULL) {
printf("<P>Error! Error in passing data from form to script.");
return 0;
}
char delimiters[] = "&";
char* str = strtok( data, delimiters);
sscanf(str, "s=%s", s);
int i;
for (i=0; i < strlen(s); i++ ) {
if (s[i] == '+')
s[i] = ' ';
}
str = strtok(NULL, delimiters);
//s is the name collected from the text area in the html file
if(mkdir(s, 0777)) {
perror("mkdir");
exit(EXIT_FAILURE);
}
exit(EXIT_SUCCESS);
return 0;
}
所以我所做的是将第一个代码添加到第二个代码中,另一个代码从 html 的文本字段中收集信息,并由 CGI 脚本处理。 它工作完美,但该目录是为用户“daemon”创建的,如果我不从控制台再次将权限设置为 777,我将无法在其中添加任何内容,为什么会发生这种情况?有什么不对吗? PD:如果我 运行 来自控制台的第一个代码工作完美,我就是用户 :(
你没有办法真正解决这个问题,因为执行scripts/programs的用户是daemon
用户。这是您设置 Web 服务器的方式,Web 服务器本身是 daemon
用户的 运行,这意味着每个 script/program 调用Web 服务器也将由同一用户 运行。
此外,c programs you posted are really poorly coded, and it doesn't seem like you need to write them in the c 语言。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(void)
{
const char *content_length;
char *data;
char s[100];
content_length = getenv("CONTENT_LENGTH");
if (content_length != NULL)
{
int length;
int j;
char c;
printf("Content-Type: text/html;charset=iso-8859-1\r\n\n");
length = atoi(content_length);
c = getchar();
data = malloc(length + 1);
if (data != NULL)
{
const char *delimiters;
char *token;
delimiters = "&";
while ((c != EOF) && (j < length))
{
data[j] = c;
c = getchar();
j++;
}
data[j] = '[=10=]';
printf("<TITLE>Make directorys</TITLE>");
printf("<H3> Make new directory \n <br> %s ");
token = strtok(data, delimiters);
if ((token != NULL) && (sscanf(str, "s=%99s", s) == 1))
{
int i;
for (i = 0 ; s[i] != '[=10=]' ; i++ )
{
if (s[i] == '+')
s[i] = ' ';
}
if (mkdir(s, 0777))
{
perror("mkdir");
exit(EXIT_FAILURE);
}
}
free(data);
}
}
return 0;
}