/[muddleftpd]/muddleftpd/src/ftpwho.c
ViewVC logotype

Diff of /muddleftpd/src/ftpwho.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.1 by ganneff, Thu Sep 26 07:55:42 2002 UTC revision 1.1.6.1 by ganneff, Mon Oct 21 19:52:58 2002 UTC
# Line 1  Line 1 
1    
2  /* Copyright (C) 1999 Beau Kuiper  /* Copyright (C) 1999 Beau Kuiper
3     hmmm, could be setuid root.     hmmm, could be setuid root.
4    
# Line 26  SHRMEMHEADER *shrmemptr; Line 27  SHRMEMHEADER *shrmemptr;
27  char *getipstr(unsigned int server)  char *getipstr(unsigned int server)
28  {  {
29          static char ipstr[20];          static char ipstr[20];
30          snprintf(ipstr, 20, "%d.%d.%d.%d", server >> 24,  
31                                             (server >> 16) & 0xFF,          snprintf(ipstr, 20, "%d.%d.%d.%d", server >> 24,
32                                             (server >> 8) & 0xFF,                           (server >> 16) & 0xFF, (server >> 8) & 0xFF, server & 0xFF);
33                                             server & 0xFF);          return (ipstr);
         return(ipstr);  
34  }  }
35    
36  int lockarea(int fd, int pos, int len, int locktype, int do_wait)  int lockarea(int fd,
37                             int pos,
38                             int len,
39                             int locktype,
40                             int do_wait)
41  {  {
42          struct flock lock;          struct flock lock;
43          int lockt;          int lockt;
44          register int err;          register int err;
45            
46          lockt = (do_wait == TRUE ? F_SETLKW : F_SETLK);          lockt = (do_wait == TRUE ? F_SETLKW : F_SETLK);
47          lock.l_type = locktype;          lock.l_type = locktype;
48          lock.l_whence = SEEK_SET;          lock.l_whence = SEEK_SET;
49          lock.l_start = pos;          lock.l_start = pos;
50          lock.l_len = len;          lock.l_len = len;
51          err = fcntl(fd, lockt, &lock);          err = fcntl(fd, lockt, &lock);
52            
53          if (err == -1)          if (err == -1)
54          {          {
55                  if (errno != EAGAIN)                  if (errno != EAGAIN)
56                          ERRORMSGFATAL(strerror(errno));                          ERRORMSGFATAL(strerror(errno));
57                  else                  else
58                          return(FALSE);                          return (FALSE);
59          }          }
60          return(TRUE);          return (TRUE);
61  }  }
62    
63  void *shmem_connect(char *configfile)  void *shmem_connect(char *configfile)
# Line 63  void *shmem_connect(char *configfile) Line 67  void *shmem_connect(char *configfile)
67    
68          lockfd = open(configfile, O_RDWR);          lockfd = open(configfile, O_RDWR);
69          if (lockfd == -1)          if (lockfd == -1)
70                  ERRORMSGFATAL(safe_snprintf("Could not open lock file, reason: %s", strerror(errno)));                  ERRORMSGFATAL(safe_snprintf
71                                                      ("Could not open lock file, reason: %s",
72                                               strerror(errno)));
73    
74  #ifdef DEBUG  #ifdef DEBUG
75          printf("[shmat_init]\n");          printf("[shmat_init]\n");
76  #endif    #endif
77          shmemnum = shmget(ipckey, 0, 0);          shmemnum = shmget(ipckey, 0, 0);
78          if (shmemnum == -1)          if (shmemnum == -1)
79                  return(NULL);                  return (NULL);
80    
81          shrmemptr = shmat(shmemnum, NULL, 0600);          shrmemptr = shmat(shmemnum, NULL, 0600);
82          if ((int)shrmemptr == -1)          if ((int) shrmemptr == -1)
83                  ERRORMSGFATAL(safe_snprintf("Could not connect to shared memory: %s", strerror(errno)));                  ERRORMSGFATAL(safe_snprintf
84                                                      ("Could not connect to shared memory: %s",
85          /* get semaphores. Using the config file so others can join in                                             strerror(errno)));
86             (we use file locks for portability, simplicity  
87             and usability.) */          /*
88                     * get semaphores. Using the config file so others can join in
89          /* assume that if standalone isn't active, inetd is */           * (we use file locks for portability, simplicity
90                     * and usability.)
91             */
92    
93            /*
94             * assume that if standalone isn't active, inetd is
95             */
96    
97          if (lockarea(lockfd, 3, 1, F_WRLCK, FALSE))          if (lockarea(lockfd, 3, 1, F_WRLCK, FALSE))
98                  return(NULL);                  return (NULL);
99    
100          if (shrmemptr->magic != CURRENTMAGIC)          if (shrmemptr->magic != CURRENTMAGIC)
101                  ERRORMSGFATAL("Incorrect version!");                  ERRORMSGFATAL("Incorrect version!");
102    
103          return(shrmemptr);          return (shrmemptr);
104  }  }
105    
106  void pnums_showpid(void)  void pnums_showpid(void)
107  {  {
108          int pid;          int pid;
109            
110          lockarea(lockfd, 0, 1, F_WRLCK, TRUE);          lockarea(lockfd, 0, 1, F_WRLCK, TRUE);
111          pid = (int)shrmemptr->pid;          pid = (int) shrmemptr->pid;
112          lockarea(lockfd, 0, 1, F_UNLCK, TRUE);          lockarea(lockfd, 0, 1, F_UNLCK, TRUE);
113          printf("%d\n", pid);          printf("%d\n", pid);
114          exit(0);          exit(0);
115  }  }
116            
117  void pnums_count(char *username)  void pnums_count(char *username)
118  {  {
119          SHRMEMDATA *dpos;          SHRMEMDATA *dpos;
# Line 109  void pnums_count(char *username) Line 121  void pnums_count(char *username)
121          int count;          int count;
122    
123          shdata = mallocwrapper(SHMEMPROCSTART);          shdata = mallocwrapper(SHMEMPROCSTART);
124                            
125          /* make a working copy of the stuff we want to display so          /*
126             I can release the locks quickly */           * make a working copy of the stuff we want to display so
127             * I can release the locks quickly
128             */
129          lockarea(lockfd, 0, 3, F_WRLCK, TRUE);          lockarea(lockfd, 0, 3, F_WRLCK, TRUE);
130          memcpy(shdata, shrmemptr, sizeof(SHRMEMHEADER) + sizeof(SHRMEMDATA) *                                      memcpy(shdata, shrmemptr, sizeof(SHRMEMHEADER) + sizeof(SHRMEMDATA) *
131                                  (shrmemptr->numvserver + shrmemptr->numgroups));                     (shrmemptr->numvserver + shrmemptr->numgroups));
132          lockarea(lockfd, 0, 3, F_UNLCK, TRUE);          lockarea(lockfd, 0, 3, F_UNLCK, TRUE);
133    
134          dpos = SHRMEMDATPOS(shdata, 0);          dpos = SHRMEMDATPOS(shdata, 0);
135            
136          if (username)          if (username)
137          {          {
138                  int done = FALSE;                  int done = FALSE;
139                  for (count = 0; count < shdata->numvserver + shdata->numgroups; count++)  
140                    for (count = 0; count < shdata->numvserver + shdata->numgroups;
141                             count++)
142                  {                  {
143                          if (strncmp(dpos->name, username, MAXNAMELEN) == 0)                          if (strncmp(dpos->name, username, MAXNAMELEN) == 0)
144                          {                          {
# Line 130  void pnums_count(char *username) Line 146  void pnums_count(char *username)
146                                  printf("%d %d\n", dpos->count, dpos->max);                                  printf("%d %d\n", dpos->count, dpos->max);
147                          }                          }
148                          dpos++;                          dpos++;
149                  }                                                }
150                  if (!done)                  if (!done)
151                          printf("Group/virtual server not found!\n");                          printf("Group/virtual server not found!\n");
152          }          }
153          else          else
154          {          {
155                  printf("User counts: (%d users, maximum %d users)\n", shdata->serverusercount,                  printf("User counts: (%d users, maximum %d users)\n",
156                          shdata->servermaxcount);                             shdata->serverusercount, shdata->servermaxcount);
157            
158                  if (shdata->numvserver > 0)                  if (shdata->numvserver > 0)
159                  {                  {
160                          printf("Virtual Server                                  Current Users   Max Users\n");                          printf
161                                    ("Virtual Server                                        Current Users   Max Users\n");
162                          for (count = 0; count < shdata->numvserver; count++)                          for (count = 0; count < shdata->numvserver; count++)
163                          {                          {
164                                  printf("%-50s %-15d %d\n", dpos->name, dpos->count, dpos->max);                                  printf("%-50s %-15d %d\n", dpos->name, dpos->count,
165                                               dpos->max);
166                                  dpos++;                                  dpos++;
167                          }                          }
168                          printf("\n");                          printf("\n");
169                  }                  }
170            
171                  printf("Group                                           Current Users   Max Users\n");                  printf("Group                                           Current Users   Max Users\n");
172                  for (count = 0; count < shdata->numgroups; count++)                  for (count = 0; count < shdata->numgroups; count++)
173                  {                  {
174                          printf("%-50s %-15d %d\n", dpos->name, dpos->count, dpos->max);                          printf("%-50s %-15d %d\n", dpos->name, dpos->count, dpos->max);
175                          dpos++;                          dpos++;
176                  }                                }
177                  printf("\n");                  printf("\n");
178          }                }
179          exit(0);          exit(0);
180  }  }
181    
182  char **buildgrouplist(CONFIGFILECACHE *c, char **vserverlist)  char **buildgrouplist(CONFIGFILECACHE * c,
183                                              char **vserverlist)
184  {  {
185          char **temp_grouplist, **grouplist;          char **temp_grouplist, **grouplist;
186          char **vserver, **group, **currentgroup;          char **vserver, **group, **currentgroup;
187          int doadd, len;          int doadd, len;
188            
189          /* this is cheating, but is easier */          /*
190          grouplist = (char **)mallocwrapper(sizeof(char *) * c->sectioncount);           * this is cheating, but is easier
191             */
192            grouplist = (char **) mallocwrapper(sizeof(char *) * c->sectioncount);
193          grouplist[0] = NULL;          grouplist[0] = NULL;
194          vserver = vserverlist;          vserver = vserverlist;
195          len = 0;          len = 0;
196          while(*vserver != NULL)          while (*vserver != NULL)
197          {          {
198                  temp_grouplist = makeconfiglist(c, *vserver, "group");                  temp_grouplist = makeconfiglist(c, *vserver, "group");
199                  currentgroup = temp_grouplist;                  currentgroup = temp_grouplist;
200                  while(*currentgroup != NULL)                  while (*currentgroup != NULL)
201                  {                  {
202                          doadd = TRUE;                          doadd = TRUE;
203                          group = grouplist;                          group = grouplist;
204                          while (*group != NULL)                          while (*group != NULL)
205                          {                          {
206                                  if (strcmp(*group, *currentgroup) == 0)                                  if (strcmp(*group, *currentgroup) == 0)
# Line 196  char **buildgrouplist(CONFIGFILECACHE *c Line 217  char **buildgrouplist(CONFIGFILECACHE *c
217                  }                  }
218                  vserver++;                  vserver++;
219          }          }
220            
221          return(grouplist);          return (grouplist);
222  }  }
223    
224  void inetd_count(CONFIGFILECACHE *c, char *username)  void inetd_count(CONFIGFILECACHE * c,
225                                     char *username)
226  {  {
227          SCRFILEREC dat;          SCRFILEREC dat;
228          int count, pos, sectionid, count2;          int count, pos, sectionid, count2;
229          int max, defmax, isvserver, found;          int max, defmax, isvserver, found;
230          int usercount, *vcnt, *gcnt;          int usercount, *vcnt, *gcnt;
231          char **vserverlist, **grouplist;          char **vserverlist, **grouplist;
232            
233          /* build required information from config file */          /*
234             * build required information from config file
235             */
236          sectionid = getsectionid(c, "main");          sectionid = getsectionid(c, "main");
237          loadintfromconfig(c, sectionid, "maxusers", &defmax, MAXUSERS);          loadintfromconfig(c, sectionid, "maxusers", &defmax, MAXUSERS);
238          vserverlist = makeconfiglist(c, "main", "vserver");          vserverlist = makeconfiglist(c, "main", "vserver");
# Line 216  void inetd_count(CONFIGFILECACHE *c, cha Line 240  void inetd_count(CONFIGFILECACHE *c, cha
240                  grouplist = makeconfiglist(c, "main", "group");                  grouplist = makeconfiglist(c, "main", "group");
241          else          else
242                  grouplist = buildgrouplist(c, vserverlist);                  grouplist = buildgrouplist(c, vserverlist);
243                                    
244          /* check name lengths */          /*
245             * check name lengths
246             */
247          count = 0;          count = 0;
248          while (vserverlist[count] != NULL)          while (vserverlist[count] != NULL)
249          {          {
250                  if (strlen(vserverlist[count]) >= MAXSECTIONLEN)                  if (strlen(vserverlist[count]) >= MAXSECTIONLEN)
251                          ERRORMSGFATAL(safe_snprintf("Vserver '%s', name too long.", vserverlist[count]));                          ERRORMSGFATAL(safe_snprintf
252                                                      ("Vserver '%s', name too long.",
253                                                       vserverlist[count]));
254                  count++;                  count++;
255          }          }
256            
257          count = 0;          count = 0;
258          while (grouplist[count] != NULL)          while (grouplist[count] != NULL)
259          {          {
260                  if (strlen(grouplist[count]) >= MAXSECTIONLEN)                  if (strlen(grouplist[count]) >= MAXSECTIONLEN)
261                          ERRORMSGFATAL(safe_snprintf("Group '%s', name too long.", grouplist[count]));                          ERRORMSGFATAL(safe_snprintf
262                                                      ("Group '%s', name too long.", grouplist[count]));
263                  count++;                  count++;
264          }          }
265            
266          if (username)          if (username)
267          {          {
268                  /* get information about part user asked for */                  /*
269                     * get information about part user asked for
270                     */
271                  sectionid = getsectionid(c, username);                  sectionid = getsectionid(c, username);
272                  count = 0; isvserver = FALSE; found = FALSE;                  count = 0;
273                  while((vserverlist[count] != NULL) && (!isvserver))                  isvserver = FALSE;
274                    found = FALSE;
275                    while ((vserverlist[count] != NULL) && (!isvserver))
276                  {                  {
277                          if (strcmp(vserverlist[count], username) == 0)                          if (strcmp(vserverlist[count], username) == 0)
278                                  isvserver = TRUE;                                  isvserver = TRUE;
279                          count++;                          count++;
280                  }                  }
281                  count = 0; found = isvserver;                  count = 0;
282                  while((grouplist[count] != NULL) && (!found))                  found = isvserver;
283                    while ((grouplist[count] != NULL) && (!found))
284                  {                  {
285                          if (strcmp(grouplist[count], username) == 0)                          if (strcmp(grouplist[count], username) == 0)
286                                  found = TRUE;                                  found = TRUE;
# Line 258  void inetd_count(CONFIGFILECACHE *c, cha Line 292  void inetd_count(CONFIGFILECACHE *c, cha
292                  loadintfromconfig(c, sectionid, "maxusers", &max, defmax);                  loadintfromconfig(c, sectionid, "maxusers", &max, defmax);
293                  lockarea(lockfd, 0, 1, F_WRLCK, TRUE);                  lockarea(lockfd, 0, 1, F_WRLCK, TRUE);
294                  lseek(lockfd, 0, SEEK_SET);                  lseek(lockfd, 0, SEEK_SET);
295                  count = 0; pos = 0;                  count = 0;
296                  while(read(lockfd, &dat, sizeof(SCRFILEREC)) == sizeof(SCRFILEREC))                  pos = 0;
297                    while (read(lockfd, &dat, sizeof(SCRFILEREC)) == sizeof(SCRFILEREC))
298                  {                  {
299                          if (lockarea(lockfd, 10 + pos, 1, F_WRLCK, FALSE))                          if (lockarea(lockfd, 10 + pos, 1, F_WRLCK, FALSE))
300                                  lockarea(lockfd, 10 + pos, 1, F_UNLCK, TRUE);                                  lockarea(lockfd, 10 + pos, 1, F_UNLCK, TRUE);
# Line 267  void inetd_count(CONFIGFILECACHE *c, cha Line 302  void inetd_count(CONFIGFILECACHE *c, cha
302                          {                          {
303                                  if (isvserver)                                  if (isvserver)
304                                  {                                  {
305                                          if (strcmp(username, dat.vserver) == 0)                                          if (strcmp(username, dat.vserver) == 0)
306                                                  count++;                                                  count++;
307                                  }                                  }
308                                  else                                  else
# Line 283  void inetd_count(CONFIGFILECACHE *c, cha Line 318  void inetd_count(CONFIGFILECACHE *c, cha
318          }          }
319          else          else
320          {          {
321                  vcnt = (int *)mallocwrapper(sizeof(int) * c->sectioncount * 2);                  vcnt = (int *) mallocwrapper(sizeof(int) * c->sectioncount * 2);
322                  gcnt = (int *)mallocwrapper(sizeof(int) * c->sectioncount * 2);                  gcnt = (int *) mallocwrapper(sizeof(int) * c->sectioncount * 2);
323                  memset(vcnt, 0, c->sectioncount * sizeof(int)* 2);                  memset(vcnt, 0, c->sectioncount * sizeof(int) * 2);
324                  memset(gcnt, 0, c->sectioncount * sizeof(int)* 2);                  memset(gcnt, 0, c->sectioncount * sizeof(int) * 2);
325    
326                  /* determine maximum users */                  /*
327                     * determine maximum users
328                     */
329    
330                  count2 = 0;                  count2 = 0;
331                  while(vserverlist[count2])                  while (vserverlist[count2])
332                  {                  {
333                          sectionid = getsectionid(c, vserverlist[count2]);                          sectionid = getsectionid(c, vserverlist[count2]);
334                          if (sectionid == -1)                          if (sectionid == -1)
335                                  ERRORMSGFATAL(safe_snprintf("could not find vserver section '%s'", vserverlist[count2]));                                  ERRORMSGFATAL(safe_snprintf
336                          loadintfromconfig(c, sectionid, "maxusers",                                                            ("could not find vserver section '%s'",
337                                            vcnt + (c->sectioncount + count2), defmax);                                                             vserverlist[count2]));
338                            loadintfromconfig(c, sectionid, "maxusers",
339                                                              vcnt + (c->sectioncount + count2), defmax);
340                          count2++;                          count2++;
341                  }                  }
342                  count2 = 0;                  count2 = 0;
343                  while(grouplist[count2])                  while (grouplist[count2])
344                  {                  {
345                          sectionid = getsectionid(c, grouplist[count2]);                          sectionid = getsectionid(c, grouplist[count2]);
346                          if (sectionid == -1)                          if (sectionid == -1)
347                                  ERRORMSGFATAL(safe_snprintf("could not find group section '%s'", vserverlist[count2]));                                  ERRORMSGFATAL(safe_snprintf
348                          loadintfromconfig(c, sectionid, "maxusers",                                                            ("could not find group section '%s'",
349                                            gcnt + (c->sectioncount + count2), defmax);                                                             vserverlist[count2]));
350                            loadintfromconfig(c, sectionid, "maxusers",
351                                                              gcnt + (c->sectioncount + count2), defmax);
352                          count2++;                          count2++;
353                  }                  }
354                    
355                  lockarea(lockfd, 0, 1, F_WRLCK, TRUE);                  lockarea(lockfd, 0, 1, F_WRLCK, TRUE);
356                  lseek(lockfd, 0, SEEK_SET);                  lseek(lockfd, 0, SEEK_SET);
357                  count = 0; pos = 0; usercount = 0;                  count = 0;
358                  while(read(lockfd, &dat, sizeof(SCRFILEREC)) == sizeof(SCRFILEREC))                  pos = 0;
359                    usercount = 0;
360                    while (read(lockfd, &dat, sizeof(SCRFILEREC)) == sizeof(SCRFILEREC))
361                  {                  {
362                          if (lockarea(lockfd, 10 + pos, 1, F_WRLCK, FALSE))                          if (lockarea(lockfd, 10 + pos, 1, F_WRLCK, FALSE))
363                                  lockarea(lockfd, 10 + pos, 1, F_UNLCK, TRUE);                                  lockarea(lockfd, 10 + pos, 1, F_UNLCK, TRUE);
# Line 322  void inetd_count(CONFIGFILECACHE *c, cha Line 365  void inetd_count(CONFIGFILECACHE *c, cha
365                          {                          {
366                                  usercount++;                                  usercount++;
367                                  count2 = 0;                                  count2 = 0;
368                                  while(vserverlist[count2])                                  while (vserverlist[count2])
369                                  {                                  {
370                                          if (strcmp(vserverlist[count2], dat.vserver) == 0)                                          if (strcmp(vserverlist[count2], dat.vserver) == 0)
371                                                  vcnt[count2]++;                                                  vcnt[count2]++;
372                                          count2++;                                          count2++;
373                                  }                                  }
374                                  count2 = 0;                                  count2 = 0;
375                                  while(grouplist[count2])                                  while (grouplist[count2])
376                                  {                                  {
377                                          if (strcmp(grouplist[count2], dat.groupname) == 0)                                          if (strcmp(grouplist[count2], dat.groupname) == 0)
378                                                  gcnt[count2]++;                                                  gcnt[count2]++;
# Line 339  void inetd_count(CONFIGFILECACHE *c, cha Line 382  void inetd_count(CONFIGFILECACHE *c, cha
382                          pos++;                          pos++;
383                  }                  }
384                  lockarea(lockfd, 0, 1, F_UNLCK, TRUE);                  lockarea(lockfd, 0, 1, F_UNLCK, TRUE);
385                    
386                  printf("User counts: (%d users, maximum %d users)\n", usercount,                  printf("User counts: (%d users, maximum %d users)\n", usercount,
387                          defmax);                             defmax);
388    
389                  if (vserverlist[0] != NULL)                  if (vserverlist[0] != NULL)
390                  {                  {
391                          printf("Virtual Server                                  Current Users   Max Users\n");                          printf
392                                    ("Virtual Server                                        Current Users   Max Users\n");
393                          for (count = 0; vserverlist[count] != NULL; count++)                          for (count = 0; vserverlist[count] != NULL; count++)
394                                  printf("%-50s %-15d %d\n", vserverlist[count], vcnt[count], vcnt[c->sectioncount + count]);                                  printf("%-50s %-15d %d\n", vserverlist[count], vcnt[count],
395                                               vcnt[c->sectioncount + count]);
396    
397                          printf("\n");                          printf("\n");
398                  }                  }
399            
400                  printf("Group                                           Current Users   Max Users\n");                  printf("Group                                           Current Users   Max Users\n");
401                  for (count = 0; grouplist[count]; count++)                  for (count = 0; grouplist[count]; count++)
402                          if (gcnt[c->sectioncount + count] != 0)                          if (gcnt[c->sectioncount + count] != 0)
403                                  printf("%-50s %-15d %d\n", grouplist[count], gcnt[count], gcnt[c->sectioncount + count]);                                  printf("%-50s %-15d %d\n", grouplist[count], gcnt[count],
404                                               gcnt[c->sectioncount + count]);
405    
406                  printf("\n");                  printf("\n");
407    
408          }                }
409          exit(0);          exit(0);
410  }  }
411    
412  void pnums_listdisplay(SCRMEMREC *newdat, SHRMEMHEADER *shdata, int useips)  void pnums_listdisplay(SCRMEMREC * newdat,
413                                               SHRMEMHEADER * shdata,
414                                               int useips)
415  {  {
416          char *usergroupstr = NULL;          char *usergroupstr = NULL;
417          SHRMEMDATA *dpos;          SHRMEMDATA *dpos;
418            
419          if (newdat->group == -1)          if (newdat->group == -1)
420                  usergroupstr = safe_snprintf("not logged in", newdat->username);                  usergroupstr = safe_snprintf("not logged in", newdat->username);
421          else if (newdat->group == -2)          else if (newdat->group == -2)
# Line 377  void pnums_listdisplay(SCRMEMREC *newdat Line 425  void pnums_listdisplay(SCRMEMREC *newdat
425                  dpos = SHRMEMDATPOS(shdata, newdat->group + shdata->numvserver);                  dpos = SHRMEMDATPOS(shdata, newdat->group + shdata->numvserver);
426                  usergroupstr = safe_snprintf("%s/%s", newdat->username, dpos->name);                  usergroupstr = safe_snprintf("%s/%s", newdat->username, dpos->name);
427          }          }
428            
429          if (useips)          if (useips)
430                  printf("%-5d %-20s %-20s %s\n", newdat->pid,                  printf("%-5d %-20s %-20s %s\n", newdat->pid,
431                          usergroupstr, getipstr(newdat->ip), newdat->currentop);                             usergroupstr, getipstr(newdat->ip), newdat->currentop);
432          else          else
433                  printf("%-5d %-20s %-20s %s\n", newdat->pid,                  printf("%-5d %-20s %-20s %s\n", newdat->pid,
434                          usergroupstr, newdat->remotehost, newdat->currentop);                             usergroupstr, newdat->remotehost, newdat->currentop);
435                            
436          freewrapper(usergroupstr);          freewrapper(usergroupstr);
437  }  }
438    
439  void inetd_listdisplay(STRING **s, SCRFILEREC *newdat, int useips)  void inetd_listdisplay(STRING ** s,
440                                               SCRFILEREC * newdat,
441                                               int useips)
442  {  {
443          char *usergroupstr = NULL;          char *usergroupstr = NULL;
444            
445          if (strcmp(newdat->groupname, "none") == 0)          if (strcmp(newdat->groupname, "none") == 0)
446                  usergroupstr = strdupwrapper("not logged in");                  usergroupstr = strdupwrapper("not logged in");
447          else          else
448                  usergroupstr = safe_snprintf("%s/%s", newdat->username, newdat->groupname);                  usergroupstr =
449                            safe_snprintf("%s/%s", newdat->username, newdat->groupname);
450    
451          if (useips)          if (useips)
452                  string_catprintf(s, "%-5d %-20s %-20s %s\n", newdat->pid,                  string_catprintf(s, "%-5d %-20s %-20s %s\n", newdat->pid,
453                          usergroupstr, getipstr(newdat->ip), newdat->currentop);                                                   usergroupstr, getipstr(newdat->ip),
454                                                     newdat->currentop);
455          else          else
456                  string_catprintf(s, "%-5d %-20s %-20s %s\n", newdat->pid,                  string_catprintf(s, "%-5d %-20s %-20s %s\n", newdat->pid,
457                          usergroupstr, newdat->remotehost, newdat->currentop);                                                   usergroupstr, newdat->remotehost, newdat->currentop);
458    
459          freewrapper(usergroupstr);          freewrapper(usergroupstr);
460  }  }
461    
462  void pnums_list(char *username, int useips)  void pnums_list(char *username,
463                                    int useips)
464  {  {
465          SCRMEMREC *dat, *newdat;          SCRMEMREC *dat, *newdat;
466          SHRMEMHEADER *shdata;          SHRMEMHEADER *shdata;
# Line 418  void pnums_list(char *username, int usei Line 471  void pnums_list(char *username, int usei
471          newdat = mallocwrapper(sizeof(SCRMEMREC));          newdat = mallocwrapper(sizeof(SCRMEMREC));
472    
473          lockarea(lockfd, 0, 3, F_WRLCK, TRUE);          lockarea(lockfd, 0, 3, F_WRLCK, TRUE);
474          memcpy(shdata, shrmemptr, sizeof(SHRMEMHEADER) + sizeof(SHRMEMDATA) *                                      memcpy(shdata, shrmemptr, sizeof(SHRMEMHEADER) + sizeof(SHRMEMDATA) *
475                                  (shrmemptr->numvserver + shrmemptr->numgroups));                     (shrmemptr->numvserver + shrmemptr->numgroups));
476          lockarea(lockfd, 0, 3, F_UNLCK, TRUE);          lockarea(lockfd, 0, 3, F_UNLCK, TRUE);
477    
478          dat = (SCRMEMREC *)((char *)shrmemptr + (SHMEMPROCSTART));          dat = (SCRMEMREC *) ((char *) shrmemptr + (SHMEMPROCSTART));
479          printf("Pid   Username/Group       Host                 Operation\n");          printf("Pid   Username/Group       Host                 Operation\n");
480            
481          for (count = 0; count < shrmemptr->numrecs; count++)          for (count = 0; count < shrmemptr->numrecs; count++)
482          {          {
483                  lockarea(lockfd, 10+count, 1, F_WRLCK, TRUE);                  lockarea(lockfd, 10 + count, 1, F_WRLCK, TRUE);
484                  memcpy(newdat, dat, sizeof(SCRMEMREC));                  memcpy(newdat, dat, sizeof(SCRMEMREC));
485                  lockarea(lockfd, 10+count, 1, F_UNLCK, TRUE);                  lockarea(lockfd, 10 + count, 1, F_UNLCK, TRUE);
486                  if (newdat->pid > 0)                  if (newdat->pid > 0)
487                  {                  {
488                          if (username != NULL)                          if (username != NULL)
489                          {                          {
490                                  if ((strcmp(username, newdat->username) == 0)                                  if ((strcmp(username, newdat->username) == 0)
491                                      && (newdat->group != -1))                                          && (newdat->group != -1))
492                                  {                                  {
493                                          pnums_listdisplay(newdat, shdata, useips);                                          pnums_listdisplay(newdat, shdata, useips);
494                                          count2++;                                          count2++;
# Line 448  void pnums_list(char *username, int usei Line 501  void pnums_list(char *username, int usei
501                          }                          }
502                  }                  }
503                  dat++;                  dat++;
504          }                }
505          printf("------- %d users.\n", count2);          printf("------- %d users.\n", count2);
506  }  }
507    
508  void inetd_list(char *username, int useips)  void inetd_list(char *username,
509                                    int useips)
510  {  {
511          SCRFILEREC dat;          SCRFILEREC dat;
512            
513          /* I need to cache the output into memory instead of to the terminal          /*
514             so that the master lock on the scratchfile is held as little as           * I need to cache the output into memory instead of to the terminal
515             possible! */           * so that the master lock on the scratchfile is held as little as
516             * possible!
517             */
518          STRING *outstring = string_new();          STRING *outstring = string_new();
519            
520          int count;          int count;
521          int count2 = 0;          int count2 = 0;
522    
523          lockarea(lockfd, 0, 1, F_WRLCK, TRUE);          lockarea(lockfd, 0, 1, F_WRLCK, TRUE);
524          lseek(lockfd, 0, SEEK_SET);          lseek(lockfd, 0, SEEK_SET);
525    
526          string_catprintf(&outstring, "Pid   Username/Group         Host                 Operation\n");          string_catprintf(&outstring,
527                                                     "Pid   Username/Group     Host                 Operation\n");
528    
529          count = 0;          count = 0;
530          while(read(lockfd, &dat, sizeof(SCRFILEREC)) == sizeof(SCRFILEREC))          while (read(lockfd, &dat, sizeof(SCRFILEREC)) == sizeof(SCRFILEREC))
531          {          {
532                  if (lockarea(lockfd, 10 + count, 1, F_WRLCK, FALSE))                  if (lockarea(lockfd, 10 + count, 1, F_WRLCK, FALSE))
533                          lockarea(lockfd, 10 + count, 1, F_UNLCK, TRUE);                          lockarea(lockfd, 10 + count, 1, F_UNLCK, TRUE);
# Line 491  void inetd_list(char *username, int usei Line 548  void inetd_list(char *username, int usei
548                          }                          }
549                  }                  }
550                  count++;                  count++;
551          }                }
552          lockarea(lockfd, 0, 1, F_UNLCK, TRUE);          lockarea(lockfd, 0, 1, F_UNLCK, TRUE);
553    
554          string_catprintf(&outstring, "------- %d users.\n", count2);          string_catprintf(&outstring, "------- %d users.\n", count2);
555          /* now print the output */          /*
556             * now print the output
557             */
558          printf("%s", STRTOCHAR(outstring));          printf("%s", STRTOCHAR(outstring));
559  }  }
560    
   
561  void usage(char *name)  void usage(char *name)
562  {  {
563          printf("ftpwho, shows who is logged in and what they are doing.\n\n");          printf("ftpwho, shows who is logged in and what they are doing.\n\n");
564          printf("Usage: %s [-V][-h][-c configfile]\n\n", name);          printf("Usage: %s [-V][-h][-c configfile]\n\n", name);
565          printf("        -V              Show version information.\n");          printf("        -V              Show version information.\n");
566          printf("        -h              Show usage information.\n");          printf("        -h              Show usage information.\n");
567          printf("        -p              Get pid of "PROGNAME" deamon.\n");          printf("        -p              Get pid of " PROGNAME " deamon.\n");
568          printf("        -c configfile   Specify config file "PROGNAME" is running as.\n");          printf("        -c configfile   Specify config file " PROGNAME
569                       " is running as.\n");
570          printf("        -C              Return counts of groups and vservers.\n");          printf("        -C              Return counts of groups and vservers.\n");
571          printf("        -n              Return IP's instead of hostnames.\n");          printf("        -n              Return IP's instead of hostnames.\n");
572          printf("        -u namespec     Return logins of user 'namespec' or if\n");          printf("        -u namespec     Return logins of user 'namespec' or if\n");
573          printf("                        -C is used, counts of specific group or vserver.\n\n");          printf
574                    ("                      -C is used, counts of specific group or vserver.\n\n");
575          exit(1);          exit(1);
576  }  }
577    
578  int main(int argc, char **argv)  int main(int argc,
579                     char **argv)
580  {  {
581          CONFIGFILECACHE *cfiledata;          CONFIGFILECACHE *cfiledata;
582          int count = FALSE;          int count = FALSE;
583          int do_getpid = FALSE;          int do_getpid = FALSE;
584          int useips = FALSE;          int useips = FALSE;
585          char *username = NULL;          char *username = NULL;
# Line 531  int main(int argc, char **argv) Line 592  int main(int argc, char **argv)
592          int saved_gid = getegid();          int saved_gid = getegid();
593          int extraperms = FALSE;          int extraperms = FALSE;
594          extern char *optarg;          extern char *optarg;
595            
596          if ((saved_uid != getuid()) || (saved_gid != getgid()))          if ((saved_uid != getuid()) || (saved_gid != getgid()))
597                  extraperms = TRUE;                  extraperms = TRUE;
598            
599          while((ch = getopt(argc, argv, "Vc:hu:Cnp")) != EOF)          while ((ch = getopt(argc, argv, "Vc:hu:Cnp")) != EOF)
600          {          {
601                  switch(ch)                  switch (ch)
602                  {                  {
603                  case 'V':                  case 'V':
604                          showversion("ftpwho");                          showversion("ftpwho");
# Line 560  int main(int argc, char **argv) Line 621  int main(int argc, char **argv)
621                  default:                  default:
622                          usage(argv[0]);                          usage(argv[0]);
623                  }                  }
624                    
625          }                                }
626    
627          if (fconfig == NULL)          if (fconfig == NULL)
628                  fconfig = CONFIGFILE;                  fconfig = CONFIGFILE;
# Line 573  int main(int argc, char **argv) Line 634  int main(int argc, char **argv)
634          }          }
635    
636          cfiledata = loadconfigcache(fconfig, &line, &error);          cfiledata = loadconfigcache(fconfig, &line, &error);
637            
638          /* obtain scratchfile name */          /*
639             * obtain scratchfile name
640             */
641          if (cfiledata == NULL)          if (cfiledata == NULL)
642                  ERRORMSGFATAL(safe_snprintf("Could not load line %d of config file: %s", line, config_errorstr(error)));                  ERRORMSGFATAL(safe_snprintf
643                                              ("Could not load line %d of config file: %s", line,
644                                               config_errorstr(error)));
645          section = getsectionid(cfiledata, "main");          section = getsectionid(cfiledata, "main");
646          if (section == -1)          if (section == -1)
647                  ERRORMSGFATAL("Could not find main section in config file");                  ERRORMSGFATAL("Could not find main section in config file");
648          loadstrfromconfig(cfiledata, section, "scratchfile",          loadstrfromconfig(cfiledata, section, "scratchfile",
649                            &scratchfile, SCRATCHFILE);                                            &scratchfile, SCRATCHFILE);
650          if (scratchfile[0] != '/')          if (scratchfile[0] != '/')
651                  ERRORMSGFATAL("Scratchfile is not a valid absolute filename");                  ERRORMSGFATAL("Scratchfile is not a valid absolute filename");
652    
653          sharea = shmem_connect(scratchfile);          sharea = shmem_connect(scratchfile);
654            
655          if (sharea != NULL)          if (sharea != NULL)
656          {          {
657                  if (do_getpid)                  if (do_getpid)
# Line 605  int main(int argc, char **argv) Line 670  int main(int argc, char **argv)
670                  else                  else
671                          inetd_list(username, useips);                          inetd_list(username, useips);
672    
673          }          }
674          return(0);          return (0);
675  }  }

Legend:
Removed from v.1.1  
changed lines
  Added in v.1.1.6.1

savannah-hackers-public@gnu.org
ViewVC Help
Powered by ViewVC 1.1.26