pinentry-dmenu

a pinentry program based on dmenu
git clone anongit@rnpnr.xyz:pinentry-dmenu.git
Log | Files | Refs | Feed | README | LICENSE

util.c (3206B)


      1 /* Quintuple Agent
      2  * Copyright (C) 1999 Robert Bihlmeyer <robbe@orcus.priv.at>
      3  *
      4  *  This program is free software; you can redistribute it and/or modify
      5  *  it under the terms of the GNU General Public License as published by
      6  *  the Free Software Foundation; either version 2 of the License, or
      7  *  (at your option) any later version.
      8  *
      9  *  This program is distributed in the hope that it will be useful,
     10  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
     11  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     12  *  GNU General Public License for more details.
     13  *
     14  *  You should have received a copy of the GNU General Public License
     15  *  along with this program; if not, write to the Free Software
     16  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
     17  */
     18 
     19 #ifdef HAVE_CONFIG_H
     20 #include <config.h>
     21 #endif
     22 
     23 #define _GNU_SOURCE 1
     24 
     25 #include <unistd.h>
     26 #ifndef HAVE_W32CE_SYSTEM
     27 # include <errno.h>
     28 #endif
     29 #include <stdarg.h>
     30 #include <stdio.h>
     31 #include <stdlib.h>
     32 #include <string.h>
     33 #include <assert.h>
     34 
     35 #include "util.h"
     36 
     37 #ifndef HAVE_DOSISH_SYSTEM
     38 static int uid_set = 0;
     39 static uid_t real_uid, file_uid;
     40 #endif /*!HAVE_DOSISH_SYSTEM*/
     41 
     42 /* Write DATA of size BYTES to FD, until all is written or an error
     43    occurs.  */
     44 ssize_t 
     45 xwrite(int fd, const void *data, size_t bytes)
     46 {
     47   char *ptr;
     48   size_t todo;
     49   ssize_t written = 0;
     50 
     51   for (ptr = (char *)data, todo = bytes; todo; ptr += written, todo -= written)
     52     {
     53       do
     54         written = write (fd, ptr, todo);
     55       while (
     56 #ifdef HAVE_W32CE_SYSTEM
     57              0
     58 #else
     59              written == -1 && errno == EINTR
     60 #endif
     61              );
     62       if (written < 0)
     63         break;
     64     }
     65   return written;
     66 }
     67 
     68 #if 0
     69 extern int debug;
     70 
     71 int 
     72 debugmsg(const char *fmt, ...)
     73 {
     74   va_list va;
     75   int ret;
     76 
     77   if (debug) {
     78     va_start(va, fmt);
     79     fprintf(stderr, "\e[4m");
     80     ret = vfprintf(stderr, fmt, va);
     81     fprintf(stderr, "\e[24m");
     82     va_end(va);
     83     return ret;
     84   } else
     85     return 0;
     86 }
     87 #endif
     88 
     89 /* initialize uid variables */
     90 #ifndef HAVE_DOSISH_SYSTEM
     91 static void 
     92 init_uids(void)
     93 {
     94   real_uid = getuid();
     95   file_uid = geteuid();
     96   uid_set = 1;
     97 }
     98 #endif
     99 
    100 
    101 #if 0 /* Not used. */
    102 /* lower privileges to the real user's */
    103 void 
    104 lower_privs()
    105 {
    106   if (!uid_set)
    107     init_uids();
    108   if (real_uid != file_uid) {
    109 #ifdef HAVE_SETEUID
    110     if (seteuid(real_uid) < 0) {
    111       perror("lowering privileges failed");
    112       exit(EXIT_FAILURE);
    113     }
    114 #else
    115     fprintf(stderr, _("Warning: running q-agent setuid on this system is dangerous\n"));
    116 #endif /* HAVE_SETEUID */
    117   }
    118 }
    119 #endif /* if 0 */
    120 
    121 #if 0 /* Not used. */
    122 /* raise privileges to the effective user's */
    123 void 
    124 raise_privs()
    125 {
    126   assert(real_uid >= 0);	/* lower_privs() must be called before this */
    127 #ifdef HAVE_SETEUID
    128   if (real_uid != file_uid && seteuid(file_uid) < 0) {
    129    perror("Warning: raising privileges failed");
    130   }
    131 #endif /* HAVE_SETEUID */
    132 }
    133 #endif /* if 0 */
    134 
    135 /* drop all additional privileges */
    136 void 
    137 drop_privs()
    138 {
    139 #ifndef HAVE_DOSISH_SYSTEM
    140   if (!uid_set)
    141     init_uids();
    142   if (real_uid != file_uid) {
    143     if (setuid(real_uid) < 0) {
    144       perror("dropping privileges failed");
    145       exit(EXIT_FAILURE);
    146     }
    147     file_uid = real_uid;
    148   }
    149 #endif
    150 }