Exemplo n.º 1
0
func Remove(conf *cjdngo.Conf, iface *cjdngo.InterfaceBlock, target string) {
	// Try to convert it to a number. If so, then remove a
	// password. Otherwise, remove a connection.
	index, err := strconv.Atoi(target)
	switch err {
	case nil: // Password case
		if index >= len(conf.AuthorizedPasswords) || index < 0 {
			println("There is no password of that index.")
			return
		}
		// Initialize a new array with length - 1
		newAuth := make([]cjdngo.AuthPass, len(conf.AuthorizedPasswords)-1)

		// Copy the first part, stopping before removed index.
		copy(newAuth[:index], conf.AuthorizedPasswords[:index])
		// Copy the second part, starting after the removed index.
		copy(newAuth[index:], conf.AuthorizedPasswords[index+1:])
		conf.AuthorizedPasswords = newAuth

	default: // Connection case
		oldLen := len(iface.ConnectTo)
		if oldLen == 0 {
			println("There are no entries in that interface.")
			return
		}
		delete(iface.ConnectTo, target)
		if oldLen == len(iface.ConnectTo) {
			println("There is no connection identified by that string.")
			return
		}
	}
}
Exemplo n.º 2
0
// Authorize takes a cjdngo.Conf object, the details to create the
// displayed connection block with (either TunConn or EthConn), an
// array index, a signed integer, and a cjdngo.AuthPass, which is
// meant to be parsed from user input. If the user input has been
// parsed into an AuthBlock already, then it can be passed and added
// directly. If they are not supplied (nil) then Authorize initiates a
// dialogue with the user to recieve them.  The third argument refers
// to the index at which to edit or add the new password. If it is -1,
// then a new password and authorization block is added and appended
// to the list.
func Authorize(conf *cjdngo.Conf, details string, index int, jsonArg []byte) {
	var auth *cjdngo.AuthPass
	var willAppend bool

	// If index is out of the bounds of the existing array, or if it's
	// -1, then auth will be empty.
	if len(conf.AuthorizedPasswords) <= index || index == -1 {
		auth = &cjdngo.AuthPass{}
		willAppend = true
	} else {
		auth = &conf.AuthorizedPasswords[index]
	}

	// If we weren't passed any details already, we have to ask for
	// them. This is the hard bit.
	if len(jsonArg) == 0 {
		// Take the name from the user. This is optional.
		ui("Please enter a name", &auth.Name)

		// Likewise, take the location from the user.
		ui("Please enter a location", &auth.Location)

		// The IPv6 address isn't usually known, but ask anyway.
		ui("Please enter an IPv6 address", &auth.IPv6)

		if len(auth.Password) == 0 {
			auth.Password = getPass(auth.Name)
		}
	} else {
		var inAuth cjdngo.AuthPass
		err := json.Unmarshal(jsonArg, &inAuth)
		if err != nil {
			println("Invalid authorized password.")
			return
		}
		auth = &inAuth
	}
	// Now we check whether we should append it to the end, or assume
	// the changes are already in place. (The latter case is always
	// true if we are editing.)
	if willAppend {
		conf.AuthorizedPasswords = append(conf.AuthorizedPasswords, *auth)
	}

	// Finally, we need to generate some connection details that the
	// authorized party can use. We check to make sure that the
	// connection details are in place, but must only ask
	// interactively if we haven't been given JSON.
	if len(conf.Name) == 0 && len(jsonArg) == 0 {
		// If the user did not supply a name, ask for one, which will
		// be written back to the configuration.
		ui("Please enter your name or username", &conf.Name)
	}
	if len(conf.Location) == 0 && len(jsonArg) == 0 {
		// Similarly for location.
		ui("Please enter your displayed location", &conf.Location)
	}
	for len(conf.TunConn) == 0 && len(jsonArg) == 0 {
		var ipv4 string
		// If there aren't any details in place, force the user to add
		// them. These will be written back to the configuration.
		ui("Please enter your IPv4 address", &ipv4)

		// BUG(DuoNoxSol): Panic if UDPInterface isn't present
		ipv4 += conf.Interfaces.UDPInterface[0].Bind[7:] // cjdns port
		conf.TunConn = ipv4
	}

	// Initialize a map with length 1, which will be used to display
	// our new details.
	display := make(map[string]*cjdngo.Connection, 1)

	display[details] = &cjdngo.Connection{
		Name:      conf.Name,
		Location:  conf.Location,
		IPv6:      conf.IPv6,
		Password:  auth.Password,
		PublicKey: conf.PublicKey,
	}
	b, err := json.MarshalIndent(display, "            ", "    ")
	if err != nil {
		log.Fatal(err)
	}
	println("\n            ", string(b), "\nPlease send these credentials to your peer.")
}