gpt4 book ai didi

c - Root-SUID C 包装器调试

转载 作者:行者123 更新时间:2023-11-30 15:40:58 28 4
gpt4 key购买 nike

我是 C 新手,这是我编写的一个简单包装器,用于以不同用户身份运行执行脚本。我知道我可以在 etc/sudoers 中执行 visudo 但是,我已经这样做了,我不想浪费它,它也将帮助我提高 C 语言的写作水平。我似乎哈 问题是我在执行时遇到错误我编译它。我的操作系统是 Ubuntu 12.04.03 LTS。有人可以帮我修复这些错误吗?

rootsuidwrapper.c: In function ‘trusted’:
rootsuidwrapper.c:60:15: warning: assignment makes pointer from integer without a cast [enabled by default]
rootsuidwrapper.c: In function ‘main’:
rootsuidwrapper.c:116:48: error: too many arguments to function ‘stat’
/usr/include/x86_64-linux-gnu/sys/stat.h:211:12: note: declared here

如果有人可以修复这些错误并给我工作代码,那就太好了。另外,我想知道我做错了什么。

 * This program must be run as root to work.
*/

#if !defined(lint) && !defined(SABER) || defined(RCS_HDRS)
#endif /* !lint && !SABER || RCS_HDRS */
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <netdb.h>
#include <sys/stat.h>

#define TRUSTED_GROUP "trusted"

typedef enum { false = 0, true } bool;

#ifdef __STDC__
bool trusted(char *whoami)
#else
bool trusted(whoami)
char *whoami;
#endif /* __STDC__ */
{
char *user;
char host[BUFSIZ + 1];
char domain[BUFSIZ + 1];
struct hostent *hp;

/*
* Figure out whether this user on this host in this domain is
* trusted.
*/

/*
* Determine our domain name
*/
(void) memset(domain, '\0', sizeof(domain));
getdomainname(domain, sizeof(domain) - 1);

/*
* Figure out our fully canonicalized hostname
*/

(void) memset(host, '\0', sizeof(host));
gethostname(host, sizeof(host) - 1);
if ((hp = gethostbyname(host)) == NULL) {
strcat(host, ".");
strcat(host, domain);
fprintf(stderr,
"%s: WARNING: can't canonlicalize hostname; assuming %s.\n",
whoami, host);
}
else {
strcpy(host, hp->h_name);
}

/*
* Get login name of current user
*/
if ((user = cuserid(NULL)) == NULL) {
fprintf(stderr, " %s: You do not seem to be in the passwd file!\n",
whoami);
return(false);
}

/*
* Look this triple up in the trusted netgroup
*/

return ((innetgr(TRUSTED_GROUP, host, user, domain) == 1) ? true : false);
}


#ifdef __STDC__
main(int argc, char *argv[])
#else
main(argc, argv)
int argc;
char *argv[];
#endif /* __STDC__ */
{
char *whoami;
int ouruid; /* uid we set to run chown and chmod */
int proguid; /* uid we are chowning program to */
char *filename;
struct stat statbuf;
int error = 0;

if (whoami = strrchr(argv[0], '/'))
whoami ++;
else
whoami = argv[0];

if (argc == 3)
proguid = atoi(argv[2]);
else if (argc == 2)
proguid = 0;
else {
fprintf(stderr, "usage: %s filename [proguid]\n", whoami);
exit(1);
}

filename = argv[1];

if (trusted(whoami))
ouruid = 0;
else
ouruid = getuid();

if (setuid(ouruid) == -1) {
fprintf(stderr, "%s: Warning: setuid(%d) failed: ", whoami, ouruid);
perror(NULL);
exit(1);
}

if (stat(filename, &statbuf, sizeof(struct stat)) == -1) {
fprintf(stderr, "%s: failure statting %s: ", whoami, filename);
perror(NULL);
exit(1);
}

if (chown(filename, proguid, -1) == -1) {
error++;
fprintf(stderr, "%s: chown %d %s failed: ", whoami, proguid, filename);
perror(NULL);
fprintf(stderr, "continuing...\n");
}

if (chmod(filename, statbuf.st_mode | S_ISUID)) {
error++;
fprintf(stderr, "%s: chmod u+s %s failed: ", whoami, filename);
perror(NULL);
}

return(error);
}

感谢帮助,

最佳答案

NAME
stat, fstat, lstat - get file status

SYNOPSIS
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>

int stat(const char *path, struct stat *buf);
int fstat(int filedes, struct stat *buf);
int lstat(const char *path, struct stat *buf);

删除 stat() 调用中的第三个参数。您的代码应该是:

if (stat(filename, &statbuf) == -1) {

不需要告诉stat()缓冲区的大小,因为stat()需要一个struct stat *,它有一个固定大小。

关于c - Root-SUID C 包装器调试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20753513/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com