【问题标题】:what are the files needs to be edit to add a new user to ubuntu?需要编辑哪些文件才能将新用户添加到 ubuntu?
【发布时间】:2014-08-13 03:18:41
【问题描述】:

我正在尝试编写一个 C 程序来向我的 Ubuntu 14.04 添加一个新用户。 我编辑了

1) /etc/passwd

sachin:x:65535:1:sachin:/home/sachin:/bin/bash

2) /etc/shadow

sachin:$6$VwBWgroA$t4KXLWIf81sWtiA1/a.fRLrXaOAflGtMo73hGvCzp/M6S8oizZ4iqk.vYbkblXZj2hgGXJxlJ.M2hghGO.a650:16294::::::

3) .profile

stty istrip
PATH=/bin:/usr/bin:/usr/local/bin:/usr/share/bin:.
export PATH

之后当我运行 - ls -l /home/

drwxr-xr-x 43 mrcr mrcr 4096 Aug 12 10:27 mrcr
d-w------t  2 sachin    sachin    4096 Aug 12 11:11 sachin

“sachin”是由我的 rpogram 创建的,“mrcr”是正常创建的。我像普通用户主页一样更改了权限

drwxr-xr-x 2 sachin    sachin    4096 Aug 12 11:11 sachin

我将所有文件从“mrcr”主目录复制到“sachin”主目录(.bashrc、Desktop ..etc)

现在在登录屏幕“sachin”可以登录。但即使我输入正确的密码,我也无法登录。它将再次加载相同的登录屏幕。下面给出的是我的整个代码,

#include<stdio.h>
#include<string.h>
#include<stdlib.h>

#include <sys/types.h>
#include <pwd.h>
#include<signal.h>
#include<unistd.h>

main(int argc, char **argv)
{

    struct passwd *userlist;
    int count, usernumber, len1;
    FILE *tmp, *stmp, *mailer, *profile;
    char *commentfield, *username, *userdir, *home;
    char *mailcomment, *mailmail, reply;

    commentfield = (char *)malloc(1024*sizeof(char));
    username = (char *)malloc(8*sizeof(char));
    userdir = (char *)malloc(256*sizeof(char));
    home = (char *)malloc(256*sizeof(char));
    mailcomment = (char *)malloc(1024*sizeof(char));
    mailmail = (char *)malloc(512*sizeof(char));

    if (argc!=4)
    {
        printf("usage : %s [dirname- no slashes] [logname] [comment - in quotes]\n", argv[0]);
        exit(1);
    }

    if( (strlen(argv[2]) < 5) || (strlen(argv[2]) > 8) )
    {
        printf("pls enter logname between 5-8 \n");
        exit(1);
    }

    signal(SIGHUP, SIG_IGN);
    signal(SIGINT, SIG_IGN);

    setpwent();

    count = 0;

    while((userlist = getpwent()) != NULL)
    {
        if(count < userlist->pw_uid)
        {
            count = userlist->pw_uid ; 
            usernumber = count + 1;
        }
    }
    printf("usernumber : %d\n", usernumber);

    endpwent();

    sprintf(commentfield,"%s", argv[3]);
    sprintf(username, "%s", argv[2]);
    sprintf(userdir, "%s", argv[1]);
    sprintf(home, "/%s/%s", argv[1], argv[2]);



    printf("\n Check this out here: \n");
    printf("-----------------------------------------------");
    printf("\n username      :\t %s", username);
    printf("\n Home Directory:\t %s", home);
    printf("\n comment       :\t %s", commentfield);
    printf("\n______________________________________________\n\n");

    printf("all of this ok? n/y: ");
    scanf("%c", &reply);

    if(reply != 'y')
    {
        printf("\n exiting....u entered not y");
        exit(1);
    } 

    tmp = fopen("/etc/passwd", "a");
    if (tmp == NULL)
    {
        printf("\npermission denied\n");
        exit(1);
    }
    fprintf(tmp, "%s:x:%d:1:%s:%s:/bin/bash\n", username, usernumber, commentfield, home);
    fclose(tmp);



    stmp = fopen("/etc/shadow", "a");
    if (stmp == NULL)
    {
        printf("\npermission denied\n");
        exit(1);
    }
    fprintf(stmp, "%s:*LK*:::::::\n", username);
    fclose(stmp);

    mkdir(home, 0755);
    chdir(home);


    profile = fopen(".profile", "a");
    fprintf(profile, "stty istrip\n");
    fprintf(profile, "PATH=/bin:/usr/bin:/usr/local/bin:/usr/share/bin:.\n");
    fprintf(profile, "export PATH\n");
    fprintf(profile, "\n\n");
    fclose(profile);

    chown(home, usernumber, 1);
    chown(".profile", usernumber, 1);
    chmod(".profile", 0644);


    printf("\n\nALL done!!!!!!!\n Now set the password: ");
    execl("/usr/bin/passwd", "passwd", username, NULL);
    printf("\n\n SUCCESS PASSWORD IS SET.....BYE!!!\n\n");

}

问题:

需要编辑哪些文件?

如何在没有root用户的情况下运行程序?

我需要在我的代码中进行哪些更改?

【问题讨论】:

  • 你为什么要糟糕地重新创建useradd
  • 这只是一个彻头彻尾的坏主意。

标签: c linux permissions ubuntu-14.04


【解决方案1】:

这可能不会直接回答您的问题,但我认为这很重要 - 因为 Unix 命令通常具有经过良好测试和定义的函数来执行创建、编辑和删除用户所需的几乎所有事情,我认为您应该不要试图自己重新创建一个。除其他可能出错的事情外,我认为如果不使用操作系统中包含的实用程序,您将无法成功编辑/etc/shadow。我能给你的最好建议(有些人可能不同意,但恕我直言,对我来说似乎更好)是使用 C 代码中所需的参数调用 useradd 命令,而不是尝试再次创建轮子。

【讨论】:

  • 当我们尝试手动添加新用户时,“useradd”是 ubuntu 调用的函数吗?那么我怎样才能获得我的 ubuntu 14.04 的完整代码?
  • 根据libuser包的信息,源代码应该在https://fedorahosted.org/libuser/下。但我不能 100% 确定这是正确的包。其他发行版似乎使用shadow* 包。
  • 我不明白.. 我使用的是 ubuntu 14.04。我不能得到一个名为“source code ubuntu 14.04”的文件吗?什么是 libuser?
【解决方案2】:

这听起来是个有趣的项目!

我正在运行 Arch,所以我不能给你确切的答案,但我所做的检查是创建一个文件来标记时间,添加一个用户,然后找到自启动文件以来修改的所有文件已创建:

$ touch start
$ useradd -m temp
$ find / -cnewer start -print

您可以在find 上进行分区(即仅搜索/etc 或可能有更改的文件夹),但最后看起来您正在触摸除/etc/gshadow 和@987654326 之外的所有主要文件夹@。

同时阅读 useradd 的手册页,看起来您可能需要查看 /etc/subgid/etc/subuid,因为它们处理组和用户 ID。

至于您在不使用 sudo 的情况下运行代码的问题,您可以考虑设置 SUID or GUID bits。这将让您在正常情况下以 root 身份运行代码。请记住,您将以普通用户身份运行某些东西,这在做之前总是要了解的。

【讨论】:

    猜你喜欢
    • 2022-01-16
    • 2013-12-29
    • 2013-09-08
    • 2017-01-25
    • 1970-01-01
    • 2016-09-13
    • 1970-01-01
    • 1970-01-01
    • 2011-06-28
    相关资源
    最近更新 更多