Chinaunix

标题: 请教:用c写一个自动修改unix用户密码的程序 [打印本页]

作者: cpfly    时间: 2006-08-31 18:47
标题: 请教:用c写一个自动修改unix用户密码的程序
可能是我了解得不足,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. 父进程写那个文件,就相当于写入 子进程的标准输入里。。。

失败了,请教该如何做,或者有更好的办法,谢谢。。
作者: flw    时间: 2006-08-31 19:13
呵呵,又是一个光凭想象的。
你打开 passwd.c 看一下,就知道你那个方法肯定是不管用的了——是谁告诉你 passwd 读的是 stdin/fd0 的了?
作者: chrisyan    时间: 2006-08-31 20:36
chpasswd
作者: aple_smx    时间: 2006-09-01 14:18
提示: 作者被禁止或删除 内容自动屏蔽
作者: 新手上路2世    时间: 2006-09-01 14:20
expect !!! 去看看这个脚本语言吧! 它是支持交互式的!!

可以在bash 里内嵌的!
作者: connet    时间: 2006-09-01 14:21
参考 busybox 原码 loginutils/passwd.c ,比较简洁。
作者: wuqing    时间: 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 */
};

看你需要实现的自动化的程度
作者: chzht001    时间: 2006-09-01 14:42
标题: 回复 1楼 cpfly 的帖子
自己写用户帐号等信息到系统就行了,生成密码替换原密码即可

[ 本帖最后由 chzht001 于 2006-9-1 14:43 编辑 ]
作者: cpfly    时间: 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 编辑 ]
作者: chrisyan    时间: 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,装完基本系统就有这个命令了
作者: cpfly    时间: 2006-09-01 16:16
AIX Version 5
(C) Copyrights by IBM and by others 1982, 2000.
作者: flw    时间: 2006-09-01 16:26
原帖由 cpfly 于 2006-9-1 15:42 发表
新手上路2世:
/home/cpfly/> ls -l /usr/bin/expect
ls: 0653-341 The file /usr/bin/expect does not exist.

你这个方法是错误的,
你应该用
1, expect
2, which expect
3, whereis expect
作者: 新手上路2世    时间: 2006-09-01 17:17
原帖由 cpfly 于 2006-9-1 15:42 发表
谢谢大家,我一个一个回复吧,看看我的理解正确吗:
flw:
谢谢你的点醒...

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

aple_smx (Bowe) :
要是可以自己实现一个shell,passwd在 ...



如果没有expect你可以去下一个啊!
作者: isnowran    时间: 2006-09-02 01:50

  1. echo 111 | passwd --stdin binary
复制代码

作者: cpfly    时间: 2006-09-07 17:09
flw:
/home/cpfly/> expect "autopasswd" hello hello2
ksh: expect:  not found.
/home/cpfly/> which expect
which: 0652-141 There is no expect in ...
/home/cpfly/> whereis expect
expect:

新手上路2世:
好像要用tcl,这个我也不会。。。唉。。别笑话我了

isnowran:
那个stdin好像是linux可以用。。。AIX 不太支持

我自己这几天想到一个笨办法:
本地telnet本地,
用socket连接本地,发送命令和密码过去都没有问题,已经试验了,用着还不错,谢谢大家!
作者: jackalchen    时间: 2006-09-07 17:17
本地telnet本地, 这个方法确实有点笨!!

我觉得很多人已经提供了很多很好的方法,为什么不调查一下呢?

还想着telnet本地。要是系统不支持telnet呢?




欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2