How to modify ascii text file (/etc/passwd)

How to modify ascii text file (/etc/passwd)

Post by Lee All » Fri, 04 Jul 1997 04:00:00



I have to write a program to maintain users.  That is, create new
accounts, change or delete existing accounts, etc.  The program must
maintain entries in /etc/passwd and /etc/group as well as our own
database files, which contain additional information about each user.

There are system & library routines (and plenty of good documention)
enabling me to read /etc/passwd and /etc/group.  And putpwent is OK
for adding new users to the end of the file.  But how do I delete an
entry, or modify an entry?  I cannot find any system or library
routines to do this (Linux 2.0.25).

As I struggle to figure this out, I realize I have no idea how to do
this with ANY simple ASCII text file!  That is: given a file
containing variable-length records, how does one modify or delete a
record in the middle of the file?

Of course there is the brute force approach: Copy it to another file,
modifying it along the way, then replace the original file with the
new one.  But I am scared codeless of manhandling the /etc/passwd file
in this manner.

I must be missing something.

-Lee Allen

 
 
 

How to modify ascii text file (/etc/passwd)

Post by Boyd Rober » Fri, 04 Jul 1997 04:00:00



>Of course there is the brute force approach: Copy it to another file,
>modifying it along the way, then replace the original file with the
>new one.  But I am scared codeless of manhandling the /etc/passwd file
>in this manner.

That's what you do.  It's a flat file, not a database.

--

``Not only is UNIX dead, it's starting to smell really bad.'' -- rob

 
 
 

How to modify ascii text file (/etc/passwd)

Post by Andrew Giert » Fri, 04 Jul 1997 04:00:00



 >> Of course there is the brute force approach: Copy it to another file,
 >> modifying it along the way, then replace the original file with the
 >> new one.  But I am scared codeless of manhandling the /etc/passwd file
 >> in this manner.

 Boyd> That's what you do.  It's a flat file, not a database.

Of course, you also find out what the necessary locking convention is,
so that your changes don't get blown away just because someone happened
to run passwd(1) at the same time.

--
Andrew.

comp.unix.programmer FAQ: see <URL: http://www.erlenstar.demon.co.uk/unix/>

 
 
 

How to modify ascii text file (/etc/passwd)

Post by Jeffrey C. De » Sat, 05 Jul 1997 04:00:00





> >> Of course there is the brute force approach: Copy it to another file,
> >> modifying it along the way, then replace the original file with the
> >> new one.  But I am scared codeless of manhandling the /etc/passwd file
> >> in this manner.

> Boyd> That's what you do.  It's a flat file, not a database.

>Of course, you also find out what the necessary locking convention is,
>so that your changes don't get blown away just because someone happened
>to run passwd(1) at the same time.

VIPW(8)                  UNIX System Manager's Manual                  VIPW(8)

NAME
     vipw - edit the password file

SYNOPSIS
     vipw

DESCRIPTION
     Vipw edits the password file after setting the appropriate locks, and
     does any necessary processing after the password file is unlocked.  If
     the password file is already locked for editing by another user, vipw
     will ask you to try again later. The default editor for vipw is vi(1).

--
        "I quite agree with you," said the Duchess; "and the m*of
that is -- `Be what you would seem to be' -- or, if you'd like it put
more simply -- `Never imagine yourself not to be otherwise than what it
might appear to others that what you were or might have been was not
otherwise than what you had been would have appeared to them to be
otherwise.'"
                -- Lewis Carrol, "Alice in Wonderland"

 
 
 

How to modify ascii text file (/etc/passwd)

Post by Lee All » Sat, 05 Jul 1997 04:00:00


Quote:> >> But I am scared codeless of manhandling the /etc/passwd file
> >> in this manner.
> Boyd> That's what you do.  It's a flat file, not a database.
>Of course, you also find out what the necessary locking convention is,
>so that your changes don't get blown away just because someone happened
>to run passwd(1) at the same time.

Would you enlighten me?  

I think the steps are..
1. Copy /etc/passwd to /etc/passwd.new, changing it along the way
2. rm /etc/passwd
3. mv /etc/passwd.new /etc/passwd

Tho I may be able to modify the directory entry directly, to point to
the new file, instead of steps 2 & 3.   That way there would not be a
gap in time in which /etc/passwd doesn't exist.

But I am ignorant of the "locking convention" .

-Lee

 
 
 

How to modify ascii text file (/etc/passwd)

