Esempio n. 1
0
func main() {

	if len(os.Args) < 2 {
		fmt.Fprintf(os.Stderr, "Usage: %s -h for help\n", os.Args[0])
		os.Exit(1)
	}

	config_file := goopt.String([]string{"-c", "--config"}, "nrpe.cfg",
		"config file to use")
	//the first option, will be the default, if the -m isnt given
	run_mode := goopt.Alternatives([]string{"-m", "--mode"},
		[]string{"foreground", "daemon", "systemd"}, "operating mode")
	goopt.Parse(nil)

	//implement different run modes..
	fmt.Println(*run_mode)
	config_obj := new(read_config.ReadConfig)
	config_obj.Init(*config_file)
	err := config_obj.ReadConfigFile()
	common.CheckError(err)
	//extract the commands command[cmd_name] = "/bin/foobar"
	config_obj.ReadCommands()
	config_obj.ReadPrivileges()
	//TODO check for errors
	//what we gonna do with the group?
	pwd := drop_privilege.Getpwnam(config_obj.Nrpe_user)
	drop_privilege.DropPrivileges(int(pwd.Uid), int(pwd.Gid))
	//we have to read it from config
	service := ":5666"
	err = setupSocket(4, service, config_obj)
	common.CheckError(err)
}
Esempio n. 2
0
func prepareConnection(endpoint string, transport_type int) net.Conn {
	tcpAddr, err := net.ResolveTCPAddr("tcp4", endpoint)
	common.CheckError(err)
	conn, err := getSocket(transport_type, endpoint, tcpAddr)

	common.CheckError(err)

	if conn != nil {
		return conn
	}
	return nil
}
Esempio n. 3
0
func handleClient(conn net.Conn, config_obj *read_config.ReadConfig) {
	// close connection on exit
	defer conn.Close()
	pkt_rcv, _ := common.ReceivePacket(conn)

	cmd := string(pkt_rcv.CommandBuffer[:common.GetLen(pkt_rcv.CommandBuffer[:])])
	pkt_rcv_crc32value := pkt_rcv.CRC32Value

	if crc32, _ := common.DoCRC32(&pkt_rcv); crc32 != pkt_rcv_crc32value {
		fmt.Println("WARNING: CRC not matching", crc32, pkt_rcv_crc32value)
	}

	pkt_send := common.PrepareToSend(cmd, common.RESPONSE_PACKET)

	if pkt_send.ResultCode == common.STATE_UNKNOWN { //its a response, but not to the HELLO_COMMAND
		if config_obj.IsCommandAllowed(cmd) {
			str_cmd := config_obj.GetCommand(cmd)
			fmt.Println("executing:", str_cmd)
			return_id, return_stdout := common.ExecuteCommand(str_cmd)
			pkt_send.ResultCode = return_id
			copy(pkt_send.CommandBuffer[:], return_stdout)
			pkt_send.CRC32Value, _ = common.DoCRC32(&pkt_send)
		} else {
			pkt_send.ResultCode = common.STATE_CRITICAL
		}
	}

	err := common.SendPacket(conn, pkt_send)
	common.CheckError(err)
}
Esempio n. 4
0
func setupSocket(socket_version int, service string, config_obj *read_config.ReadConfig) error {

	socket_type := "tcp4"

	if socket_version == 6 {
		socket_type = "tcp6"
	}

	tcpAddr, err := net.ResolveTCPAddr(socket_type, service)
	common.CheckError(err)

	listener, err := net.ListenTCP("tcp", tcpAddr)

	if err != nil {
		return err
	}

	for {
		if conn, err := listener.Accept(); err != nil {
			continue
		} else {
			// run as a goroutine
			go handleClient(conn, config_obj)
		}
	}

	return nil
}
Esempio n. 5
0
func main() {

	if len(os.Args) < 2 {
		fmt.Printf("%s -h for help\n", os.Args[0])
		os.Exit(1)
	}

	var host = goopt.String([]string{"-H", "--host"}, "127.0.0.1", "The remote host running NRPE-Server")
	var port = goopt.Int([]string{"-p", "--port"}, 5666, "The remote port on which the NRPE-server listens")
	var transport = goopt.Int([]string{"-t", "--transport"}, 0, "Transport type: 0 - clear, 1 - ssl, 2 -ssh")
	var command = goopt.String([]string{"-c", "--command"}, "version",
		"The check command defined in the nrpe.cfg file you would like to trigger")

	goopt.Parse(nil)
	service := fmt.Sprintf("%s:%d", *host, *port)
	conn := prepareConnection(service, *transport)
	pkt_to_send := common.PrepareToSend(*command, common.QUERY_PACKET)
	err := common.SendPacket(conn, pkt_to_send)
	common.CheckError(err)
	response_from_command, _ := common.ReceivePacket(conn)
	fmt.Println(string(response_from_command.CommandBuffer[:]))
	os.Exit(int(response_from_command.ResultCode))
}