chiark / gitweb /
audit: initialize audit only if it is enabled
[elogind.git] / src / hostname-setup.c
1 /*-*- Mode: C; c-basic-offset: 8 -*-*/
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
33 #if defined(TARGET_FEDORA)
34 #define FILENAME "/etc/sysconfig/network"
35 #elif defined(TARGET_SUSE) || defined(TARGET_SLACKWARE)
36 #define FILENAME "/etc/HOSTNAME"
37 #elif defined(TARGET_DEBIAN)
38 #define FILENAME "/etc/hostname"
39 #elif defined(TARGET_ARCH)
40 #define FILENAME "/etc/rc.conf"
41 #elif defined(TARGET_GENTOO)
42 #define FILENAME "/etc/conf.d/hostname"
43 #endif
44
45 static char* strip_bad_chars(char *s) {
46         char *p, *d;
47
48         for (p = s, d = s; *p; p++)
49                 if ((*p >= 'a' && *p <= 'z') ||
50                     (*p >= 'A' && *p <= 'Z') ||
51                     (*p >= '0' && *p <= '9') ||
52                     *p == '-' ||
53                     *p == '_' ||
54                     *p == '.')
55                         *(d++) = *p;
56
57         *d = 0;
58
59         return s;
60 }
61
62 static int read_hostname(char **hn) {
63
64 #if defined(TARGET_FEDORA) || defined(TARGET_ARCH) || defined(TARGET_GENTOO)
65         int r;
66         FILE *f;
67
68         assert(hn);
69
70         if (!(f = fopen(FILENAME, "re")))
71                 return -errno;
72
73         for (;;) {
74                 char line[LINE_MAX];
75                 char *s, *k;
76
77                 if (!fgets(line, sizeof(line), f)) {
78                         if (feof(f))
79                                 break;
80
81                         r = -errno;
82                         goto finish;
83                 }
84
85                 s = strstrip(line);
86
87                 if (!startswith_no_case(s, "HOSTNAME="))
88                         continue;
89
90                 if (!(k = strdup(s+9))) {
91                         r = -ENOMEM;
92                         goto finish;
93                 }
94
95                 strip_bad_chars(k);
96
97                 if (k[0] == 0) {
98                         free(k);
99                         r = -ENOENT;
100                         goto finish;
101                 }
102
103                 *hn = k;
104                 r = 0;
105                 goto finish;
106         }
107
108         r = -ENOENT;
109
110 finish:
111         fclose(f);
112         return r;
113
114 #elif defined(TARGET_SUSE) || defined(TARGET_DEBIAN) || defined(TARGET_SLACKWARE)
115         int r;
116         char *s, *k;
117
118         assert(hn);
119
120         if ((r = read_one_line_file(FILENAME, &s)) < 0)
121                 return r;
122
123         k = strdup(strstrip(s));
124         free(s);
125
126         if (!k)
127                 return -ENOMEM;
128
129         strip_bad_chars(k);
130
131         if (k[0] == 0) {
132                 free(k);
133                 return -ENOENT;
134         }
135
136         *hn = k;
137
138 #else
139 #warning "Don't know how to read the hostname"
140
141         return -ENOENT;
142 #endif
143
144         return 0;
145 }
146
147 int hostname_setup(void) {
148         int r;
149         char *hn;
150
151         if ((r = read_hostname(&hn)) < 0) {
152                 if (r != -ENOENT)
153                         log_warning("Failed to read configured hostname: %s", strerror(-r));
154
155                 return r;
156         }
157
158         r = sethostname(hn, strlen(hn)) < 0 ? -errno : 0;
159
160         if (r < 0)
161                 log_warning("Failed to set hostname to <%s>: %s", hn, strerror(-r));
162         else
163                 log_info("Set hostname to <%s>.", hn);
164
165         free(hn);
166
167         return r;
168 }