Beispiel #1
0
    def testWindowsChown(self):
        basefile = os.path.join(self.basedir, "foo")
        f = open(basefile, "w")
        f.write("foo")
        f.close
        OSUtils.chown(self.basedir, username="******")

        null = open(OSUtils.null_file(), 'w')
        tfile = tempfile.TemporaryFile()
        popen = subprocess.Popen(["icacls", self.basedir],
                                 stdout=tfile,
                                 stderr=null)
        popen.wait()
        tfile.seek(0)
        c = tfile.read()
        self.assertNotEqual(1, len(re.findall(".*mmorsi.*(F).*\n.*", c)))
        tfile.close()

        tfile = tempfile.TemporaryFile()
        popen = subprocess.Popen(["icacls", basefile],
                                 stdout=tfile,
                                 stderr=null)
        popen.wait()
        tfile.seek(0)
        c = tfile.read()
        self.assertNotEqual(1, len(re.findall(".*mmorsi.*(F).*\n.*", c)))
        tfile.close()
Beispiel #2
0
 def testLinuxChown(self):
     basefile = os.path.join(self.basedir, "foo")
     f = open(basefile, "w")
     f.write("foo")
     f.close
     OSUtils.chown(self.basedir, uid=100, gid=100)
     
     st = os.stat(self.basedir)
     self.assertEqual(100, st.st_uid)
     self.assertEqual(100, st.st_gid)
     st = os.stat(basefile)
     self.assertEqual(100, st.st_uid)
     self.assertEqual(100, st.st_gid)
     
     import pwd
     pwo = pwd.getpwnam("nobody")
     uid = pwo.pw_uid
     gid = pwo.pw_gid
     OSUtils.chown(self.basedir, username="******")
     st = os.stat(self.basedir)
     self.assertEqual(uid, st.st_uid)
     self.assertEqual(gid, st.st_gid)
     st = os.stat(basefile)
     self.assertEqual(uid, st.st_uid)
     self.assertEqual(gid, st.st_gid)
Beispiel #3
0
    def testLinuxChown(self):
        basefile = os.path.join(self.basedir, "foo")
        f = open(basefile, "w")
        f.write("foo")
        f.close
        OSUtils.chown(self.basedir, uid=100, gid=100)

        st = os.stat(self.basedir)
        self.assertEqual(100, st.st_uid)
        self.assertEqual(100, st.st_gid)
        st = os.stat(basefile)
        self.assertEqual(100, st.st_uid)
        self.assertEqual(100, st.st_gid)

        import pwd
        pwo = pwd.getpwnam("nobody")
        uid = pwo.pw_uid
        gid = pwo.pw_gid
        OSUtils.chown(self.basedir, username="******")
        st = os.stat(self.basedir)
        self.assertEqual(uid, st.st_uid)
        self.assertEqual(gid, st.st_gid)
        st = os.stat(basefile)
        self.assertEqual(uid, st.st_uid)
        self.assertEqual(gid, st.st_gid)
Beispiel #4
0
 def testLinuxIsSuperUser(self):
     ouid = os.geteuid()
     self.assertTrue(OSUtils.is_superuser())
     
     os.seteuid(100)
     self.assertFalse(OSUtils.is_superuser())
     
     os.seteuid(ouid)
Beispiel #5
0
    def testLinuxIsSuperUser(self):
        ouid = os.geteuid()
        self.assertTrue(OSUtils.is_superuser())

        os.seteuid(100)
        self.assertFalse(OSUtils.is_superuser())

        os.seteuid(ouid)
Beispiel #6
0
    def restore(self, basedir):
        dispatcher = Dispatcher.os_dispatcher()
        null = open(OSUtils.null_file(), 'w')

        if OS.is_linux():
            Postgresql.set_root_pass()

        # init the postgresql db
        Postgresql.init_db()

        # start the postgresql service
        dispatcher.start_service(Postgresql.DAEMON)

        # get env containing the postgresql password
        penv = Postgresql.set_pgpassword_env()

        # use pipe to invoke postgres, restoring database
        infile = file(basedir + "/dump.psql", "r")
        popen = subprocess.Popen(
            [Postgresql.PSQL_CMD, "--username", "postgres"],
            env=penv,
            stdin=infile,
            stdout=null,
            stderr=null)
        popen.wait()
