免费注册 查看新帖 |

Chinaunix

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

修改linux密码 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2006-12-24 12:11 |只看该作者 |倒序浏览
/************************
gcc -o test_shadow test_shadow.c -lcrypt
test_shadow username old_passwd new_passwd
/usr/bin/test_shadow
************************/
#include
#include
#include
#include
#include
#include
#include
#include
#define L_PASSWD 100
#define N_PASSWD 160
char *get_salt();
char line[L_PASSWD][N_PASSWD];
int main(int argc,char *argv[])
{
    struct passwd *pw;
    char str[N_PASSWD];
    char *crypt_code;
    char *salt;
    struct spwd *sp;
    char salt_cmp[13];
    if(argc != 4)
    {
        fprintf(stderr,"Usage:%s sys_user old_passwd new_passwd\n",argv[0]);
        exit(1);
    }
   
    sp = getspnam(argv[1]);
    strncpy(salt_cmp,sp->sp_pwdp,12);
    salt_cmp[12] = '\0';
    if(strcmp(sp->sp_pwdp,(char*)crypt(argv[2],salt_cmp)))
    {
        fprintf(stderr,"You input a wrong old_passwd for user %s.\n",argv[1]);
        fprintf(stderr,"Retry or discard.\n");
        exit(254);
    }
    salt = get_salt();
    pw = getpwnam(argv[1]);
    crypt_code = (char *)crypt(argv[3],salt);
   
    if((strlen(pw->pw_name)+strlen(crypt_code)+strlen(pw->pw_gecos)+
strlen(pw->pw_dir)+strlen(pw->pw_shell)+8) pw_name,crypt_code,\
pw->pw_uid,pw->pw_gid,pw->pw_gecos,pw->pw_dir,pw->pw_shell);
        }
        else
        {
                fprintf(stderr,"Too short buffer!\n");
                exit(255);
        }
    char *fpath = "/etc/passwd";
    FILE *fp;
    int i = 0;
    char tmp[N_PASSWD];
    if((fp=fopen(fpath,"r")) == NULL)
    {
        perror("fopen error:\n");
        exit(2);
    }
   
    while(fgets(tmp,N_PASSWD-1,fp))
    {
        if(strncmp(tmp,pw->pw_name,strlen(pw->pw_name)))
        {
            strcpy(line,tmp);
        }
        else
        {
            strcpy(line,str);
        }
        i++;
    }
    fclose(fp);
    int j = 0;
        
    if((fp=fopen(fpath,"w")) == NULL)
        {
                perror("fopen error:\n");
                exit(2);
        }
    while(j
    fclose(fp);
    free(salt);
    return 0;
}
char *get_salt()
{
    int i = 0;
    int n = 0;
    char *alpha = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
    char *salt;
    salt = (char *)malloc(13*sizeof(char));
    salt[0] = '$';
    salt[1] = '1';
    salt[2] = '$';   
    srand(time(NULL));   
    while(i
    return salt;
}


本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u/11689/showart_220349.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP