Beispiel #1
0
func Put(log *logging.Logger, conn net.Conn, command *common.JSONCommand) {
	var res common.JSONResult

	if _, err := os.Stat(command.Element.Name); err == nil || os.IsExist(err) {
		os.Rename(command.Element.Name, command.Element.Name+"."+time.Now().String())
	}

	switch command.Element.Type {
	case "directory":
		os.Mkdir(command.Element.Name, 0755)
		res = fsSetAttrs(log, command)
	case "file":
		if hash, err := common.ReceiveFile(command.Element.Name, conn); hash != command.Element.Hash {
			log.Debug("Error: hash mismatch")
			res = common.JSONResult{Result: "ko", Message: "Hash mismatch"}
		} else if err != nil {
			log.Debug("Error:", err)
			res = common.JSONResult{Result: "ko", Message: "Error: " + err.Error()}
		} else {
			res = fsSetAttrs(log, command)
		}
	case "symlink":
		if err := os.Symlink(command.Element.Link, command.Element.Name); err != nil {
			log.Debug("Error:", err)
			res = common.JSONResult{Result: "ko", Message: "Error: " + err.Error()}
		} else {
			res = common.JSONResult{Result: "ok"}
		}
	}

	res.Send(conn)
}
Beispiel #2
0
func (v visit) visitFile(fp string, fi os.FileInfo, err error) error {
	var res common.JSONResult
	var file common.JSONFile

	if err != nil {
		res = common.JSONResult{Result: "ko", Message: err.Error()}
		res.Send(v.connection)
		return nil
	}

	if v.exclude != "" && isMatch(v.exclude, fp) {
		return nil
	}

	// Set the file name and the operating system
	file.Name = fp
	file.Os = runtime.GOOS
	file.Mtime = fi.ModTime().Unix()

	if runtime.GOOS != "windows" {
		file.User, _ = getUserName(fi)
		file.Group, _ = getGroupName(fi)
		file.Mode = fi.Mode().String()
		file.Ctime = getCtime(fi)
	}

	// Set type of element (file or directory)
	if fi.IsDir() {
		file.Type = "directory"
		file.Size = fi.Size()
	} else if fi.Mode()&os.ModeSymlink == os.ModeSymlink {
		file.Type = "symlink"
		file.Size = fi.Size()

		link, err := os.Readlink(fp)
		if err != nil {
			v.log.Debug("Error when readlink for " + fp + ": " + err.Error())
		} else {
			file.Link = link
		}
	} else {
		file.Type = "file"
		file.Size = fi.Size()
		file.Hash = hex.EncodeToString(common.Md5(fp))
	}

	if v.acl && file.Type != "symlink" {
		if runtime.GOOS != "windows" {
			fa := FileACL(fp)
			file.Acl = fa.List(v.log)
		}
	}

	// Set result
	res.Result = "ok"
	res.Data = file
	res.Send(v.connection)
	return nil
}
Beispiel #3
0
func List(logger *logging.Logger, conn net.Conn, command *common.JSONCommand) {
	v := visit{
		connection: conn,
		acl:        command.ACL,
		exclude:    command.Exclude,
		log:        logger,
	}

	if len(command.Paths) > 0 {
		for _, path := range command.Paths {
			filepath.Walk(path, v.visitFile)
		}
	} else {
		res := common.JSONResult{Result: "ko", Message: "No directory specified"}
		res.Send(v.connection)
	}
}
Beispiel #4
0
func Parse(log *logging.Logger, data []uint8, conn net.Conn) {
	var cmd common.JSONMessage

	if err := json.Unmarshal(data, &cmd); err != nil {
		log.Debug("Error when parsing data: " + string(data))
		res := common.JSONResult{Result: "ko", Message: "Malformed command: " + err.Error()}
		res.Send(conn)
		return
	}
	log.Debug("Received data: " + string(data))

	switch cmd.Context {
	case "system":
		log.Debug("System context requested")
		switch cmd.Command.Name {
		case "exit":
			log.Debug("Exit command requested")
			res := common.JSONResult{Result: "ok", Message: "Client closed"}
			res.Send(conn)
			os.Exit(0)
		case "exec":
			log.Debug("Execute command requested")
			if err := common.ExecuteCMD(cmd.Command.Cmd); err != nil {
				log.Debug("Error when executing command: " + err.Error())
			}
			res := common.JSONResult{Result: "ok", Message: "Command executed"}
			res.Send(conn)
		default:
			log.Debug("Invalid command requested: " + cmd.Command.Name)
			res := common.JSONResult{Result: "ko", Message: "Command unknown: " + cmd.Command.Name}
			res.Send(conn)
		}
	case "file":
		log.Debug("File context requested")
		switch cmd.Command.Name {
		case "list":
			log.Debug("List command requested")
			files.List(log, conn, &cmd.Command)
		case "get":
			log.Debug("Get command requested")
			common.SendFile(cmd.Command.Element.Name, conn)
		case "put":
			log.Debug("Put command requested")
			files.Put(log, conn, &cmd.Command)
		default:
			log.Debug("Invalid command requested: " + cmd.Command.Name)
			res := common.JSONResult{Result: "ko", Message: "Command unknown: " + cmd.Command.Name}
			res.Send(conn)
		}
	default:
		log.Debug("Invalid context requested: " + cmd.Context)
		res := common.JSONResult{Result: "ko", Message: "Context unknown: " + cmd.Context}
		res.Send(conn)
	}
}