summaryrefslogtreecommitdiff
path: root/src/pluto/myid.c
blob: c90d14ef8a5108f589df014330b3699b18061670 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/* identity representation, as in IKE ID Payloads (RFC 2407 DOI 4.6.2.1)
 * Copyright (C) 1999-2001  D. Hugh Redelmeier
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2 of the License, or (at your
 * option) any later version.  See <http://www.fsf.org/copyleft/gpl.txt>.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * for more details.
 */

#include <errno.h>
#include <unistd.h>

#ifndef HOST_NAME_MAX        /* POSIX 1003.1-2001 says <unistd.h> defines this */
# define HOST_NAME_MAX  255 /* upper bound, according to SUSv2 */
#endif

#include <utils/identification.h>

#include <freeswan.h>

#include "myid.h"
#include "constants.h"
#include "defs.h"
#include "log.h"
#include "connections.h"
#include "packet.h"
#include "whack.h"

enum myid_state myid_state = MYID_UNKNOWN;

identification_t *myids[MYID_SPECIFIED+1];    /* %myid */

/**
 * Fills in myid from environment variable IPSECmyid or defaultrouteaddr
 */
void init_myid(void)
{
	myid_state = MYID_UNKNOWN;
	{
		enum myid_state s;

		for (s = MYID_UNKNOWN; s <= MYID_SPECIFIED; s++)
		{
			myids[s] = identification_create_from_string("%any");
		}
	}
	set_myid(MYID_SPECIFIED, getenv("IPSECmyid"));
	set_myid(MYID_IP, getenv("defaultrouteaddr"));
	set_myFQDN();
}

/**
 *  Free myid module
 */
void free_myid(void)
{
	enum myid_state s;

	for (s = MYID_UNKNOWN; s <= MYID_SPECIFIED; s++)
	{
		DESTROY_IF(myids[s]);
	}
}

void set_myid(enum myid_state s, char *idstr)
{
	if (idstr)
	{
		myids[s]->destroy(myids[s]);
		myids[s] = identification_create_from_string(idstr);
		if (s == MYID_SPECIFIED)
		{
				myid_state = MYID_SPECIFIED;
		}
	}
}

void set_myFQDN(void)
{
	char FQDN[HOST_NAME_MAX + 1];
	int r = gethostname(FQDN, sizeof(FQDN));
	size_t len;

	if (r != 0)
	{
		log_errno((e, "gethostname() failed in set_myFQDN"));
	}
	else
	{
		FQDN[sizeof(FQDN) - 1] = '\0';  /* insurance */
		len = strlen(FQDN);

		if (len > 0 && FQDN[len-1] == '.')
		{
			/* nuke trailing . */
			FQDN[len-1] = '\0';
		}
		if (!strcaseeq(FQDN, "localhost.localdomain"))
		{
			myids[MYID_HOSTNAME]->destroy(myids[MYID_HOSTNAME]);
			myids[MYID_HOSTNAME] = identification_create_from_string(FQDN);
		}
	}
}

void show_myid_status(void)
{
	whack_log(RC_COMMENT, "%%myid = '%Y'", myids[myid_state]);
}

/*
 * Local Variables:
 * c-basic-offset:4
 * c-style: pluto
 * End:
 */