rlm@2
|
1 #!/usr/bin/env perl
|
rlm@2
|
2
|
rlm@3
|
3
|
rlm@3
|
4 #this is the root of the package users' homes
|
rlm@3
|
5 $pkg_home_root = "/pkg";
|
rlm@3
|
6
|
rlm@3
|
7
|
rlm@2
|
8 if ($ARGV[0] eq "--help"){
|
rlm@2
|
9 print <<"HERE";
|
rlm@2
|
10
|
rlm@2
|
11 pkg -- create a package user.
|
rlm@2
|
12
|
rlm@2
|
13 syntax:
|
rlm@2
|
14 pkg package-name description
|
rlm@2
|
15 pkg --help
|
rlm@3
|
16
|
rlm@2
|
17 Create a package user for the purpose of compiling
|
rlm@2
|
18 a particular package.
|
rlm@2
|
19
|
rlm@2
|
20 The user will be created with user and group both
|
rlm@2
|
21 equal to the name you specify.
|
rlm@2
|
22
|
rlm@2
|
23 You will then be put into the user's home
|
rlm@2
|
24 directory where you may proceed with
|
rlm@3
|
25 compiling/installing the package.
|
rlm@2
|
26
|
rlm@2
|
27 If the user already exists, this is equivalent to
|
rlm@3
|
28 su --login package-name
|
rlm@2
|
29
|
rlm@2
|
30 Written by Robert McIntyre. This software is free
|
rlm@2
|
31 software and is released to the public domain.
|
rlm@2
|
32
|
rlm@2
|
33 HERE
|
rlm@2
|
34 exit 0;
|
rlm@2
|
35 }
|
rlm@3
|
36 $" = " ";
|
rlm@2
|
37
|
rlm@2
|
38 $pkg_name = $ARGV[0];
|
rlm@2
|
39
|
rlm@2
|
40 $pkg_desc = $ARGV[1];
|
rlm@2
|
41
|
rlm@2
|
42 # Validate pkg_user as a user name.
|
rlm@3
|
43 if (!($pkg_name =~ m/^[a-z_][a-z0-9_-]*\$?$/)){
|
rlm@2
|
44 print "$pkg_name is not a valid user name!\n";
|
rlm@3
|
45 exit 1;
|
rlm@2
|
46 }
|
rlm@2
|
47
|
rlm@2
|
48 # Validate description.
|
rlm@2
|
49 if ($pkg_desc =~ m/:/){
|
rlm@2
|
50 print "Package Description cannot contain \":\"\n";
|
rlm@3
|
51 exit 1;
|
rlm@2
|
52 }
|
rlm@2
|
53
|
rlm@2
|
54
|
rlm@3
|
55 $pkg_home = "$pkg_home_root/$pkg_name";
|
rlm@2
|
56
|
rlm@3
|
57 #set default for $pkg_desc.
|
rlm@3
|
58 if (!($pkg_desc)){$pkg_desc = $pkg_name;}
|
rlm@2
|
59
|
rlm@3
|
60 @pkg_switch_cmd = ("su", "--login", $pkg_name);
|
rlm@3
|
61
|
rlm@3
|
62 @pkg_create_cmd =
|
rlm@3
|
63 ("useradd", "-c", $pkg_desc, "-d", $pkg_home,
|
rlm@3
|
64 "-g", $pkg_name, "-s", "/bin/bash",
|
rlm@3
|
65 $pkg_name);
|
rlm@3
|
66
|
rlm@3
|
67 @pkg_create_group_cmd =
|
rlm@3
|
68 ("groupadd", "--force", $pkg_name);
|
rlm@3
|
69
|
rlm@3
|
70 @pkg_create_home_cmd =
|
rlm@7
|
71 ("install", "-d", "-g", $pkg_name, "-o", $pkg_name,
|
rlm@7
|
72 "-m", "700", $pkg_home);
|
rlm@7
|
73
|
rlm@7
|
74 @pkg_add_install_cmd =
|
rlm@7
|
75 ("gpasswd", "--add", $pkg_name, "install");
|
rlm@3
|
76
|
rlm@4
|
77 sub execute{print "\t@_\n"; system(@_) and die $!;}
|
rlm@3
|
78
|
rlm@7
|
79
|
rlm@7
|
80 @pkg_link_bashrc_cmd =
|
rlm@7
|
81 ("install", "-g", $pkg_name, "-o", $pkg_name,
|
rlm@8
|
82 "/pkg/defaults/.profile", "$pkg_home/.profile");
|
rlm@7
|
83
|
rlm@7
|
84 @pkg_copy_hg_wrapper_cmd =
|
rlm@8
|
85 (install, "-g", "hg-commit", "-o", "hg-commit",
|
rlm@8
|
86 "-m", "4711", "/pkg/defaults/.hg-wrapper", $pkg_home);
|
rlm@7
|
87
|
rlm@3
|
88 #determine if the user already exists:
|
rlm@3
|
89 `id $pkg_name 2>/dev/null`;
|
rlm@4
|
90 if (!$?){
|
rlm@4
|
91 print "Change user.\n";
|
rlm@4
|
92 execute(@pkg_switch_cmd); exit 0;}
|
rlm@3
|
93
|
rlm@3
|
94 print "Creating package group.\n";
|
rlm@3
|
95 execute(@pkg_create_group_cmd);
|
rlm@3
|
96
|
rlm@3
|
97 print "Creating package user.\n";
|
rlm@3
|
98 execute(@pkg_create_cmd);
|
rlm@3
|
99
|
rlm@3
|
100 print "Create home directory for $pkg_name.\n";
|
rlm@3
|
101 execute(@pkg_create_home_cmd);
|
rlm@3
|
102
|
rlm@7
|
103 print "Add $pkg_name to install group.\n";
|
rlm@7
|
104 execute(@pkg_add_install_cmd);
|
rlm@7
|
105
|
rlm@7
|
106 print "Copy Default Files.\n";
|
rlm@7
|
107 execute(@pkg_link_bashrc_cmd);
|
rlm@7
|
108 execute(@pkg_copy_hg_wrapper_cmd);
|
rlm@7
|
109
|
rlm@3
|
110 print "Change user.\n";
|
rlm@3
|
111 execute(@pkg_switch_cmd);
|
rlm@3
|
112
|
rlm@3
|
113
|
rlm@3
|
114
|
rlm@3
|
115
|
rlm@3
|
116
|
rlm@3
|
117
|
rlm@3
|
118
|
rlm@7
|
119
|