]>
Commit | Line | Data |
---|---|---|
d6c9574f | 1 | /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/ |
302e8c4c LP |
2 | |
3 | /*** | |
4 | This file is part of systemd. | |
5 | ||
6 | Copyright 2010 Lennart Poettering | |
7 | ||
8 | systemd is free software; you can redistribute it and/or modify it | |
9 | under the terms of the GNU General Public License as published by | |
10 | the Free Software Foundation; either version 2 of the License, or | |
11 | (at your option) any later version. | |
12 | ||
13 | systemd is distributed in the hope that it will be useful, but | |
14 | WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
16 | General Public License for more details. | |
17 | ||
18 | You should have received a copy of the GNU General Public License | |
19 | along with systemd; If not, see <http://www.gnu.org/licenses/>. | |
20 | ***/ | |
21 | ||
22 | #include <unistd.h> | |
23 | #include <stdio.h> | |
24 | #include <errno.h> | |
25 | #include <string.h> | |
26 | #include <stdlib.h> | |
27 | ||
28 | #include "hostname-setup.h" | |
29 | #include "macro.h" | |
30 | #include "util.h" | |
31 | #include "log.h" | |
32 | ||
a338bab5 | 33 | #if defined(TARGET_FEDORA) || defined(TARGET_ALTLINUX) |
ea6145da | 34 | #define FILENAME "/etc/sysconfig/network" |
f5c88ec1 | 35 | #elif defined(TARGET_SUSE) || defined(TARGET_SLACKWARE) || defined(TARGET_FRUGALWARE) |
ea6145da | 36 | #define FILENAME "/etc/HOSTNAME" |
ea6145da LP |
37 | #elif defined(TARGET_ARCH) |
38 | #define FILENAME "/etc/rc.conf" | |
3177a7fa MAP |
39 | #elif defined(TARGET_GENTOO) |
40 | #define FILENAME "/etc/conf.d/hostname" | |
ea6145da | 41 | #endif |
302e8c4c | 42 | |
a06b0b56 LP |
43 | static char* strip_bad_chars(char *s) { |
44 | char *p, *d; | |
45 | ||
46 | for (p = s, d = s; *p; p++) | |
47 | if ((*p >= 'a' && *p <= 'z') || | |
48 | (*p >= 'A' && *p <= 'Z') || | |
49 | (*p >= '0' && *p <= '9') || | |
50 | *p == '-' || | |
10a49d70 LP |
51 | *p == '_' || |
52 | *p == '.') | |
a06b0b56 LP |
53 | *(d++) = *p; |
54 | ||
55 | *d = 0; | |
56 | ||
57 | return s; | |
58 | } | |
59 | ||
28695e0f LP |
60 | static int read_and_strip_hostname(const char *path, char **hn) { |
61 | char *s, *k; | |
62 | int r; | |
63 | ||
64 | assert(path); | |
65 | assert(hn); | |
66 | ||
67 | if ((r = read_one_line_file(path, &s)) < 0) | |
68 | return r; | |
69 | ||
70 | k = strdup(strstrip(s)); | |
71 | free(s); | |
72 | ||
73 | if (!k) | |
74 | return -ENOMEM; | |
75 | ||
76 | strip_bad_chars(k); | |
77 | ||
78 | if (k[0] == 0) { | |
79 | free(k); | |
80 | return -ENOENT; | |
81 | } | |
82 | ||
83 | *hn = k; | |
84 | ||
85 | return 0; | |
86 | } | |
87 | ||
e5907703 | 88 | static int read_distro_hostname(char **hn) { |
302e8c4c | 89 | |
a338bab5 | 90 | #if defined(TARGET_FEDORA) || defined(TARGET_ARCH) || defined(TARGET_GENTOO) || defined(TARGET_ALTLINUX) |
d7c114c0 DR |
91 | int r; |
92 | FILE *f; | |
93 | ||
94 | assert(hn); | |
95 | ||
ea6145da | 96 | if (!(f = fopen(FILENAME, "re"))) |
d7c114c0 DR |
97 | return -errno; |
98 | ||
99 | for (;;) { | |
100 | char line[LINE_MAX]; | |
101 | char *s, *k; | |
102 | ||
103 | if (!fgets(line, sizeof(line), f)) { | |
104 | if (feof(f)) | |
105 | break; | |
106 | ||
107 | r = -errno; | |
108 | goto finish; | |
109 | } | |
110 | ||
111 | s = strstrip(line); | |
112 | ||
3177a7fa | 113 | if (!startswith_no_case(s, "HOSTNAME=")) |
d7c114c0 DR |
114 | continue; |
115 | ||
116 | if (!(k = strdup(s+9))) { | |
117 | r = -ENOMEM; | |
118 | goto finish; | |
119 | } | |
120 | ||
a06b0b56 LP |
121 | strip_bad_chars(k); |
122 | ||
123 | if (k[0] == 0) { | |
124 | free(k); | |
125 | r = -ENOENT; | |
3177a7fa MAP |
126 | goto finish; |
127 | } | |
128 | ||
d7c114c0 | 129 | *hn = k; |
84b00965 LP |
130 | r = 0; |
131 | goto finish; | |
d7c114c0 DR |
132 | } |
133 | ||
84b00965 | 134 | r = -ENOENT; |
d7c114c0 DR |
135 | |
136 | finish: | |
137 | fclose(f); | |
138 | return r; | |
139 | ||
f5c88ec1 | 140 | #elif defined(TARGET_SUSE) || defined(TARGET_SLACKWARE) || defined(TARGET_FRUGALWARE) |
28695e0f | 141 | return read_and_strip_hostname(FILENAME, hn); |
302e8c4c | 142 | #else |
302e8c4c LP |
143 | return -ENOENT; |
144 | #endif | |
e5907703 LP |
145 | } |
146 | ||
147 | static int read_hostname(char **hn) { | |
148 | int r; | |
e5907703 LP |
149 | |
150 | assert(hn); | |
151 | ||
152 | /* First, try to load the generic hostname configuration file, | |
153 | * that we support on all distributions */ | |
154 | ||
28695e0f | 155 | if ((r = read_and_strip_hostname("/etc/hostname", hn)) < 0) { |
e5907703 LP |
156 | |
157 | if (r == -ENOENT) | |
158 | return read_distro_hostname(hn); | |
159 | ||
160 | return r; | |
161 | } | |
162 | ||
302e8c4c LP |
163 | return 0; |
164 | } | |
165 | ||
166 | int hostname_setup(void) { | |
167 | int r; | |
28695e0f LP |
168 | char *b = NULL; |
169 | const char *hn = NULL; | |
302e8c4c | 170 | |
28695e0f LP |
171 | if ((r = read_hostname(&b)) < 0) { |
172 | if (r == -ENOENT) | |
173 | log_info("No hostname configured."); | |
174 | else | |
302e8c4c LP |
175 | log_warning("Failed to read configured hostname: %s", strerror(-r)); |
176 | ||
28695e0f LP |
177 | hn = "localhost"; |
178 | } else | |
179 | hn = b; | |
302e8c4c | 180 | |
28695e0f LP |
181 | if (sethostname(hn, strlen(hn)) < 0) { |
182 | log_warning("Failed to set hostname to <%s>: %m", hn); | |
183 | r = -errno; | |
184 | } else | |
302e8c4c LP |
185 | log_info("Set hostname to <%s>.", hn); |
186 | ||
28695e0f | 187 | free(b); |
302e8c4c LP |
188 | |
189 | return r; | |
190 | } |