00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #include <grass/config.h>
00025
00026 #include <stdio.h>
00027
00028 #ifdef HAVE_SYS_TIME_H
00029 #include <sys/time.h>
00030 #endif
00031
00032 #ifdef HAVE_SYS_RESOURCE_H
00033 #include <sys/resource.h>
00034 #endif
00035
00036 #ifdef HAVE_SYS_TYPES_H
00037 #include <sys/types.h>
00038 #endif
00039
00040 #ifdef HAVE_UNISTD_H
00041 #include <unistd.h>
00042 #endif
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053 static int swap_re_uids (void);
00054
00055 #ifndef PRIO_PROCESS
00056 #define PRIO_PROCESS 0
00057 #endif
00058
00059
00060
00061
00062
00063
00064
00065 #define NORMAL 0
00066 #define PRIORITY -10
00067
00068
00069
00070 static int priority_set = 0;
00071
00072 int init_priority (void)
00073 {
00074
00075 #ifdef MASSCOMP
00076
00077 if (getuid() == 0 || geteuid() == 0)
00078 if (getuid() != 0)
00079 setuid (getuid());
00080 else
00081 setuid (geteuid ());
00082 #endif
00083 return 0;
00084 }
00085
00086
00087
00088
00089 int set_priority ()
00090 {
00091 if (priority_set)
00092 return(priority_set);
00093
00094 swap_re_uids ();
00095
00096 #ifdef HAVE_SETPRIORITY
00097 setpriority (PRIO_PROCESS, (int) getpid (), PRIORITY);
00098 #else
00099 #ifdef HAVE_NICE
00100 nice (PRIORITY);
00101 #endif
00102 #endif
00103
00104 swap_re_uids ();
00105
00106 priority_set = 1;
00107 return(0);
00108 }
00109
00110 int unset_priority ()
00111 {
00112 swap_re_uids ();
00113
00114 #ifdef HAVE_SETPRIORITY
00115 setpriority (PRIO_PROCESS, (int) getpid (), NORMAL);
00116 #else
00117 #ifdef HAVE_NICE
00118 nice (-(PRIORITY));
00119 #endif
00120 #endif
00121
00122 swap_re_uids ();
00123
00124 priority_set = 0;
00125 return(0);
00126 }
00127
00128 static int swap_re_uids (void)
00129 {
00130 #ifndef __MINGW32__
00131 static int flipflop = 0;
00132
00133 #ifdef HAVE_SETREUID
00134 setreuid ((int)geteuid(), (int)getuid());
00135 #else
00136 #ifdef HAVE_SETRUID
00137 #ifdef HAVE_SETEUID
00138
00139
00140
00141 if (! flipflop)
00142 {
00143
00144 hold = getuid ();
00145 setruid (0);
00146 seteuid (hold);
00147
00148 flipflop = 1;
00149 }
00150 else
00151 {
00152
00153 hold = geteuid ();
00154 seteuid (0);
00155 setruid (hold);
00156 flipflop = 0;
00157 }
00158 #endif
00159 #endif
00160 #endif
00161 #endif
00162 return 0;
00163 }
00164
00165
00166 int set_uid_to_user ()
00167 {
00168 #ifndef __MINGW32__
00169 uid_t user;
00170
00171 user = geteuid ();
00172 if (!user)
00173 user = getuid ();
00174 if (!user)
00175 {
00176 fprintf (stderr, "Set_uid_to_user () failed!\n");
00177 return (-1);
00178 }
00179
00180 if (setuid (user) == -1) {
00181 fprintf (stderr, "Set_uid_to_user () failed!\n");
00182 return (-1);
00183 }
00184 #endif
00185 return (0);
00186 }
00187
00188
00189
00190
00191
00192
00193
00194
00195
00196
00197
00198
00199
00200
00201
00202