This file is indexed.

/usr/share/cherokee/admin/Cherokee.py is in cherokee-admin 1.2.101-1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
# -*- coding: utf-8 -*-
#
# Cherokee-admin
#
# Authors:
#      Alvaro Lopez Ortega <alvaro@alobbs.com>
#
# Copyright (C) 2001-2011 Alvaro Lopez Ortega
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of version 2 of the GNU General Public
# License as published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
# 02110-1301, USA.
#

import CTK

# Python
import os
import time
import errno
import fcntl
import signal

from subprocess import *
from select import select

# Cheroke-admin
import popen
from util import *
from consts import *
from configured import *
from config_version import *


DEFAULT_PATH = ['/usr/local/sbin', '/usr/local/bin', '/opt/local/sbin', '/opt/local/sbin', '/usr/sbin', '/usr/bin', '/sbin', '/bin']

POLLING_LAPSE   = 1
READ_CHUNK_SIZE = 2**20


class PID:
    def __init__ (self):
        self.pid       = None
        self._pid_file = None

        # Initialize
        self.refresh()

    def refresh (self):
        # It might be alive
        if self.pid and _pid_is_alive (self.pid):
            return

        self.pid = None

        # Need a PID
        self._read_pid_file()
        if not self.pid:
            self._figure_pid()

    def _read_pid_file (self):
        # Check the configuration
        self._pid_file = CTK.cfg.get_val("server!pid_file")
        if not self._pid_file:
            return

        # Read the file
        try:
            f = open(self._pid_file, 'r')
        except: return

        self.pid = int(f.readline())

        try: f.close()
        except: pass

    def _figure_pid (self):
        # Execture ps
        ret = popen.popen_sync ("ps aux")
        ps  = ret['stdout']

        # Try to find the Cherokee process
        for l in ps.split("\n"):
            if "cherokee " in l and "-C %s"%(CTK.cfg.file) in l:
                pid = filter (lambda x: x.isdigit(), l.split())[0]
                self.pid = int(pid)


class Server:
    def is_alive (self):
        return _pid_is_alive (pid.pid)

    def stop (self):
        if not pid.pid: return

        # Kill the process
        return _pid_kill(pid.pid)

    def restart (self, graceful=True):
        if not pid.pid: return

        try:
            os.kill (pid.pid, (signal.SIGUSR1,signal.SIGHUP)[graceful])
        except:
            pass

    def launch (self):
        def daemonize():
            os.setsid()

        def set_non_blocking (fd):
            fl = fcntl.fcntl (fd, fcntl.F_GETFL)
            fcntl.fcntl (fd, fcntl.F_SETFL, fl | os.O_NONBLOCK)

        # Ensure the a minimum $PATH is set
        environ = os.environ.copy()
        if not "PATH" in environ:
            environ["PATH"] = ':'.join(DEFAULT_PATH)

        # Launch the process
        p = Popen ([CHEROKEE_SERVER, '--admin_child', '-C', CTK.cfg.file],
                   stdout=PIPE, stderr=PIPE, env=environ,
                   preexec_fn=daemonize, close_fds=True)

        stdout_f,  stderr_f  = (p.stdout, p.stderr)
        stdout_fd, stderr_fd = stdout_f.fileno(), stderr_f.fileno()

        stdout = ''
        stderr = ''

        set_non_blocking (stdout_fd)
        set_non_blocking (stderr_fd)

        # Check the first few lines of the output
        while True:
            r,w,e = select([stdout_fd, stderr_fd], [], [stdout_fd, stderr_fd], POLLING_LAPSE)

            # Error
            if e:
                return _("Could not access file descriptors: ") + str(e)

            # stdout
            if stdout_fd in r:
                data = ''
                try:
                    data = os.read (stdout_fd, READ_CHUNK_SIZE)
                except OSError, e:
                    if e[0] in (errno.EAGAIN,):
                        raise
                if data:
                    stdout += data

            # stderr
            if stderr_fd in r:
                data = ''
                try:
                    data = os.read (stderr_fd, READ_CHUNK_SIZE)
                except OSError, e:
                    if e[0] in (errno.EAGAIN,):
                        raise
                if data:
                    stderr += data


            nl = stderr.find('\n')
            if nl != -1:
                for e in ["{'type': \"error\"",
                          "{'type': \"critical\"", '(error) ', '(critical) ', 'ERROR']:
                    if e in stderr:
                        _pid_kill (p.pid)
                        return stderr
                stderr = stderr[nl+1:]

            if stdout.count('\n') > 1:
                break

        return None


class Admin:
    def halt (self):
        CTK.stop()


class Support:
    def __init__ (self):
        # Get server info
        try:
            ret = popen.popen_sync ("%s -i" %(CHEROKEE_WORKER))
            self._server_info = ret['stdout']
        except:
            self._server_info = ''

    def get_info_section (self, filter):
        filter_string = " %s: " % (filter)

        for line in self._server_info.split("\n"):
            if line.startswith (filter_string):
                line = line.replace (filter_string, "")
                return line.split(" ")
        return []

    def has_plugin (self, name):
        try:
            mods = filter(lambda x: name in x, os.listdir(CHEROKEE_PLUGINDIR))
            if len(mods) >= 1:
                return True
        except:
            pass

        return name in self.get_info_section("Built-in")

    def has_polling_method (self, name):
        return name in self.get_info_section("Polling methods")

    def filter_polling_methods (self, methods_list):
        polling_methods = []

        for name, desc in methods_list:
            if not name or self.has_polling_method(name):
                polling_methods.append((name, desc))

        return polling_methods

    def filter_available (self, module_list):
        new_module_list = []

        for entry in module_list:
            assert (type(entry) == tuple)
            assert (len(entry) == 2)
            plugin, name = entry

            if (not len(plugin) or
                self.has_plugin (plugin)):
                new_module_list.append(entry)

        return new_module_list


#
# Globals
#
pid     = PID()
server  = Server()
support = Support()
admin   = Admin()


#
# Helper functions
#
def _pid_is_alive (pid):
    if not pid:
        return False

    # It might be a zombie process already
    try:
        os.waitpid (pid, os.WNOHANG)
    except OSError:
        pass

    # Is it alive?
    try:
        os.kill (pid, 0)
    except OSError:
        return False

    return True

def _pid_kill (pid):
    # Kill it
    try:
        os.kill (pid, signal.SIGTERM)
    except:
        return not _pid_is_alive(pid)

    # Ensure it died
    retries = 6
    while retries:
        try:
            os.waitpid (pid, 0)
            return True
        except OSError, e:
            if e[0] == errno.ECHILD:
                return True
            time.sleep (0.5)
            retries -= 1

    # Did not succeed
    return False