Beispiel #7
0
    def backup(self, basedir):
        dispatcher = Dispatcher.os_dispatcher()
        null = open(OSUtils.null_file(), 'w')

        if OS.is_linux():
            Postgresql.set_root_pass()

        # check to see if service is running
        already_running = dispatcher.service_running(Postgresql.DAEMON)

        # start the postgresql server
        dispatcher.start_service(Postgresql.DAEMON)

        # get env containing postgres password
        penv = Postgresql.set_pgpassword_env()

        outfile = file(basedir + "/dump.psql", "w")
        pipe = subprocess.Popen(
            [Postgresql.PGDUMPALL_CMD, "--username", "postgres"],
            env=penv,
            stdout=outfile,
            stderr=null)
        pipe.wait()

        # if postgresql was running b4hand, start up again
        if not already_running:
            dispatcher.stop_service(Postgresql.DAEMON)
Beispiel #8
0
    def set_root_pass():
        '''helper to set the mysql root password'''
        null = open(OSUtils.null_file(), 'w')
        dispatcher = Dispatcher.os_dispatcher()

        mysql_password = snap.config.options.service_options['mysql_password']

        already_running = dispatcher.service_running(Mysql.DAEMON)
        if already_running:
            dispatcher.stop_service(Mysql.DAEMON)

        server = subprocess.Popen(
            [Mysql.MYSQLDSAFE_CMD, '--skip-grant-tables'],
            stdout=null,
            stderr=null)
        client = subprocess.Popen([
            Mysql.MYSQL_CMD, 'mysql', '-u', 'root', '-e',
            "update user set password=PASSWORD('" + mysql_password +
            "') where user='******'; flush privileges;"
        ],
                                  stdout=null,
                                  stderr=null)
        client.wait()
        client = subprocess.Popen([Mysql.MYSQLADMIN_CMD, 'shutdown'],
                                  stdout=null,
                                  stderr=null)
        client.wait()
        if server.poll() == None:  # race condition?
            server.kill()

        if already_running:
            dispatcher.start_service(Mysql.DAEMON)
Beispiel #9
0
    def backup(self, basedir):
        dispatcher = Dispatcher.os_dispatcher()
        null = open(OSUtils.null_file(), 'w')

        if OS.is_linux():
            Mysql.set_root_pass()

        mysql_password = snap.config.options.service_options['mysql_password']

        # check to see if service is running
        already_running = dispatcher.service_running(Mysql.DAEMON)

        # start the mysql server
        dispatcher.start_service(Mysql.DAEMON)

        # use a pipe to invoke mysqldump and capture output
        outfile = file(basedir + "/dump.mysql", "w")
        popen = subprocess.Popen([
            Mysql.MYSQLDUMP_CMD, "-u", "root", "-p" + mysql_password,
            "--all-databases"
        ],
                                 stdout=outfile,
                                 stderr=null)
        popen.wait()

        # if mysql was stopped b4hand, start up again
        if not already_running:
            dispatcher.stop_service(Mysql.DAEMON)
Beispiel #10
0
 def service_running(service):
     '''helper to return boolean indicating if the specified service is running'''
     out = open(OSUtils.null_file(), 'w')
     popen = subprocess.Popen(["service", service, "status"],
                              stdout=out,
                              stderr=out)
     popen.wait()
     return popen.returncode == 0
Beispiel #11
0
 def stop_service(service):
     '''helper to stop the specified service
     
     @returns boolean indicating if service was stopped or not'''
     out = open(OSUtils.null_file(), 'w')
     popen = subprocess.Popen(["service", service, "stop"], stdout=out, stderr=out)
     popen.wait()
     return popen.returncode == 0
Beispiel #12
0
    def check_permission(self):
        '''
        ensure current user has permissions to run Snap!

        @raises InsufficientPermissionError - if an error occurs when backing up the files
        '''
        if not OSUtils.is_superuser():
            raise InsufficientPermissionError("Must be root to run this program")
Beispiel #13
0
    def flush_privileges():
        '''helper to flush database privileges'''
        null = open(OSUtils.null_file(), 'w')
        
        mysql_password = snap.config.options.service_options['mysql_password']

        popen = subprocess.Popen([Mysql.MYSQL_CMD, "-p" + mysql_password, "-e", "flush privileges;"],
                                 stdout=null, stderr=null)
        popen.wait()
Beispiel #14
0
    def check_permission(self):
        '''
        ensure current user has permissions to run Snap!

        @raises InsufficientPermissionError - if an error occurs when backing up the files
        '''
        if not OSUtils.is_superuser():
            raise InsufficientPermissionError(
                "Must be root to run this program")
Beispiel #15
0
 def stop_service(service):
     '''helper to stop the specified service
     
     @returns boolean indicating if service was stopped or not'''
     out = open(OSUtils.null_file(), 'w')
     popen = subprocess.Popen(["sc", "stop", service], stdout=out, stderr=out)
     popen.wait()
     time.sleep(WindowsDispatcher.SERVICE_QUERY_DELAY)
     return not WindowsDispatcher.service_running(service)
