$NetBSD: patch-au,v 1.1 2001/05/01 09:46:37 skrll Exp $

--- kdesu/kdesu_stub.c.orig	Sun Feb 18 15:26:55 2001
+++ kdesu/kdesu_stub.c
@@ -173,6 +173,7 @@
     return list;
 }
 
+#define BUFSIZE	8192
 
 /**
  * The main program
@@ -180,9 +181,9 @@
 
 int main()
 {
-    char buf[1024];
-    char command[200], xauthority[200], iceauthority[200];
-    char **host, **auth, *fname;
+    char buf[BUFSIZE+1];
+    char xauthority[200], iceauthority[200];
+    char **host, **auth;
     int i/*, res, sycoca*/, prio;
     pid_t pid;
     FILE *fout;
@@ -194,7 +195,7 @@
     {
 	printf("%s\n", params[i].name);
 	fflush(stdout);
-	if (fgets(buf, 500, stdin) == 0L) 
+	if (fgets(buf, BUFSIZE, stdin) == 0L) 
 	{
 	    printf("end\n"); fflush(stdout);
 	    perror("kdesu_stub: fgets()");
@@ -276,22 +277,34 @@
 	xsetenv("DISPLAY", params[P_DISPLAY].value);
 	if (params[P_DISPLAY_AUTH].value[0]) 
 	{
-	    fname = tmpnam(0L);
-	    fout = fopen(fname, "w");
-	    if (!fout) 
-	    {
-		perror("kdesu_stub: fopen()");
+           int	fd2;
+           /*
+           ** save umask and set to 077, so we create those files only
+	   ** readable for root. (if someone else could read them, we
+	   ** are in deep shit).
+           */
+	   int oldumask = umask(077);
+
+           strcpy(xauthority, "/tmp/xauth.XXXXXXXXXX");
+	   fd2 = mkstemp(xauthority);
+	   umask(oldumask);
+
+           if (fd2 == -1) {
+                perror("kdesu_stub: mkstemp()");
+                exit(1);
+           } else
+                close(fd2);
+           xsetenv("XAUTHORITY", xauthority);
+
+	   fout = popen("xauth >/dev/null 2>&1","w");
+           if (fout == NULL)
+	   {
+		perror("kdesu_stub: popen(xauth)");
 		exit(1);
-	    }
-	    fprintf(fout, "add %s %s\n", params[P_DISPLAY].value,
+	   }
+	   fprintf(fout, "add %s %s\n", params[P_DISPLAY].value,
 		    params[P_DISPLAY_AUTH].value);
-	    fclose(fout);
-	    tmpnam(xauthority);
-	    xsetenv("XAUTHORITY", xauthority);
-	    sprintf(command, "xauth source %s >/dev/null 2>&1", fname);
-	    if (system(command))
-		printf("kdesu_stub: failed to add X authentication");
-	    unlink(fname);
+	   pclose(fout);
 	}
     }
 
@@ -305,11 +318,22 @@
 	auth = xstrsep(params[P_ICE_AUTH].value);
 	if (host[0]) 
 	{
-	    fname = tmpnam(0L);
-	    fout = fopen(fname, "w");
-	    if (!fout) 
-	    {
-		perror("kdesu_stub: fopen()");
+            int fd;
+	    int	oldumask = umask(077);
+
+            strcpy(iceauthority, "/tmp/iceauth.XXXXXXXXXX");
+            fd = mkstemp(iceauthority);
+	    umask(oldumask);
+            if (fd == -1) {
+                perror("kdesu_stub: mkstemp()");
+                exit(1);
+            } else
+                close(fd);
+	    xsetenv("ICEAUTHORITY", iceauthority);
+
+	    fout = popen("iceauth >/dev/null 2>&1", "w");
+	    if (!fout) {
+		perror("kdesu_stub: popen iceauth");
 		exit(1);
 	    }
 	    for (i=0; host[i]; i++)
@@ -317,13 +341,7 @@
 	    auth = xstrsep(params[P_DCOP_AUTH].value);
 	    for (i=0; host[i]; i++)
 		fprintf(fout, "add DCOP \"\" %s %s\n", host[i], auth[i]);
-	    fclose(fout);
-	    tmpnam(iceauthority);
-	    xsetenv("ICEAUTHORITY", iceauthority);
-	    sprintf(command, "iceauth source %s >/dev/null 2>&1", fname);
-	    if (system(command))
-		printf("kdesu_stub: failed to add DCOP authentication\n");
-	    unlink(fname);
+	    pclose(fout);
 	}
     }
 
