2008年4月4日 星期五

檔案建立與群組目錄共享(SGID的使用):

實作範例:Each department(hr, web, sales) for which you created a group also needs a shared directory. This will allow users in each department to share files, but will prevent users in other department from altering, or even seeing those files.

只有相同群組的使用者具有進入群組目錄的權限,其餘使用者無,且相同使用者具有共享檔案的權限。


[root@server1 depts]# mkdir -p /depts/{sales,hr,web}

[root@server1 depts]# ls | grep -v tech | sed 's/^/chmod 770 /' | sh –x

+ chmod 770 hr

+ chmod 770 sales

+ chmod 770 web

[root@server1 depts]# ls | grep -v tech | sed 's/^/chmod g+s /' | sh -x

+ chmod g+s hr

+ chmod g+s sales

+ chmod g+s web


根據以上的設定,相同群組的用戶,將不用變更檔案的權限(permission),即可達到檔案共享的目的。

檔案存取清單(ACLs):

實作範例:Some data need to be accessible to several groups. Use ACLs to accomplish this. Create a new directory in /depts/ called tech. Change the permissions such that root is the owner and hr is the group. Use ACLs to give full permission for /depts/tech/ to the web group. Allow alex read/execute (but not write)permission on the /depts/tech/ directry. Set the default ACL of read/write for alex on that directory/


[root@server1 ~]# vi /etc/fstab

LABEL=/ / ext3 defaults,acl 1 1


[root@server1 ~]# mount

/dev/sda2 on / type ext3 (rw,acl)


[root@server1 depts]# setfacl -b tech

[root@server1 depts]# ll

total 32

drwxrws--- 2 root hr 4096 Jan 11 10:50 hr

drwxrws--- 2 root sales 4096 Jan 11 10:50 sales

drwxr-s--- 2 root hr 4096 Jan 11 13:55 tech

drwxrws--- 2 root web 4096 Jan 11 10:50 web

[root@server1 depts]# setfacl -m g:web:rwx tech

[root@server1 depts]# setfacl -m u:alex:rx tech

[root@server1 depts]# setfacl -m d:u:alex:rw tech

[root@server1 depts]# getfacl tech

[root@server1 depts]# getfacl tech/

# file: tech

# owner: root

# group: hr

user::rwx

user:alex:r-x

group::rwx

group:web:rwx

mask::rwx

other::---

default:user::rwx

default:user:alex:rw-

default:group::rwx

default:mask::rwx

default:other::---


ACLs為權限擴張的表現,在原本的權限下(root.hr rwxrwx---),另外提供web grouprwx權限,以及user alexrx權限,default ACLs的設定,讓alex有使用檔案的權限,無新建檔案的權限;其中user::rwx user:alex:r-x代表目錄的所有人具有rwx的權限之外,且用戶 alex具有列出目錄中的檔案與進入目錄的權限;group::rwx group:web:rwx代表原有的group hr外且web group同樣具有rwx的權限。


SETFACL(1) Access Control Lists SETFACL(1)

NAME

setfacl - set file access control lists

SYNOPSIS

setfacl [-bkndRLPvh] [{-m|-x} acl_spec] [{-M|-X} acl_file] file ...

setfacl --restore=file


DESCRIPTION
This utility sets Access Control Lists (ACLs) of files and directories.
On the command line, a sequence of commands is followed by a sequence
of files (which in turn can be followed by another sequence of com-
mands, ...).

The options -m, and -x expect an ACL on the command line. Multiple ACL
entries are separated by comma characters (‘,’). The options -M, and -X
read an ACL from a file or from standard input. The ACL entry format is
described in Section ACL ENTRIES.

The --set and --set-file options set the ACL of a file or a directory.

The previous ACL is replaced. ACL entries for this operation must
include permissions.

The -m (--modify) and -M (--modify-file) options modify the ACL of a
file or directory. ACL entries for this operation must include permis-
sions.

The -x (--remove) and -X (--remove-file) options remove ACL enries.
Only ACL entries without the perms field are accepted as parameters,
unless POSIXLY_CORRECT is defined.

When reading from files using the -M, and -X options, setfacl accepts
the output getfacl produces. There is at most one ACL entry per line.

