summaryrefslogtreecommitdiffstats
path: root/loader/urlinstall.c
blob: f3713d02b5ac6d64613a1903c3dc64eace665a09 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
/*
 * urlinstall.c - code to set up url (ftp/http) installs
 *
 * Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003  Red Hat, Inc.
 * All rights reserved.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * Author(s): Erik Troan <ewt@redhat.com>
 *            Matt Wilson <msw@redhat.com>
 *            Michael Fulbright <msf@redhat.com>
 *            Jeremy Katz <katzj@redhat.com>
 */

#include <newt.h>
#include <stdlib.h>
#include <string.h>
#include <sys/mount.h>
#include <unistd.h>
#include <errno.h>
#include <glib.h>

#include "../pyanaconda/isys/iface.h"
#include "../pyanaconda/isys/imount.h"
#include "../pyanaconda/isys/log.h"

#include "copy.h"
#include "kickstart.h"
#include "loader.h"
#include "loadermisc.h"
#include "lang.h"
#include "method.h"
#include "net.h"
#include "method.h"
#include "urlinstall.h"
#include "cdinstall.h"
#include "urls.h"
#include "windows.h"
#include "unpack.h"

/* boot flags */
extern uint64_t flags;

char **extraHeaders = NULL;

static char **headers() {
    int len = 2;

    /* The list of HTTP headers is unlikely to change, unless a new ethernet
     * device suddenly shows up since last time we downloaded a file.  So,
     * cache the result here to save some time.
     */
    if (extraHeaders != NULL)
        return extraHeaders;

    if ((extraHeaders = realloc(extraHeaders, 2*sizeof(char *))) == NULL) {
        logMessage(CRITICAL, "%s: %d: %m", __func__, __LINE__);
        abort();
    }
    
    checked_asprintf(&extraHeaders[0], "X-Anaconda-Architecture: %s", getProductArch());
    checked_asprintf(&extraHeaders[1], "X-Anaconda-System-Release: %s", getProductName());

    if (FL_KICKSTART_SEND_MAC(flags)) {
        /* find all ethernet devices and make a header entry for each one */
        int i;
        char *dev, *mac;
        struct device **devices;

        devices = getDevices(DEVICE_NETWORK);
        for (i = 0; devices && devices[i]; i++) {
            dev = devices[i]->device;
            mac = iface_mac2str(dev);

            if (mac) {
                extraHeaders = realloc(extraHeaders, (len+1)*sizeof(char *));
                checked_asprintf(&extraHeaders[len], "X-RHN-Provisioning-MAC-%d: %s %s",
                                 i, dev, mac);

                len++;
                free(mac);
            }
        }
    }

    if (FL_KICKSTART_SEND_SERIAL(flags) && !access("/usr/sbin/dmidecode", X_OK)) {
        FILE *f;
        char sn[1024];
        size_t sn_len;

        if ((f = popen("/usr/sbin/dmidecode -s system-serial-number", "r")) == NULL) {
            logMessage(CRITICAL, "%s: %d: %m", __func__, __LINE__);
            abort();
        }

        sn_len = fread(sn, sizeof(char), 1023, f);
        if (ferror(f)) {
            logMessage(CRITICAL, "%s: %d: %m", __func__, __LINE__);
            abort();
        }

        sn[sn_len] = '\0';
        pclose(f);

        extraHeaders = realloc(extraHeaders, (len+1)*sizeof(char *));

        checked_asprintf(&extraHeaders[len], "X-System-Serial-Number: %s", sn);

        len++;
    }

    extraHeaders = realloc(extraHeaders, (len+1)*sizeof(char *));
    extraHeaders[len] = NULL;
    return extraHeaders;
}

static int loadSingleUrlImage(struct loaderData_s *loaderData, const char *src,
                              char *dest, char *mntpoint, int silentErrors) {
    char **ehdrs = NULL;
    int status;

    if (!strncmp(src, "http", 4))
        ehdrs = headers();

    status = urlinstTransfer(loaderData, src, ehdrs, dest);
    if (status) {
        if (!silentErrors) {
            newtWinMessage(_("Error"), _("OK"),
                           _("Unable to retrieve %s."), src);
        }

        return 2;
    }

    if (dest != NULL) {
        if (doPwMount(dest, mntpoint, "auto", "ro", NULL)) {
            logMessage(ERROR, "Error mounting %s: %m", dest);
            return 1;
        }
    }

    return 0;
}

static void copyWarnFn (char *msg) {
   logMessage(WARNING, msg);
}

static void copyErrorFn (char *msg) {
   newtWinMessage(_("Error"), _("OK"), _(msg));
}

int loadUrlImages(struct loaderData_s *loaderData) {
    char *url;

    logMessage(DEBUGLVL, "looking for extras for HTTP/FTP install");

    if (!loaderData->instRepo)
        return 0;

    checked_asprintf(&url, "%s/images/%s", loaderData->instRepo, "updates.img");

    if (!loadSingleUrlImage(loaderData, url, "/tmp/updates-disk.img", "/mnt/install/update-disk", 1)) {
        copyDirectory("/mnt/install/update-disk", "/tmp/updates", copyWarnFn,
                      copyErrorFn);
        umount("/mnt/install/update-disk");
        unlink("/tmp/updates-disk.img");
        unlink("/mnt/install/update-disk");
    } else if (!access("/tmp/updates-disk.img", R_OK)) {
        unpack_archive_file("/tmp/updates-disk.img", "/tmp/updates");
        unlink("/tmp/updates-disk.img");
    }

    free(url);

    checked_asprintf(&url, "%s/images/%s", loaderData->instRepo, "product.img");

    if (!loadSingleUrlImage(loaderData, url, "/tmp/product-disk.img", "/mnt/install/product-disk", 1)) {
        copyDirectory("/mnt/install/product-disk", "/tmp/product", copyWarnFn,
                      copyErrorFn);
        umount("/mnt/install/product-disk");
        unlink("/tmp/product-disk.img");
        unlink("/mnt/install/product-disk");
    }

    free(url);
    return 0;
}

int promptForUrl(struct loaderData_s *loaderData) {
    char *url;

    do {
        if (urlMainSetupPanel(loaderData) == LOADER_BACK) {
            loaderData->instRepo = NULL;
            return LOADER_BACK;
        }

        checked_asprintf(&url, "%s/.treeinfo", loaderData->instRepo);

        if (getFileFromUrl(url, "/tmp/.treeinfo", loaderData)) {
            newtWinMessage(_("Error"), _("OK"),
                           _("The URL provided does not contain an installable tree."));
            free(url);
            continue;
        }

        free(url);
        break;
    } while (1);

    return LOADER_OK;
}

int getFileFromUrl(char * url, char * dest, 
                   struct loaderData_s * loaderData) {
    char **ehdrs = NULL;
    int rc;
    iface_t iface;

    iface_init_iface_t(&iface);

    if (kickstartNetworkUp(loaderData, &iface)) {
        logMessage(ERROR, "unable to bring up network");
        return 1;
    }

    logMessage(INFO, "file location: %s", url);

    if (!strncmp(url, "http", 4)) {
        ehdrs = headers();
    }

    rc = urlinstTransfer(loaderData, url, ehdrs, dest);
    if (rc) {
        logMessage(ERROR, "failed to retrieve %s", url);
        return 1;
    }

    return 0;
}

/* pull kickstart configuration file via http */
int kickstartFromUrl(char * url, struct loaderData_s * loaderData) {
    return getFileFromUrl(url, "/tmp/ks.cfg", loaderData);
}

/* vim:set shiftwidth=4 softtabstop=4: */