Ejemplo n.º 1
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;
}
Ejemplo n.º 2
0
Archivo: ocsp.c Proyecto: kush789/wget2
static int _ocsp_db_load(wget_ocsp_db_t *ocsp_db, FILE *fp, int load_hosts)
{
	wget_ocsp_t ocsp;
	char *buf = NULL, *linep, *p;
	size_t bufsize = 0;
	ssize_t buflen;
	time_t now = time(NULL);
	int ok;

	while ((buflen = wget_getline(&buf, &bufsize, fp)) >= 0) {
		linep = buf;

		while (isspace(*linep)) linep++; // ignore leading whitespace
		if (!*linep) continue; // skip empty lines

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

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

		wget_ocsp_init(&ocsp);
		ok = 0;

		// parse cert's sha-256 checksum
		if (*linep) {
			for (p = linep; *linep && !isspace(*linep);) linep++;
			ocsp.key = wget_strmemdup(p, linep - p);
		}

		// parse max age
		if (*linep) {
			for (p = ++linep; *linep && !isspace(*linep);) linep++;
			ocsp.maxage = atol(p);
			if (ocsp.maxage < now) {
				// drop expired entry
				wget_ocsp_deinit(&ocsp);
				continue;
			}
			ok = 1;
		}

		// parse mtime (age of this entry)
		if (*linep) {
			for (p = ++linep; *linep && !isspace(*linep);) linep++;
			ocsp.mtime = atol(p);
		}

		// parse mtime (age of this entry)
		if (*linep) {
			for (p = ++linep; *linep && !isspace(*linep);) linep++;
			ocsp.valid = atoi(p);
		}

		if (ok) {
			if (load_hosts)
				wget_ocsp_db_add_host(ocsp_db, wget_memdup(&ocsp, sizeof(ocsp)));
			else
				wget_ocsp_db_add_fingerprint(ocsp_db, wget_memdup(&ocsp, sizeof(ocsp)));
		} else {
			wget_ocsp_deinit(&ocsp);
			error_printf(_("Failed to parse OCSP line: '%s'\n"), buf);
		}
	}

	xfree(buf);

	if (ferror(fp))
		return -1;

	return 0;
}