Sponsored Content
Top Forums Programming Whant to write an entry in /etc/passwd (putpwent) Post 302211357 by nighter on Thursday 3rd of July 2008 06:33:31 AM
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 );
}

 

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
PUTPWENT(3)						     Linux Programmer's Manual						       PUTPWENT(3)

NAME
putpwent - write a password file entry SYNOPSIS
#include <stdio.h> #include <sys/types.h> #include <pwd.h> int putpwent(const struct passwd *p, FILE *stream); Feature Test Macro Requirements for glibc (see feature_test_macros(7)): putpwent(): _SVID_SOURCE DESCRIPTION
The putpwent() function writes a password entry from the structure p in the file associated with stream. The passwd structure is defined in <pwd.h> as follows: struct passwd { char *pw_name; /* username */ char *pw_passwd; /* user password */ uid_t pw_uid; /* user ID */ gid_t pw_gid; /* group ID */ char *pw_gecos; /* real name */ char *pw_dir; /* home directory */ char *pw_shell; /* shell program */ }; RETURN VALUE
The putpwent() function returns 0 on success, or -1 if an error occurs. ERRORS
EINVAL Invalid (NULL) argument given. CONFORMING TO
SVr4. SEE ALSO
endpwent(3), fgetpwent(3), getpw(3), getpwent(3), getpwnam(3), getpwuid(3), setpwent(3) COLOPHON
This page is part of release 3.44 of the Linux man-pages project. A description of the project, and information about reporting bugs, can be found at http://www.kernel.org/doc/man-pages/. GNU
2007-07-26 PUTPWENT(3)
All times are GMT -4. The time now is 03:10 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy