[net.unix-wizards] SYS V unlink

sjm@dayton.UUCP (Steven J. McDowall) (02/12/86)

Our system (a CRDS Universe/32 w/ Unos 6.1) is based
on Unix V, or to be more precise, is SYS V compatible.

My question is this: In SYSV do you have* to be SU
to unlink directories? I wrote a program that 
unlinks whatever file name is passed to it to 
confirm my ideas, and sure enough, it would not
remove (unlink) a file that is a directory even if
I owned it, and it had write permission. The error 
I get back is EPERM..Looking it up in the good olde
System V Interface Definition Book (Spring 1985 - Issue 1)
on page 143:

	[EPERM]		The named file is a directory and
			the effective user ID of the process
			is not super-user.

Does that mean that to delete *any* directory the program
must be set uid'ed to root? I can't believe that this is
what is really meant here, and it must be some sort of typo.
Could anyone from Bell please clarify this?

Thanks!



-- 
Steven J. McDowall	
Dayton-Hudson Dept. Store. Co.		UUCP: ihnp4!rosevax!dayton!sjm
700 on the Mall				ATT:  1 612 375 2816
Mpls, Mn. 55408

ka@hropus.UUCP (Kenneth Almquist) (02/15/86)

> Does that mean that to delete [meaning unlink - ka] *any* directory
> the program must be set uid'ed to root? I can't believe that this is
> what is really meant here, and it must be some sort of typo.
> Could anyone from Bell please clarify this?

Yes, the manual means what it says.

This is true for all versions of UN*X.  The reason is that if normal users
were permitted to unlink directories, they might forget to unlink all the
entries in the directory first.  If they forgot to unlink the entry for ".",
the directory would become inaccessible when the entry for it in the parent
directory was unlinked, but the directory would not be deleted because there
would still be an entry pointing to it, namely the entry "." in the directory
itself.  If they remembered to unlink the entry for ".", but forgot to unlink
other entries in the directory, then when the directory disappeared the link
counts on the files or directories that these entries pointed to would be
incorrect.

4.2 BSD has a separate set of system calls called "mkdir" and "rmdir" which
allow non-superusers to create and delete directories and which check that
"." and ".." are handled correctly.  (Actually, the documentation claims
that you have to be superuser to invoke the mkdir system call, but this is
never checked for in the code.)  Under System V, all you have to do to create
and delete directories is to run the mkdir and rmdir programs.
				Kenneth Almquist
				ihnp4!houxm!hropus!ka	(official name)
				ihnp4!opus!ka		(shorter path)

ark@alice.UucP (Andrew Koenig) (02/15/86)

> Our system (a CRDS Universe/32 w/ Unos 6.1) is based
> on Unix V, or to be more precise, is SYS V compatible.
>
> My question is this: In SYSV do you have* to be SU
> to unlink directories? I wrote a program that 
> unlinks whatever file name is passed to it to 
> confirm my ideas, and sure enough, it would not
> remove (unlink) a file that is a directory even if
> I owned it, and it had write permission. The error 
> I get back is EPERM..Looking it up in the good olde
> System V Interface Definition Book (Spring 1985 - Issue 1)
> on page 143:
>
>	[EPERM]		The named file is a directory and
>			the effective user ID of the process
>			is not super-user.
> 
> Does that mean that to delete *any* directory the program
> must be set uid'ed to root? I can't believe that this is
> what is really meant here, and it must be some sort of typo.
> Could anyone from Bell please clarify this?

Correct: only the super-user can unlink a directory.  It has
always been thus (at least as far back as V6).  The reason is
that removing a directory is not simply a matter of breaking the
link to it from its parent.  One must also break the link FROM
the directory TO its parent (always named "..") and the link
from the directory to itself (always named ".").  Unless all three
of these things are done in the proper order, there is the possibility
of various kinds of file system damage.

In order to restrict unlinking of directories to programs that take
pains to do the right things, the system allows only the super-user
to break a link to a directory.

Why can't you believe this is what is really meant?

bzs@bucsd.UUCP (Barry Shein) (02/16/86)

Re: need to be root or setuid to unlink files in SYS/V

Tis true, if you are struggling over a program that wants to unlink
a directory try:

#define RMDIR "/bin/rmdir"
	...time passes...
	sprintf(buf,"%s %s",RMDIR,directoryname);
	system(buf);

with appropriate error checks and customizing. And stop feeling
sorry for the wires :-)

	-Barry Shein, Boston University

gwyn@brl-smoke.ARPA (Doug Gwyn ) (02/18/86)

> My question is this: In SYSV do you have* to be SU
> to unlink directories?

Yes, insofar as the unlink() system call is concerned.
(Ditto for making links to directories.)  This should
be true for any UNIX that does not supply special
directory system calls.

> Does that mean that to delete *any* directory the program
> must be set uid'ed to root?

No, you can always use something like:
	system( "rm directory-name" );
"rm", or a set-UID subprocess "rmdir" that it might
invoke, has the necessary privilege.

sjm@dayton.UUCP (Steven J. McDowall) (02/18/86)

In article <4980@alice.uUCp> ark@alice.UucP (Andrew Koenig) writes:
>
>Why can't you believe this is what is really meant?
>

Ok ok.. As the original author:

1)  I believe! I believe!
2)  It seemed to me (still does) that this restriction is
    a hinder to developing major systems that uses directories
    for structuring information. 
3)  Yes, I know* that I can do a system() call to issue either
    a mkdir or rmdir.. My point is that, if a program can
    perform the that function (via system) then it would seem
    logical for the OS to provide the service w/o the overhead
    of invoking the system call. (system() is pretty expensive, 
    isn't it?)

    Anyway, thanks for the answers.. It does* make sense that
    you can't arbitrarily delete directories, thought I would
    have thought that if unlink() (I do like BSD's rmdir, mkdir)
    would allow us to remove directories, it would take care of
    all the checking for us..Same of course if we could
    do a mkdir()....


-- 
Steven J. McDowall	
Dayton-Hudson Dept. Store. Co.		UUCP: ihnp4!rosevax!dayton!sjm
700 on the Mall				ATT:  1 612 375 2816
Mpls, Mn. 55408

levy@ttrdc.UUCP (Daniel R. Levy) (03/01/86)

<Oh oh here it comes.  Watch out boy, it'll chew you up! \
Oh oh here it comes.  The LINE EATER!  [Line eater]>

In article <1019@brl-smoke.ARPA>, gwyn@brl-smoke.UUCP writes:
>> My question is this: In SYSV do you have* to be SU
>> to unlink directories?
>Yes, insofar as the unlink() system call is concerned.
>(Ditto for making links to directories.)  This should
>be true for any UNIX that does not supply special
>directory system calls.
>> Does that mean that to delete *any* directory the program
>> must be set uid'ed to root?
>No, you can always use something like:
>	system( "rm directory-name" );
>"rm", or a set-UID subprocess "rmdir" that it might
>invoke, has the necessary privilege.

Surely you mean "rm -r directory-name" or even better "rm -rf directory-name".

Just system("rm directory-name") will evince the diagnostic:

rm: directory-name directory

to stderr.
-- 
 -------------------------------    Disclaimer:  The views contained herein are
|       dan levy | yvel nad      |  my own and are not at all those of my em-
|         an engihacker @        |  ployer or the administrator of any computer
| at&t computer systems division |  upon which I may hack.
|        skokie, illinois        |
 --------------------------------   Path: ..!{akgua,homxb,ihnp4,ltuxa,mvuxa,
						vax135}!ttrdc!levy