68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
..
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
...
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
...
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
...
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
...
299
300
301
302
303
304
305
306
307
308
309
310
311
312
...
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
|
TODO add a randomizer call that works on BSD
TODO document flags
TODO implement/remove lock flag
TODO add flag to bring window to current desktop */
#include <pwd.h>
#include <pty.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdint.h>
................................................................................
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <X11/Xlib.h>
#include <errno.h>
#include <sys/random.h> //TODO bsd compat
#define shmem_prefix "/k.xpriv:"
typedef enum { false, true } bool;
enum mode { mode_usage, mode_go, mode_register,
mode_kill, mode_lock };
enum res { ok, fail_parse, fail_arg, fail_opt, fail_shm,
fail_mem, fail_pty, fail_nop, fail_win,
fail_wid, fail_x11};
enum res bad(enum res code) {
................................................................................
void sigusr(int a) { if (global -> op == mode_kill) run = false; }
void sigterm(int a) { run = false; }
void spawn(pid_t ssha, const char* const sockn) {
if (ssha = fork()) {
char pid_s_buf[16];
char* pid_s = itoa(ssha, pid_s_buf, sizeof(pid_s_buf));
while (access(sockn, F_OK));
// avoid nasty race condition
setenv("SSH_AGENT_PID", pid_s, true);
setenv("SSH_AUTH_SOCK", sockn, true);
global -> agent = ssha;
} else {
close(1); close(0);
execlp("ssh-agent","ssh-agent","-D", "-a",sockn,0);
}
}
enum res register_window(const char* const id, bool weak, const char* const name) {
// the id field denotes the path to the shared memory
// in use, and allows the user to have multiple
// contexts by creating aliases to the binary
int fd = shm_open(id, O_CREAT | O_EXCL | O_RDWR, 0600);
ftruncate(fd, sizeof(struct signal));
if (fd == -1) return fail_shm;
struct signal* s = mmap(0, sizeof(struct signal),
PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
if(s == MAP_FAILED) return fail_mem;
else global = s;
Display* xdpy = XOpenDisplay(NULL);
Atom xvital;
/* x11 */ {
if (xdpy == NULL) return fail_win;
Window win;
................................................................................
}
if(s -> op == mode_kill) {
kill(s -> pid, SIGTERM);
}
kill(s -> agent, SIGTERM);
sigprocmask(SIG_BLOCK, &mask, NULL);
shm_unlink(id);
if (!weak) {
XDeleteProperty(xdpy,s -> wid,xvital);
XSync(xdpy,false);
}
XCloseDisplay(xdpy);
} else {
// now we start ssh-agent and set the proper environment
// variables
pid_t ssha;
if (name == NULL) {
/* messy part */
const char* tmp; //tmpdir defined?
if (!(tmp = getenv("TMPDIR"))) tmp = "/tmp";
size_t tmpsz = strlen(tmp);
char sockn[tmpsz + 1 + sizeof "ssh."
+ 11];
strcpy(sockn, tmp);
sockn[tmpsz] = '/';
strcpy(sockn+tmpsz+1,"ssh.");
................................................................................
// assuming ascii…
for(uint8_t*i=rndid;i<rndid+11;++i) {
*i = '0' + (*i % (25 * 2 + 10));
if (*i > '9') *i += 7;
if (*i > 'Z') *i += ('a' - 'Z');
}
spawn(ssha, sockn);
} else spawn(ssha, name);
pid_t sad;
int p;
if (sad = fork()) {
int added;
waitpid(sad, &added, 0);
if (added == 0) {
if (weak == false) {
XChangeProperty(xdpy,s -> wid,xvital,xvital,8,PropModeReplace,"\01", 1);
XSync(xdpy,false);
}
write(1,"\033c",3);
execlp("fish","fish",NULL);
} else { return ok; }
} else {
execlp("ssh-add","ssh-add",NULL);
}
}
return ok; // this is kind of pointless but w/e
}
enum res kill_window(const char* id) {
int fd = shm_open(id, O_RDWR, 0600);
if (fd == -1) return fail_nop;
struct signal* s = mmap(0, sizeof(struct signal),
PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
if(s == MAP_FAILED) return fail_mem;
s -> op = mode_kill;
kill(s -> pid, SIGUSR1);
return ok;
}
enum res activate_window(Window w) {
................................................................................
if(!XSendEvent(dpy, DefaultRootWindow(dpy), False, mask, &ev)) return fail_x11;
XSync(dpy,false);
return ok;
}
int main(int sz, char** argv) {
enum mode op = mode_go;
bool init_weak = false;
const char* init_named = NULL;
for(int i = 1; i<sz; ++i) {
char* v = argv[i];
if (*v != '-') return bad(fail_arg);
................................................................................
seen_string_arg = true;
++ i; break;
default: return bad(fail_opt);
}
if(opt[1] != 0) { ++opt; goto read_opt; }
}
size_t nsz;
const char* basename = argv[0], *p;
for (p = argv[0]; *p!=0; ++p) {
if(*p == '/') basename = p + 1;
}
nsz = p - basename;
char shid[nsz + sizeof shmem_prefix];
strncpy(shid,shmem_prefix,sizeof shmem_prefix);
strncpy(shid + sizeof shmem_prefix - 1, basename, nsz);
shid[nsz + sizeof shmem_prefix - 1] = 0;
if (op == mode_go) {
int fd;
if ((fd = shm_open(shid, O_RDWR, 0600)) == -1) {
const char* args[] = {
"urxvtc", "-bg", "[90]#4b0024",
"-e", argv[0],
(init_weak?"-aw":"-a"), 0, 0, 0};
const uint8_t argsz = sizeof args/sizeof(const char*);
if (init_named != NULL) {
args[argsz - 3] = "-n"; // im sorry
args[argsz - 2] = init_named;
}
execvp("urxvtc", (char* const*)args);
} else {
struct signal*s = mmap(0, sizeof(struct signal),
PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
return bad(activate_window(s->wid));
}
} else if (op == mode_register)
return bad(register_window(shid,init_weak,init_named));
else if (op == mode_kill)
return bad(kill_window(shid));
else if (op == mode_usage) {
write(1,"\e[1musage:\e[0m ",15);
write(1, argv[0], strlen(argv[0]));
write(1, " [-aklw [arg]]\n",16);
return fail_parse;
}
}
|
|
>
>
>
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
>
>
>
>
>
>
<
>
>
>
>
>
>
<
|
|
|
>
>
|
|
|
|
|
|
|
|
>
>
>
>
>
>
>
>
>
>
|
>
>
>
>
>
>
|
>
|
>
|
|
|
|
>
>
|
|
|
|
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
|
|
|
|
|
|
|
|
|
>
>
>
>
|
|
>
|
>
>
>
|
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
|
|
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
..
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
...
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
...
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
...
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
...
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
...
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
|
TODO add a randomizer call that works on BSD
TODO document flags
TODO implement/remove lock flag
TODO add flag to bring window to current desktop
TODO rewrite using sysv shmem */
#include <pwd.h>
#include <pty.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdint.h>
................................................................................
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <sys/shm.h>
#include <X11/Xlib.h>
#include <errno.h>
#include <sys/random.h>
#ifdef _RAND_SYSCALL
# include <sys/syscall.h>
# define getrandom(a,b,c) syscall(SYS_getrandom,a,b,c)
/* this is necessary on certain platforms due to
* certainly ungodly libc issues, i think. */
#endif
#ifdef _SHM_LINUX
/* xpriv was originally written with linux shared
* memory in mind, because i'm an idiot. i've since
* redesigned it to use the superior old sysv shm
* api that works on every other unix, not just
* linux, but if for some ungodly reason you want
* to use the linux one, then just pass -D_SHM_LINUX */
# define shmem_prefix "/k.xpriv:"
# define shmlinux(...) __VA_ARGS__
# define shmsysv(...)
#else
# define shmlinux(...)
# define shmsysv(...) __VA_ARGS__
#endif
enum /* constants */ {
false = 0, true = 1,
shmsysv(shmem_key = 0x53373EC3,) /* ye olde magique numbre */
};
typedef _Bool bool;
enum mode { mode_usage, mode_go, mode_register,
mode_kill, mode_lock };
enum res { ok, fail_parse, fail_arg, fail_opt, fail_shm,
fail_mem, fail_pty, fail_nop, fail_win,
fail_wid, fail_x11};
enum res bad(enum res code) {
................................................................................
void sigusr(int a) { if (global -> op == mode_kill) run = false; }
void sigterm(int a) { run = false; }
void spawn(pid_t ssha, const char* const sockn) {
if (ssha = fork()) {
char pid_s_buf[16];
char* pid_s = itoa(ssha, pid_s_buf, sizeof(pid_s_buf));
while (access(sockn, F_OK)); // avoid nasty race condition
setenv("SSH_AGENT_PID", pid_s, true);
setenv("SSH_AUTH_SOCK", sockn, true);
global -> agent = ssha;
} else {
close(1); close(0);
execlp("ssh-agent","ssh-agent","-D","-a",sockn,0);
}
}
enum res register_window(shmlinux(const char* const id,) bool weak, const char* name) {
// the id field denotes the path to the shared memory
// in use, and allows the user to have multiple
// contexts by creating aliases to the binary
struct signal* s;
shmlinux ({
int fd = shm_open(id, O_CREAT | O_EXCL | O_RDWR, 0600);
ftruncate(fd, sizeof(struct signal));
if (fd == -1) return fail_shm;
s = mmap(0, sizeof(struct signal),
PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
if(s == MAP_FAILED) return fail_mem;
else global = s;
}) shmsysv (int shmem;
shmem = shmget(shmem_key, sizeof (struct signal), IPC_CREAT | 0777);
if (shmem == -1) return fail_shm;
s = shmat(shmem, 0,0);
if (s == (void*)-1) return fail_mem;
else global = s;
)
Display* xdpy = XOpenDisplay(NULL);
Atom xvital;
/* x11 */ {
if (xdpy == NULL) return fail_win;
Window win;
................................................................................
}
if(s -> op == mode_kill) {
kill(s -> pid, SIGTERM);
}
kill(s -> agent, SIGTERM);
sigprocmask(SIG_BLOCK, &mask, NULL);
shmlinux(shm_unlink(id));
shmsysv({
struct shmid_ds bleh; /* ?? */
shmctl(shmem, IPC_RMID, &bleh);
})
if (!weak) {
XDeleteProperty(xdpy,s -> wid,xvital);
XSync(xdpy,false);
}
XCloseDisplay(xdpy);
} else {
// now we start ssh-agent and set the proper environment
// variables
pid_t ssha;
if (name == NULL) {
/* messy part */
const char* tmp; //tmpdir defined?
if (!(tmp = getenv("XDG_RUNTIME_DIR")))
if (!(tmp = getenv("TMPDIR"))) tmp = "/tmp";
size_t tmpsz = strlen(tmp);
char sockn[tmpsz + 1 + sizeof "ssh."
+ 11];
strcpy(sockn, tmp);
sockn[tmpsz] = '/';
strcpy(sockn+tmpsz+1,"ssh.");
................................................................................
// assuming ascii…
for(uint8_t*i=rndid;i<rndid+11;++i) {
*i = '0' + (*i % (25 * 2 + 10));
if (*i > '9') *i += 7;
if (*i > 'Z') *i += ('a' - 'Z');
}
name = sockn;
}
spawn(ssha, name);
pid_t sad;
int p;
if (sad = fork()) {
int added;
waitpid(sad, &added, 0);
if (added == 0) {
if (weak == false) {
XChangeProperty(xdpy,s -> wid,xvital,xvital,8,PropModeReplace,"\01", 1);
XSync(xdpy,false);
}
write(1,"\033c",3);
execlp("fish","fish",NULL);
}
} else {
execlp("ssh-add","ssh-add",NULL);
}
}
return ok;
}
enum res kill_window(shmlinux(const char* id)) {
struct signal* s;
shmlinux({
int fd = shm_open(id, O_RDWR, 0600);
if (fd == -1) return fail_nop;
s = mmap(0, sizeof(struct signal),
PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
if(s == MAP_FAILED) return fail_mem;
}) shmsysv (int shmem; {
shmem = shmget(shmem_key, sizeof(struct signal), 0);
if (shmem == -1) return fail_nop;
s = shmat(shmem,0,0);
if (s == (void*)-1) return fail_mem;
})
shmsysv({
struct shmid_ds bleh; /* ?? */
shmctl(shmem, IPC_RMID, &bleh);
})
s -> op = mode_kill;
kill(s -> pid, SIGUSR1);
return ok;
}
enum res activate_window(Window w) {
................................................................................
if(!XSendEvent(dpy, DefaultRootWindow(dpy), False, mask, &ev)) return fail_x11;
XSync(dpy,false);
return ok;
}
int main(int sz, char** argv) {
char* binname = "xpriv";
if(sz > 0) binname = argv[0];
enum mode op = mode_go;
bool init_weak = false;
const char* init_named = NULL;
for(int i = 1; i<sz; ++i) {
char* v = argv[i];
if (*v != '-') return bad(fail_arg);
................................................................................
seen_string_arg = true;
++ i; break;
default: return bad(fail_opt);
}
if(opt[1] != 0) { ++opt; goto read_opt; }
}
shmlinux(
size_t nsz;
const char* basename = argv[0], *p;
for (p = binname; *p!=0; ++p) {
if(*p == '/') basename = p + 1;
}
nsz = p - basename;
char shid[nsz + sizeof shmem_prefix];
strncpy(shid,shmem_prefix,sizeof shmem_prefix);
strncpy(shid + sizeof shmem_prefix - 1, basename, nsz);
shid[nsz + sizeof shmem_prefix - 1] = 0;
) shmsysv ({
/* TODO: implement ability to have multiple
* keys based on the name of the binary */
})
tryagain: if (op == mode_go) {
int shm;
if ((shm = shmlinux(
shm_open(shid, O_RDWR, 0600)
) shmsysv (
shmget(shmem_key,sizeof (struct signal),0)
)) == -1) {
const char* args[] = {
"urxvtc", "-bg", "[90]#4b0024",
"-e", argv[0],
(init_weak?"-aw":"-a"), 0, 0, 0};
const uint8_t argsz = sizeof args/sizeof(const char*);
if (init_named != NULL) {
args[argsz - 3] = "-n"; // im sorry
args[argsz - 2] = init_named;
}
execvp("urxvtc", (char* const*)args);
} else {
struct signal*s = shmlinux(
mmap(0, sizeof(struct signal), PROT_READ | PROT_WRITE, MAP_SHARED, shm, 0)
) shmsysv (
shmat(shm, 0, 0)
);
struct shmid_ds stat;
shmctl(shm, IPC_STAT, &stat);
if (stat.shm_nattch == 0) {
/* if the shm segment is not attached to any
* process, it's a relic that needs to be
* cleaned up before we do anything else. on
* a sane OS, there would be built-in non-
* persistence mechanisms for shared mem, but
* alas, as you already know, POSIX */
shmctl(shm, IPC_RMID, &stat);
goto tryagain;
}
if (s == shmlinux(MAP_FAILED) shmsysv((void*)-1))
return bad(fail_mem);
return bad(activate_window(s->wid));
}
} else if (op == mode_register)
return bad(register_window(shmlinux(shid,) init_weak,init_named));
else if (op == mode_kill)
return bad(kill_window(shmlinux(shid)));
else if (op == mode_usage) {
write(1,"\e[1musage:\e[0m ",15);
write(1, argv[0], strlen(argv[0]));
write(1, " [-aklw [arg]]\n",16);
return fail_parse;
}
}
|