Beispiel #1
0
static int setACL(Connection *Conn,int add,PCStr(aclID),PCStr(user),PCStr(url),FILE *cfp,FILE *tc,void *env,sFUNCP ckfunc)
{	CStr(ctl,2048);
	CStr(com,2048);
	CStr(arg,2048);
	CStr(ac1,2048);
	CStr(Ver,256);
	CStr(AdmClass,128);
	CStr(Base,1024);
	int len;
	int errors = 0;
	int nl,cnl;
	FILE *acl;
	CStr(admid,2048);
	const char *xp;
	CStr(xmbox,128);
	int off;
	const char *cp;
	CStr(stime,64);
	const char *err;
	int bytes;

	cnl = 0;
	bytes = 0;
	Ver[0] = 0;
	AdmClass[0] = 0;
	Base[0] = 0;
	StrftimeLocal(AVStr(stime),sizeof(stime),"%Y/%m/%d-%H:%M:%S",time(0),0);

	sprintf(admid,"<%s>",aclID);

	for( nl = 1; fgets(ctl,sizeof(ctl),cfp) != NULL; nl++ ){
		bytes += strlen(ctl);
		if( ACL_MAXBYTES < bytes ){
fprintf(tc,"[WARNING] your ACL should be smaller than %d bytes.\r\n",
ACL_MAXBYTES);
			fprintf(tc,"[WARNING] remaining list is ignored.\r\n");
			errors++;
			break;
		}

		if( cp = strchr(ctl,'#') )
			truncVStr(cp);
		if( ctl[0] == 0 )
			continue;

		com[0] = arg[0] = 0;
		Xsscanf(ctl,"%[^: \t\r\n]%*[: \t\r\n]%[^\r\n]",AVStr(com),AVStr(arg));
		if( com[0] == 0 )
			continue;

		if( strcaseeq(com,"ACL-Version") ){
			strcpy(Ver,arg);
			continue;
		}
		if( strcaseeq(com,"Admin-Class") ){
			if( !strcaseeq(arg,"anonymous") ){
				fprintf(tc,"[FATAL: unknown Admin-Class] %s\r\n",arg);
				return -1;
			}
			strcpy(AdmClass,arg);
			continue;
		}
		if( strcaseeq(com,"Base-URL") ){
			CStr(proto,128);
			CStr(login,128);
			CStr(path,128);
			CStr(clogin,128);

			decomp_absurl(arg,AVStr(proto),AVStr(login),AVStr(path),sizeof(path));
			HostPort(AVStr(clogin),DST_PROTO,DST_HOST,DST_PORT);
			if( strcmp(proto,DST_PROTO) || hostcmp(login,clogin) ){

fprintf(tc,"[FATAL: Base-URL arrogation] <%s> should be <%s://%s>\r\n",
arg,DST_PROTO,clogin);

				return -1;
			}
			strcpy(Base,arg);
			continue;
		}

		if( Ver[0] == 0 ){
			fprintf(tc,"[FATAL: no ACL-Version specified]\r\n");
			return -1;
		}
		if( AdmClass[0] == 0 ){
			strcpy(AdmClass,"anonymous");
			fprintf(tc,"[WARNING: no Admin-Class specified] assumed %s\r\n",
				AdmClass);
		}
		if( ACL_MAXLINES < nl ){
fprintf(tc,"[WARNING] your ACL should be less than %d lines.\r\n",
ACL_MAXLINES);
			fprintf(tc,"[WARNING] remaining list is ignored.\r\n");
			errors++;
			break;
		}

		if( strcaseeq(com,"On") || strcaseeq(com,"Off") ){
			++cnl;

			if( add && ACLMAX < cnl ){
fprintf(tc,"[WARNING] you cannot control ACLs for more than %d newsgroups.\r\n",
ACLMAX);
				fprintf(tc,"[WARNING] remaining list is ignored.\r\n");
				errors++;
				break;
			}
			if( add && (err = (*ckfunc)(env,Base,arg)) ){
				fprintf(tc,"[ERROR] %d:[%s] %s: %s\r\n",
					nl,err,com,arg);
				errors++;
				continue;
			}

			acl = openAclFile(1,DST_PROTO,DST_HOST,DST_PORT,arg);
			fseek(acl,0,0);
			for(;;){
				off = ftell(acl);
				if( fgets(ac1,sizeof(ac1),acl) == NULL )
					break;
				if( xp = strstr(ac1,admid) ){
					if( add )
					fprintf(tc,"[UPDATE] %d: %s: %s\r\n",nl,com,arg);
					fseek(acl,off,0);
					break;
				}
			}
			if( add ){
				fprintf(acl,"%s %-10s %-10s %-10s %s\r\n",
					stime,com,user,AdmClass,admid);
			}else{
				int fsize,rcc;
				const char *buff;

				fsize = file_size(fileno(acl));
				buff = (char*)malloc(fsize);
				rcc = fread((char*)buff,1,fsize,acl);
				fseek(acl,off,0);
				fwrite(buff,1,rcc,acl);
				free((char*)buff);
				Ftruncate(acl,off,0);
			}
			fclose(acl);
			continue;
		}
	}
	return errors;
}
Beispiel #2
0
int build_request(struct connection *conn)
{
	char *url, *host, *p;

	url = is_http(conn->url);
	if (!url) {
#ifdef WANT_SSL
		printf("Only http/https supported\n");
#else
		printf("Only http supported\n");
#endif
		return 1;
	}

	p = strchr(url, '/');
	if (p) {
		*p = '\0';
		host = strdup(url);
		*p = '/';
		url = p;
	} else {
		host = strdup(url);
		url = "/";
	}

	if (!host) {
		printf("Out of memory\n");
		return 1;
	}

#ifdef REUSE_SOCKET
	if (CONN_OPEN) {
		if (hostcmp(conn->host, host)) {
			if (verbose)
				printf("New connection for %s\n", host);
			release_connection(conn);
		} else if (verbose)
			printf("Reuse connection for %s\n", conn->host);
	}
#endif

	if (!get_buf(conn)) {
		free(host);
		return 1;
	}

	if (!CONN_OPEN)
		if (open_socket(conn, host)) {
			printf("Connection failed to %s\n", host);
			free(host);
			free_buf(conn);
			return 1;
		}

	if (proxy)
		snprintf(conn->buf, BUFSIZE, "%s http://%s/%s %s\r\n",
				method, host, url, http);
	else if (strchr(url, ' ')) {
		/* Some sites cannot handle spaces in the url. */
		int n = sprintf(conn->buf, "%s ", method);
		char *in = url, *out = conn->buf + n;
		while (*in)
			if (*in == ' ') {
				*out++ = '%';
				*out++ = '2';
				*out++ = '0';
				++in;
			} else
				*out++ = *in++;
		sprintf(out, " %s\r\n", http);
	} else
		snprintf(conn->buf, BUFSIZE, "%s %s %s\r\n", method, url, http);

	add_full_header(conn, host);

	free(host);

	if (verbose > 1)
		printf("%s %s", proxy ? ">P" : ">", conn->buf);

	if (conn->referer)
		sprintf(conn->buf + strlen(conn->buf),
			"Referer: %.200s\r\n", conn->referer);

	strcat(conn->buf, "\r\n");

	conn->curp = conn->buf;
	conn->length = strlen(conn->buf);

	return 0;
}