Beispiel #1
0
static int _ocsp_db_save_hosts(void *ocsp_db, FILE *fp)
{
	wget_hashmap_t *map = ((wget_ocsp_db_t *)ocsp_db)->hosts;

	if ((wget_hashmap_size(map)) > 0) {
		fputs("#OCSP 1.0 host file\n", fp);
		fputs("#Generated by Wget " PACKAGE_VERSION ". Edit at your own risk.\n", fp);
		fputs("<hostname> <time_t maxage> <time_t mtime>\n\n", fp);
		wget_hashmap_browse(map, (int(*)(void *, const void *, void *))_ocsp_save_host, fp);

		if (ferror(fp))
			return -1;
	}

	return 0;
}
Beispiel #2
0
int wget_netrc_db_load(wget_netrc_db_t *netrc_db, const char *fname)
{
	wget_netrc_t netrc;
	FILE *fp;
	char *buf = NULL, *linep, *p, *key = NULL;
	size_t bufsize = 0;
	ssize_t buflen;
	int nentries = 0, in_macdef = 0, in_machine = 0;

	if (!netrc_db || !fname || !*fname)
		return -1;

	if ((fp = fopen(fname, "r"))) {
		while ((buflen = wget_getline(&buf, &bufsize, fp)) >= 0) {
			linep = buf;

			while (isspace(*linep)) linep++; // ignore leading whitespace

			if (*linep == '#')
				continue; // skip comments

			// strip off \r\n
			while (buflen > 0 && (buf[buflen] == '\n' || buf[buflen] == '\r'))
				buf[--buflen] = 0;

			if (!*linep) {
				// empty lines reset macro processing
				in_macdef = 0;
				continue;
			} else if (in_macdef)
				continue; // still processing 'macdef' macro

			// now we expect key value pairs, e.g.: machine example.com
			xfree(key);
			for (p = linep; *linep && !isspace(*linep);) linep++;
			key = wget_strmemdup(p, linep - p);

			if (!strcmp(key, "machine") || !strcmp(key, "default")) {
				if (in_machine)
					wget_netrc_db_add(netrc_db, wget_memdup(&netrc, sizeof(netrc)));

				wget_netrc_init(&netrc);
				in_machine = 1;

				if (!strcmp(key, "default")) {
					netrc.key = wget_strdup("default");
					continue;
				}
			} else if (!in_machine)
				continue; // token outside of machine or default

			while (isspace(*linep)) linep++;
			for (p = linep; *linep && !isspace(*linep);) linep++;

			if (!strcmp(key, "login")) {
				if (!netrc.login)
					netrc.login = wget_strmemdup(p, linep - p);
			} else if (!strcmp(key, "password")) {
				if (!netrc.password)
					netrc.password = wget_strmemdup(p, linep - p);
			} else if (!strcmp(key, "macdef")) {
				in_macdef = 1; // the above code skips until next empty line
			}
		}

		if (in_machine)
			wget_netrc_db_add(netrc_db, wget_memdup(&netrc, sizeof(netrc)));

		xfree(key);
		xfree(buf);
		fclose(fp);

		nentries = wget_hashmap_size(netrc_db->machines);

		debug_printf("loaded %d .netrc %s\n", nentries, nentries != 1 ? "entries" : "entry");
	} else if (errno != ENOENT)
		error_printf(_("Failed to open .netrc file '%s' (%d)\n"), fname, errno);

	return nentries;
}