/etc/shadow update password entry! ( getspent? )


 
Thread Tools Search this Thread
Top Forums Programming /etc/shadow update password entry! ( getspent? )
# 1  
Old 07-03-2008
/etc/shadow update password entry! ( getspent? )

Hi i just whant to update an password entry in /etc/shadow.
But dosen't get it to work. Something is wrong! in this code.

What i try do do is if user kalle exist in shadow.
I whant it to update it's password for just that entry.

#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <pwd.h>
#include <grp.h>
#include <string.h>
#include <shadow.h>
//#include <userpw.h>

int main()
{
// -------------------------- //
// ------- passwd file ------ //
// -------------------------- //
FILE* fp;
struct passwd *p;

memset(&p, 0, sizeof(p));

if (!(fp = fopen("/etc/passwd", "a"))) {
perror("Problem ");
return(1);
}

// --------------------------- //
// ----- Password Crypt ------ //
// --------------------------- //
char salt[2];
char password[8] = "password";
char t[11];

salt[0] = 'W'; salt[1] = 'M';
strcpy(t,(char *)crypt(password, salt));

// --------------------------- //
// ------ shadow file -------- //
// --------------------------- //

FILE* fps;
struct spwd *sp;
memset(&sp, 0, sizeof(sp));
if (!(fps = fopen("/etc/shadow", "rw"))) {
perror("Problem");
return(1);
}

char user[20] = "kalle";

/* Loop thru passwd file */
while ((p = getpwent()) != NULL) {
printf("%s\n",p->pw_name);

//IF user found
if (strcmp(p->pw_name, user) == 0 ) {

if(!(sp=getspnam(p->pw_name))){
printf("user missing in shadow file");
} else {
strcpy(sp->sp_pwdp,t);
prinf("run the train %s\n",sp->sp_pwdp);
putspent(sp,fps);
}


}

}


fclose(fps);

return( EXIT_SUCCESS );
}
# 2  
Old 07-03-2008
Now i got a bit thurder. But have a differnet problem.
In end of the shadow file it writes "GjgGToqZ1:14063:0:99999:7:"
dont understand why!




#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <pwd.h>
#include <grp.h>
#include <string.h>
#include <shadow.h>
//#include <userpw.h>

int main()
{
// -------------------------- //
// ------- passwd file ------ //
// -------------------------- //
FILE* fp;
struct passwd *p;

memset(&p, 0, sizeof(p));

if (!(fp = fopen("/etc/passwd", "r"))) {
perror("Problem ");
return(1);
}

// --------------------------- //
// ------ shadow file -------- //
// --------------------------- //

FILE* fps;

struct spwd *sp;
//char currpass[255];
memset(&sp, 0, sizeof(sp));
//memset(&currpass, 0, sizeof(currpass));
if (!(fps = fopen("/etc/shadow", "r+"))) {
perror("Problem");
return(1);
}



char user[20] = "kalle";
//strcpy(sp->sp_pwdp,t);
//char password[10] = "password";

/* Loop thru passwd file */

while ((sp = getspent()) != NULL) {
if (strcmp(sp->sp_namp,user) == 0 ) {
//if(!(sp=getspnam(sp->sp_namp))) {
// printf("that user does not exist in shadow file\n");
//} else {
strcpy(sp->sp_pwdp,"testpass");
putspent(sp, fps);
}

else {
//strcpy(sp->sp_pwdp,"temp");
putspent(sp,fps);
}
}
fclose(fp);
fclose(fps); return( EXIT_SUCCESS );
}

exit(0);
# 3  
Old 07-14-2008
Problem solved!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Using the encrypted password of the shadow file

i have an application that uses the encrypted password that's in the /etc/shadow file. i copied the line for the particular username i was interested it in from shadow file and i pasted it into the password file of the application. the application is nagios. this application allowed that... (5 Replies)
Discussion started by: SkySmart
5 Replies

2. Shell Programming and Scripting

Users who have never changed their password from /etc/shadow.

Hello, I have to do a script which returns users who have never changed their password from /etc/shadow. Here is what have I done and I'm not sure if it's ok. I tried to return just users who doesn;t have password set or are locked. Can be there other kind of user who never changed the... (3 Replies)
Discussion started by: catalint
3 Replies

3. UNIX for Advanced & Expert Users

/etc/shadow encrypted password

Hi I wonder whether is possible to generate enrypted passwd for some user and paste it into /etc/shadow file ? What kind of encryption is used in /etc/shadow file ? ths for help. (1 Reply)
Discussion started by: presul
1 Replies

4. Solaris

Password Recovery From /etc/shadow file

Is it possible to reset a normal user password , by editing password field in /etc/shadow file? Thanks (6 Replies)
Discussion started by: ksvaisakh
6 Replies

5. UNIX for Dummies Questions & Answers

shadow entry

In /etc/shadow ..we have all the user entries ..? Like when ever an user is created a corresponding entry is created in /etc/shadow...? (2 Replies)
Discussion started by: rachna
2 Replies

6. Solaris

hash password in shadow show 'x' ???

Hello, I want to know the reason behing the scene why some systems have hashed password in /etc/shadow file as "x" not the alienoid language like A1Dksxi3kaA.. (13 characters) As far as I understand, etc/password will show password as "x" and move all hashed password to etc/shadow.... (8 Replies)
Discussion started by: Smith
8 Replies

7. Programming

userpw.h AIX ( delete entry from the shadow password database )

HI i need to delete an entry in /etc/security/passwd. can't find a way to do it with userpw.h api ( AIX ). the passwd file i delete like this. Write all entrys to passwd file except the one we are removing. can't find any function that works like getspent / getpwent do in AIX userpw api.... (4 Replies)
Discussion started by: nighter
4 Replies

8. UNIX for Dummies Questions & Answers

shadow file after a password reset

hi, I had to reset a lost root password by editing the /etc/passwd and /etc/shadow files ( this is a xen vm file, so i mounted and chrooted the file ) after the reboot with an empty password on root , i have set a new password with passwd but it only changed the /etc/passwd file.... (0 Replies)
Discussion started by: progressdll
0 Replies

9. Shell Programming and Scripting

I want to append password in /etc/shadow file

Hi, I want to append password into /etc/shadow file using a shell script. My below script does add the users to both /etc/passwd and /etc/shadow but how can I add the hordcoded passwords to /etc/shadow file can some one help me ? # To add the groups into /etc/group file for a_user... (5 Replies)
Discussion started by: modgil
5 Replies

10. UNIX for Dummies Questions & Answers

remove shadow password

Does anyone know how to remove a stanza in the shadow password file if the user account has already been removed on an AIX box? I know it can be done by editing the file itself but I would prefer not to do it that way. cheers gizaa (2 Replies)
Discussion started by: gizaa
2 Replies
Login or Register to Ask a Question