]>
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 | ||
302e8c4c | 33 | #if defined(TARGET_FEDORA) |
ea6145da | 34 | #define FILENAME "/etc/sysconfig/network" |
65c8976a | 35 | #elif defined(TARGET_SUSE) || defined(TARGET_SLACKWARE) |
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 | ||
e5907703 | 60 | static int read_distro_hostname(char **hn) { |
302e8c4c | 61 | |
3177a7fa | 62 | #if defined(TARGET_FEDORA) || defined(TARGET_ARCH) || defined(TARGET_GENTOO) |
d7c114c0 DR |
63 | int r; |
64 | FILE *f; | |
65 | ||
66 | assert(hn); | |
67 | ||
ea6145da | 68 | if (!(f = fopen(FILENAME, "re"))) |
d7c114c0 DR |
69 | return -errno; |
70 | ||
71 | for (;;) { | |
72 | char line[LINE_MAX]; | |
73 | char *s, *k; | |
74 | ||
75 | if (!fgets(line, sizeof(line), f)) { | |
76 | if (feof(f)) | |
77 | break; | |
78 | ||
79 | r = -errno; | |
80 | goto finish; | |
81 | } | |
82 | ||
83 | s = strstrip(line); | |
84 | ||
3177a7fa | 85 | if (!startswith_no_case(s, "HOSTNAME=")) |
d7c114c0 DR |
86 | continue; |
87 | ||
88 | if (!(k = strdup(s+9))) { | |
89 | r = -ENOMEM; | |
90 | goto finish; | |
91 | } | |
92 | ||
a06b0b56 LP |
93 | strip_bad_chars(k); |
94 | ||
95 | if (k[0] == 0) { | |
96 | free(k); | |
97 | r = -ENOENT; | |
3177a7fa MAP |
98 | goto finish; |
99 | } | |
100 | ||
d7c114c0 | 101 | *hn = k; |
84b00965 LP |
102 | r = 0; |
103 | goto finish; | |
d7c114c0 DR |
104 | } |
105 | ||
84b00965 | 106 | r = -ENOENT; |
d7c114c0 DR |
107 | |
108 | finish: | |
109 | fclose(f); | |
110 | return r; | |
111 | ||
e5907703 | 112 | #elif defined(TARGET_SUSE) || defined(TARGET_SLACKWARE) |
206bf5c2 KS |
113 | int r; |
114 | char *s, *k; | |
115 | ||
116 | assert(hn); | |
117 | ||
ea6145da | 118 | if ((r = read_one_line_file(FILENAME, &s)) < 0) |
206bf5c2 KS |
119 | return r; |
120 | ||
121 | k = strdup(strstrip(s)); | |
122 | free(s); | |
123 | ||
124 | if (!k) | |
125 | return -ENOMEM; | |
126 | ||
a06b0b56 LP |
127 | strip_bad_chars(k); |
128 | ||
129 | if (k[0] == 0) { | |
130 | free(k); | |
aa411267 | 131 | return -ENOENT; |
a06b0b56 LP |
132 | } |
133 | ||
206bf5c2 | 134 | *hn = k; |
e5907703 | 135 | return 0; |
206bf5c2 | 136 | |
302e8c4c | 137 | #else |
302e8c4c LP |
138 | return -ENOENT; |
139 | #endif | |
e5907703 LP |
140 | } |
141 | ||
142 | static int read_hostname(char **hn) { | |
143 | int r; | |
144 | char *s, *k; | |
145 | ||
146 | assert(hn); | |
147 | ||
148 | /* First, try to load the generic hostname configuration file, | |
149 | * that we support on all distributions */ | |
150 | ||
151 | if ((r = read_one_line_file("/etc/hostname", &s)) < 0) { | |
152 | ||
153 | if (r == -ENOENT) | |
154 | return read_distro_hostname(hn); | |
155 | ||
156 | return r; | |
157 | } | |
158 | ||
159 | k = strdup(strstrip(s)); | |
160 | free(s); | |
161 | ||
162 | if (!k) | |
163 | return -ENOMEM; | |
164 | ||
165 | strip_bad_chars(k); | |
166 | ||
167 | if (k[0] == 0) { | |
168 | free(k); | |
169 | return -ENOENT; | |
170 | } | |
171 | ||
172 | *hn = k; | |
302e8c4c LP |
173 | |
174 | return 0; | |
175 | } | |
176 | ||
177 | int hostname_setup(void) { | |
178 | int r; | |
179 | char *hn; | |
180 | ||
181 | if ((r = read_hostname(&hn)) < 0) { | |
182 | if (r != -ENOENT) | |
183 | log_warning("Failed to read configured hostname: %s", strerror(-r)); | |
184 | ||
185 | return r; | |
186 | } | |
187 | ||
188 | r = sethostname(hn, strlen(hn)) < 0 ? -errno : 0; | |
189 | ||
190 | if (r < 0) | |
191 | log_warning("Failed to set hostname to <%s>: %s", hn, strerror(-r)); | |
192 | else | |
193 | log_info("Set hostname to <%s>.", hn); | |
194 | ||
195 | free(hn); | |
196 | ||
197 | return r; | |
198 | } |