2007-05-19 Marcus Brinkmann <marcus@g10code.de>
[gnupg.git] / tools / addgnupghome
blobfb032b67467607ba30b0e2825c57208a51752348
1 #!/bin/sh
2 # Add a new .gnupg home directory for a list of users -*- sh -*-
4 # Copyright 2004 Free Software Foundation, Inc.
6 # This file is free software; as a special exception the author gives
7 # unlimited permission to copy and/or distribute it, with or without
8 # modifications, as long as this notice is preserved.
10 # This file is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY, to the extent permitted by law; without even the
12 # implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14 PGM=addgnupghome
15 any_error=0
18 error () {
19 echo "$PGM: $*" >&2
20 any_error=1
23 info () {
24 echo "$PGM: $*" >&2
27 # Do it for one user
28 one_user () {
29 user="$1"
30 home=$(${cat_passwd} | awk -F: -v n="$user" '$1 == n {print $6}')
31 if [ -z "$home" ]; then
32 if ${cat_passwd} | awk -F: -v n="$user" '$1 == n {exit 1}'; then
33 error "no such user \`$user'"
34 else
35 error "no home directory for user \`$user'"
37 return
39 if [ ! -d "$home" ]; then
40 error "home directory \`$home' of user \`$user' does not exist"
41 return
43 if [ -d "$home/.gnupg" ]; then
44 info "skipping user \`$user': \`.gnupg' already exists"
45 return
47 info "creating home directory \`$home/.gnupg' for \`$user'"
48 if ! mkdir "$home/.gnupg" ; then
49 error "error creating \`$home/.gnupg'"
50 return
53 if ! chown $user "$home/.gnupg" ; then
54 error "error changing ownership of \`$home/.gnupg'"
55 return
58 group=$(id -g "$user")
59 [ -z "$group" ] && group="0"
61 if [ "$group" -gt 0 ]; then
62 if ! chgrp $group "$home/.gnupg" ; then
63 error "error changing group of \`$home/.gnupg'"
64 return
68 if ! cd "$home/.gnupg" ; then
69 error "error cd-ing to \`$home/.gnupg'"
70 return
72 for f in $filelist; do
73 if [ -d /etc/skel/.gnupg/$f ]; then
74 mkdir $f
75 else
76 cp /etc/skel/.gnupg/$f $f
78 if ! chown $user $f ; then
79 error "error changing ownership of \`$f'"
80 return
82 if [ "$group" -gt 0 ]; then
83 if ! chgrp $group "$f" ; then
84 error "error changing group of \`$f'"
85 return
88 done
92 if [ -z "$1" ]; then
93 echo "usage: $PGM userids"
94 exit 1
97 # Check whether we can use getent
98 if getent --help </dev/null >/dev/null 2>&1 ; then
99 cat_passwd='getent passwd'
100 else
101 cat_passwd='cat /etc/passwd'
102 info "please note that only users from /etc/passwd are checked"
105 if [ ! -d /etc/skel/.gnupg ]; then
106 error "skeleton directory \`/etc/skel/.gnupg' does not exist"
107 exit 1
109 cd "/etc/skel/.gnupg" || (error "error cd-ing to \`/etc/skel/.gnupg'"; exit 1)
110 filelist=$(find . \( -type f -or -type d \) -not -name '*~' -not -name . -print)
113 if ! umask 0077 ; then
114 error "error setting umask"
115 exit 1
118 for name in $*; do
119 one_user $name
120 done
122 exit $any_error