此 C 包装器中的分段错误



我是C的新手,这是一个简单的包装器(rootsetuidwrapper.c),用于以其他用户的身份执行脚本。我要执行的脚本 ubuntu-server-secure.sh 路径(/home/neehar)作为根。我的操作系统是 Ubuntu 12.04.03 LTS。有人可以帮助我修复这些错误吗?我用 gcc 编译,并通过执行 gcc -o rootsuidwrap rootsetuidwrapper.c 将编译后的代码称为 rootsuidwrap。当我通过执行 ./rootsuidwrap ubuntu-server-secure.sh [0] 在终端中运行它时。这是正确的称呼方式吗?代码中的用法部分说要这样做。它说:Segmentation Fault (core dumped).它是否必须与代码的这一部分有关:*user=cuserid(NULL); .它可能是流氓指针。如果是这样,修复程序是什么,它是什么样子?

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

 * 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
     */
    memset (domain, '', sizeof domain );
    getdomainname (domain, sizeof domain - 1);
    /* 
     * Figure out our fully canonicalized hostname
     */
    memset (host, '', 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
     */
    *user = cuserid (NULL);        
    if (user == 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);
}

感谢帮助,

尝试,

user = cuserid (NULL);

而不是

*user = cuserid (NULL);

在编译程序时,编译器可能会显示与此行相关的警告。 我会研究您在编译程序时收到的任何其他警告,并努力删除它们。

相关内容

  • 没有找到相关文章

最新更新