Beispiel #16
0
    def drop_db(dbname):
        '''helper to drop the specified database'''
        null = open(OSUtils.null_file(), 'w')

        mysql_password = snap.config.options.service_options['mysql_password']

        # destroy the db
        popen = subprocess.Popen([Mysql.MYSQLADMIN_CMD, "-u", "root", "-p" + mysql_password, "-f", "drop", dbname],
                                 stdout=null, stderr=null)
        popen.wait()
Beispiel #17
0
 def stop_service(service):
     '''helper to stop the specified service
     
     @returns boolean indicating if service was stopped or not'''
     out = open(OSUtils.null_file(), 'w')
     popen = subprocess.Popen(["service", service, "stop"],
                              stdout=out,
                              stderr=out)
     popen.wait()
     return popen.returncode == 0
Beispiel #18
0
 def get_features(matching_pattern=".*"):
     '''helper to return list of features, optionally matching specified pattern
     
     @param matching_pattern - pattern to match features against'''
     out = open(OSUtils.null_file(), 'w')
     features = []
     c = FileManager.capture_output(["dism", "/online", "/Get-features"])
     
     for f in re.findall(matching_pattern, c):
         features.append(f)
     return features
Beispiel #19
0
    def drop_db(dbname):
        '''helper to drop the specified database'''
        null = open(OSUtils.null_file(), 'w')

        # get env containing the postgres password
        penv = Postgresql.set_pgpassword_env()

        # destroy the db
        popen = subprocess.Popen([Postgresql.PSQL_CMD, "--username", "postgres", "-c", "DROP DATABASE " + dbname],
                                 env=penv, stdout=null, stderr=null)
        popen.wait()
Beispiel #20
0
    def init_db():
        '''helper to initialize the database server'''

        # db already initialized, just return
        if os.path.isdir(Postgresql.DATADIR) and len(os.listdir(Postgresql.DATADIR)) > 0:
            return

        null = open(OSUtils.null_file(), 'w')

        # FIXME should run initdb manually
        popen = subprocess.Popen(["service", "postgresql", "initdb"], stdout=null)
        popen.wait()
Beispiel #21
0
    def flush_privileges():
        '''helper to flush database privileges'''
        null = open(OSUtils.null_file(), 'w')

        mysql_password = snap.config.options.service_options['mysql_password']

        popen = subprocess.Popen([
            Mysql.MYSQL_CMD, "-p" + mysql_password, "-e", "flush privileges;"
        ],
                                 stdout=null,
                                 stderr=null)
        popen.wait()
Beispiel #22
0
 def service_running(service):
     '''helper to return boolean indicating if the specified service is running'''
     for i in range(WindowsDispatcher.SERVICE_QUERY_TRIES):
         out = open(OSUtils.null_file(), 'w')
         popen = subprocess.Popen(["sc", "query", service], stdout=subprocess.PIPE, stderr=out)
         popen.wait()
         output = popen.stdout.read()
         if re.search(".*STATE.*PENDING", output):
             time.sleep(WindowsDispatcher.SERVICE_QUERY_DELAY)
             break
     if re.search(".*STATE.*RUNNING.*", output):
         return True
     return False
Beispiel #23
0
    def testWindowsChown(self):
        basefile = os.path.join(self.basedir, "foo")
        f = open(basefile, "w")
        f.write("foo")
        f.close
        OSUtils.chown(self.basedir, username="******")

        null = open(OSUtils.null_file(), 'w')
        tfile = tempfile.TemporaryFile()
        popen = subprocess.Popen(["icacls", self.basedir], stdout=tfile, stderr=null)
        popen.wait()
        tfile.seek(0)
        c = tfile.read()
        self.assertNotEqual(1, len(re.findall(".*mmorsi.*(F).*\n.*", c))) 
        tfile.close()

        tfile = tempfile.TemporaryFile()
        popen = subprocess.Popen(["icacls", basefile], stdout=tfile, stderr=null)
        popen.wait()
        tfile.seek(0)
        c = tfile.read()
        self.assertNotEqual(1, len(re.findall(".*mmorsi.*(F).*\n.*", c)))
        tfile.close()
Beispiel #24
0
    def init_db():
        '''helper to initialize the database server'''

        # db already initialized, just return
        if os.path.isdir(Postgresql.DATADIR) and len(
                os.listdir(Postgresql.DATADIR)) > 0:
            return

        null = open(OSUtils.null_file(), 'w')

        # FIXME should run initdb manually
        popen = subprocess.Popen(["service", "postgresql", "initdb"],
                                 stdout=null)
        popen.wait()