After a Pound sign (‘#’), everything up to the end of the line is
treated as a comment.

If setfacl is used on a file system which does not support ACLs, set-
facl operates on the file mode permission bits. If the ACL does not fit
completely in the permission bits, setfacl modifies the file mode per-
mission bits to reflect the ACL as closely as possible, writes an error
message to standard error, and returns with an exit status greater than
0.


PERMISSIONS
The file owner and processes capable of CAP_FOWNER are granted the
right to modify ACLs of a file. This is analogous to the permissions
required for accessing the file mode. (On current Linux systems, root
is the only user with the CAP_FOWNER capability.)


OPTIONS

-b, --remove-all
Remove all extended ACL entries. The base ACL entries of the owner,
group and others are retained.

-k, --remove-default
Remove the Default ACL. If no Default ACL exists, no warnings are
issued.

-n, --no-mask
Do not recalculate the effective rights mask. The default behavior
of setfacl is to recalculate the ACL mask entry, unless a mask
entry was explicitly given. The mask entry is set to the union of
all permissions of the owning group, and all named user and group
entries. (These are exactly the entries affected by the mask
entry).

--mask
Do recalculate the effective rights mask, even if an ACL mask entry
was explicitly given. (See the -n option.)

-d, --default
All operations apply to the Default ACL. Regular ACL entries in the
input set are promoted to Default ACL entries. Default ACL entries
in the input set are discarded. (A warning is issued if that hap-
pens).

--restore=file
Restore a permission backup created by ‘getfacl -R’ or similar. All
permissions of a complete directory subtree are restored using this
mechanism. If the input contains owner comments or group comments,
and setfacl is run by root, the owner and owning group of all files
are restored as well. This option cannot be mixed with other
options except ‘--test’.

--test
Test mode. Instead of changing the ACLs of any files, the resulting
ACLs are listed.

-R, --recursive
Apply operations to all files and directories recursively. This
option cannot be mixed with ‘--restore’.

-L, --logical
Logical walk, follow symbolic links. The default behavior is to
follow symbolic link arguments, and to skip symbolic links encoun-
tered in subdirectories. This option cannot be mixed with
‘--restore’.

-P, --physical
Physical walk, skip all symbolic links. This also skips symbolic
link arguments. This option cannot be mixed with ‘--restore’.

--version
Print the version of setfacl and exit.

--help
Print help explaining the command line options.

-- End of command line options. All remaining parameters are inter-
preted as file names, even if they start with a dash.

- If the file name parameter is a single dash, setfacl reads a list
of files from standard input.


ACL ENTRIES
The setfacl utility recognizes the following ACL entry formats (blanks
inserted for clarity):


[d[efault]:] [u[ser]:]uid [:perms]
Permissions of a named user. Permissions of the file owner if
uid is empty.

[d[efault]:] g[roup]:gid [:perms]
Permissions of a named group. Permissions of the owning group if
gid is empty.

[d[efault]:] m[ask][:] [:perms]
Effective rights mask

[d[efault]:] o[ther][:] [:perms]
Permissions of others.

Whitespace between delimiter characters and non-delimiter characters is
ignored.


Proper ACL entries including permissions are used in modify and set
operations. (options -m, -M, --set and --set-file). Entries without
the perms field are used for deletion of entries (options -x and -X).

For uid and gid you can specify either a name or a number.

The perms field is a combination of characters that indicate the per-
missions: read (r), write (w), execute (x), execute only if the file is
a directory or already has execute permission for some user (X).
Alternatively, the perms field can be an octal digit (0-7).


AUTOMATICALLY CREATED ENTRIES

Initially, files and directories contain only the three base ACL
entries for the owner, the group, and others. There are some rules that
need to be satisfied in order for an ACL to be valid:

* The three base entries cannot be removed. There must be exactly one
entry of each of these base entry types.

* Whenever an ACL contains named user entries or named group objects,
it must also contain an effective rights mask.

* Whenever an ACL contains any Default ACL entries, the three Default
ACL base entries (default owner, default group, and default others)
must also exist.

* Whenever a Default ACL contains named user entries or named group
objects, it must also contain a default effective rights mask.

To help the user ensure these rules, setfacl creates entries from
existing entries under the following conditions:

* If an ACL contains named user or named group entries, and no mask
entry exists, a mask entry containing the same permissions as the
group entry is created. Unless the -n option is given, the permis-
sions of the mask entry are further adjusted to include the union
of all permissions affected by the mask entry. (See the -n option
description).

* If a Default ACL entry is created, and the Default ACL contains no
owner, owning group, or others entry, a copy of the ACL owner, own-
ing group, or others entry is added to the Default ACL.

* If a Default ACL contains named user entries or named group
entries, and no mask entry exists, a mask entry containing the same
permissions as the default Default ACL’s group entry is added.
Unless the -n option is given, the permissions of the mask entry
are further adjusted to inclu de the union of all permissions
affected by the mask entry. (See the -n option description).


EXAMPLES
Granting an additional user read access
setfacl -m u:lisa:r file

Revoking write access from all groups and all named users (using the
effective rights mask)
setfacl -m m::rx file

Removing a named group entry from a file’s ACL
setfacl -x g:staff file

Copying the ACL of one file to another
getfacl file1 | setfacl --set-file=- file2

Copying the access ACL into the Default ACL
getfacl --access dir | setfacl -d -M- dir

CONFORMANCE TO POSIX 1003.1e DRAFT STANDARD 17
If the environment variable POSIXLY_CORRECT is defined, the default
behavior of setfacl changes as follows: All non-standard options are
disabled. The ‘‘default:’’ prefix is disabled. The -x and -X options
also accept permission fields (and ignore them).

AUTHOR
Andreas Gruenbacher, <a.gruenbacher@bestbits.at>.

Please send your bug reports, suggested features and comments to the
above address.

SEE ALSO
getfacl(1), chmod(1), umask(1), acl(5)

May 2000 ACL File Utilities SETFACL(1)

2008年1月16日 星期三

Service: Sendmail – grnericstable

前言

Sendmail提供重新編寫寄件者(The header of sander)的檔頭資料的功能,可以針對usernamedimainnameusername@domainname)個別進行資料重編,在建立Sendmail Sender Rewrite功能時先修改/etc/sendmail.mcSendmail開啟重新編寫郵件訊息中寄件者資料的功能,再建立寄件者資料對應資料庫(genericstable)與收件者對應資料庫(/etc/aliases)以讓收件者可以收發信件。


