package serverctl import ( "net" "os" "strings" "github.com/gravitl/netmaker/database" "github.com/gravitl/netmaker/logger" "github.com/gravitl/netmaker/logic" "github.com/gravitl/netmaker/logic/acls" "github.com/gravitl/netmaker/logic/acls/nodeacls" "github.com/gravitl/netmaker/logic/pro" "github.com/gravitl/netmaker/netclient/ncutils" "github.com/gravitl/netmaker/nm-proxy/manager" "github.com/gravitl/netmaker/servercfg" "golang.zx2c4.com/wireguard/wgctrl/wgtypes" ) const ( // NETMAKER_BINARY_NAME - name of netmaker binary NETMAKER_BINARY_NAME = "netmaker" ) // InitServerNetclient - intializes the server netclient // 1. Check if config directory exists, if not attempt to make // 2. Check current networks and run pull to get interface up to date in case of restart func InitServerNetclient() error { netclientDir := ncutils.GetNetclientPath() _, err := os.Stat(netclientDir + "/config") if os.IsNotExist(err) { os.MkdirAll(netclientDir+"/config", 0700) } else if err != nil { logger.Log(1, "could not find or create", netclientDir) return err } var networks, netsErr = logic.GetNetworks() if netsErr == nil || database.IsEmptyRecord(netsErr) { for _, network := range networks { var currentServerNode, nodeErr = logic.GetNetworkServerLocal(network.NetID) if nodeErr == nil { if currentServerNode.Version != servercfg.Version { currentServerNode.Version = servercfg.Version logic.UpdateNode(¤tServerNode, ¤tServerNode) } if err = logic.ServerPull(¤tServerNode, true); err != nil { logger.Log(1, "failed pull for network", network.NetID, ", on server node", currentServerNode.ID) } } if err = logic.InitializeNetUsers(&network); err != nil { logger.Log(0, "something went wrong syncing usrs on network", network.NetID, "-", err.Error()) } } } return nil } func SyncServerNetworkWithProxy() error { networks, err := logic.GetNetworks() if err != nil { logger.Log(1, "error retrieving networks for keepalive", err.Error()) } for _, network := range networks { serverNetworkSettings, err := logic.GetNetwork(network.NetID) if err != nil { continue } localnets, err := net.Interfaces() if err != nil { return err } ifaceExists := false for _, localnet := range localnets { if serverNetworkSettings.DefaultInterface == localnet.Name { ifaceExists = true } } if ifaceExists { serverNode, err := logic.GetNetworkServerLocal(network.NetID) if err != nil { logger.Log(1, "failed to retrieve local server node: ", serverNode.ID) continue } peers, err := logic.GetPeersForProxy(&serverNode) if err != nil && !ncutils.IsEmptyRecord(err) { logger.Log(1, "failed to retrieve peers for server node: ", serverNode.ID) continue } logger.Log(0, "----> HEREEEEEEEE1") proxyPayload := manager.ManagerPayload{ IsRelay: serverNode.IsRelay == "yes", InterfaceName: serverNode.Interface, Peers: peers, } if proxyPayload.IsRelay { relayedNodes, err := logic.GetRelayedNodes(&serverNode) if err != nil { logger.Log(1, "failed to relayed nodes: ", serverNode.Name, err.Error()) proxyPayload.IsRelay = false } else { relayPeersMap := make(map[string][]wgtypes.PeerConfig) for _, relayedNode := range relayedNodes { peers, err := logic.GetPeersForProxy(&relayedNode) if err == nil { relayPeersMap[relayedNode.PublicKey] = peers } } proxyPayload.RelayedPeers = relayPeersMap } } logic.ProxyMgmChan <- &manager.ManagerAction{ Action: manager.AddInterface, Payload: proxyPayload, } } } return nil } // SyncServerNetwork - ensures a wg interface and node exists for server func SyncServerNetwork(network string) error { serverNetworkSettings, err := logic.GetNetwork(network) if err != nil { return err } localnets, err := net.Interfaces() if err != nil { return err } ifaceExists := false for _, localnet := range localnets { if serverNetworkSettings.DefaultInterface == localnet.Name { ifaceExists = true } } serverNode, err := logic.GetNetworkServerLocal(network) if !ifaceExists && (err == nil && serverNode.ID != "") { return logic.ServerUpdate(&serverNode, true) } else if !ifaceExists { _, err := logic.ServerJoin(&serverNetworkSettings) if err != nil { logger.Log(0, "network add failed for "+serverNetworkSettings.NetID) } } return nil } func SetDefaults() error { if err := setNodeDefaults(); err != nil { return err } if err := setNetworkDefaults(); err != nil { return err } if err := setUserDefaults(); err != nil { return err } return nil } // setNodeDefaults - runs through each node and set defaults func setNodeDefaults() error { // upgraded systems will not have ACL's set, which is why we need this function nodes, err := logic.GetAllNodes() if err != nil { return err } for i := range nodes { logic.SetNodeDefaults(&nodes[i]) logic.UpdateNode(&nodes[i], &nodes[i]) currentNodeACL, err := nodeacls.FetchNodeACL(nodeacls.NetworkID(nodes[i].Network), nodeacls.NodeID(nodes[i].ID)) if (err != nil && (database.IsEmptyRecord(err) || strings.Contains(err.Error(), "no node ACL present"))) || currentNodeACL == nil { if _, err = nodeacls.CreateNodeACL(nodeacls.NetworkID(nodes[i].Network), nodeacls.NodeID(nodes[i].ID), acls.Allowed); err != nil { logger.Log(1, "could not create a default ACL for node", nodes[i].ID) } } } return nil } func setNetworkDefaults() error { // upgraded systems will not have NetworkUsers's set, which is why we need this function networks, err := logic.GetNetworks() if err != nil && !database.IsEmptyRecord(err) { return err } for _, net := range networks { if err = pro.InitializeNetworkUsers(net.NetID); err != nil { logger.Log(0, "could not initialize NetworkUsers on network", net.NetID) } pro.AddProNetDefaults(&net) update := false newNet := net if strings.Contains(net.NetID, ".") { newNet.NetID = strings.ReplaceAll(net.NetID, ".", "") newNet.DefaultInterface = strings.ReplaceAll(net.DefaultInterface, ".", "") update = true } if strings.ContainsAny(net.NetID, "ABCDEFGHIJKLMNOPQRSTUVWXYZ") { newNet.NetID = strings.ToLower(net.NetID) newNet.DefaultInterface = strings.ToLower(net.DefaultInterface) update = true } if update { newNet.SetDefaults() if err := logic.SaveNetwork(&newNet); err != nil { logger.Log(0, "error saving networks during initial update:", err.Error()) } if err := logic.DeleteNetwork(net.NetID); err != nil { logger.Log(0, "error deleting old network:", err.Error()) } } else { net.SetDefaults() _, _, _, _, _, _, err = logic.UpdateNetwork(&net, &net) if err != nil { logger.Log(0, "could not set defaults on network", net.NetID) } } } return nil } func setUserDefaults() error { users, err := logic.GetUsers() if err != nil && !database.IsEmptyRecord(err) { return err } for _, user := range users { updateUser, err := logic.GetUser(user.UserName) if err != nil { logger.Log(0, "could not update user", updateUser.UserName) } logic.SetUserDefaults(&updateUser) copyUser := updateUser copyUser.Password = "" if _, err = logic.UpdateUser(copyUser, updateUser); err != nil { logger.Log(0, "could not update user", updateUser.UserName) } } return nil }