Beispiel #25
0
    def create_db(dbname):
        '''helper to create the specified database'''
        null = open(OSUtils.null_file(), 'w')

        mysql_password = snap.config.options.service_options['mysql_password']

        # create the db
        popen = subprocess.Popen([
            Mysql.MYSQLADMIN_CMD, "-u", "root", "-p" + mysql_password,
            "create", dbname
        ],
                                 stdout=null,
                                 stderr=null)
        popen.wait()
Beispiel #26
0
    def create_db(dbname):
        '''helper to create the specified database'''
        null = open(OSUtils.null_file(), 'w')

        # get env containing the postgres password
        penv = Postgresql.set_pgpassword_env()

        # create the db
        popen = subprocess.Popen([
            Postgresql.PSQL_CMD, "--username", "postgres", "-c",
            "CREATE DATABASE " + dbname
        ],
                                 env=penv,
                                 stdout=null,
                                 stderr=null)
        popen.wait()
Beispiel #27
0
    def restore(self, basedir):
        dispatcher = Dispatcher.os_dispatcher()
        null = open(OSUtils.null_file(), 'w')

        if OS.is_linux():
            Mysql.set_root_pass()

        mysql_password = snap.config.options.service_options['mysql_password']
        
        # start the mysql server
        dispatcher.start_service(Mysql.DAEMON)

        # use pipe to invoke mysql, restoring database
        infile = file(basedir + "/dump.mysql", "r")
        popen = subprocess.Popen([Mysql.MYSQL_CMD, "-u", "root", "-p" + mysql_password],
                                 stdin=infile, stdout=null, stderr=null)
        popen.wait()

        # flush privileges incase any roles were restored and whatnot
        Mysql.flush_privileges()
Beispiel #28
0
    def restore(self, basedir):
        dispatcher = Dispatcher.os_dispatcher()
        null = open(OSUtils.null_file(), 'w')
        
        if OS.is_linux():
            Postgresql.set_root_pass()

        # init the postgresql db
        Postgresql.init_db()

        # start the postgresql service
        dispatcher.start_service(Postgresql.DAEMON)

        # get env containing the postgresql password
        penv = Postgresql.set_pgpassword_env()

        # use pipe to invoke postgres, restoring database
        infile = file(basedir + "/dump.psql", "r")
        popen = subprocess.Popen([Postgresql.PSQL_CMD, "--username", "postgres"],
                                 env=penv, stdin=infile, stdout=null, stderr=null)
        popen.wait()
Beispiel #29
0
    def restore(self, basedir):
        out = open(OSUtils.null_file(), 'w')
        record_file = os.path.join(basedir, "service-iis.xml")
        
        # if files record file isn't found, simply return
        if not os.path.isfile(record_file):
            return

        # stop the httpd service if already running
        if WindowsDispatcher.is_feature_enabled(Iis.WEBSERVER_FEATURE):
            WindowsDispatcher.disable_feature(Iis.WEBSERVER_FEATURE)

        # read files from the record file
        record = FilesRecordFile(record_file)
        sfiles = record.read()

        # restore those to their original locations
        for sfile in sfiles:
            try:
                sfile.copy_to(path_prefix=basedir)
            except IOError, e:
                pass # just silently ignore errors for now
Beispiel #30
0
    def set_root_pass():
        '''helper to set the mysql root password'''
        null = open(OSUtils.null_file(), 'w')
        dispatcher = Dispatcher.os_dispatcher()
        
        mysql_password = snap.config.options.service_options['mysql_password']
        
        already_running = dispatcher.service_running(Mysql.DAEMON)
        if already_running:
            dispatcher.stop_service(Mysql.DAEMON)

        server = subprocess.Popen([Mysql.MYSQLDSAFE_CMD, '--skip-grant-tables'], stdout=null, stderr=null)
        client = subprocess.Popen([Mysql.MYSQL_CMD, 'mysql', '-u', 'root', '-e', "update user set password=PASSWORD('" + mysql_password + "') where user='******'; flush privileges;"],
                                  stdout=null, stderr=null)
        client.wait()
        client = subprocess.Popen([Mysql.MYSQLADMIN_CMD, 'shutdown'], stdout=null, stderr=null)
        client.wait()
        if server.poll() == None: # race condition?
            server.kill()

        if already_running:
            dispatcher.start_service(Mysql.DAEMON)
