免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12下一页
最近访问板块 发新帖
查看: 5003 | 回复: 15
打印 上一主题 下一主题

请教:用c写一个自动修改unix用户密码的程序 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2006-08-31 18:47 |只看该作者 |倒序浏览
可能是我了解得不足,unix下passwd只能手动输入密码来修改,不能自动化这个过程。。。
例子:
/home/cpfly/> passwd
Changing password for "cpfly"
cpfly's Old password:

这个时候输入的密码没有回显,但是确实输入进去了,敲入回车就可以判断密码是否正确,然后更改密码。。

我想实现这个地方的输入由程序来输入,大体想法是:
1. 定义一个文件操作符,可读可写,非阻塞
2. fork() 子进程重定义 标准输入,close(0), dup(fd), close(fd)... 然后execl("/bin/passwd","passwd",(char *)0);
3. 父进程写那个文件,就相当于写入 子进程的标准输入里。。。

失败了,请教该如何做,或者有更好的办法,谢谢。。

论坛徽章:
1
2015年辞旧岁徽章
日期:2015-03-03 16:54:15
2 [报告]
发表于 2006-08-31 19:13 |只看该作者
呵呵,又是一个光凭想象的。
你打开 passwd.c 看一下,就知道你那个方法肯定是不管用的了——是谁告诉你 passwd 读的是 stdin/fd0 的了?

论坛徽章:
0
3 [报告]
发表于 2006-08-31 20:36 |只看该作者
chpasswd

论坛徽章:
0
4 [报告]
发表于 2006-09-01 14:18 |只看该作者
提示: 作者被禁止或删除 内容自动屏蔽

论坛徽章:
0
5 [报告]
发表于 2006-09-01 14:20 |只看该作者
expect !!! 去看看这个脚本语言吧! 它是支持交互式的!!

可以在bash 里内嵌的!

论坛徽章:
0
6 [报告]
发表于 2006-09-01 14:21 |只看该作者
参考 busybox 原码 loginutils/passwd.c ,比较简洁。

论坛徽章:
0
7 [报告]
发表于 2006-09-01 14:30 |只看该作者
struct passwd {
        char        *pw_name;                /* user name */
        char        *pw_passwd;                /* encrypted password */
        uid_t        pw_uid;                        /* user uid */
        gid_t        pw_gid;                        /* user gid */
        time_t        pw_change;                /* password change time */
        char        *pw_class;                /* user access class */
        char        *pw_gecos;                /* Honeywell login info */
        char        *pw_dir;                /* home directory */
        char        *pw_shell;                /* default shell */
        time_t        pw_expire;                /* account expiration */
        int        pw_fields;                /* internal: fields filled in */
};

看你需要实现的自动化的程度

论坛徽章:
0
8 [报告]
发表于 2006-09-01 14:42 |只看该作者

回复 1楼 cpfly 的帖子

自己写用户帐号等信息到系统就行了,生成密码替换原密码即可

[ 本帖最后由 chzht001 于 2006-9-1 14:43 编辑 ]

论坛徽章:
0
9 [报告]
发表于 2006-09-01 15:42 |只看该作者
谢谢大家,我一个一个回复吧,看看我的理解正确吗:
flw:
谢谢你的点醒...

chrisyan:
/home/cpfly/> chpasswd
ksh: chpasswd:  not found.

aple_smx (Bowe) :
要是可以自己实现一个shell,passwd在这个shell里面用,不知道可不可以,看来还要读一下passwd.c

新手上路2世:
/home/cpfly/> ls -l /usr/bin/expect
ls: 0653-341 The file /usr/bin/expect does not exist.

connet (help u & me):
找到bb_askpass(), 它重新定义了terminal的属性,不管我开始怎么重定义,它都会改成它的。。。
        new.c_iflag &= ~(IUCLC|IXON|IXOFF|IXANY);
        new.c_lflag &= ~(ECHO|ECHOE|ECHOK|ECHONL|TOSTOP);
        tcsetattr(STDIN_FILENO, TCSANOW, &new);

wuqing:
尽管我可以调用系统函数得到这些变量,可以怎么设置呢。。

chzht001:
那样参考源码是怎么写的,最后落实的时候,我没有足够的权限,没有办法写/etc/passwd, shadow这类的文件

[ 本帖最后由 cpfly 于 2006-9-1 16:13 编辑 ]

论坛徽章:
0
10 [报告]
发表于 2006-09-01 16:12 |只看该作者
NAME
       chpasswd - update password file in batch

SYNOPSIS
       chpasswd [-e]

DESCRIPTION
       chpasswd         reads        a  file         of user name and password pairs from standard
       input and uses this information to update a group  of  existing        users.
       Without the -e switch, the passwords are expected to be cleartext. With
       the -e switch, the passwords are expected  to  be  in  encrypted         form.
       Each line is of the format

              user_name:password

       The  named user must exist.  The supplied password will be encrypted as
       necessary, and the password age updated, if present.

       This command is intended to be used in a large system environment where
       many accounts are created at a single time.

CAVEATS
       The  input file must be protected if it contains unencrypted passwords.

SEE ALSO
       passwd(1), newusers(, useradd(

AUTHOR
       Julianne Frances Haugh (jockgrrl@ix.netcom.com)

                                                                   CHPASSWD(


我装的debian,装完基本系统就有这个命令了
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP