1+ <?php
2+
3+ /*
4+ Copyright (c) 2008, Till Brehm, projektfarm Gmbh
5+ All rights reserved.
6+
7+ Redistribution and use in source and binary forms, with or without modification,
8+ are permitted provided that the following conditions are met:
9+
10+ * Redistributions of source code must retain the above copyright notice,
11+ this list of conditions and the following disclaimer.
12+ * Redistributions in binary form must reproduce the above copyright notice,
13+ this list of conditions and the following disclaimer in the documentation
14+ and/or other materials provided with the distribution.
15+ * Neither the name of ISPConfig nor the names of its contributors
16+ may be used to endorse or promote products derived from this software without
17+ specific prior written permission.
18+
19+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
20+ ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21+ WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22+ IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
23+ INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
24+ BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25+ DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
26+ OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
27+ NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
28+ EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29+ */
30+
31+ class shelluser_jailkit_plugin {
32+
33+ //* $plugin_name and $class_name have to be the same then the name of this class
34+ var $ plugin_name = 'shelluser_jailkit_plugin ' ;
35+ var $ class_name = 'shelluser_jailkit_plugin ' ;
36+
37+
38+ /*
39+ This function is called when the plugin is loaded
40+ */
41+
42+ function onLoad () {
43+ global $ app ;
44+
45+ /*
46+ Register for the events
47+ */
48+
49+ $ app ->plugins ->registerEvent ('shell_user_insert ' ,$ this ->plugin_name ,'insert ' );
50+ $ app ->plugins ->registerEvent ('shell_user_update ' ,$ this ->plugin_name ,'update ' );
51+ $ app ->plugins ->registerEvent ('shell_user_delete ' ,$ this ->plugin_name ,'delete ' );
52+
53+
54+ }
55+
56+ //* This function is called, when a shell user is inserted in the database
57+ function insert ($ event_name ,$ data ) {
58+ global $ app , $ conf ;
59+
60+ /**
61+ * Setup Jailkit Chroot System If Enabled
62+ */
63+ if ($ data ['new ' ]['chroot ' ] == "jailkit " )
64+ {
65+ // load the server configuration options
66+ $ app ->uses ("getconf " );
67+ $ this ->data = $ data ;
68+ $ this ->app = $ app ;
69+ $ this ->jailkit_config = $ app ->getconf ->get_server_config ($ conf ["server_id " ], 'jailkit ' );
70+
71+ $ this ->_setup_jailkit_chroot ();
72+ $ this ->_add_jailkit_user ();
73+ }
74+
75+ $ app ->log ("Jalikit Plugin -> insert username: " .$ data ['new ' ]['username ' ],LOGLEVEL_DEBUG );
76+
77+ }
78+
79+ //* This function is called, when a shell user is updated in the database
80+ function update ($ event_name ,$ data ) {
81+ global $ app , $ conf ;
82+
83+ /**
84+ * Setup Jailkit Chroot System If Enabled
85+ */
86+ if ($ data ['new ' ]['chroot ' ] == "jailkit " )
87+ {
88+ // load the server configuration options
89+ $ app ->uses ("getconf " );
90+ $ this ->data = $ data ;
91+ $ this ->app = $ app ;
92+ $ this ->jailkit_config = $ app ->getconf ->get_server_config ($ conf ["server_id " ], 'jailkit ' );
93+
94+ $ this ->_setup_jailkit_chroot ();
95+ $ this ->_add_jailkit_user ();
96+ }
97+
98+ $ app ->log ("Jalikit Plugin -> update username: " .$ data ['new ' ]['username ' ],LOGLEVEL_DEBUG );
99+
100+ }
101+
102+ //* This function is called, when a shell user is deleted in the database
103+ function delete ($ event_name ,$ data ) {
104+ global $ app , $ conf ;
105+
106+ if ($ data ['old ' ]['chroot ' ] == "jailkit " )
107+ {
108+ $ app ->uses ("getconf " );
109+ $ this ->jailkit_config = $ app ->getconf ->get_server_config ($ conf ["server_id " ], 'jailkit ' );
110+
111+ $ jailkit_chroot_userhome = $ this ->_get_home_dir ($ data ['old ' ]['username ' ]);
112+
113+ exec ('rm -rf ' .$ data ['old ' ]['dir ' ].$ jailkit_chroot_userhome );
114+
115+
116+ $ app ->log ("Jalikit Plugin -> delete chroot home: " .$ data ['old ' ]['dir ' ].$ jailkit_chroot_userhome ,LOGLEVEL_DEBUG );
117+ }
118+
119+ $ app ->log ("Jalikit Plugin -> delete username: " .$ data ['old ' ]['username ' ],LOGLEVEL_DEBUG );
120+
121+
122+ }
123+
124+ function _setup_jailkit_chroot ()
125+ {
126+ //check if the chroot environment is created yet if not create it with a list of program sections from the config
127+ if (!is_dir ($ this ->data ['new ' ]['dir ' ].'/etc/jailkit ' ))
128+ {
129+ $ command = '/usr/local/ispconfig/server/scripts/create_jailkit_chroot.sh ' ;
130+ $ command .= ' ' .escapeshellcmd ($ this ->data ['new ' ]['dir ' ]);
131+ $ command .= ' \'' .$ this ->jailkit_config ['jailkit_chroot_app_sections ' ].'\'' ;
132+ exec ($ command );
133+
134+ $ this ->app ->log ("Added jailkit chroot with command: " .$ command ,LOGLEVEL_DEBUG );
135+
136+ $ this ->_add_jailkit_programs ();
137+
138+
139+ }
140+ }
141+
142+ function _add_jailkit_programs ()
143+ {
144+ //copy over further programs and its libraries
145+ $ command = '/usr/local/ispconfig/server/scripts/create_jailkit_programs.sh ' ;
146+ $ command .= ' ' .escapeshellcmd ($ this ->data ['new ' ]['dir ' ]);
147+ $ command .= ' \'' .$ this ->jailkit_config ['jailkit_chroot_app_programs ' ].'\'' ;
148+ exec ($ command );
149+
150+ $ this ->app ->log ("Added programs to jailkit chroot with command: " .$ command ,LOGLEVEL_DEBUG );
151+ }
152+
153+ function _get_home_dir ($ username )
154+ {
155+ return str_replace ("[username] " ,escapeshellcmd ($ username ),$ this ->jailkit_config ["jailkit_chroot_home " ]);
156+ }
157+
158+ function _add_jailkit_user ()
159+ {
160+ //add the user to the chroot
161+ $ jailkit_chroot_userhome = $ this ->_get_home_dir ($ this ->data ['new ' ]['username ' ]);
162+ $ jailkit_chroot_puserhome = $ this ->_get_home_dir ($ this ->data ['new ' ]['puser ' ]);
163+
164+ if (!is_dir ($ this ->data ['new ' ]['dir ' ].$ jailkit_chroot_userhome ))
165+ {
166+ $ command = '/usr/local/ispconfig/server/scripts/create_jailkit_user.sh ' ;
167+ $ command .= ' ' .escapeshellcmd ($ this ->data ['new ' ]['username ' ]);
168+ $ command .= ' ' .escapeshellcmd ($ this ->data ['new ' ]['dir ' ]);
169+ $ command .= ' ' .$ jailkit_chroot_userhome ;
170+ $ command .= ' ' .escapeshellcmd ($ this ->data ['new ' ]['shell ' ]);
171+ $ command .= ' ' .$ this ->data ['new ' ]['puser ' ];
172+ $ command .= ' ' .$ jailkit_chroot_puserhome ;
173+ exec ($ command );
174+
175+ $ this ->app ->log ("Added jailkit user to chroot with command: " .$ command ,LOGLEVEL_DEBUG );
176+
177+ exec ("mkdir -p " .$ this ->data ['new ' ]['dir ' ].$ jailkit_chroot_userhome );
178+ exec ("chown " .$ this ->data ['new ' ]['username ' ].": " .$ this ->data ['new ' ]['pgroup ' ]." " .$ this ->data ['new ' ]['dir ' ].$ jailkit_chroot_userhome );
179+
180+ $ this ->app ->log ("Added created jailkit user home in : " .$ this ->data ['new ' ]['dir ' ].$ jailkit_chroot_userhome ,LOGLEVEL_DEBUG );
181+
182+ exec ("mkdir -p " .$ this ->data ['new ' ]['dir ' ].$ jailkit_chroot_puserhome );
183+ exec ("chown " .$ this ->data ['new ' ]['puser ' ].": " .$ this ->data ['new ' ]['pgroup ' ]." " .$ this ->data ['new ' ]['dir ' ].$ jailkit_chroot_puserhome );
184+
185+ $ this ->app ->log ("Added created jailkit parent user home in : " .$ this ->data ['new ' ]['dir ' ].$ jailkit_chroot_puserhome ,LOGLEVEL_DEBUG );
186+ }
187+ }
188+
189+
190+
191+ } // end class
192+
193+ ?>
0 commit comments