diff --git a/kippo/commands/base.py b/kippo/commands/base.py index 0393817..47b9635 100644 --- a/kippo/commands/base.py +++ b/kippo/commands/base.py @@ -1,7 +1,8 @@ # Copyright (c) 2009 Upi Tamminen # See the COPYRIGHT file for more information -import os, time, anydbm, datetime +import os, time, anydbm, datetime, getopt +from copy import deepcopy, copy from kippo.core.honeypot import HoneyPotCommand from kippo.core.fs import * from twisted.internet import reactor @@ -77,6 +78,140 @@ class command_rm(HoneyPotCommand): dir.remove(i) commands['/bin/rm'] = command_rm +class command_cp(HoneyPotCommand): + def call(self): + if not len(self.args): + self.writeln("cp: missing file operand") + self.writeln("Try `cp --help' for more information.") + return + try: + optlist, args = getopt.gnu_getopt(self.args, + '-abdfiHlLPpRrsStTuvx') + except getopt.GetoptError, err: + self.writeln('Unrecognized option') + return + recursive = False + for opt in optlist: + if opt[0] in ('-r', '-a', '-R'): + recursive = True + + def resolv(path): + return self.fs.resolve_path(path, self.honeypot.cwd) + + if len(args) < 2: + self.writeln("cp: missing destination file operand after `%s'" % \ + (self.args[0],)) + self.writeln("Try `cp --help' for more information.") + return + sources, dest = args[:-1], args[-1] + if len(sources) > 1 and not self.fs.is_dir(resolv(dest)): + self.writeln("cp: target `%s' is not a directory" % (dest,)) + return + + if dest[-1] == '/' and not self.fs.exists(resolv(dest)) and \ + not recursive: + self.writeln( + "cp: cannot create regular file `%s': Is a directory" % \ + (dest,)) + return + + if self.fs.is_dir(resolv(dest)): + is_dir = True + else: + is_dir = False + parent = os.path.dirname(resolv(dest)) + if not self.fs.exists(parent): + self.writeln("cp: cannot create regular file " + \ + "`%s': No such file or directory" % (dest,)) + return + + for src in sources: + if not self.fs.exists(resolv(src)): + self.writeln( + "cp: cannot stat `%s': No such file or directory" % (src,)) + continue + if not recursive and self.fs.is_dir(resolv(src)): + self.writeln("cp: omitting directory `%s'" % (src,)) + continue + s = deepcopy(self.fs.getfile(resolv(src))) + if is_dir: + dir = self.fs.get_path(resolv(dest)) + outfile = os.path.basename(src) + else: + dir = self.fs.get_path(os.path.dirname(resolv(dest))) + outfile = os.path.basename(dest.rstrip('/')) + if outfile in [x[A_NAME] for x in dir]: + dir.remove([x for x in dir if x[A_NAME] == outfile][0]) + s[A_NAME] = outfile + dir.append(s) +commands['/bin/cp'] = command_cp + +class command_mv(HoneyPotCommand): + def call(self): + if not len(self.args): + self.writeln("mv: missing file operand") + self.writeln("Try `mv --help' for more information.") + return + + try: + optlist, args = getopt.gnu_getopt(self.args, '-bfiStTuv') + except getopt.GetoptError, err: + self.writeln('Unrecognized option') + self.exit() + + def resolv(path): + return self.fs.resolve_path(path, self.honeypot.cwd) + + if len(args) < 2: + self.writeln("mv: missing destination file operand after `%s'" % \ + (self.args[0],)) + self.writeln("Try `mv --help' for more information.") + return + sources, dest = args[:-1], args[-1] + if len(sources) > 1 and not self.fs.is_dir(resolv(dest)): + self.writeln("mv: target `%s' is not a directory" % (dest,)) + return + + if dest[-1] == '/' and not self.fs.exists(resolv(dest)) and \ + len(sources) != 1: + self.writeln( + "mv: cannot create regular file `%s': Is a directory" % \ + (dest,)) + return + + if self.fs.is_dir(resolv(dest)): + is_dir = True + else: + is_dir = False + parent = os.path.dirname(resolv(dest)) + if not self.fs.exists(parent): + self.writeln("mv: cannot create regular file " + \ + "`%s': No such file or directory" % \ + (dest,)) + return + + for src in sources: + if not self.fs.exists(resolv(src)): + self.writeln( + "mv: cannot stat `%s': No such file or directory" % \ + (src,)) + continue + s = self.fs.getfile(resolv(src)) + if is_dir: + dir = self.fs.get_path(resolv(dest)) + outfile = os.path.basename(src) + else: + dir = self.fs.get_path(os.path.dirname(resolv(dest))) + outfile = os.path.basename(dest) + if dir != os.path.dirname(resolv(src)): + s[A_NAME] = outfile + dir.append(s) + sdir = self.fs.get_path(os.path.dirname(resolv(src))) + sdir.remove(s) + else: + s[A_NAME] = outfile +commands['/bin/mv'] = command_mv + class command_mkdir(HoneyPotCommand): def call(self): for f in self.args: