Ejemplo n.º 1
0
func (c *Server) writeExports() error {
	network := c.network
	if network == "0.0.0.0/0" {
		network = "*" // turn this in to nfs 'allow all hosts' syntax
	}
	serviced_exports := fmt.Sprintf("%s\t%s(rw,fsid=0,no_root_squash,insecure,no_subtree_check,async)\n"+
		"%s/%s\t%s(rw,no_root_squash,nohide,insecure,no_subtree_check,async)",
		exportsPath, network, exportsPath, c.exportedName, network)
	if err := os.MkdirAll(exportsDir, 0775); err != nil {
		return err
	}
	edir := exportsDir + "/" + c.exportedName
	if err := os.MkdirAll(edir, 0775); err != nil {
		return err
	}
	if err := bindMount(c.basePath, edir); err != nil {
		return err
	}

	originalContents, err := readFileIfExists(etcExports)
	if err != nil {
		return err
	}

	// comment out lines that conflicts with serviced exported mountpoints
	mountpaths := map[string]bool{exportsPath: true, path.Join(exportsPath, c.exportedName): true}
	filteredContent := ""
	scanner := bufio.NewScanner(strings.NewReader(originalContents))
	for scanner.Scan() {
		line := strings.TrimSpace(scanner.Text())
		if !strings.HasPrefix(line, "#") {
			fields := strings.Fields(line)
			if len(fields) > 0 {
				mountpoint := fields[0]
				if _, ok := mountpaths[mountpoint]; ok {
					filteredContent += etcExportsRemoveComment + line + "\n"
					continue
				}
			}
		}

		filteredContent += line + "\n"
	}

	// create file content
	preamble, postamble := filteredContent, ""
	if index := strings.Index(filteredContent, etcExportsStartMarker); index >= 0 {
		preamble = filteredContent[:index]
		remainder := filteredContent[index:]
		if index := strings.Index(remainder, etcExportsEndMarker); index >= 0 {
			postamble = remainder[index+len(etcExportsEndMarker):]
		}
	}
	fileContents := preamble + etcExportsStartMarker + serviced_exports + etcExportsEndMarker + postamble

	return atomicfile.WriteFile(etcExports, []byte(fileContents), 0664)
}
Ejemplo n.º 2
0
func (c *Server) hostsDeny() error {

	s, err := readFileIfExists(hostDenyDefaults)
	if err != nil {
		return err
	}
	if strings.Contains(s, hostDenyDefaults) {
		return nil
	}

	if index := strings.Index(s, hostDenyMarker); index > 0 {
		s = s[:index-1]
	}
	s = s + hostDenyDefaults
	return atomicfile.WriteFile(etcHostsDeny, []byte(s), 0664)
}
Ejemplo n.º 3
0
func (c *Server) hostsAllow() error {
	s, err := readFileIfExists(etcHostsAllow)
	if err != nil {
		return err
	}

	if index := strings.Index(s, hostAllowMarker); index > 0 {
		s = s[:index-1]
	}

	hosts := make([]string, len(c.clients))
	i := 0
	for key := range c.clients {
		hosts[i] = key
		i++
	}
	sort.Strings(hosts)
	s = s + hostAllowDefaults + " " + strings.Join(hosts, " ") + "\n\n"

	return atomicfile.WriteFile(etcHostsAllow, []byte(s), 0664)
}