此C包装器中的分段错误

时间:2013-12-24 05:08:20

标签: c debugging segmentation-fault root setuid

我是C的新手,这是一个简单的包装器(rootsetuidwrapper.c),用于以不同的用户身份执行脚本。我想要以root身份在路径(/ home / neehar)上执行的脚本是ubuntu-server-secure.sh。我的操作系统是Ubuntu 12.04.03 LTS。有人可以帮我解决这些错误吗?我用gcc编译并通过执行gcc -o rootsuidwrap rootsetuidwrapper.c将编译后的代码称为rootsuwwrap。当我通过执行./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, '\0', sizeof domain );
    getdomainname (domain, sizeof domain - 1);

    /* 
     * Figure out our fully canonicalized hostname
     */

    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
     */
    *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);
}

感谢帮助,

1 个答案:

答案 0 :(得分:1)

尝试,

user = cuserid (NULL);

而不是

*user = cuserid (NULL);

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