| 1 | /* Copyright (C) 1995-2023 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 | <https://www.gnu.org/licenses/>. */ |
| 17 | |
| 18 | #include <errno.h> |
| 19 | #include <fcntl.h> |
| 20 | #include <unistd.h> |
| 21 | #include <netdb.h> |
| 22 | #include <not-cancel.h> |
| 23 | #include <stdbool.h> |
| 24 | |
| 25 | #define HOSTIDFILE "/etc/hostid" |
| 26 | |
| 27 | #ifdef SET_PROCEDURE |
| 28 | int |
| 29 | sethostid (long int id) |
| 30 | { |
| 31 | int fd; |
| 32 | ssize_t written; |
| 33 | int32_t id32 = id; |
| 34 | |
| 35 | /* Test for appropriate rights to set host ID. */ |
| 36 | if (__libc_enable_secure) |
| 37 | { |
| 38 | __set_errno (EPERM); |
| 39 | return -1; |
| 40 | } |
| 41 | |
| 42 | /* Make sure the ID is not too large. Needed for bi-arch support. */ |
| 43 | if (id32 != id) |
| 44 | { |
| 45 | __set_errno (EOVERFLOW); |
| 46 | return -1; |
| 47 | } |
| 48 | |
| 49 | /* Open file for writing. Everybody is allowed to read this file. */ |
| 50 | fd = __open_nocancel (HOSTIDFILE, O_CREAT|O_WRONLY|O_TRUNC, 0644); |
| 51 | if (fd < 0) |
| 52 | return -1; |
| 53 | |
| 54 | written = __write_nocancel (fd, &id32, sizeof (id32)); |
| 55 | |
| 56 | __close_nocancel_nostatus (fd); |
| 57 | |
| 58 | return written != sizeof (id32) ? -1 : 0; |
| 59 | } |
| 60 | |
| 61 | #else |
| 62 | # include <string.h> |
| 63 | # include <sys/param.h> |
| 64 | # include <resolv/netdb.h> |
| 65 | # include <netinet/in.h> |
| 66 | # include <scratch_buffer.h> |
| 67 | |
| 68 | long int |
| 69 | gethostid (void) |
| 70 | { |
| 71 | char hostname[MAXHOSTNAMELEN + 1]; |
| 72 | struct hostent hostbuf, *hp; |
| 73 | int32_t id; |
| 74 | struct in_addr in; |
| 75 | int herr; |
| 76 | int fd; |
| 77 | |
| 78 | /* First try to get the ID from a former invocation of sethostid. */ |
| 79 | fd = __open_nocancel (HOSTIDFILE, O_RDONLY|O_LARGEFILE, 0); |
| 80 | if (fd >= 0) |
| 81 | { |
| 82 | ssize_t n = __read_nocancel (fd, &id, sizeof (id)); |
| 83 | |
| 84 | __close_nocancel_nostatus (fd); |
| 85 | |
| 86 | if (n == sizeof (id)) |
| 87 | return id; |
| 88 | } |
| 89 | |
| 90 | /* Getting from the file was not successful. An intelligent guess |
| 91 | for a unique number of a host is its IP address. To get the IP |
| 92 | address we need to know the host name. */ |
| 93 | if (__gethostname (hostname, MAXHOSTNAMELEN) < 0 || hostname[0] == '\0') |
| 94 | /* This also fails. Return and arbitrary value. */ |
| 95 | return 0; |
| 96 | |
| 97 | /* Determine the IP address of the host name. */ |
| 98 | struct scratch_buffer tmpbuf; |
| 99 | scratch_buffer_init (&tmpbuf); |
| 100 | while (true) |
| 101 | { |
| 102 | int ret = __gethostbyname_r (hostname, &hostbuf, |
| 103 | tmpbuf.data, tmpbuf.length, &hp, &herr); |
| 104 | if (ret == 0 && hp != NULL) |
| 105 | break; |
| 106 | else |
| 107 | { |
| 108 | /* Enlarge the buffer on ERANGE. */ |
| 109 | if (ret != 0 && herr == NETDB_INTERNAL && errno == ERANGE) |
| 110 | { |
| 111 | if (!scratch_buffer_grow (&tmpbuf)) |
| 112 | return 0; |
| 113 | } |
| 114 | /* Other errors are a failure. Return an arbitrary value. */ |
| 115 | else |
| 116 | { |
| 117 | scratch_buffer_free (&tmpbuf); |
| 118 | return 0; |
| 119 | } |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | in.s_addr = 0; |
| 124 | memcpy (&in, hp->h_addr, |
| 125 | (int) sizeof (in) < hp->h_length ? (int) sizeof (in) : hp->h_length); |
| 126 | scratch_buffer_free (&tmpbuf); |
| 127 | /* For the return value to be not exactly the IP address we do some |
| 128 | bit fiddling. */ |
| 129 | return (int32_t) (in.s_addr << 16 | in.s_addr >> 16); |
| 130 | } |
| 131 | #endif |
| 132 | |