Quote:
Originally Posted by leojau
I'll try to explain better... I'm also a little confused yet about the final result.
I'm working on an existing project and extending its capabilities. The system worked with a single server and multiple clients. It uses Fedora13.
My job is to make the system capable of running with multiple (N) servers and replicate the data between them.
I achieved the replication and my systems works. It still needs improvements and one of them is trying to be solved here.
I'll describe the problem:
Server A has one of it's files updated. Now it needs to replicate and make sure the other N servers have this same file version.
The problem occurs when servers A and B are updated locally at almost the same time before they spread their local update to the others. Update on A could override update on B, or B override A.
|
This is a synchronization problem and can be very hard to solve well.
Quote:
I have some daemon-like script that watches for these local updates and triggers the replication.
What I need is to develop a manner to lock the file inside the others N servers.
|
Creating a lock or a semaphore to prevent simultaneous access is the classical solution.
But what does the application do if it tries to update the file and fails to gain write permission ?
Quote:
I need to develop a script that lock (no writing) the specified local files when necessary.
I can do that locking by "chmod a=rx myfile" being the owner of the myfile or as root (sudo ...).
|
I think "chmod a-w myfile" is closer to what you want - you don't want to add read execute permission needlessly, you want to remove write access.
If I understand you correclty the problem is that you must create this 'lock' on all other systems BEFORE the first system can modify the file. That's very difficult to do accurately in the case another system is trying to create a lock on the same file at the same time. The fact the cron is involved puzzles me - maybe I don't understand your problem.
Quote:
These files that are locked could belong to a regular user (Joe) or could be system files like "/etc/group". This lock command will be given by a "crontab job" as root. That means it can lock or unlock anything.
Problem 1)
I will need a user's manager that will be capable of "rwx" any file inside the /home/ directory. That is problem number 1.
How I do that? I haven't thought enough yet, but I'm pretty sure that the person operating this manager-user will not have root's password.
Let's focus on problem 1 for now.
|
How often does the cron job run ? What happens if the file to be locked has already been modified locally ?
The user-manager does what exactly - does it copy the modified files from the other servers ? Then remove the locks ?
You can create a privileged application in C for example) that users may run, but its unclear what this user-manager does.
Quote:
I'm pretty new with Linux and i don't know yet how Linux permissions work, except for the -rwxr-xr-x Joe users meaning.
Thanks a lot for your help so far!
---------- Post added at 05:53 AM ---------- Previous post was at 05:42 AM ----------
Due to this System's nature, the Joe-like users have no access to any of it's files. It's all isolated from them.
|
==========================
Let me point to a few things just to give you some ideas to think about. I still don't understand your task well enough to describe a solution.
When a user logs into a system they are assigned a numberical userid (uid) and a list of several group-ids (gid) where there is one special primary gid. Each process also has a SELinux context.
Quote:
[stevea@crucibulum Desktop]$ id
uid=1020(stevea) gid=1020(stevea) groups=1020(stevea),10(wheel),1099(everyone) context=unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023
|
So this says my uid is 1020, my primary-gid is also 1020, and I belong to groups 'everyone', 'wheel' and have an SELcontext of "context=unconfined_u:unconfined_r:unconfined_ t:s0-s0:c0.c1023".
If you have a basic POSIX DAC permission scheme (rwxrwxrwx) then yo uhave a set of premissions for the file-owner, the file-group and other' or everyone else.
Quote:
[stevea@crucibulum Desktop]$ ls -l /tmp/8061.pdf
-r--------. 1 stevea stevea 226849 Jul 7 08:47 /tmp/8061.pdf
|
So this pdf file is owned by uid stevea(1020) has group-owner gid stevea(1020) and only grants read permission to the owner uid.
If you mount a filesystem with the acl' option, then we can introduce more permissions for more users per file. Like ....
Code:
[stevea@crucibulum Desktop]$ sudo mount -o remount,acl /
[stevea@crucibulum Desktop]$ getfacl /tmp/8061.pdf
getfacl: Removing leading '/' from absolute path names
# file: tmp/8061.pdf
# owner: stevea
# group: stevea
user::r--
group::---
other::---
[stevea@crucibulum Desktop]$ setfacl -m u:pulse:rw /tmp/8061.pdf
[stevea@crucibulum Desktop]$ setfacl -m g:wireshark:rwx /tmp/8061.pdf
[stevea@crucibulum Desktop]$ getfacl /tmp/8061.pdf
getfacl: Removing leading '/' from absolute path names
# file: tmp/8061.pdf
# owner: stevea
# group: stevea
user::r--
user:pulse:rw-
group::---
group:wireshark:rwx
mask::rwx
other::---
But the file owner is able to changing these DAC properties at will.
==============
A non-owner user can gain the privileges of file owner by becoming root (see 'su and 'sudo') by running a program theat grants privilege (se 'setuid' on "man chown"), or by running a program with 'capabilities' like CAP_DAC_OVERRIDE (see "man 7 capabilities" ,"man setcap" ,"man capsh"), so for example you might create a special copy of 'cat' that only group 'stevea' can execute that is capable of reading any file ....
# one time - root creates special 'cat' for stevea
sudo cp /usr/bin/cat /usr/bin/steveacat
sudo chgrp stevea /usr/bin/steveacat
sudo chmod uo-rwx /usr/bin/steveacat
sudo setcap cap_dac_override+ep /usr/bin/steveacat
So then members of group stevea can use this special copy of 'cat' to gain DAC (rwx) control of any file.
Quote:
[stevea@crucibulum Desktop]$ cat /root/.bashrc
cat: /root/.bashrc: Permission denied
[stevea@crucibulum Desktop]$ steveacat /root/.bashrc
# .bashrc
# User specific aliases and functions
alias rm='rm -i'
alias cp='cp -i'
....
|
So capabilities applied to a specific binary can give very specific privileges to a specific users or groups.
It's better to grant privilege base on group membership rather than owner since then the useer doesn't own the file and cannot change it's DAC.
========================
Linux/UNIX has a concept of locking files,
man flock
man 2 flock
If one program has an exclusive lock, then no other can gain that,
It's possible to use shared file systems , like NFS that can support file locking across a network.
========================
"man inotify" descibes a scheme for watching for changes in files. A socket connection to a file can do similar.
========================
I *think* you want some way to have a central lock authority, perhaps across a network. It might make sense to custom-build a file lock© manager server. Unclear.