Beispiel #31
0
    def restore(self, basedir):
        out = open(OSUtils.null_file(), 'w')
        record_file = os.path.join(basedir, "service-iis.xml")

        # if files record file isn't found, simply return
        if not os.path.isfile(record_file):
            return

        # stop the httpd service if already running
        if WindowsDispatcher.is_feature_enabled(Iis.WEBSERVER_FEATURE):
            WindowsDispatcher.disable_feature(Iis.WEBSERVER_FEATURE)

        # read files from the record file
        record = FilesRecordFile(record_file)
        sfiles = record.read()

        # restore those to their original locations
        for sfile in sfiles:
            try:
                sfile.copy_to(path_prefix=basedir)
            except IOError, e:
                pass  # just silently ignore errors for now
Beispiel #32
0
    def restore(self, basedir):
        dispatcher = Dispatcher.os_dispatcher()
        null = open(OSUtils.null_file(), 'w')

        if OS.is_linux():
            Mysql.set_root_pass()

        mysql_password = snap.config.options.service_options['mysql_password']

        # start the mysql server
        dispatcher.start_service(Mysql.DAEMON)

        # use pipe to invoke mysql, restoring database
        infile = file(basedir + "/dump.mysql", "r")
        popen = subprocess.Popen(
            [Mysql.MYSQL_CMD, "-u", "root", "-p" + mysql_password],
            stdin=infile,
            stdout=null,
            stderr=null)
        popen.wait()

        # flush privileges incase any roles were restored and whatnot
        Mysql.flush_privileges()
Beispiel #33
0
    def backup(self, basedir):
        dispatcher = Dispatcher.os_dispatcher()
        null = open(OSUtils.null_file(), 'w')

        if OS.is_linux():
            Mysql.set_root_pass()

        mysql_password = snap.config.options.service_options['mysql_password']

        # check to see if service is running
        already_running = dispatcher.service_running(Mysql.DAEMON) 

        # start the mysql server
        dispatcher.start_service(Mysql.DAEMON)

        # use a pipe to invoke mysqldump and capture output
        outfile = file(basedir + "/dump.mysql", "w")
        popen = subprocess.Popen([Mysql.MYSQLDUMP_CMD, "-u", "root", "-p" + mysql_password, "--all-databases"],
                                 stdout=outfile, stderr=null)
        popen.wait()

        # if mysql was stopped b4hand, start up again
        if not already_running:
            dispatcher.stop_service(Mysql.DAEMON)
Beispiel #34
0
    def backup(self, basedir):
        dispatcher = Dispatcher.os_dispatcher()
        null = open(OSUtils.null_file(), 'w')
                
        if OS.is_linux():
            Postgresql.set_root_pass()
        
        # check to see if service is running
        already_running = dispatcher.service_running(Postgresql.DAEMON)

        # start the postgresql server
        dispatcher.start_service(Postgresql.DAEMON)

        # get env containing postgres password
        penv = Postgresql.set_pgpassword_env()

        outfile = file(basedir + "/dump.psql", "w")
        pipe = subprocess.Popen([Postgresql.PGDUMPALL_CMD, "--username", "postgres"],
                                env=penv, stdout=outfile, stderr=null)
        pipe.wait()

        # if postgresql was running b4hand, start up again
        if not already_running:
            dispatcher.stop_service(Postgresql.DAEMON)
Beispiel #35
0
 def testLinuxNullFile(self):
     self.assertEqual("/dev/null", OSUtils.null_file())
Beispiel #36
0
 def testLinuxNullFile(self):
     self.assertEqual("/dev/null", OSUtils.null_file())
Beispiel #37
0
 def testWindowsNullFile(self):
     self.assertEqual("nul", OSUtils.null_file())
Beispiel #38
0
 def is_feature_enabled(feature):
     '''helper to return true/false pertaining to whether or not feature is enabled'''
     out = open(OSUtils.null_file(), 'w')
     c = FileManager.capture_output(['dism', '/online', '/Get-FeatureInfo', '/featurename:' + feature])
     m = re.search('State\s*:\s*([^\s]*)\s*', c)
     return m != None and m.group(1) == "Enabled"
Beispiel #39
0
 def enable_feature(feature):
     '''helper to enable the specified feature'''
     out = open(OSUtils.null_file(), 'w')
     popen = subprocess.Popen(["dism", "/online", "/enable-feature", "/featurename:" + feature, "/norestart"],
                              stdout=out, stderr=out)
     popen.wait()
Beispiel #40
0
 def testWindowsNullFile(self):
     self.assertEqual("nul", OSUtils.null_file())
Beispiel #41
0
 def service_running(service):
     '''helper to return boolean indicating if the specified service is running'''
     out = open(OSUtils.null_file(), 'w')
     popen = subprocess.Popen(["service", service, "status"], stdout=out, stderr=out)
     popen.wait()
     return popen.returncode == 0