/*
 * SCREWS - NOUPDIR
 * author: pancake@phreaker.net
 */

#include "modskel.h"

/* STRING IDS */
char *type="exec", *name="noupdir";

/* INIT FUNCTION */
/* XXX Directory traversal */
bool init() { printf("[m] Crosspath protection.\n"); return true; }

/* JUST PLAY A WHILE */
bool exec(opts)
	char **opts;
{
	char *str=0,*p;
	int i,ups=0;

	str=(char *)getenv("FILE");
	if (!str) return false; /* FILE not environed */
	
	/* clean damn chars */
	while( (p=(char *)strstr(str,"...")) ) /* "..." -> ".." */
		strcpy(p,p+1);
	while( (p=(char *)strstr(str,"//")) ) /* "//" -> "/" */
		strcpy(p,p+1);
	
	for(i=1;i<strlen(str);i++)
	{
		if (str[i]=='/')
		{
			switch(str[i-1])
			{
			case '.': /* UPDIR (../) */
				if (!ups)
				{
				i-=3;
				strcpy(str+i,str+i+3);
				} else ups--;
				break;
			default: /* CHDIR */
				ups++;
				break;
			}
		}
	}
	/* remove last updir */
	if ( (p=strstr(str,"..")) )
		strcpy(p,p+3);
	setenv("FILE",str,1);
	return false; /* notendexec */
}
