【发布时间】:2016-02-08 23:03:42
【问题描述】:
在 getuserinfo() 中,如果行 struct passwd *theUser = getpwnam(username); 尝试使用不存在的用户名,则程序会以-1 的错误。它永远不会到达函数的错误处理部分。它没有返回 main,我不知道为什么。
它应该返回 NULL 并打印出错误消息并让程序的其余部分尝试运行。
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <ctype.h>
#include <string.h>
#include <pwd.h>
#include <grp.h>
#include <errno.h>
//awk 'BEGIN { FS=":"; print "User\t\tUID\n--------------------"; } { print $1,"\t\t",$3;} END { print "--------------------\nAll Users and UIDs Printed!" }' /etc/passwd
#define SHELLSCRIPT "\
#/bin/bash \n\
awk 'BEGIN { FS=\":\"; print \"User\t\tUID\n--------------------\"; } { print $1,\"\t\t\",$3;} END { print \"--------------------\nAll Users and UIDs Printed!\" }' /etc/passwd \n\
"
struct passwd *getuserinfo(char *username)
{
//Set errno to 0 so we can use it
errno = 0;
//create a variable to store the user info in
struct passwd *theUser = getpwnam(username);
//error check getpwnam()
if(theUser == NULL)
{
printf("getpwnam() ERROR, errno = %d", errno);
return NULL;
}
return theUser;
}
struct group *getgroupinfo(long int groupid)
{
//Set errno to 0 so we can use it
errno = 0;
//create a variable to store the group info in
struct group *theGroup = getgrgid(groupid);
//error check getgrgid()
if(theGroup == NULL)
{
printf("getgrgid() ERROR, errno = %d", errno);
return NULL;
}
return theGroup;
}
void displayusers()
{
system(SHELLSCRIPT);
}
/*
*
* 4. A main function which will
* Call getuserinfo passing a hard-coded username, and using the return object, display the user id. (10 points)
* Call getgroupinfo passing the return object from getuserinfo, and using the return object, display the user’s group name. (10 points)
* Call displayusers. (10 points)
*
* My main function does those things but I don't pass objects since the function prototypes you gave us
* don't accept that input, they want ints and strings
*/
int main(int argc, char **argv)
{
//Take in the username we want to look up
char username[50];
printf("Enter username: ");
scanf("%s", username);
//look up the user and get their info
struct passwd *theUser = getuserinfo(username);
//use the user's gid and look up its info
struct group *theGroup = getgroupinfo(theUser->pw_gid);
printf("User: %s\nUser ID: %d\nGroup Name: %s\n\n\n", theUser->pw_name, theUser->pw_uid, theGroup->gr_name);
displayusers();
return 0;
}
【问题讨论】:
-
代码是否具有 setuid 权限,或者您是否以 root 身份运行?
-
我只是在 Eclipse for C/C++ 控制台中运行它。我猜代码然后以我的管理员用户身份运行,但不使用 sudo。
-
您假设代码将通过
struct passwd *theUser = getuserinfo(username);并愉快地转到下一行以获取该用户的组 ID,在调用函数 @ 之前您是否没有检查NULL987654324@? -
在
getuserinfo中的printf()之后添加fflush(),您将看到错误消息。 (注意:errno 为 0,表示未找到用户。)
标签: c linux return system-calls