37
37
#include "su.h"
38
38
#include "utils.h"
39
39
40
- /* Still lazt, will fix this */
41
- static char socket_path [PATH_MAX ];
42
-
43
40
static int from_init (struct su_initiator * from )
44
41
{
45
42
char path [PATH_MAX ], exe [PATH_MAX ];
@@ -143,19 +140,30 @@ void set_identity(unsigned int uid)
143
140
}
144
141
}
145
142
146
- static void socket_cleanup (void )
143
+ static void socket_cleanup (struct su_context * ctx )
147
144
{
148
- unlink (socket_path );
145
+ if (ctx && ctx -> sock_path [0 ]) {
146
+ if (unlink (ctx -> sock_path ))
147
+ PLOGE ("unlink (%s)" , ctx -> sock_path );
148
+ ctx -> sock_path [0 ] = 0 ;
149
+ }
149
150
}
150
151
152
+ /*
153
+ * For use in signal handlers/atexit-function
154
+ * NOTE: su_ctx points to main's local variable.
155
+ * It's OK due to the program uses exit(3), not return from main()
156
+ */
157
+ static struct su_context * su_ctx = NULL ;
158
+
151
159
static void cleanup (void )
152
160
{
153
- socket_cleanup ();
161
+ socket_cleanup (su_ctx );
154
162
}
155
163
156
164
static void cleanup_signal (int sig )
157
165
{
158
- socket_cleanup ();
166
+ socket_cleanup (su_ctx );
159
167
exit (128 + sig );
160
168
}
161
169
@@ -304,7 +312,7 @@ static __attribute__ ((noreturn)) void deny(const struct su_context *ctx)
304
312
{
305
313
char * cmd = get_command (& ctx -> to );
306
314
307
- send_intent (ctx , "" , 0 , ACTION_RESULT );
315
+ send_intent (ctx , DENY , ACTION_RESULT );
308
316
LOGW ("request rejected (%u->%u %s)" , ctx -> from .uid , ctx -> to .uid , cmd );
309
317
fprintf (stderr , "%s\n" , strerror (EACCES ));
310
318
exit (EXIT_FAILURE );
@@ -316,7 +324,7 @@ static __attribute__ ((noreturn)) void allow(const struct su_context *ctx)
316
324
int argc , err ;
317
325
318
326
umask (ctx -> umask );
319
- send_intent (ctx , "" , 1 , ACTION_RESULT );
327
+ send_intent (ctx , ALLOW , ACTION_RESULT );
320
328
321
329
arg0 = strrchr (ctx -> to .shell , '/' );
322
330
arg0 = (arg0 ) ? arg0 + 1 : ctx -> to .shell ;
@@ -435,9 +443,9 @@ int main(int argc, char *argv[])
435
443
},
436
444
};
437
445
struct stat st ;
438
- int socket_serv_fd , fd ;
446
+ int c , socket_serv_fd , fd ;
439
447
char buf [64 ], * result ;
440
- int c , dballow ;
448
+ allow_t dballow ;
441
449
struct option long_opts [] = {
442
450
{ "command" , required_argument , NULL , 'c' },
443
451
{ "help" , no_argument , NULL , 'h' },
@@ -557,17 +565,18 @@ int main(int argc, char *argv[])
557
565
558
566
dballow = database_check (& ctx );
559
567
switch (dballow ) {
560
- case DB_DENY : deny ( & ctx ) ;
561
- case DB_ALLOW : allow (& ctx );
562
- case DB_INTERACTIVE : break ;
563
- default : deny (& ctx );
568
+ case INTERACTIVE : break ;
569
+ case ALLOW : allow (& ctx ); /* never returns */
570
+ case DENY :
571
+ default : deny (& ctx ); /* never returns too */
564
572
}
565
573
566
- socket_serv_fd = socket_create_temp (socket_path , sizeof (socket_path ));
574
+ socket_serv_fd = socket_create_temp (ctx . sock_path , sizeof (ctx . sock_path ));
567
575
if (socket_serv_fd < 0 ) {
568
576
deny (& ctx );
569
577
}
570
578
579
+ su_ctx = & ctx ;
571
580
signal (SIGHUP , cleanup_signal );
572
581
signal (SIGPIPE , cleanup_signal );
573
582
signal (SIGTERM , cleanup_signal );
@@ -576,7 +585,7 @@ int main(int argc, char *argv[])
576
585
signal (SIGABRT , cleanup_signal );
577
586
atexit (cleanup );
578
587
579
- if (send_intent (& ctx , socket_path , -1 , ACTION_REQUEST ) < 0 ) {
588
+ if (send_intent (& ctx , INTERACTIVE , ACTION_REQUEST ) < 0 ) {
580
589
deny (& ctx );
581
590
}
582
591
@@ -593,7 +602,7 @@ int main(int argc, char *argv[])
593
602
594
603
close (fd );
595
604
close (socket_serv_fd );
596
- socket_cleanup ();
605
+ socket_cleanup (& ctx );
597
606
598
607
result = buf ;
599
608
0 commit comments