- for (i = 0; i < strlen(id); i++)
- {
- if(!isalpha(id[i]) && id[i] != '_')
- {
- return 0;
- }
- }
-
- return 1;
-}
-
-/* Generic outgoing request routine - takes care of logging and error detection as well */
-
-int send_request(conn_list_t *cl, const char *format, int request, /*args*/ ...)
-{
- va_list args;
- char *buffer = NULL;
- int len;
-
-cp
- if(debug_lvl >= DEBUG_PROTOCOL)
- syslog(LOG_DEBUG, _("Sending %s to %s (%s)"), request_name[request], cl->name, cl->hostname);
-
- va_start(args, request);
- len = vasprintf(&buffer, format, args);
- va_end(args);
-
- if(len < 0 || !buffer)
- {
- syslog(LOG_ERR, _("Error during vasprintf(): %m"));
- return -1;
- }
-
- if(debug_lvl >= DEBUG_META)
- syslog(LOG_DEBUG, _("Sending meta data to %s (%s): %s"),
- cl->name, cl->hostname, buffer);
-
- if(cl->status.encryptin)
- {
- /* FIXME: Do encryption */
- }
-
- if((write(cl->meta_socket, buffer, len)) < 0)
- {
- syslog(LOG_ERR, _("Sending meta data failed: %m"));
- return -1;
- }
-cp
-}
-
-/* Connection protocol:
-
- Client Server
- send_id(u)
- send_challenge(R)
- send_chal_reply(BH)
- send_id(B)
- send_challenge(BR)
- send_chal_reply(BH)
- send_ack(B)
- send_ack(B)
-
- (u) Unencrypted,
- (R) RSA,
- (H) SHA1,
- (B) Blowfish.
-
- Part of the challenge is directly used to set the blowfish key and the initial vector.
- (Twee vliegen in één klap!)
-*/
-
-int send_id(conn_list_t *cl)
-{
-cp
- return send_request(cl, "%d %s %d %s", ID,
- myself->name, myself->protocol_version, opt2str(myself->options));
-}
-
-int id_h(conn_list_t *cl)
-{
- conn_list_t *old;
- char *options;
-cp
- if(sscanf(cl->buffer, "%*d %as %d %as", &cl->name, &cl->protocol_version, &options) != 3)
- {
- syslog(LOG_ERR, _("Got bad ID from %s"), cl->hostname);
- return -1;
- }
-
- /* Check if version matches */
-
- if(cl->protocol_version != myself->protocol_version)
- {
- syslog(LOG_ERR, _("Peer %s (%s) uses incompatible version %d"),
- cl->name, cl->hostname, cl->protocol_version);
- return -1;
- }
-
- /* Check if option string is valid */
-
- if((cl->options = str2opt(options)) == -1)
- {
- syslog(LOG_ERR, _("Peer %s uses invalid option string"), cl->hostname);
- return -1;
- }
-
- /* Check if identity is a valid name */
-
- if(!check_id(cl->name))
- {
- syslog(LOG_ERR, _("Peer %s uses invalid identity name"), cl->hostname);
- return -1;
- }
-
- /* Load information about peer */
-
- if(!read_id(cl))
- {
- syslog(LOG_ERR, _("Peer %s had unknown identity (%s)"), cl->hostname, cl->name);
- return -1;
- }
-
-
- /* First check if the host we connected to is already in our
- connection list. If so, we are probably making a loop, which
- is not desirable.
- */
-
- if(cl->status.outgoing)
- {
- if((old = lookup_id(cl->name)))
- {
- if(debug_lvl > DEBUG_CONNECTIONS)
- syslog(LOG_NOTICE, _("Uplink %s (%s) is already in our connection list"), cl->name, cl->hostname);
- cl->status.outgoing = 0;
- old->status.outgoing = 1;
- terminate_connection(cl);
- return 0;
- }
- }
-
- /* Send a challenge to verify the identity */
-
- cl->allow_request = CHAL_REPLY;
-cp
- return send_challenge(cl);
-}
-
-int send_challenge(conn_list_t *cl)
-{
- char *buffer;
- int keylength;
- int x;
-cp
- if(cl->chal_answer)
- free(cl->chal_answer);
-
- /* Allocate buffers for the challenge and the hash */
-
- cl->chal_answer = xmalloc(SHA_DIGEST_LENGTH);
- keylength = BN_num_bytes(cl->rsakey->length);
- buffer = xmalloc(keylength*2);
-
- /* Copy random data and the public key to the buffer */
-
- RAND_bytes(buffer, keylength);
- BN_bn2bin(cl->rsakey->length, buffer+keylength);
-
- /* If we don't have a blowfish key set yet, use the random data from the challenge to do so. */
-
- if(!cl->status.encryptin)
- {
- set_metakey(cl, buffer, keylength);
- }
-
- /* Calculate the hash from that */
-
- SHA1(buffer, keylength*2, cl->chal_answer);
-
- /* Convert the random data to a hexadecimal formatted string */
-
- bin2hex(buffer,buffer,keylength);
- buffer[keylength*2] = '\0';
-
- /* Send the challenge */
-
- cl->allow_request = CHAL_REPLY;
- x = send_request(cl, "%d %s", CHALLENGE, buffer);
- free(buffer);
- cl->status.encryptout = 1;
-cp
- return x;
-}
-
-int challenge_h(conn_list_t *cl)
-{
- char *challenge;
- int x;
-
-cp
- if(sscanf(cl->buffer, "%*d %as", &cl->name, &challenge) != 1)
- {
- syslog(LOG_ERR, _("Got bad CHALLENGE from %s (%s)"), cl->name, cl->hostname);
- return -1;
- }
-
- /* Rest is done by send_chal_reply() */
-
- x = send_chal_reply(cl, challenge);
- free(challenge);
-cp
- return x;
-}
-
-int send_chal_reply(conn_list_t *cl, char *challenge)
-{
- char *buffer;
- int keylength;
- char *hash;
- int x;
-
-cp
- keylength = BN_num_bytes(myself->rsakey->length);
-
- /* Check if the length of the challenge is all right */
-
- if(strlen(challenge) != keylength*2)
- {
- syslog(LOG_ERR, _("Intruder: wrong challenge length from %s (%s)"), cl->name, cl->hostname);
- return -1;
- }
-
- /* Allocate buffers for the challenge and the hash */
-
- buffer = xmalloc(keylength*2);
- hash = xmalloc(SHA_DIGEST_LENGTH*2+1);
-
- /* Copy the incoming random data and our public key to the buffer */
-
- hex2bin(challenge, buffer, keylength);
- BN_bn2bin(myself->rsakey->length, buffer+keylength);
-
- /* Calculate the hash from that */
-
- SHA1(buffer, keylength*2, hash);
-
- /* If we don't have a blowfish key set yet, use the random data from the challenge to do so. */
-
- if(!cl->status.encrypted)
- {
- set_metakey(cl, buffer, keylength);
- cl->status.encrypted = 1;
- }
-
- free(buffer);
-
- /* Convert the hash to a hexadecimal formatted string */
-
- bin2hex(hash,hash,SHA_DIGEST_LENGTH);
- hash[SHA_DIGEST_LENGTH*2] = '\0';
-
- /* Send the reply */
-
- if(cl->status.outgoing)
- cl->allow_request = ID;
- else
- cl->allow_request = ACK;
-
- x = send_request(cl, "%d %s", CHAL_REPLY, hash);
- free(hash);
-cp
- return x;
-}
-
-int chal_reply_h(conn_list_t *cl)
-{
- char *hash;
-cp
- if(sscanf(cl->buffer, "%*d %as", &cl->name, &hash) != 2)
- {
- syslog(LOG_ERR, _("Got bad CHAL_REPLY from %s (%s)"), cl->name, cl->hostname);
- return -1;
- }
-
- /* Check if the length of the hash is all right */
-
- if(strlen(hash) != SHA_DIGEST_LENGTH*2)
- {
- syslog(LOG_ERR, _("Intruder: wrong challenge reply length from %s (%s)"), cl->name, cl->hostname);
- return -1;
- }
-
- /* Convert the hash to binary format */
-
- hex2bin(hash, hash, SHA_DIGEST_LENGTH);
-
- /* Verify the incoming hash with the calculated hash */
-
- if(!memcmp(hash, cl->chal_answer, SHA_DIGEST_LENGTH))
- {
- syslog(LOG_ERR, _("Intruder: wrong challenge reply from %s (%s)"), cl->name, cl->hostname);
- return -1;
- }
-
- /* Identity has now been positively verified.
- If we are accepting this new connection, then send our identity,
- if we are making this connecting, acknowledge.
- */
-
- free(hash);
- free(cl->chal_answer);
-
-cp
- if(cl->status.outgoing)
- {
- cl->allow_request = ACK;
- return send_ack(cl);
- }
- else
- {
- cl->allow_request = CHALLENGE;
- return send_id(cl);
- }
-}
-
-int send_ack(conn_list_t *cl)
-{
-cp
- return send_request(cl, "%d", ACK);
-}
-
-int ack_h(conn_list_t *cl)
-{
- conn_list_t *old;
-
-cp
- /* Okay, before we active the connection, we check if there is another entry
- in the connection list with the same vpn_ip. If so, it presumably is an
- old connection that has timed out but we don't know it yet.
- */
-
- while((old = lookup_id(cl->name)))
- {
- if(debug_lvl > DEBUG_CONNECTIONS)
- syslog(LOG_NOTICE, _("Removing old entry for %s at %s in favour of new connection from %s"),
- cl->name, old->hostname, cl->hostname);
- old->status.active = 0;
- terminate_connection(old);
- }
-
- /* Activate this connection */
-
- cl->allow_request = ALL;
- cl->status.active = 1;
-
- if(debug_lvl > DEBUG_CONNECTIONS)
- syslog(LOG_NOTICE, _("Connection with %s (%s) activated"), cl->name, cl->hostname);
-
- /* Exchange information about other tinc daemons */
-
- notify_others(cl, NULL, send_add_host);
- notify_one(cl);
-
- upstreamindex = 0;
-
-cp
- if(cl->status.outgoing)
- return 0;
- else
- return send_ack(cl);
-}
-
-/* Address and subnet information exchange */
-
-int send_add_subnet(conn_list_t *cl, conn_list_t *other, subnet_t *subnet)
-{
-cp
-/* return send_request(cl, "%d %s %d %s", ADD_SUBNET,
- other->name, subnet->type, net2str(subnet)); */
- return send_request(cl, "%d %s %s", ADD_SUBNET,
- other->name, net2str(subnet));
-}