def exec_pipe(args): if not sys.stdin.isatty(): fd = fcntl.open(args.path, openfile.O_WRONLY) if fd == -1: fd = fcntl.creat( args.path, inodetable.S_IWGRP | inodetable.S_IRGRP | inodetable.S_IWUSR | inodetable.S_IRUSR | inodetable.S_IROTH) if fd == -1: print("Can't open or create file.") return for l in sys.stdin: buf = l.encode(encoding='utf-8') fcntl.write(fd, buf, len(buf)) fcntl.close(fd) fd = fcntl.open(args.path, openfile.O_RDONLY) if fd == -1: print("Can't open file.") return filesize = sys_stat.stat(args.path)[4] if filesize > 0: buf = fcntl.read(fd, filesize) print(buf.decode(encoding='utf-8')) fcntl.close(fd)
def useradd_exec(args): passwd_fd = fcntl.open('/etc/passwd', openfile.O_RDWR) group_fd = fcntl.open('/etc/group', openfile.O_RDWR) if passwd_fd == -1: print('Only root can add users.') return print('[useradd] password for ' + args.username + ':') upass = getpass('Enter new ' + args.username + ' password:'******'Retype new ' + args.username + ' password:'******'You typed password incorrectly. Type again:') upass = getpass('Enter new ' + args.username + ' password:'******'Retype new ' + args.username + ' password:'******'/etc/passwd')[4] f = fcntl.read(passwd_fd, filesize).decode(encoding='ASCII') users = tuple(tuple(l.split(':')) for l in f.splitlines() if l[0] != '#') last_uid = int(users[-1][2]) filesize = sys_stat.stat('/etc/group')[4] f = fcntl.read(group_fd, filesize).decode(encoding='ASCII') groups = tuple(tuple(l.split(':')) for l in f.splitlines() if l[0] != '#') last_gid = int(groups[-1][1]) userline = (':'.join([ args.username, hashlib.sha224(upass.encode()).hexdigest(), str(last_uid + 1), str(last_gid + 1) ]) + ':\n').encode(encoding='ASCII') fcntl.write(passwd_fd, userline, len(userline)) groupline = (':'.join([args.username, str(last_gid + 1)]) + ':\n').encode(encoding='ASCII') fcntl.write(group_fd, groupline, len(groupline)) fcntl.close(passwd_fd) fcntl.close(group_fd)
def show_dir_list(path): fd = dcntl.opendir(path) if fd == -1: print('Unable to open directory ' + path) return users = None groups = None if verbose: passwd_fd = fcntl.open('/etc/passwd') buf = fcntl.read(passwd_fd, sys_stat.stat('/etc/passwd')[4]) users = tuple( tuple(l.split(':')) for l in buf.decode(encoding='ASCII').splitlines() if l.lstrip()[0] != '#') fcntl.close(passwd_fd) group_fd = fcntl.open('/etc/group') buf = fcntl.read(group_fd, sys_stat.stat('/etc/group')[4]) groups = tuple( tuple(l.split(':')) for l in buf.decode(encoding='ASCII').splitlines() if l.lstrip()[0] != '#') fcntl.close(group_fd) r = dcntl.readdir(fd) while r != -1: ino, reclen, name = r if verbose: show_dir_record(name, path, users, groups) else: print(name, end='\t') print() r = dcntl.readdir(fd) dcntl.closedir(fd)
def recursive_remove_dir(path): fd = dcntl.opendir(path) rec = dcntl.readdir(fd) try: while rec != -1: rec_path = path + '/' + rec[2] mode = sys_stat.stat(rec_path)[1] if sys_stat.is_dir(mode): recursive_remove_dir(rec_path) else: if fcntl.unlink(rec_path) == -1: raise StopRecursiveRemove(rec_path) rec = dcntl.readdir(fd) finally: dcntl.closedir(fd) dcntl.rmdir(path)
def exec_rm(args): if not args.recursive: if fcntl.unlink(args.path) == -1: print("Can't remove " + args.path) else: st = sys_stat.stat(args.path) if st != -1: if sys_stat.is_dir(st[1]): try: recursive_remove_dir(args.path) except StopRecursiveRemove as e: print("Can't remove " + str(e) + ' file.') else: fcntl.unlink(args.path) else: print("Can't remove " + args.path)
def show_dir_record(name, path, users, groups): st = sys_stat.stat(path + '/' + name) if st != -1: print('d' if sys_stat.is_dir(st[1]) else '-', end='') print('r' if st[1] & inodetable.S_IRUSR else '-', end='') print('w' if st[1] & inodetable.S_IWUSR else '-', end='') print('x' if st[1] & inodetable.S_IXUSR else '-', end='') print('r' if st[1] & inodetable.S_IRGRP else '-', end='') print('w' if st[1] & inodetable.S_IWGRP else '-', end='') print('x' if st[1] & inodetable.S_IXGRP else '-', end='') print('r' if st[1] & inodetable.S_IROTH else '-', end='') print('w' if st[1] & inodetable.S_IWOTH else '-', end='') print('x' if st[1] & inodetable.S_IXOTH else '-', end=' ') print('{0:4} {1:4} {2:4} {3:12} {4}'.format( next(u[0] for u in users if int(u[2]) == st[2]), next(g[0] for g in groups if int(g[1]) == st[3]), st[4], time.strftime('%b %d %H:%M', time.gmtime(st[6])).lower(), name))
def exec_cp(args): src_fd = fcntl.open(args.source) if src_fd == -1: print("Can't open " + args.source) return fcntl.unlink(args.dest) dst_fd = fcntl.creat( args.dest, inodetable.S_IRUSR | inodetable.S_IWUSR | inodetable.S_IRGRP | inodetable.S_IWGRP | inodetable.S_IROTH) if dst_fd == -1: print("Can't open or create " + args.dest) return buf = fcntl.read(src_fd, sys_stat.stat(args.source)[4]) fcntl.write(dst_fd, buf, len(buf)) fcntl.close(dst_fd) fcntl.close(src_fd)
def userblock_exec(args): passwd_fd = fcntl.open('/etc/passwd', openfile.O_RDWR) if passwd_fd == -1: print('Only root can block users.') return filesize = sys_stat.stat('/etc/passwd')[4] f = fcntl.read(passwd_fd, filesize).decode(encoding='ASCII') users = [l.split(':') for l in f.splitlines() if l[0] != '#'] for u in users: if u[0] == args.username: u[4] = 'nologin' fcntl.seek(passwd_fd, 0) userslines = [':'.join(u) + '\n' for u in users] for l in userslines: buf = l.encode(encoding='ASCII') fcntl.write(passwd_fd, buf, len(buf)) fcntl.close(passwd_fd)