3535#include <netdb.h>
3636
3737#ifdef __CYGWIN__
38- #include <sys/vfs.h>
39- #define NO_UID_CHECK
40- /* Define out set* and get* calls for cygwin as they are unnecessary and can cause issues */
41- #define seteuid (x )
42- #define setegid (x )
43- #define getuid ()
44- #define getgid ()
38+ #include <sys/vfs.h>
39+ #define NO_UID_CHECK
4540#else
46- #include <sys/statvfs.h>
41+ #include <sys/statvfs.h>
4742#endif
4843
4944#ifdef READLINE_SHELL
@@ -219,13 +214,9 @@ int euid_usb_bulk_write(libusb_device_handle *dev, int ep, char *bytes, int size
219214 V_PRINTF (2 , "Bulk Write dev %p, ep 0x%x, bytes %p, size %d, timeout %d\n" ,
220215 dev , ep , bytes , size , timeout );
221216
222- seteuid (0 );
223- setegid (0 );
224217 int ret = libusb_bulk_transfer (dev , ep , (unsigned char * )bytes , size , & wrbytes , timeout );
225218 if (!ret )
226219 ret = wrbytes ;
227- seteuid (getuid ());
228- setegid (getgid ());
229220
230221 V_PRINTF (2 , "Bulk Write returned %d\n" , ret );
231222
@@ -239,89 +230,93 @@ int euid_usb_bulk_read(libusb_device_handle *dev, int ep, char *bytes, int size,
239230
240231 V_PRINTF (2 , "Bulk Read dev %p, ep 0x%x, bytes %p, size %d, timeout %d\n" ,
241232 dev , ep , bytes , size , timeout );
242- seteuid (0 );
243- setegid (0 );
244233 int ret = libusb_bulk_transfer (usbhdr , ep , (unsigned char * )bytes , size , & rdbytes , timeout );
245234 if (!ret )
246235 ret = rdbytes ;
247- seteuid (getuid ());
248- setegid (getgid ());
249236
250237 V_PRINTF (2 , "Bulk Read returned %d\n" , ret );
251238
252239 return ret ;
253240}
254241
242+ int configure_usb (libusb_device_handle * devh ) {
243+ int cfgn ;
244+ int r = libusb_get_configuration (devh , & cfgn );
245+ if (r ) {
246+ fprintf (stderr , "Failed at reading selected USB configuration: %d\n" , r );
247+ return r ;
248+ }
249+ if (cfgn != USB_CONFIG_NUM )
250+ {
251+ r = libusb_set_configuration (devh , 1 );
252+ if (r ) {
253+ fprintf (stderr , "Failed at selecting USB configuration: %d\n" , r );
254+ return r ;
255+ }
256+ }
257+ r = libusb_claim_interface (devh , USB_IFACE_NUM );
258+ if (r ) {
259+ fprintf (stderr , "Failed at claiming USB interface: %d\n" , r );
260+ return r ;
261+ }
262+
263+ return 0 ;
264+ }
265+
255266libusb_device_handle * open_device (libusb_device * usbdev )
256267{
257268 libusb_device_handle * ret = NULL ;
258269
259- seteuid (0 );
260- setegid (0 );
261-
270+ int escalated = 0 ;
262271 int r = libusb_open (usbdev , & ret );
263272 if (r == LIBUSB_ERROR_ACCESS ) {
264- seteuid (getuid ());
265- setegid (getgid ());
266-
267- fprintf (stderr , "Permission error while opening the USB device.\n" );
268-
269- #ifndef NO_UID_CHECK
270- if (geteuid ())
271- fprintf (stderr , "Fix device permissions or run as root.\n" );
272- #endif
273-
274- return NULL ;
275- }
276- if (!r ) {
277- int cfgn ;
278- r = libusb_get_configuration (ret , & cfgn );
279- if (r ) {
280- seteuid (getuid ());
281- setegid (getgid ());
282- fprintf (stderr , "Failed at reading selected USB configuration: %d\n" , r );
273+ /* We do not seem to have permissions, try elevating privileges (if sbit is set) */
274+ #ifdef NO_UID_CHECK
275+ fprintf (stderr , "Permission error while opening the USB device.\n" );
283276 return NULL ;
284- }
285- if (cfgn != USB_CONFIG_NUM )
286- {
287- r = libusb_set_configuration (ret , 1 );
288- if (r ) {
277+ #else
278+ if (seteuid (0 ) < 0 || setegid (0 ) < 0 ) {
279+ fprintf (stderr , "Permission error while opening the USB device.\n" );
280+ fprintf (stderr , " You might need to enable USB permissions for this program.\n" );
281+ fprintf (stderr , " On Linux you mgiht need to install a udev config file (50-psplink.rules)\n" );
282+ fprintf (stderr , " You can also set the `setuid` bit on the binary alternatively\n" );
283+
289284 seteuid (getuid ());
290285 setegid (getgid ());
291- fprintf (stderr , "Failed at selecting USB configuration: %d\n" , r );
292286 return NULL ;
293287 }
288+ escalated = 1 ;
289+ #endif
290+
291+ /* Try again, see if the sbit helps. */
292+ r = libusb_open (usbdev , & ret );
293+ if (r == LIBUSB_ERROR_ACCESS ) {
294+ fprintf (stderr , "Permission error while opening the USB device.\n" );
295+ ret = NULL ;
294296 }
295- r = libusb_claim_interface (ret , USB_IFACE_NUM );
296- if (r ) {
297+ }
298+ if (!r ) {
299+ if (configure_usb (ret ))
300+ ret = NULL ;
301+ }
302+
303+ #ifndef NO_UID_CHECK
304+ if (escalated ) {
297305 seteuid (getuid ());
298306 setegid (getgid ());
299- fprintf (stderr , "Failed at claiming USB interface: %d\n" , r );
300- return NULL ;
301307 }
302-
303- seteuid (getuid ());
304- setegid (getgid ());
305- return ret ;
306- }
307-
308- seteuid (getuid ());
309- setegid (getgid ());
310- return NULL ;
308+ #endif
309+ return ret ;
311310}
312311
313312void close_device (libusb_device_handle * dev )
314313{
315- seteuid (0 );
316- setegid (0 );
317314 if (dev )
318315 {
319316 libusb_release_interface (dev , 0 );
320317 libusb_reset_device (dev );
321318 libusb_close (dev );
322319 }
323- seteuid (getuid ());
324- setegid (getgid ());
325320}
326321
327322int gen_path (char * path , int dir )
@@ -2479,8 +2474,6 @@ int exit_app(void)
24792474 if (usbhdr )
24802475 {
24812476 /* Nuke the connection */
2482- seteuid (0 );
2483- setegid (0 );
24842477 close_device (usbhdr );
24852478 }
24862479 exit (1 );
0 commit comments