diff --git a/src/modules/rlm_linelog/rlm_linelog.c b/src/modules/rlm_linelog/rlm_linelog.c
index bfdb516..74fc10b 100644
--- a/src/modules/rlm_linelog/rlm_linelog.c
+++ b/src/modules/rlm_linelog/rlm_linelog.c
@@ -39,6 +39,8 @@ RCSID("$Id$")
 #endif
 #endif
 
+#include <libgen.h>
+
 /*
  *	Define a structure for our module configuration.
  */
@@ -191,6 +193,7 @@ static int do_linelog(void *instance, REQUEST *request)
 {
 	int fd = -1;
 	char buffer[4096];
+	char *p;
 	char line[1024];
 	rlm_linelog_t *inst = (rlm_linelog_t*) instance;
 	const char *value = inst->line;
@@ -239,7 +242,16 @@ static int do_linelog(void *instance, REQUEST *request)
 	if (strcmp(inst->filename, "syslog") != 0) {
 		radius_xlat(buffer, sizeof(buffer), inst->filename, request,
 			    NULL);
-		
+		/* check path and eventually create subdirs */
+		p = strrchr(buffer,'/');
+		if (p) {
+			*p = '\0';
+			if (rad_mkdir(buffer, 0700) < 0) {
+				radlog_request(L_ERR, 0, request, "rlm_linelog: Failed to create directory %s: %s", buffer, strerror(errno));
+				return RLM_MODULE_FAIL;
+			}
+		}
+		*p = '/';
 		fd = open(buffer, O_WRONLY | O_APPEND | O_CREAT, 0600);
 		if (fd == -1) {
 			radlog(L_ERR, "rlm_linelog: Failed to open %s: %s",