Post by Andrew Giert » Sat, 05 Jul 1997 04:00:00


 >> Of course, you also find out what the necessary locking convention is,
 >> so that your changes don't get blown away just because someone happened
 >> to run passwd(1) at the same time.

 Lee> Would you enlighten me?  

 Lee> I think the steps are..
 Lee> 1. Copy /etc/passwd to /etc/passwd.new, changing it along the way
 Lee> 2. rm /etc/passwd
 Lee> 3. mv /etc/passwd.new /etc/passwd

Not quite.

On most systems, the existence of a specific file (usually /etc/ptmp
or /etc/passwd.tmp) is regarded as a lock. Therefore, you find out
what that is (if your system has a vipw command, this is easy).
Create /etc/ptmp (or whatever) using the O_EXCL flag (if it fails
because the file already exists, sleep for a bit and then try again);
build your new passwd file in ptmp, then use rename() to replace
/etc/passwd.

Another approach is to play games with the EDITOR variable and call
vipw; that's the only safe way of doing it in a script that I know of.

The other question is: does your system have any other files that
depend on /etc/passwd? The most obvious example is systems that
have a password database, or a shadow password file.

--
Andrew.

comp.unix.programmer FAQ: see <URL: http://www.erlenstar.demon.co.uk/unix/>

 
 
 

How to modify ascii text file (/etc/passwd)

Post by Juha Lai » Mon, 07 Jul 1997 04:00:00



Quote:>>Of course, you also find out what the necessary locking convention is,
>>so that your changes don't get blown away just because someone happened
>>to run passwd(1) at the same time.

>Would you enlighten me?  

>I think the steps are..
>1. Copy /etc/passwd to /etc/passwd.new, changing it along the way
>2. rm /etc/passwd
>3. mv /etc/passwd.new /etc/passwd

>Tho I may be able to modify the directory entry directly, to point to
>the new file, instead of steps 2 & 3.   That way there would not be a
>gap in time in which /etc/passwd doesn't exist.

That is certainly true, but you wouldn't know what data finally ends up in
the /etc/passwd.

Quote:>But I am ignorant of the "locking convention" .

Suppose two persons change their password at (almost) the same time.
The first one is going somewhere along step 1 when the second one begins,
creating (empty!) passwd.new while the first one was still writing it.
The steps 2 and 3 most possibly write an incomplete copy of the passwd
file when the first pass goes through - and at best the second pass only
has some duplicates and misses the changed data from the first use - at
worst you'd be looking at a severely corrupt passwd file.

One thing I've seen used as the lock to prevent multiple simultaneous edits
on passwd is to use the passwd.new itself as lock - don't proceed while
it exists. Naturally, here come some more problems; f.ex. the process that
created the passwd.new might have been killed before it finished so the
passwd.new never gets removed and no new changes can be committed.

One more problem will be to be compatible with the system "passwd"
command locking - you have also to prevent it from running while your
editing system is making changes. The problem here is that the passwd
command locking mechanism is often not documented, so you have to
make some educated guesses here (run passwd under "truss" or "strace",
run "strings" on the passwd binary to find out the locking mechanism).
--
Wolf  a.k.a.  Juha Laiho     Espoo, Finland
(GC 3.0) GIT d- s+: a- C++ UH++++$ UL++++ P+ L+++ E-(---) W+ N+++ !K w !O
         !M V PS(+) PE Y+ PGP(+) t- 5? !X R tv--- b+ DI? D+ G e+ h--- r++ y+
"...cancel my subscription to the resurrection!" (Jim Morrison)

 
 
 

1. Help for Shadow Password problem after modify the file /etc/passwd !!

Hi, dear all !!

I had deleted one user (one line only) in the file /etc/passwd with one
text editor "joe" but I forgot to delete the same user in the file
/etc/shadow.  

Afterwards, I have found one problem,  that all users cannot login the
linux server successfully via either telnet or virtual console locally.
It shows "Login incorrect" although I have entered the right login user
name and password.

But I can login this server with "rlogin" without the "Login incorrect"
problem. Besides, it is also no problem while using ftp, samba ...etc.

Only using telnet and login can get "Login incorrect" problem.

Anyone can give me one help !!!?
Please tell me how to fix this problem.

Thanks a lot for your kind attention !

Best Rgds. Louie

2. mounting and formatting floppy

3. Convert NIS passwd back to standard /etc/passwd & /etc/shadow

4. Can mono xterm support AN

5. /etc/passwd & /etc/security/passwd

6. Help on vector

7. "Login Incorrect" after modifying /etc/passwd !??

8. Problem with DNS/DHCP server after reboot

9. Modifying /etc/passwd's comment field

10. Solaris 8-- How to modify /etc/passwd before login?

11. patch to support non-/etc/passwd passwd files

12. rconsiling the /etc/shadow and /etc/passwd files

13. how to print ascii text compressed on deskjet, laser, etc..