When Can You Modify a Timestamp?

File this under: ‘gotchas’.

Question: when can you Modify/Update the timestamp on a file?
Answer: when you own the file, not just have write permission on it.

Disclaimer: I’m still a bit confused why this is true. It may be a bug or I may be missing a concept.

I came upon a nameserver that wasn’t updating its slaves properly. It gave the error "refresh: could not set file modification time on thefilenamehere". The directory (on ext3) was permissioned thusly:

ls -ld slaves
drwxrwxr-x 2 myuser named 4096 Sep  7 17:31 slaves/

Files inside it looked like:

#ls -l slaves/
total 108
-rw-rw-r-- 1 myuser named 502 Sep  7 17:31 example.com
-rw-rw-r-- 1 myuser named 429 Sep  7 17:31 example.org
-rw-rw-r-- 1 myuser named 519 Sep  7 17:31 example.net

Doing an strace on BIND revealed this syscall:

utimes("slaves/example.com", {1157939653, 964786}) = -1 EPERM (Operation not permitted)

Well, why the heck not? BIND was running as user named and the named user is in the named group, which has write permissions on both the files and the directories.

The man page for utimes says:

int utimes(const char *filename, const struct timeval tv[2]);

Changing  time  stamps  is permitted when: either the process has appropriate privileges (Linux:
has the CAP_FOWNER capability), or the effective user ID equals the user ID of the file, or  buf
must is NULL and the process has write permission to the file.

What’s not clear is when each of the OR conditions applies. Figuring the second one must be the case (for some reason) I tried the following:

chown -R named.named slaves

and added myuser to the named group.

Voila, timestamps got updated and the zone files slaved.

Tricksty this linux can be.