Key words: Sendmail, genericstable,


  1. 相關設定與調校

    1. 啟用寄件者資料重編功能:

修改/etc/sendmail.mc以啟用Sender write功能,在此一檔案中,以下的設定值必需是在MAILER( )之前;

FEATURE(`genericstable')dnl

FEATURE(always_add_domain)dnl

FEATURE(`generics_entire_domain')dnl

GENERICS_DOMAIN_FILE(`/etc/mail/local-host-names')dnl


# make –C /etc/mail

# service sendmail restart


    1. 建立寄件者資料對應資料庫(以myuser1為例):

[root@server1 mail]# cat > genericstable

myuser1 myuser1.alias@example.com

myuser2 myuser2.alias@example.com

[root@server1 mail]# makemap hash genericstable < genericstable


    1. 建立服務網域資料庫:

[root@server1 mail]# cat >> local-host-names

server1.example.com

example.com


    1. 建立收件者對應資料庫

[root@server1 mail]# cat >> /etc/aliases

myuser1.alias: myuser1

myuser2.alias: myuser2


[root@server1 mail]# newaliases

/etc/aliases: 80 aliases, longest 15 bytes, 848 bytes total


  1. 驗證

Local

[root@server1 mail]# su - myuser1

[myuser1@server1 ~]$ echo 'test alias' | mail -vs 'test' root@station5.example.com

root@station5.example.com... Connecting to [127.0.0.1] via relay...

220 server1.example.com ESMTP Sendmail 8.13.5/8.13.5; Thu, 17 Jan 2008 13:48:44 +0800

>>> EHLO server1.example.com

250-server1.example.com Hello localhost.localdomain [127.0.0.1], pleased to meet you

250-ENHANCEDSTATUSCODES

250-PIPELINING

250-8BITMIME

250-SIZE

250-DSN

250-ETRN

250-DELIVERBY

250 HELP

>>> MAIL From:<myuser1@server1.example.com> SIZE=56

250 2.1.0 <myuser1@server1.example.com>... Sender ok

>>> RCPT To:<root@station5.example.com>

>>> DATA

250 2.1.5 <root@station5.example.com>... Recipient ok

354 Enter mail, end with "." on a line by itself

>>> .

250 2.0.0 m0H5miI1025855 Message accepted for delivery

root@station5.example.com... Sent (m0H5miI1025855 Message accepted for delivery)

Closing connection to [127.0.0.1]

>>> QUIT

221 2.0.0 server1.example.com closing connection


Remote

[root@station5 root]# mutt

Date: Thu, 17 Jan 2008 13:48:44 +0800

From: myuser1.aliases@example.com

To: root@station5.example.com

Subject: test


test alias


回信驗證 /etc/aliases 的設定:

Date: Thu, 17 Jan 2008 14:20:50 +0800

From: root <root@station5.example.com>

To: myuser1.alias@example.com

Subject: Re: test

User-Agent: Mutt/1.2.5.1i

In-Reply-To: <200801170555.m0H5tevD025923@server1.example.com>; from

+myuser1.alias@example.com on Thu, Jan 17, 2008 at 01:55:40PM +0800


Alias is OK to go


On Thu, Jan 17, 2008 at 01:55:40PM +0800, myuser1.alias@example.com wrote:

> test alias

2008年1月13日 星期日

Security: The Pluggable Authentication Module (PAM) system

I.Module type

  1. auth

  2. account

  3. passwd

  4. session

II.Control flag

  1. required

  2. requested

  3. sufficient

  4. optional


實際範例 /etc/pam.d/login


[root@server1 pam.d]# cat /etc/pam.d/login

#%PAM-1.0

auth [user_unknown=ignore success=ok ignore=ignore default=bad] pam_securetty.so

auth include system-auth

account required pam_nologin.so

account include system-auth

password include system-auth

# pam_selinux.so close should be the first session rule

session required pam_selinux.so close

session include system-auth

session required pam_loginuid.so

session optional pam_console.so

# pam_selinux.so open should only be followed by sessions to be executed in the user context

session required pam_selinux.so open

session optional pam_keyinit.so force revoke


實際範例 – 自訂PAM (限制特定的使用者存取檔案)

模組檔存放位置:/lib/security/pam_listfile.so

模組檔說明位置:/usr/share/doc/pam-0.99.6.2/txts

模組用途:限制使用者存取

模組形式:auth,

範例:

auth required pam_listfile.so oneer=succeed item=user sense=allow file=/etc/special

當加入此項於PAM時,任何列名於/etc/special中的使用者,會無法存取。

例如在 /etc/pam.d/vsftpd中的設定便會檢查 /etc/vsftpd/ftpuser中所列的使用者,並限制列名其中的使用者其使用權。

[root@server1 pam.d]# cat vsftpd

#%PAM-1.0

session optional pam_keyinit.so force revoke

auth required pam_listfile.so item=user sense=deny file=/etc/vsftpd/ftpusers onerr=succeed

auth required pam_shells.so

auth include system-auth

account include system-auth

session include system-auth

session required pam_loginuid.so


實際範例 – tally.so模組 (追蹤錯誤的登入行為)

首先我們要了解登入時所帶入的PAM模組的位置,/etc/pam.d/login再其第一行中定義了root的登入僅能藉由secure tty/etc/securetty)中,而第二行中定義了一般user在登入時的身分驗證行為,故在以下的實際操作中利用在system-auth中加入tally.so模組來追蹤使用者錯誤的登入行為。

# cd /etc/pam.d

# vi /etc/pam.d/system-auth

#%PAM-1.0

# This file is auto-generated.

# User changes will be destroyed the next time authconfig is run.

auth required pam_tally.so no_magic_root 加入

auth required pam_env.so

auth sufficient pam_unix.so nullok try_first_pass

auth requisite pam_succeed_if.so uid >= 500 quiet

auth required pam_deny.so

account required pam_unix.so

account sufficient pam_succeed_if.so uid < 500 quiet

account required pam_permit.so

password requisite pam_cracklib.so try_first_pass retry=3

password sufficient pam_unix.so md5 shadow nullok try_first_pass use_authtok

password required pam_deny.so

session optional pam_keyinit.so revoke

session required pam_limits.so

session [success=1 default=ignore] pam_succeed_if.so service in crond quiet use_uid

session required pam_unix.so

存檔之後進行驗證:

利用其他的tty以一般user進行登入(以下以student做介紹),並故意輸入錯誤的密碼直到出現”Permission denied (publickey,gssapi-with-mic,password).”再進入系統輸入faillog –u student.

[root@server1 pam.d]# faillog -u student

Login Failures Maximum Latest On

student 6 0 01/12/08 09:47:49 +0800 192.168.1.1


2008年1月11日 星期五

用戶建立與預設家目錄中的檔案

家目錄中預設檔案的用途,以umask為例,一般我們預設umask的值為0022,亦即我們所建立的目錄權限為755,而檔案權限為644,當我們要讓相同group內的使用者可以共享檔案(g=rw)時,便可以如此做:


echo “umask 0002” >> /etc/skel/.bashrc


利用for loop快速建立使用者

實作範例:A system with user Joshua and alex in the sales group; dax and byran in the hr group; zak and ed in the web group, and manager in the sales, hr, and web groups.

#for group in sales hr web

>do

>groupadd $group

>done


#for user in Joshua alex dax byran zak ed manager

>do

>useradd $user

>done


#usermod –G sales,hr,web manager