Exemple #1
0
func main() {
	flag.Parse()

	// ossec.conf
	configFile := getOssecConfPath(configfile)
	if configFile == "" {
		log.Errorf("ossec.conf not found. Please specify OSSEC install directory")
		return
	}

	// client.keys
	keysFile := getClientKeysPath(clientkeys)
	if keysFile == "" {
		log.Infof("client.keys not found, creating an empty file")
		createDefaultClientKeys()
	}

	key, err := register(manager, port)
	if err != nil {
		log.Error(err)
		return
	}

	isclient := false

	if !listen {
		isclient = true
	}

	if err := appendkey(keysFile, key, isclient); err != nil {
		log.Error(err)
		return
	}

	if err := writeconfig(configFile, manager); err != nil {
		log.Error(err)
		return
	}

	if restartService {
		if err := ossecsvc.Stop(); err != nil {
			log.Error(err)
		} else {
			log.Infof("Service Stopped")
		}

		if err := ossecsvc.Start(); err != nil {
			log.Error(err)
		} else {
			log.Infof("Service Started")
		}
	}
}
Exemple #2
0
func appendkey(file, line string, isclient bool) error {
	var fh *os.File
	var err error

	if file == "" {
		return fmt.Errorf("client.keys not found")
	}

	if isclient {
		fh, err = os.OpenFile(file, os.O_CREATE|os.O_RDWR|os.O_SYNC, 0700)
		if err != nil {
			fh.Close()
			if os.IsNotExist(err) {
				createDefaultClientKeys()
			} else {
				return fmt.Errorf("error appending key to client.keys: %v", err)
			}
		}
		defer fh.Close()
	} else {
		fh, err = os.OpenFile(file, os.O_APPEND|os.O_WRONLY|os.O_SYNC, 0700)
		if err != nil {
			fh.Close()
			return err
		}
		defer fh.Close()
	}

	log.Infof("Writing key to %s", file)

	if _, err := fh.Write([]byte(line)); err != nil {
		return fmt.Errorf("Error writing to client.keys: %v", err)
	}

	log.Infof("Key appended successfully")

	return nil
}