Whant to write an entry in /etc/passwd (putpwent)


 
Thread Tools Search this Thread
Top Forums Programming Whant to write an entry in /etc/passwd (putpwent)
# 1  
Old 06-30-2008
Whant to write an entry in /etc/passwd (putpwent)

Hi i try to use the function putpwent to write a simple
entry in "/etc/passwd" putpwnet returns 0 as it works but
notething writes to /etc/passwd. What have i missed?

My os
--------
Code:
root@nighter-laptop:/home/nighter/labb# uname -a
Linux nighter-laptop 2.6.22-14-generic #1 SMP Sun Oct 14 23:05:12 GMT 2007 i686 GNU/Linux

( Ubuntu )

My code
----
Code:
#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>



int main()
{
   struct passwd p;
   long m=900;

   FILE *fd;
   memset(&p,0x00,sizeof(p));

   fd=fopen("/etc/passwd ","a");

        p.pw_name="kalle";
        p.pw_passwd="kalle";
        p.pw_uid=m;
        p.pw_gid=m;
        p.pw_gecos="Test account";
        p.pw_dir="/home/kalle";
        p.pw_shell="/bin/bash";

        int value = putpwent(&p,fd);

        printf("%i",value);


return 0;
}

------------------------
# 2  
Old 06-30-2008
After opening /etc/passwd you need to seek to the end of the file i..e
Code:
fseek(fd, 0, SEEK_END);

You also need to close the file after writing the entry
Code:
fclose(fd);

# 3  
Old 07-01-2008
I think there is no need of opening the passwd file with fopen when using functions from pwd.h
# 4  
Old 07-03-2008
Actaully it was not that who where the problem!
But now it works! Don't understand why. But it do.
# 5  
Old 07-03-2008
/etc/shadow update password entry (putspent?)

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.

Code:
#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 );
}

# 6  
Old 07-03-2008
wrong button it was intended to be a new threat.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Big problem: shell entry in /etc/passwd corrupted for user root

did a big mistake, changing root entry of /etc/passwd to root:x:0:0:root:/root:/usr/bin/tmux split-window -v \; attach as expected, now I can't login as root anymore. sudo ed /etc/passwd etc. doesn't work. Any idea? Use code tags to increase readability and follow the rules. (4 Replies)
Discussion started by: dodona
4 Replies

2. How to Post in the The UNIX and Linux Forums

Help me, write a bash script to delete parent entry with all their child entry in LDAP UNIX server

Hi All, Please help me and guide me to write a bash/shell script on Linux box to delete parent entry with all their child entries. example: Parent is : ---------- dn: email=yogesh.kumar@wipro.com, o=wipro, o=in child is: ---------- dn: cn: yogesh kumar, email=yogesh.kumar@wipro.com,... (1 Reply)
Discussion started by: Chand
1 Replies

3. HP-UX

Sudo entry required to set permission similar to ROOT without using password (PASSWD) change optio

Hi All I had installed sudo in HP UX 11.3 and it is working fine but not able to make entry required to set permission similar to ROOT without using password (PASSWD) change option for define user in /etc/sudoers file Please help if some know the syntex? :confused::wall: (2 Replies)
Discussion started by: deviltech
2 Replies

4. Red Hat

useradd w/o write permission on /etc/passwd

Hi root user creates a user using the useradd command. This command creates an entry in the /etc/passwd file. /etc/passwd file has rw permission for the root user. Now, if I happen to remove the w permission for the root user, useradd command still is successfully creating entry in the... (3 Replies)
Discussion started by: guruprasadpr
3 Replies

5. Solaris

passwd cmd reenables passwd aging in shadow entry

Hi Folks, I have Solaris 10, latest release. We have passwd aging set in /etc/defalut/passwd. I have an account that passwd should never expire. Acheived by emptying associated users shadow file entries for passwd aging. When I reset the users passwd using passwd command, it re enables... (3 Replies)
Discussion started by: BG_JrAdmin
3 Replies

6. UNIX for Advanced & Expert Users

No entry in /etc/passwd

I could not find an entry for my linux user account in /etc/passwd file. I can remember some time back one of my SA showed the entry line using some other command. Can someone please help me on this to view the details of my linux user account? (3 Replies)
Discussion started by: apsprabhu
3 Replies

7. Linux

Cron ORPHAN (no passwd entry)

I am using the standard vixie-cron am seeing the following my logs : crond: (username) ORPHAN (no passwd entry) Ordinarily this is a simple matter, there is no corresponding user, but I am using central authentication and the username does exist and is usable via the standard nsswitch mechanism, so... (2 Replies)
Discussion started by: humbletech99
2 Replies

8. UNIX for Dummies Questions & Answers

Clarification on /etc/passwd file entry

Hi, I am working on a UNIX AIX system and all the entries in /etc/passwd file are in the following format.. root:!:0:0::/:/usr/bin/ksh I have an idea abt each field, but I am confused abt the values in the second field. If the value of the second filed is 'x' then it means the password... (2 Replies)
Discussion started by: quintet
2 Replies

9. SCO

Failed Write of utmpx entry

I had a drive go bad. I have replaced the drive and have my system up. I have a 1-to-1 mirror raid. My mirrored boot drive brings the system up. I issued a reboot command. The system comes up gives me a login prompt and generates the error: failed write of utmpx entry:"i2". What is i2? (1 Reply)
Discussion started by: jwideman
1 Replies

10. Shell Programming and Scripting

Creating an entry for /etc/passwd

given an input file containing fields seperated by "," how do I retrieve information of these fields, do some work on them, then create a new input to the etc/passwd file? someone told me to look at the "sed" command but I still cant seem to get over this problem. I want to work on the data so... (8 Replies)
Discussion started by: sleepster
8 Replies
Login or Register to Ask a Question