The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > High Level Programming
Google UNIX.COM


High Level Programming Post questions about C, C++, Java, SQL, and other programming languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Clarification on /etc/passwd file entry quintet UNIX for Dummies Questions & Answers 2 09-28-2007 05:11 AM
I want to read username and lastupdate only from /etc/security/passwd and write the s me_haroon AIX 0 07-01-2006 01:16 AM
Failed Write of utmpx entry jwideman SCO 1 02-04-2005 02:37 PM
Creating an entry for /etc/passwd sleepster Shell Programming and Scripting 8 09-12-2003 08:05 PM
shutting down 5.7 on Intel: failed to write of utmpx entry DarkLord UNIX for Advanced & Expert Users 2 12-05-2002 10:22 AM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 06-30-2008
Registered User
 

Join Date: Jun 2008
Posts: 10
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
--------
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
----
#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;
}
------------------------
Reply With Quote
Forum Sponsor
  #2  
Old 06-30-2008
Moderator
 

Join Date: Dec 2003
Location: /dev/fl
Posts: 1,059
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);
Reply With Quote
  #3  
Old 07-01-2008
Sivaswami's Avatar
Registered User
 

Join Date: Mar 2007
Location: India
Posts: 58
I think there is no need of opening the passwd file with fopen when using functions from pwd.h
Reply With Quote
  #4  
Old 07-03-2008
Registered User
 

Join Date: Jun 2008
Posts: 10
Actaully it was not that who where the problem!
But now it works! Don't understand why. But it do.
Reply With Quote
  #5  
Old 07-03-2008
Registered User
 

Join Date: Jun 2008
Posts: 10
/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.

#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 );
}
Reply With Quote
  #6  
Old 07-03-2008
Registered User
 

Join Date: Jun 2008
Posts: 10
wrong button it was intended to be a new threat.
Reply With Quote
Google The UNIX and Linux Forums
Reply

Tags
linux, solaris, ubuntu

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 09:02 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0