Root-SUID C包装器调试

时间:2013-12-24 01:10:53

标签: c debugging char wrapper stat

我是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);
}

感谢帮助,

2 个答案:

答案 0 :(得分:1)

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 *具有固定大小。

答案 1 :(得分:0)

对于编译器警告:

cuserid()返回指向字符(char*)的指针。当任何函数返回一个指针,并且你想将返回值放入缓冲区时,你必须将返回值放入缓冲区中的特定位置,通常是开头。具体来说,您应该使用:

*user = cuserid(NULL);
if(user == NULL)

请记住,cuserid()会返回指向单个字符的指针。因此,函数的返回值应该是单个字符 - 即*user或{ {1}}。使用上面的代码时,编译器不应该抱怨。然后将user[0]的结果放入cuserid(NULL),从第一个字节到分配的其余内存。