slim: Add SLiM X desktop manager and patches to ensure it is secure and supports...
[openpandora.oe.git] / recipes / slim / slim / xauth_secret_support.patch
1 # From http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=529306
2 # Commited to upstream.
3
4 diff --git a/Makefile b/Makefile
5 index a01bafd..9bd5555 100644
6 --- a/Makefile
7 +++ b/Makefile
8 @@ -7,7 +7,7 @@ CXX=/usr/bin/g++
9  CC=/usr/bin/gcc
10  CFLAGS=-Wall -I. -I/usr/include/freetype2 -I/usr/include/freetype2/config -I/usr/include/libpng12 -I/usr/include
11  CXXFLAGS=$(CFLAGS)
12 -LDFLAGS=-L/usr/X11R6/lib -lXft -lX11 -lpng12 -lm -lXmu -lpng -ljpeg
13 +LDFLAGS=-L/usr/X11R6/lib -lXft -lX11 -lpng12 -lm -lXmu -lpng -ljpeg -lrt
14  CUSTOM=-DHAVE_SHADOW
15  ifdef USE_PAM
16  LDFLAGS+= -lpam
17 @@ -25,7 +25,8 @@ VERSION=1.3.1
18  DEFINES=-DPACKAGE=\"$(NAME)\" -DVERSION=\"$(VERSION)\" \
19                 -DPKGDATADIR=\"$(PREFIX)/share/slim\" -DSYSCONFDIR=\"$(CFGDIR)\"
20  
21 -OBJECTS=jpeg.o png.o main.o image.o numlock.o cfg.o switchuser.o app.o panel.o
22 +OBJECTS=jpeg.o png.o main.o image.o numlock.o cfg.o switchuser.o app.o \
23 +       panel.o util.o
24  ifdef USE_PAM
25  OBJECTS+=PAM.o
26  endif
27 diff --git a/Makefile.freebsd b/Makefile.freebsd
28 index 3ff326e..c925a39 100644
29 --- a/Makefile.freebsd
30 +++ b/Makefile.freebsd
31 @@ -24,7 +24,8 @@ VERSION=1.3.1
32  DEFINES=-DPACKAGE=\"$(NAME)\" -DVERSION=\"$(VERSION)\" \
33                 -DPKGDATADIR=\"$(PREFIX)/share/slim\" -DSYSCONFDIR=\"$(CFGDIR)\"
34  
35 -OBJECTS=jpeg.o png.o main.o image.o numlock.o cfg.o switchuser.o app.o panel.o
36 +OBJECTS=jpeg.o png.o main.o image.o numlock.o cfg.o switchuser.o app.o \
37 +       panel.o util.o
38  .ifdef USE_PAM
39    OBJECTS+=PAM.o 
40  .endif
41 diff --git a/Makefile.netbsd b/Makefile.netbsd
42 index ad8bb8b..45f33e6 100644
43 --- a/Makefile.netbsd
44 +++ b/Makefile.netbsd
45 @@ -24,7 +24,8 @@ VERSION=1.3.1
46  DEFINES=-DPACKAGE=\"$(NAME)\" -DVERSION=\"$(VERSION)\" \
47                 -DPKGDATADIR=\"$(PREFIX)/share/slim\" -DSYSCONFDIR=\"$(CFGDIR)\"
48  
49 -OBJECTS=jpeg.o png.o main.o image.o numlock.o cfg.o switchuser.o app.o panel.o
50 +OBJECTS=jpeg.o png.o main.o image.o numlock.o cfg.o switchuser.o app.o \
51 +       panel.o util.o
52  .ifdef USE_PAM
53    OBJECTS+=PAM.o 
54  .endif
55 diff --git a/Makefile.openbsd b/Makefile.openbsd
56 index b1829f8..1205b84 100644
57 --- a/Makefile.openbsd
58 +++ b/Makefile.openbsd
59 @@ -20,7 +20,8 @@ VERSION=1.3.1
60  DEFINES=-DPACKAGE=\"$(NAME)\" -DVERSION=\"$(VERSION)\" \
61                 -DPKGDATADIR=\"$(PREFIX)/share/slim\" -DSYSCONFDIR=\"$(CFGDIR)\"
62  
63 -OBJECTS=jpeg.o png.o main.o image.o numlock.o cfg.o switchuser.o app.o panel.o
64 +OBJECTS=jpeg.o png.o main.o image.o numlock.o cfg.o switchuser.o app.o \
65 +       util.o panel.o
66  
67  .SUFFIXES: .c.o .cpp.o
68  
69 diff --git a/app.cpp b/app.cpp
70 index 83ae947..0ac8c3a 100644
71 --- a/app.cpp
72 +++ b/app.cpp
73 @@ -24,6 +24,7 @@
74  #include <algorithm>
75  #include "app.h"
76  #include "numlock.h"
77 +#include "util.h"
78  
79  
80  #ifdef HAVE_SHADOW
81 @@ -128,15 +129,18 @@ void User1Signal(int sig) {
82  
83  
84  #ifdef USE_PAM
85 -App::App(int argc, char** argv):
86 -    pam(conv, static_cast<void*>(&LoginPanel)){
87 +App::App(int argc, char** argv)
88 +  : pam(conv, static_cast<void*>(&LoginPanel)),
89  #else
90 -App::App(int argc, char** argv){
91 +App::App(int argc, char** argv)
92 +  :
93  #endif
94 +    mcookiesize(32)            // Must be divisible by 4
95 +{
96      int tmp;
97      ServerPID = -1;
98      testing = false;
99 -    mcookie = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
100 +    mcookie = string(App::mcookiesize, 'a');
101      daemonmode = false;
102      force_nodaemon = false;
103      firstlogin = true;
104 @@ -1127,13 +1131,13 @@ string App::findValidRandomTheme(const string& set)
105          name = name.substr(0, name.length() - 1);
106      }
107  
108 -    srandom(getpid()+time(NULL));
109 +    Util::srandom(Util::makeseed());
110  
111      vector<string> themes;
112      string themefile;
113      Cfg::split(themes, name, ',');
114      do {
115 -        int sel = random() % themes.size();
116 +        int sel = Util::random() % themes.size();
117  
118          name = Cfg::Trim(themes[sel]);
119          themefile = string(THEMESDIR) +"/" + name + THEMESFILE;
120 @@ -1160,33 +1164,33 @@ void App::replaceVariables(string& input,
121  }
122  
123  
124 +/*
125 + * We rely on the fact that all bits generated by Util::random()
126 + * are usable, so we are taking full words from its output.
127 + */
128  void App::CreateServerAuth() {
129      /* create mit cookie */
130 -    int i, r;
131 -    int hexcount = 0;
132 -        string authfile;
133 -    string cmd;
134 +    uint16_t word;
135 +    uint8_t hi, lo;
136 +    int i;
137 +    string authfile;
138      const char *digits = "0123456789abcdef";
139 -        srand( time(NULL) );
140 -    for ( i = 0; i < 31; i++ ) {
141 -        r = rand()%16;
142 -                mcookie[i] = digits[r];
143 -                if (r>9)
144 -                        hexcount++;
145 +    Util::srandom(Util::makeseed());
146 +    for (i = 0; i < App::mcookiesize; i+=4) {
147 +        word = Util::random() & 0xffff;
148 +        lo = word & 0xff;
149 +        hi = word >> 8;
150 +        mcookie[i] = digits[lo & 0x0f];
151 +        mcookie[i+1] = digits[lo >> 4];
152 +        mcookie[i+2] = digits[hi & 0x0f];
153 +        mcookie[i+3] = digits[hi >> 4];
154      }
155 -        /* MIT-COOKIE: even occurrences of digits and hex digits */
156 -        if ((hexcount%2) == 0) {
157 -                r = rand()%10;
158 -        } else {
159 -                r = rand()%5+10;
160 -        }
161 -        mcookie[31] = digits[r];
162      /* reinitialize auth file */
163      authfile = cfg->getOption("authfile");
164      remove(authfile.c_str());
165      putenv(StrConcat("XAUTHORITY=", authfile.c_str()));
166 -    cmd = cfg->getOption("xauth_path") + " -q -f " + authfile + " add :0 . " + mcookie;
167 -    system(cmd.c_str());
168 +    Util::add_mcookie(mcookie, ":0", cfg->getOption("xauth_path"),
169 +      authfile);
170  }
171  
172  char* App::StrConcat(const char* str1, const char* str2) {
173 diff --git a/app.h b/app.h
174 index 7b4bd10..9a44269 100644
175 --- a/app.h
176 +++ b/app.h
177 @@ -101,6 +101,8 @@ private:
178      
179      std::string themeName;
180      std::string mcookie;
181 +
182 +    const int mcookiesize;
183  };
184  
185  
186 diff --git a/switchuser.cpp b/switchuser.cpp
187 index e72a8fc..ec298e1 100644
188 --- a/switchuser.cpp
189 +++ b/switchuser.cpp
190 @@ -10,6 +10,7 @@
191  */
192  
193  #include "switchuser.h"
194 +#include "util.h"
195  
196  using namespace std;
197  
198 @@ -53,10 +54,10 @@ void SwitchUser::Execute(const char* cmd) {
199  }
200  
201  void SwitchUser::SetClientAuth(const char* mcookie) {
202 -    int r;
203 +    bool r;
204      string home = string(Pw->pw_dir);
205      string authfile = home + "/.Xauthority";
206      remove(authfile.c_str());
207 -    string cmd = cfg->getOption("xauth_path") + " -q -f " + authfile + " add :0 . " + mcookie;
208 -    r = system(cmd.c_str());
209 +    r = Util::add_mcookie(mcookie, ":0", cfg->getOption("xauth_path"),
210 +      authfile);
211  }
212 diff --git a/util.cpp b/util.cpp
213 new file mode 100644
214 index 0000000..5ed972f
215 --- /dev/null
216 +++ b/util.cpp
217 @@ -0,0 +1,69 @@
218 +/* SLiM - Simple Login Manager
219 +   Copyright (C) 2009 Eygene Ryabinkin <rea@codelabs.ru>
220 +
221 +   This program is free software; you can redistribute it and/or modify
222 +   it under the terms of the GNU General Public License as published by
223 +   the Free Software Foundation; either version 2 of the License, or
224 +   (at your option) any later version.
225 +*/
226 +
227 +#include <sys/types.h>
228 +
229 +#include <stdio.h>
230 +#include <stdlib.h>
231 +#include <time.h>
232 +#include <unistd.h>
233 +
234 +#include "util.h"
235 +
236 +/*
237 + * Adds the given cookie to the specified Xauthority file.
238 + * Returns true on success, false on fault.
239 + */
240 +bool Util::add_mcookie(const std::string &mcookie, const char *display,
241 +    const std::string &xauth_cmd, const std::string &authfile)
242 +{
243 +       FILE *fp;
244 +       std::string cmd = xauth_cmd + " -f " + authfile + " -q";
245 +
246 +       fp = popen(cmd.c_str(), "w");
247 +       if (!fp)
248 +               return false;
249 +       fprintf(fp, "remove %s\n", display);
250 +       fprintf(fp, "add %s %s %s\n", display, ".", mcookie.c_str());
251 +       fprintf(fp, "exit\n");
252 +
253 +       pclose(fp);
254 +       return true;
255 +}
256 +
257 +/*
258 + * Interface for random number generator.  Just now it uses ordinary
259 + * random/srandom routines and serves as a wrapper for them.
260 + */
261 +void Util::srandom(unsigned long seed)
262 +{
263 +       ::srandom(seed);
264 +}
265 +
266 +long Util::random(void)
267 +{
268 +       return ::random();
269 +}
270 +
271 +/*
272 + * Makes seed for the srandom() using "random" values obtained from
273 + * getpid(), time(NULL) and others.
274 + */
275 +long Util::makeseed(void)
276 +{
277 +       struct timespec ts;
278 +       long pid = getpid();
279 +       long tm = time(NULL);
280 +
281 +       if (clock_gettime(CLOCK_MONOTONIC, &ts) != 0) {
282 +               ts.tv_sec = ts.tv_nsec = 0;
283 +       }
284 +
285 +       return pid + tm + (ts.tv_sec ^ ts.tv_nsec);
286 +}
287 diff --git a/util.h b/util.h
288 new file mode 100644
289 index 0000000..b8d2993
290 --- /dev/null
291 +++ b/util.h
292 @@ -0,0 +1,24 @@
293 +/* SLiM - Simple Login Manager
294 +   Copyright (C) 2009 Eygene Ryabinkin <rea@codelabs.ru>
295 +
296 +   This program is free software; you can redistribute it and/or modify
297 +   it under the terms of the GNU General Public License as published by
298 +   the Free Software Foundation; either version 2 of the License, or
299 +   (at your option) any later version.
300 +*/
301 +#ifndef __UTIL_H__
302 +#define __UTIL_H__
303 +
304 +#include <string>
305 +
306 +namespace Util {
307 +       bool add_mcookie(const std::string &mcookie, const char *display,
308 +           const std::string &xauth_cmd, const std::string &authfile);
309 +
310 +       void srandom(unsigned long seed);
311 +       long random(void);
312 +
313 +       long makeseed(void);
314 +};
315 +
316 +#endif /* __UTIL_H__ */