Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/master/external/source/ipwn/cmd_fs.c
Views: 11766
/*1* Copyright (c) 2004-2005 vlad902 <vlad902 [at] gmail.com>2* Copyright (c) 2007 H D Moore <hdm [at] metasploit.com>3* This file is part of the Metasploit Framework.4* $Revision$5*/67#include <sys/types.h>8#include <sys/param.h>9#include <sys/stat.h>10#include <stdlib.h>11#include <limits.h>12#include <unistd.h>13#include <string.h>14#include <errno.h>15#include <stdio.h>16#include <dirent.h>17#include <pwd.h>18#include <grp.h>19#include <sys/fcntl.h>2021#include "cmd.h"222324void ls_dofile(struct stat, char *);2526void cmd_ls(int argc, char * argv[])27{28char * path = ".";29DIR * dirp;30struct dirent * dp;31struct stat sb;3233if(argc > 1)34path = argv[1];3536if(stat(path, &sb) == -1)37{38perror("stat");39return;40}4142if(!S_ISDIR(sb.st_mode))43{44ls_dofile(sb, path);45}46else47{48if((dirp = opendir(path)) == NULL)49{50perror("opendir");51return;52}5354while((dp = readdir(dirp)) != NULL)55{56char buf[MAXPATHLEN+1];5758if(strlen(path) + strlen(dp->d_name) + 1 > MAXPATHLEN)59continue;60snprintf(buf, MAXPATHLEN, "%s/%s", path, dp->d_name);61if(stat(buf, &sb) == -1)62continue;6364ls_dofile(sb, dp->d_name);65}66}67}6869void ls_dofile(struct stat sb, char * file_name)70{71char perm[11] = "----------";7273if(sb.st_mode & 0400) perm[1] = 'r';74if(sb.st_mode & 0200) perm[2] = 'w';75if(sb.st_mode & 0100) perm[3] = 'x';76if(sb.st_mode & 0040) perm[4] = 'r';77if(sb.st_mode & 0020) perm[5] = 'w';78if(sb.st_mode & 0010) perm[6] = 'x';79if(sb.st_mode & 0004) perm[7] = 'r';80if(sb.st_mode & 0002) perm[8] = 'w';81if(sb.st_mode & 0001) perm[9] = 'x';82if(sb.st_mode & S_ISVTX)83{84if(sb.st_mode & 0001)85perm[9] = 't';86else87perm[9] = 'T';88}89if(sb.st_mode & S_ISGID)90{91if(sb.st_mode & 0010)92perm[6] = 'S';93else94perm[6] = 's';95}96if(sb.st_mode & S_ISUID)97{98if(sb.st_mode & 0100)99perm[3] = 'S';100else101perm[3] = 's';102}103if(S_ISBLK(sb.st_mode)) perm[0] = 'b';104if(S_ISCHR(sb.st_mode)) perm[0] = 'c';105if(S_ISDIR(sb.st_mode)) perm[0] = 'd';106if(S_ISLNK(sb.st_mode)) perm[0] = 'l'; /* XXX: works? */107if(S_ISFIFO(sb.st_mode)) perm[0] = 'p';108if(S_ISSOCK(sb.st_mode)) perm[0] = 's';109110printf("%s %3i %s %s %6i %s %s\n", perm, (int)sb.st_nlink, \111get_uid_str(sb.st_uid), get_gid_str(sb.st_gid), \112(int)sb.st_size, get_time_str("%b %d %H:%M"), file_name);113}114115void cmd_getcwd(int argc, char * argv[])116{117/* This should be big enough to accomodate all cases. */118char buf[MAXPATHLEN + 1];119120if(getcwd(buf, sizeof(buf)) == NULL)121perror("getcwd");122else123printf("%s\n", buf);124}125126void cmd_setcwd(int argc, char * argv[])127{128if(argc < 2)129cmd_getcwd(argc, argv);130else131if(chdir(argv[1]))132perror("chdir");133}134135136void cmd_chmod(int argc, char * argv[])137{138int perm;139140errno = 0;141perm = (int)strtol(argv[1], (char **)NULL, 8);142if(errno)143{144perror("strtol");145return;146}147148if(chmod(argv[2], perm) == -1)149perror("chmod");150}151152void cmd_chown(int argc, char * argv[])153{154struct passwd * pwd;155int uid;156157errno = 0;158uid = (int)strtol(argv[1], (char **)NULL, 10);159if(errno)160{161if((pwd = getpwnam(argv[1])) == NULL)162{163perror("getpwnam");164return;165}166167uid = pwd->pw_uid;168}169170if(chown(argv[2], uid, -1) == -1)171perror("chown");172}173174void cmd_chgrp(int argc, char * argv[])175{176struct group * grp;177int gid;178179errno = 0;180gid = (int)strtol(argv[1], (char **)NULL, 10);181if(errno)182{183if((grp = getgrnam(argv[1])) == NULL)184{185perror("getgrnam");186return;187}188189gid = grp->gr_gid;190}191192if(chown(argv[2], -1, gid) == -1)193perror("chown");194}195196void cmd_chdir(int argc, char * argv[])197{198if(chdir(argv[1]) == -1)199perror("chdir");200}201202void cmd_mkdir(int argc, char * argv[])203{204int perm = 0755;205206if(argc > 2)207{208errno = 0;209perm = (int)strtol(argv[2], (char **)NULL, 8);210if(errno)211{212perror("strtol");213return;214}215}216217if(mkdir(argv[1], perm) == -1)218perror("mkdir");219}220221void cmd_rmdir(int argc, char * argv[])222{223if(rmdir(argv[1]) == -1)224perror("rmdir");225}226227void cmd_rename(int argc, char * argv[])228{229if(rename(argv[1], argv[2]) == -1)230perror("rename");231}232233void cmd_unlink(int argc, char * argv[])234{235if(unlink(argv[1]) == -1)236perror("unlink");237}238239void cmd_chroot(int argc, char * argv[])240{241if(chroot(argv[1]) == -1)242perror("chroot");243}244245void cmd_link(int argc, char * argv[])246{247if(link(argv[1], argv[2]) == -1)248perror("link");249}250251void cmd_symlink(int argc, char * argv[])252{253if(symlink(argv[1], argv[2]) == -1)254perror("symlink");255}256257void cmd_cp(int argc, char * argv[])258{259int src, dst, len;260char buff[4096];261struct stat s;262char *path, *p, *t;263264path = strdup(argv[2]);265266src = open(argv[1], O_RDONLY);267if(src == -1) {268free(path);269perror("open(src)");270}271272dst = open(path, O_RDWR | O_CREAT | O_TRUNC, S_IRWXU);273if (dst == -1) {274275if(errno == EISDIR) {276t = strrchr(argv[1], '/');277if (t != NULL) {278t++;279} else {280t = argv[1];281}282283p = malloc(strlen(path) + strlen(t) + 2);284sprintf(p, "%s/%s", path, t);285free(path);286path = p;287288dst = open(path, O_RDWR | O_CREAT | O_TRUNC, S_IRWXU);289if ( dst == -1 ) {290close(src);291free(path);292perror("open(dst)");293}294295} else {296close(src);297free(path);298perror("open(dst)");299}300}301302stat(argv[1], &s);303304while(1)305{306len = read(src, buff, sizeof(buff));307if (len == -1) break;308if (len == 0) break;309310write(dst, buff, len);311if (len < sizeof(buff)) break;312}313close(src);314close(dst);315316chmod(path, s.st_mode);317free(path);318}319320321