def string_to_remote(string):
    """Return the name and the remote dictionary matching the input string.

    The input should be in the following form:
    REMOTENAME=USERNAME:PASSWORD@HOST

    Note that the PASSWORD field (along with the ':' separator) is optional.
    Return None if the input is not valid.
    """
    if '=' not in string:
        return None
    name, rstring = string.split('=', 1)
    if not name or '@' not in rstring:
        return None
    remote = {}
    lstring, remote['host'] = rstring.rsplit('@', 1)
    if not lstring or not remote['host']:
        return None
    if ':' in lstring:
        remote['username'], remote['password'] = lstring.split(':', 1)
    else:
        remote['username'] = lstring
    if not remote['username']:
        return None
    return name, remote
