关于把变量(字符串)传给fputs()的问题
时间:2011-10-23
来源:互联网
最近在写个linux c程序,本人比较菜,问题一个接一个,还请大家多多帮忙。
上面这个代码要实现的功能是只让普通账号123运行此程序重启sshd服务,并把重启时间写入到/var/log/time.log文件中。
代码中使用了setreuid函数获取root权限(root账号编译程序,并为此程序设置suid),使用123账号运行结果:
原因应该是没有权限打开文件,但是上面重启sshd时就获得了root权限啊,小弟不明白,请哥哥们指教,谢谢。
代码:
#include <stdio.h>
#include <pwd.h>
#include <sys/types.h>
#include <time.h>
{
uid_t uid,euid,guestuid; //定义运行此程序用户的uid:uid,程序所有者(root)的uid:euid和普通用户123的uid:guestuid
time_t timep; //定义时间变量timep
euid = geteuid(); //获取此程序所有者的uid
uid = getuid(); //获取运行程序的当前账号的uid
struct passwd *user; //
user = getpwnam("123"); //从密码文件取得指定账号(123)的数据
apacheuid = user->pw_uid; //取得123账号的uid
struct tm*p; //
time(&timep); //
p=localtime(&timep); //取得当地时间
if (uid != guestuid) //判断如果当前运行程序用户不是账号123则提示,并退出程序
{
printf (" Access Denied!\n");
return 0;
}
else if(uid == guestuid) //判断如果当前运行此程序的用户是123则进行重启sshd操作
{
setreuid(euid,uid); //临时将root权限转让给123,一定要将euid(root权限)写在前面,否则不能将root权限转让给apache
system("/etc/init.d/sshd restart"); //重启sshd
printf("\n");
printf("%d/%d/%d ",(1900+p->tm_year),(1+p->tm_mon),p->tm_mday); //打印当前日期
printf("%d:%d:%d\n",p->tm_hour,p->tm_min,p->tm_sec); //打印当前时间
FILE *fp; //定义文件指针fp
if ((fp = fopen("time.log", "w")) == NULL) //打开time.txt文件
{
printf("Can not open the file !\n");
}
fprintf(fp,"%d/%d/%d %d:%d:%d\n",(1900+p->tm_year),(1+p->tm_mon),p->tm_mday,p->tm_hour,p->tm_min,p->tm_sec);//将日期时间写入time.log文件
fclose(fp);
}
return 0;
}
#include <pwd.h>
#include <sys/types.h>
#include <time.h>
{
uid_t uid,euid,guestuid; //定义运行此程序用户的uid:uid,程序所有者(root)的uid:euid和普通用户123的uid:guestuid
time_t timep; //定义时间变量timep
euid = geteuid(); //获取此程序所有者的uid
uid = getuid(); //获取运行程序的当前账号的uid
struct passwd *user; //
user = getpwnam("123"); //从密码文件取得指定账号(123)的数据
apacheuid = user->pw_uid; //取得123账号的uid
struct tm*p; //
time(&timep); //
p=localtime(&timep); //取得当地时间
if (uid != guestuid) //判断如果当前运行程序用户不是账号123则提示,并退出程序
{
printf (" Access Denied!\n");
return 0;
}
else if(uid == guestuid) //判断如果当前运行此程序的用户是123则进行重启sshd操作
{
setreuid(euid,uid); //临时将root权限转让给123,一定要将euid(root权限)写在前面,否则不能将root权限转让给apache
system("/etc/init.d/sshd restart"); //重启sshd
printf("\n");
printf("%d/%d/%d ",(1900+p->tm_year),(1+p->tm_mon),p->tm_mday); //打印当前日期
printf("%d:%d:%d\n",p->tm_hour,p->tm_min,p->tm_sec); //打印当前时间
FILE *fp; //定义文件指针fp
if ((fp = fopen("time.log", "w")) == NULL) //打开time.txt文件
{
printf("Can not open the file !\n");
}
fprintf(fp,"%d/%d/%d %d:%d:%d\n",(1900+p->tm_year),(1+p->tm_mon),p->tm_mday,p->tm_hour,p->tm_min,p->tm_sec);//将日期时间写入time.log文件
fclose(fp);
}
return 0;
}
上面这个代码要实现的功能是只让普通账号123运行此程序重启sshd服务,并把重启时间写入到/var/log/time.log文件中。
代码中使用了setreuid函数获取root权限(root账号编译程序,并为此程序设置suid),使用123账号运行结果:
代码:
[123@ldonger html]$ ./intofile
Stopping sshd: [ OK ]
Starting sshd: [ OK ]
2011/10/23 5:3:32
Can not open the file !
Segmentation fault
[123@ldonger html]$
Stopping sshd: [ OK ]
Starting sshd: [ OK ]
2011/10/23 5:3:32
Can not open the file !
Segmentation fault
[123@ldonger html]$
原因应该是没有权限打开文件,但是上面重启sshd时就获得了root权限啊,小弟不明白,请哥哥们指教,谢谢。
作者: lldonger 发布时间: 2011-10-23
相关阅读 更多
热门阅读
-
office 2019专业增强版最新2021版激活秘钥/序列号/激活码推荐 附激活工具
阅读:74
-
如何安装mysql8.0
阅读:31
-
Word快速设置标题样式步骤详解
阅读:28
-
20+道必知必会的Vue面试题(附答案解析)
阅读:37
-
HTML如何制作表单
阅读:22
-
百词斩可以改天数吗?当然可以,4个步骤轻松修改天数!
阅读:31
-
ET文件格式和XLS格式文件之间如何转化?
阅读:24
-
react和vue的区别及优缺点是什么
阅读:121
-
支付宝人脸识别如何关闭?
阅读:21
-
腾讯微云怎么修改照片或视频备份路径?
阅读:28