Exemplo n.º 1
0
// connectionListener accepts connections on tcp/port for both solicited and unsolicited
// client connections.
func (n *Node) connectionListener() {
	log.Debugln("connectionListener")
	ln, err := net.Listen("tcp", fmt.Sprintf(":%d", n.port))
	if err != nil {
		log.Fatalln(err)
	}
	for {
		conn, err := ln.Accept()
		if err != nil {
			log.Warnln(err)
			continue
		}
		n.newConnection(conn)
	}
}
Exemplo n.º 2
0
// Ready checks to see if the NBD kernel module has been loaded. If it does not
// find the module, it returns an error. NBD functions should only be used
// after this function returns no error.
func Ready() error {
	// Ensure that the kernel module has been loaded
	cmd := exec.Command(process("lsmod"))
	result, err := cmd.CombinedOutput()
	if err != nil {
		return err
	}

	if !strings.Contains(string(result), "nbd ") {
		return errors.New("add module 'nbd'")
	}

	// Warn if nbd wasn't loaded with a max_part parameter
	_, err = os.Stat("/sys/module/nbd/parameters/max_part")
	if err != nil {
		log.Warnln("no max_part parameter set for module nbd")
	}

	return nil
}
Exemplo n.º 3
0
func main() {
	var err error

	flag.Usage = usage
	flag.Parse()

	logSetup()

	// see containerShim()
	if flag.NArg() > 1 && flag.Arg(0) == CONTAINER_MAGIC {
		containerShim()
	}

	cliSetup()

	if *f_cli {
		if err := minicli.Validate(); err != nil {
			log.Fatalln(err)
		}

		doc, err := minicli.Doc()
		if err != nil {
			log.Fatal("failed to generate docs: %v", err)
		}
		fmt.Println(doc)
		os.Exit(0)
	}

	// rebase f_iomBase if f_base changed but iomBase did not
	if *f_base != BASE_PATH && *f_iomBase == IOM_PATH {
		*f_iomBase = filepath.Join(*f_base, "files")
	}

	if *f_version {
		fmt.Println("minimega", version.Revision, version.Date)
		fmt.Println(version.Copyright)
		os.Exit(0)
	}

	hostname, err = os.Hostname()
	if err != nil {
		log.Fatalln(err)
	}

	if isReserved(hostname) {
		log.Warn("hostname `%s` is a reserved word -- abandon all hope, ye who enter here", hostname)
	}

	// special case, catch -e and execute a command on an already running
	// minimega instance
	if *f_e || *f_attach {
		// try to connect to the local minimega
		mm, err := miniclient.Dial(*f_base)
		if err != nil {
			log.Fatalln(err)
		}
		mm.Pager = minipager.DefaultPager

		if *f_e {
			a := flag.Args()
			log.Debugln("got args:", a)

			// TODO: Need to escape?
			cmd := minicli.MustCompile(strings.Join(a, " "))
			log.Infoln("got command:", cmd)

			mm.RunAndPrint(cmd, false)
		} else {
			mm.Attach()
		}

		return
	}

	// warn if we're not root
	user, err := user.Current()
	if err != nil {
		log.Fatalln(err)
	}
	if user.Uid != "0" {
		log.Warnln("not running as root")
	}

	// check for a running instance of minimega
	_, err = os.Stat(filepath.Join(*f_base, "minimega"))
	if err == nil {
		if !*f_force {
			log.Fatalln("minimega appears to already be running, override with -force")
		}
		log.Warn("minimega may already be running, proceed with caution")
		err = os.Remove(filepath.Join(*f_base, "minimega"))
		if err != nil {
			log.Fatalln(err)
		}
	}

	// set up signal handling
	sig := make(chan os.Signal, 1024)
	signal.Notify(sig, os.Interrupt, syscall.SIGTERM)
	go func() {
		first := true
		for s := range sig {
			if s == os.Interrupt && first {
				// do nothing
				continue
			}

			if *f_panic {
				panic("teardown")
			}
			if first {
				log.Info("caught signal, tearing down, ctrl-c again will force quit")
				go teardown()
				first = false
			} else {
				os.Exit(1)
			}
		}
	}()

	err = checkExternal()
	if err != nil {
		log.Warnln(err.Error())
	}

	// attempt to set up the base path
	err = os.MkdirAll(*f_base, os.FileMode(0770))
	if err != nil {
		log.Fatal("mkdir base path: %v", err)
	}

	pid := os.Getpid()
	writeOrDie(filepath.Join(*f_base, "minimega.pid"), strconv.Itoa(pid))

	go commandSocketStart()

	// create a node for meshage
	host, err := os.Hostname()
	if err != nil {
		log.Fatalln(err)
	}
	meshageInit(host, *f_context, *f_degree, *f_msaTimeout, *f_port)

	// start the cc service
	ccStart()

	// start tap reaper
	go periodicReapTaps()

	fmt.Println(banner)

	// fan out to the number of cpus on the system if GOMAXPROCS env variable is
	// not set.
	if os.Getenv("GOMAXPROCS") == "" {
		cpus := runtime.NumCPU()
		runtime.GOMAXPROCS(cpus)
	}

	if !*f_nostdin {
		cliLocal()
	} else {
		<-sig
		if *f_panic {
			panic("teardown")
		}
	}
	teardown()
}
Exemplo n.º 4
0
func cliVyatta(c *minicli.Command, resp *minicli.Response) error {
	log.Warnln("the vyatta API is deprecated and will be removed in a future release")

	if c.BoolArgs["dhcp"] {
		net := c.StringArgs["network"]

		if len(c.StringArgs) == 0 {
			// List the existing DHCP services
			resp.Header = []string{"Network", "GW", "Start address", "Stop address", "DNS"}
			resp.Tabular = [][]string{}
			for k, v := range vyatta.Dhcp {
				resp.Tabular = append(resp.Tabular, []string{k, v.Gw, v.Start, v.Stop, v.Dns})
			}
		} else if c.StringArgs["gateway"] != "" {
			// Add a new DHCP service
			vyatta.Dhcp[net] = &vyattaDhcp{
				Gw:    c.StringArgs["gateway"],
				Start: c.StringArgs["low"],
				Stop:  c.StringArgs["high"],
				Dns:   c.StringArgs["dns"],
			}

			log.Debug("vyatta add dhcp %v", vyatta.Dhcp[net])
		} else {
			// Deleting a DHCP service
			if _, ok := vyatta.Dhcp[net]; !ok {
				resp.Error = "no such Dhcp service"
			} else {
				log.Debug("vyatta delete dhcp %v", net)
				delete(vyatta.Dhcp, net)
			}
		}
	} else if c.BoolArgs["interfaces"] {
		// Get or update IPv4 interfaces
		if len(c.ListArgs) == 0 {
			resp.Response = fmt.Sprintf("%v", vyatta.Ipv4)
		} else {
			vyatta.Ipv4 = c.ListArgs["net"]
		}
	} else if c.BoolArgs["interfaces6"] {
		// Get or update IPv6 interfaces
		if len(c.ListArgs) == 0 {
			resp.Response = fmt.Sprintf("%v", vyatta.Ipv6)
		} else {
			vyatta.Ipv6 = c.ListArgs["net"]
		}
	} else if c.BoolArgs["rad"] {
		// Get or update rad
		if len(c.ListArgs) == 0 {
			resp.Response = fmt.Sprintf("%v", vyatta.Rad)
		} else {
			vyatta.Rad = c.ListArgs["prefix"]
		}
	} else if c.BoolArgs["ospf"] {
		// Get or update ospf
		if len(c.ListArgs) == 0 {
			resp.Response = fmt.Sprintf("%v", vyatta.Ospf)
		} else {
			vyatta.Ospf = c.ListArgs["network"]
		}
	} else if c.BoolArgs["ospf3"] {
		// Get or update ospf
		if len(c.ListArgs) == 0 {
			resp.Response = fmt.Sprintf("%v", vyatta.Ospf3)
		} else {
			vyatta.Ospf3 = c.ListArgs["network"]
		}
	} else if c.BoolArgs["routes"] {
		if len(c.ListArgs) == 0 {
			resp.Header = []string{"Network", "Route"}
			resp.Tabular = [][]string{}

			for _, v := range vyatta.Routes {
				resp.Tabular = append(resp.Tabular, []string{v.Route, v.NextHop})
			}
		} else {
			err := vyattaUpdateRoutes(c.ListArgs["network"])
			if err != nil {
				resp.Error = err.Error()
			}
		}
	} else if c.BoolArgs["config"] {
		// override everything and just cram the listed file into the floppy
		// image
		if len(c.StringArgs) == 0 {
			resp.Response = vyatta.ConfigFile
		} else {
			vyatta.ConfigFile = c.StringArgs["filename"]
		}
	} else if c.BoolArgs["write"] {
		var err error
		resp.Response, err = vyattaWrite(c.StringArgs["filename"])
		if err != nil {
			resp.Error = err.Error()
		}
	} else {
		// Display info about running services
		var dhcpKeys []string
		for k, _ := range vyatta.Dhcp {
			dhcpKeys = append(dhcpKeys, k)
		}

		var routes []string
		for _, k := range vyatta.Routes {
			routes = append(routes, k.Route)
		}

		resp.Header = []string{
			"IPv4 addresses",
			"IPv6 addresses",
			"RAD",
			"DHCP servers",
			"OSPF",
			"OSPF3",
			"Routes",
		}
		resp.Tabular = [][]string{[]string{
			fmt.Sprintf("%v", vyatta.Ipv4),
			fmt.Sprintf("%v", vyatta.Ipv6),
			fmt.Sprintf("%v", vyatta.Rad),
			fmt.Sprintf("%v", dhcpKeys),
			fmt.Sprintf("%v", vyatta.Ospf),
			fmt.Sprintf("%v", vyatta.Ospf3),
			fmt.Sprintf("%v", routes),
		}}
	}

	return nil
}
Exemplo n.º 5
0
func smtpSendMail(server string, m mail, protocol string) error {
	// url notation requires leading and trailing [] on ipv6 addresses
	if isIPv6(server) {
		server = "[" + server + "]"
	}

	filedata, filename, err := smtpGetFile(m)
	if err != nil {
		return err
	}

	conn, err := net.Dial(protocol, server+smtpPort)
	if err != nil {
		return err
	}
	defer conn.Close()
	c, err := smtp.NewClient(conn, server)
	if err != nil {
		return err
	}
	defer c.Close()

	if *f_smtpTls {
		err = c.StartTLS(&tls.Config{InsecureSkipVerify: true})
		if err != nil {
			log.Warnln("could not start tls")
		}
	}

	c.Mail(m.From)
	c.Rcpt(m.To)
	wc, err := c.Data()
	if err != nil {
		return err
	}

	boundary := "PROTONUKE-MIME-BOUNDARY"

	// header
	header := fmt.Sprintf("Subject: %s\r\nMIME-Version: 1.0\r\nContent-Type: multipart/mixed; boundary=%s\r\n--%s", m.Subject, boundary, boundary)

	// body
	body := fmt.Sprintf("\r\nContent-Type: text/html\r\nContent-Transfer-Encoding:8bit\r\n\r\n%s\r\n--%s", m.Msg, boundary)

	wc.Write([]byte(header + body))

	if len(filedata) != 0 {
		var buf bytes.Buffer

		maxLineLen := 500
		nbrLines := len(filedata) / maxLineLen

		//append lines to buffer
		for i := 0; i < nbrLines; i++ {
			buf.Write(filedata[i*maxLineLen : (i+1)*maxLineLen])
			buf.WriteString("\n")
		}
		buf.Write(filedata[nbrLines*maxLineLen:])

		file := fmt.Sprintf("\r\nContent-Type: application/csv; name=\"file.csv\"\r\nContent-Transfer-Encoding:base64\r\nContent-Disposition: attachment; filename=\"%s\"\r\n\r\n%s\r\n--%s--", filename, buf.String(), boundary)

		wc.Write([]byte(file))
	}

	wc.Close()

	return nil
}
Exemplo n.º 6
0
func getOSVer() string {
	var fullVersion string

	//Get CurrentVersion
	cmd := exec.Command("reg", "query",
		"HKLM\\SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion",
		"/v", "CurrentVersion")
	cvBytes, err := cmd.CombinedOutput()
	if err != nil {
		log.Warnln("failed reg query: CurrentVersion")
	}
	cvStr := strings.Split(string(cvBytes), "    ")
	currentVersion := strings.TrimSpace(cvStr[len(cvStr)-1])

	//Get CurrentBuild
	cmd = exec.Command("reg", "query",
		"HKLM\\SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion",
		"/v", "CurrentBuild")

	cbBytes, err := cmd.CombinedOutput()

	if err != nil {
		log.Warnln("failed reg query: CurrentBuild")
		fullVersion = currentVersion
	} else {
		cbStr := strings.Split(string(cbBytes), "    ")
		currentBuild := strings.TrimSpace(cbStr[len(cbStr)-1])

		fullVersion = currentVersion + "." + currentBuild
	}

	switch fullVersion {
	case "1.04":
		return "Windows 1.0"
	case "2.11":
		return "Windows 2.0"
	case "3":
		return "Windows 3.0"
	case "3.11":
		return "Windows for Workgroups 3.11"
	case "2250":
		return "Whistler Server"
	case "2257":
		return "Whistler Server"
	case "2267":
		return "Whistler Server"
	case "2410":
		return "Whistler Server"
	case "3.10.528":
		return "Windows NT 3.1"
	case "3.5.807":
		return "Windows NT Workstation 3.5"
	case "3.51.1057":
		return "Windows NT Workstation 3.51"
	case "4.0.1381":
		return "Windows Workstation 4.0"
	case "4.0.950":
		return "Windows 95"
	case "4.00.950":
		return "Windows 95"
	case "4.00.1111":
		return "Windows 95"
	case "4.03.1212-1214":
		return "Windows 95"
	case "4.03.1214":
		return "Windows 95"
	case "4.1.1998":
		return "Windows 98"
	case "4.1.2222":
		return "Windows 98"
	case "4.90.2476":
		return "Windows Millenium"
	case "4.90.3000":
		return "Windows Me"
	case "5.00.1515":
		return "Windows NT 5.00"
	case "5.00.2031":
		return "Windows 2000"
	case "5.00.2128":
		return "Windows 2000"
	case "5.00.2183":
		return "Windows 2000"
	case "5.00.2195":
		return "Windows 2000"
	case "5.0.2195":
		return "Windows 2000"
	case "5.1.2505":
		return "Windows XP"
	case "5.1.2600":
		return "Windows XP"
	case "5.2.3790":
		return "Windows XP"
		//      Conflicts with Windows XP.
		//	case "5.2.3790": return "Windows Home Server"
		//	case "5.2.3790": return "Windows Server 2003"
	case "5.2.3541":
		return "Windows .NET Server"
	case "5.2.3590":
		return "Windows .NET Server"
	case "5.2.3660":
		return "Windows .NET Server"
	case "5.2.3718":
		return "Windows .NET Server 2003"
	case "5.2.3763":
		return "Windows Server 2003"
	case "6.0.5048":
		return "Windows Longhorn"
	case "6.0.5112":
		return "Windows Vista"
	case "6.0.5219":
		return "Windows Vista"
	case "6.0.5259":
		return "Windows Vista"
	case "6.0.5270":
		return "Windows Vista"
	case "6.0.5308":
		return "Windows Vista"
	case "6.0.5342":
		return "Windows Vista"
	case "6.0.5381":
		return "Windows Vista"
	case "6.0.5384":
		return "Windows Vista"
	case "6.0.5456":
		return "Windows Vista"
	case "6.0.5472":
		return "Windows Vista"
	case "6.0.5536":
		return "Windows Vista"
	case "6.0.5600":
		return "Windows Vista"
	case "6.0.5700":
		return "Windows Vista"
	case "6.0.5728":
		return "Windows Vista"
	case "6.0.5744":
		return "Windows Vista"
	case "6.0.5808":
		return "Windows Vista"
	case "6.0.5824":
		return "Windows Vista"
	case "6.0.5840":
		return "Windows Vista"
	case "6.0.6000":
		return "Windows Vista"
	case "6.0.6001":
		return "Windows Server 2008"
	case "6.0.6002":
		return "Windows Vista"
	case "6.1.7600":
		return "Windows 7"
		//      Conflicts with Windows 7.  Need more granularity (.16385)
		//	case "6.1.7600": return "Windows Server 2008 R2, RTM (Release to Manufacturing)"
	case "6.1.7601":
		return "Windows 7"
	case "6.2.9200":
		return "Windows 8"
		//	Conflicts with Windows 8.  Not sure how to tell these apart
		//	case "6.2.9200": return "Windows Server 2012"
	case "6.2.8102":
		return "Windows Server 2012"
	case "6.3.9600":
		return "Windows 8.1"
	}
	return "unknown"
}
Exemplo n.º 7
0
func main() {
	var err error

	flag.Usage = usage
	flag.Parse()
	if !strings.HasSuffix(*f_base, "/") {
		*f_base += "/"
	}

	if *f_cli {
		doc, err := minicli.Doc()
		if err != nil {
			log.Fatalln("failed to generate docs: %v", err)
		}
		fmt.Println(doc)
		os.Exit(0)
	}

	// rebase f_iomBase if f_base changed but iomBase did not
	if *f_base != BASE_PATH && *f_iomBase == IOM_PATH {
		*f_iomBase = *f_base + "files"
	}

	if !strings.HasSuffix(*f_iomBase, "/") {
		*f_iomBase += "/"
	}

	if *f_version {
		fmt.Println("minimega", version.Revision, version.Date)
		fmt.Println(version.Copyright)
		os.Exit(0)
	}

	logSetup()

	hostname, err = os.Hostname()
	if err != nil {
		log.Fatalln(err)
	}

	if isReserved(hostname) {
		log.Warn("hostname `%s` is a reserved word -- abandon all hope, ye who enter here", hostname)
	}

	vms = make(map[int]*vmInfo)

	// special case, catch -e and execute a command on an already running
	// minimega instance
	if *f_e {
		localCommand()
		return
	}
	if *f_attach {
		cliAttach()
		return
	}

	// warn if we're not root
	user, err := user.Current()
	if err != nil {
		log.Fatalln(err)
	}
	if user.Uid != "0" {
		log.Warnln("not running as root")
	}

	// check for a running instance of minimega
	_, err = os.Stat(*f_base + "minimega")
	if err == nil {
		if !*f_force {
			log.Fatalln("minimega appears to already be running, override with -force")
		}
		log.Warn("minimega may already be running, proceed with caution")
		err = os.Remove(*f_base + "minimega")
		if err != nil {
			log.Fatalln(err)
		}
	}

	// set up signal handling
	sig := make(chan os.Signal, 1024)
	signal.Notify(sig, os.Interrupt, syscall.SIGTERM)
	go func() {
		first := true
		for {
			<-sig
			if *f_panic {
				panic("teardown")
			}
			if first {
				log.Info("caught signal, tearing down, ctrl-c again will force quit")
				go teardown()
				first = false
			} else {
				os.Exit(1)
			}
		}
	}()

	err = checkExternal()
	if err != nil {
		log.Warnln(err.Error())
	}

	// attempt to set up the base path
	err = os.MkdirAll(*f_base, os.FileMode(0770))
	if err != nil {
		log.Fatal("mkdir base path: %v", err)
	}
	pid := os.Getpid()
	err = ioutil.WriteFile(*f_base+"minimega.pid", []byte(fmt.Sprintf("%v", pid)), 0664)
	if err != nil {
		log.Error("write minimega pid: %v", err)
		teardown()
	}
	go commandSocketStart()

	// create a node for meshage
	host, err := os.Hostname()
	if err != nil {
		log.Fatalln(err)
	}
	meshageInit(host, *f_namespace, uint(*f_degree), *f_port)

	fmt.Println(banner)

	// fan out to the number of cpus on the system if GOMAXPROCS env variable is
	// not set.
	if os.Getenv("GOMAXPROCS") == "" {
		cpus := runtime.NumCPU()
		runtime.GOMAXPROCS(cpus)
	}

	if !*f_nostdin {
		cliLocal()
	} else {
		<-sig
		if *f_panic {
			panic("teardown")
		}
	}
	teardown()
}