1 | /* Copyright (C) 1995-2018 Free Software Foundation, Inc. |
2 | This file is part of the GNU C Library. |
3 | |
4 | The GNU C Library is free software; you can redistribute it and/or |
5 | modify it under the terms of the GNU Lesser General Public |
6 | License as published by the Free Software Foundation; either |
7 | version 2.1 of the License, or (at your option) any later version. |
8 | |
9 | The GNU C Library is distributed in the hope that it will be useful, |
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
12 | Lesser General Public License for more details. |
13 | |
14 | You should have received a copy of the GNU Lesser General Public |
15 | License along with the GNU C Library; if not, see |
16 | <http://www.gnu.org/licenses/>. */ |
17 | |
18 | #include <alloca.h> |
19 | #include <errno.h> |
20 | #include <fcntl.h> |
21 | #include <unistd.h> |
22 | #include <netdb.h> |
23 | #include <not-cancel.h> |
24 | #include <stdbool.h> |
25 | |
26 | #define HOSTIDFILE "/etc/hostid" |
27 | |
28 | #ifdef SET_PROCEDURE |
29 | int |
30 | sethostid (long int id) |
31 | { |
32 | int fd; |
33 | ssize_t written; |
34 | int32_t id32 = id; |
35 | |
36 | /* Test for appropriate rights to set host ID. */ |
37 | if (__libc_enable_secure) |
38 | { |
39 | __set_errno (EPERM); |
40 | return -1; |
41 | } |
42 | |
43 | /* Make sure the ID is not too large. Needed for bi-arch support. */ |
44 | if (id32 != id) |
45 | { |
46 | __set_errno (EOVERFLOW); |
47 | return -1; |
48 | } |
49 | |
50 | /* Open file for writing. Everybody is allowed to read this file. */ |
51 | fd = __open_nocancel (HOSTIDFILE, O_CREAT|O_WRONLY|O_TRUNC, 0644); |
52 | if (fd < 0) |
53 | return -1; |
54 | |
55 | written = __write_nocancel (fd, &id32, sizeof (id32)); |
56 | |
57 | __close_nocancel_nostatus (fd); |
58 | |
59 | return written != sizeof (id32) ? -1 : 0; |
60 | } |
61 | |
62 | #else |
63 | # include <string.h> |
64 | # include <sys/param.h> |
65 | # include <resolv/netdb.h> |
66 | # include <netinet/in.h> |
67 | # include <scratch_buffer.h> |
68 | |
69 | long int |
70 | gethostid (void) |
71 | { |
72 | char hostname[MAXHOSTNAMELEN + 1]; |
73 | struct hostent hostbuf, *hp; |
74 | int32_t id; |
75 | struct in_addr in; |
76 | int herr; |
77 | int fd; |
78 | |
79 | /* First try to get the ID from a former invocation of sethostid. */ |
80 | fd = __open_nocancel (HOSTIDFILE, O_RDONLY|O_LARGEFILE, 0); |
81 | if (fd >= 0) |
82 | { |
83 | ssize_t n = __read_nocancel (fd, &id, sizeof (id)); |
84 | |
85 | __close_nocancel_nostatus (fd); |
86 | |
87 | if (n == sizeof (id)) |
88 | return id; |
89 | } |
90 | |
91 | /* Getting from the file was not successful. An intelligent guess |
92 | for a unique number of a host is its IP address. To get the IP |
93 | address we need to know the host name. */ |
94 | if (__gethostname (hostname, MAXHOSTNAMELEN) < 0 || hostname[0] == '\0') |
95 | /* This also fails. Return and arbitrary value. */ |
96 | return 0; |
97 | |
98 | /* Determine the IP address of the host name. */ |
99 | struct scratch_buffer tmpbuf; |
100 | scratch_buffer_init (&tmpbuf); |
101 | while (true) |
102 | { |
103 | int ret = __gethostbyname_r (hostname, &hostbuf, |
104 | tmpbuf.data, tmpbuf.length, &hp, &herr); |
105 | if (ret == 0) |
106 | break; |
107 | else |
108 | { |
109 | /* Enlarge the buffer on ERANGE. */ |
110 | if (herr == NETDB_INTERNAL && errno == ERANGE) |
111 | { |
112 | if (!scratch_buffer_grow (&tmpbuf)) |
113 | return 0; |
114 | } |
115 | /* Other errors are a failure. Return an arbitrary value. */ |
116 | else |
117 | { |
118 | scratch_buffer_free (&tmpbuf); |
119 | return 0; |
120 | } |
121 | } |
122 | } |
123 | |
124 | in.s_addr = 0; |
125 | memcpy (&in, hp->h_addr, |
126 | (int) sizeof (in) < hp->h_length ? (int) sizeof (in) : hp->h_length); |
127 | scratch_buffer_free (&tmpbuf); |
128 | /* For the return value to be not exactly the IP address we do some |
129 | bit fiddling. */ |
130 | return (int32_t) (in.s_addr << 16 | in.s_addr >> 16); |
131 | } |
132 | #endif |
133 | |