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

Diff of /muddleftpd/src/procnum.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:59 2002 UTC
# Line 1  Line 1 
1    
2  /* Copyright (C) 1999 Beau Kuiper  /* Copyright (C) 1999 Beau Kuiper
3    
4     This program is free software; you can redistribute it and/or modify     This program is free software; you can redistribute it and/or modify
# Line 20  Line 21 
21     mode shared memory is used to hold user info */     mode shared memory is used to hold user info */
22    
23  SHRMEMHEADER *shdata = NULL;    /* The start of the shared memory, containing  SHRMEMHEADER *shdata = NULL;    /* The start of the shared memory, containing
24                                     the vserver, group records */                                                                   * the vserver, group records */
25  SCRMEMREC *procdata = NULL;     /* start of array holding thread info. This  SCRMEMREC *procdata = NULL;             /* start of array holding thread info. This
26                                     is all in memory since it is much faster. */                                                                   * is all in memory since it is much faster. */
27  int nextthid = -1;              /* the next free thread id in the list */  int nextthid = -1;                              /* the next free thread id in the list */
28  int shlockfile = -1;            /* file fd used with fcntl file locking to  int shlockfile = -1;                    /* file fd used with fcntl file locking to
29                                     control access to the shared memory.                                                                   * control access to the shared memory.
30                                     This is typically the config file */                                                                   * This is typically the config file */
31  int shnumber = -1;              /* the number allocated to the shared memory  int shnumber = -1;                              /* the number allocated to the shared memory
32                                     area. */                                                                   * area. */
33                                      
34  /* these are used when the server is run in inetd mode */  /* these are used when the server is run in inetd mode */
35    
36  int scratchfile = -1;           /* The fd of the scratchfile. The scratchfile  int scratchfile = -1;                   /* The fd of the scratchfile. The scratchfile
37                                     is used to co-ordinate group limits. */                                                                   * is used to co-ordinate group limits. */
38    
39  /* used by both modes */  /* used by both modes */
40    
41  int thid = -1;                  /* The thread id of the current muddleftpd  int thid = -1;                                  /* The thread id of the current muddleftpd
42                                     client */                                                                   * client */
   
43    
44  /* these simplify writing to the process areas */  /* these simplify writing to the process areas */
45    
46  void writescratch(int pos, int size, char *buff)  void writescratch(int pos,
47                                      int size,
48                                      char *buff)
49  {  {
50          lseek(scratchfile, pos, SEEK_SET);          lseek(scratchfile, pos, SEEK_SET);
51          write(scratchfile, buff, size);          write(scratchfile, buff, size);
52  }  }
53    
54  void writeshmem(int num, int pos, int len, void *buff)  void writeshmem(int num,
55  {                                  int pos,
56          lockarea(shlockfile, 10+num, 1, F_WRLCK, TRUE);                                  int len,
57                                    void *buff)
58          /* messy. Basicly num is added to procdata as a SCRMEMREC pointer  {
59             then pos is added as a char pointer */          lockarea(shlockfile, 10 + num, 1, F_WRLCK, TRUE);
60          memcpy((char *)(procdata + num) + pos, buff, len);  
61          lockarea(shlockfile, 10+num, 1, F_UNLCK, TRUE);          /*
62  }           * messy. Basicly num is added to procdata as a SCRMEMREC pointer
63             * then pos is added as a char pointer
64  void write_procstr(int cthid, int shmem_pos, int file_pos, int length,           */
65                     char *strbuff)          memcpy((char *) (procdata + num) + pos, buff, len);
66            lockarea(shlockfile, 10 + num, 1, F_UNLCK, TRUE);
67    }
68    
69    void write_procstr(int cthid,
70                                       int shmem_pos,
71                                       int file_pos,
72                                       int length,
73                                       char *strbuff)
74  {  {
75          char endchr = 0;          char endchr = 0;
76          int writelen = strlen(strbuff) + 1;          int writelen = strlen(strbuff) + 1;
77            
78          /* if string is too long, null terminate it! */          /*
79             * if string is too long, null terminate it!
80             */
81          if (writelen >= length)          if (writelen >= length)
82          {          {
83                  endchr = strbuff[length-1];                  endchr = strbuff[length - 1];
84                  strbuff[length-1] = 0;                  strbuff[length - 1] = 0;
85                  writelen = length;                  writelen = length;
86          }          }
87            
88          if (inetd)          if (inetd)
89                  writescratch(cthid * sizeof(SCRFILEREC) + file_pos,                  writescratch(cthid * sizeof(SCRFILEREC) + file_pos,
90                               writelen, strbuff);                                           writelen, strbuff);
91          else          else
92                  writeshmem(cthid, shmem_pos, writelen, strbuff);                  writeshmem(cthid, shmem_pos, writelen, strbuff);
93            
94          if (endchr != 0)          if (endchr != 0)
95                  strbuff[length-1] = endchr;                  strbuff[length - 1] = endchr;
96  }  }
97    
98  void shinfo_addtogrouplist(VSERVER *vs)  void shinfo_addtogrouplist(VSERVER * vs)
99  {  {
100          SHRMEMDATA *dpos;          SHRMEMDATA *dpos;
101          int gpos = 0;          int gpos = 0;
102          int count, flag, section, maxcount;          int count, flag, section, maxcount;
103            
104          while(vs->grouplist[gpos] != NULL)          while (vs->grouplist[gpos] != NULL)
105          {          {
106                  /* check to see if the group is already in the list */                  /*
107                     * check to see if the group is already in the list
108                     */
109                  flag = FALSE;                  flag = FALSE;
110                  dpos = SHRMEMDATPOS(shdata, shdata->numvserver);                  dpos = SHRMEMDATPOS(shdata, shdata->numvserver);
111                  for(count = 0; count < shdata->numgroups; count++)                  for (count = 0; count < shdata->numgroups; count++)
112                  {                  {
113                          if (strncmp(vs->grouplist[gpos], dpos->name, MAXSECTIONLEN) == 0)                          if (strncmp(vs->grouplist[gpos], dpos->name, MAXSECTIONLEN) == 0)
114                                  flag = TRUE;                                  flag = TRUE;
115                          dpos++;                          dpos++;
116                  }                  }
117                    
118                  if (!flag)                  if (!flag)
119                  {                  {
120                          /* get maxuser information */                          /*
121                             * get maxuser information
122                             */
123                          section = getsectionid(config->configfile, vs->grouplist[gpos]);                          section = getsectionid(config->configfile, vs->grouplist[gpos]);
124                          loadintfromconfig(config->configfile, section, "maxusers", &maxcount, config->defaults->maxusers);                          loadintfromconfig(config->configfile, section, "maxusers",
125                          /* A group with 0 maxusers doesn't get added! */                                                            &maxcount, config->defaults->maxusers);
126                            /*
127                             * A group with 0 maxusers doesn't get added!
128                             */
129                          if (maxcount == 0)                          if (maxcount == 0)
130                                  flag = TRUE;                                  flag = TRUE;
131                  }                  }
132                            
133                  if (!flag)                  if (!flag)
134                  {                  {
135                          /* add the group, dpos is already initalized from above                          /*
136                             loop */                           * add the group, dpos is already initalized from above
137                             * loop
138                             */
139                          strncpy(dpos->name, vs->grouplist[gpos], MAXNAMLEN);                          strncpy(dpos->name, vs->grouplist[gpos], MAXNAMLEN);
140                          dpos->name[MAXSECTIONLEN-1] = 0;                          dpos->name[MAXSECTIONLEN - 1] = 0;
141                          dpos->max = maxcount;                          dpos->max = maxcount;
142                          dpos->count = 0;                          dpos->count = 0;
143                          shdata->numgroups++;                          shdata->numgroups++;
144                  }                  }
145                  gpos++;                  gpos++;
146          }          }
147  }                }
148                            
149  void shinfo_setuparea(void)  void shinfo_setuparea(void)
150  {  {
151          int count = 0;          int count = 0;
152          SHRMEMDATA *vpos;          SHRMEMDATA *vpos;
153          VSERVER *vs;          VSERVER *vs;
154            
155          shdata->magic = CURRENTMAGIC;          shdata->magic = CURRENTMAGIC;
156          shdata->serverusercount = 0;          shdata->serverusercount = 0;
157          shdata->servermaxcount = config->defaults->maxusers;          shdata->servermaxcount = config->defaults->maxusers;
# Line 140  void shinfo_setuparea(void) Line 161  void shinfo_setuparea(void)
161    
162          vpos = SHRMEMDATPOS(shdata, 0);          vpos = SHRMEMDATPOS(shdata, 0);
163          vs = config->vservers;          vs = config->vservers;
164            
165          while(config->vserverlist[count] != NULL)          while (config->vserverlist[count] != NULL)
166          {          {
167                  vpos->count = 0;                  vpos->count = 0;
168                  vpos->max = vs->maxusers;                  vpos->max = vs->maxusers;
169                  strncpy(vpos->name, config->vserverlist[count], MAXSECTIONLEN);                  strncpy(vpos->name, config->vserverlist[count], MAXSECTIONLEN);
170                  vpos->name[MAXSECTIONLEN-1] = 0;                  vpos->name[MAXSECTIONLEN - 1] = 0;
171                  vpos++;                  vpos++;
172                  vs = vs->next;                  vs = vs->next;
173                  shdata->numvserver++;                  shdata->numvserver++;
# Line 157  void shinfo_setuparea(void) Line 178  void shinfo_setuparea(void)
178          if (!vs)          if (!vs)
179                  shinfo_addtogrouplist(config->defaults);                  shinfo_addtogrouplist(config->defaults);
180          else          else
181                  while(vs)                  while (vs)
182                  {                  {
183                          shinfo_addtogrouplist(vs);                          shinfo_addtogrouplist(vs);
184                          vs = vs->next;                          vs = vs->next;
185                  }                  }
186    
187          if (((config->defaults->maxusers * sizeof(SCRMEMREC)) >          if (((config->defaults->maxusers * sizeof(SCRMEMREC)) >
188              (SHMEMSIZE - SHMEMPROCSTART)) || ((shdata->numvserver +                   (SHMEMSIZE - SHMEMPROCSTART)) || ((shdata->numvserver +
189              shdata->numgroups) * sizeof(SHRMEMDATA) + sizeof(SHRMEMHEADER)                                                                                          shdata->numgroups) *
190              > SHMEMPROCSTART))                                                                                     sizeof(SHRMEMDATA) +
191                  ERRORMSGFATAL("Not enough shared memory to support configuration.");                                                                                     sizeof(SHRMEMHEADER) >
192                                                                                       SHMEMPROCSTART))
193                    ERRORMSGFATAL("Not enough shared memory to support configuration.");
194  }  }
195    
196  void shinfo_init(char *scfilename)  void shinfo_init(char *scfilename)
197  {  {
198          int count, new;          int count, new;
199            
200          scratchfile = open(scfilename, O_RDWR | O_CREAT, 0600);          scratchfile = open(scfilename, O_RDWR | O_CREAT, 0600);
201          if (scratchfile == -1)          if (scratchfile == -1)
202                  ERRORMSGFATAL("Could not open scratch file!");                  ERRORMSGFATAL("Could not open scratch file!");
203            
204          shdata = (SHRMEMHEADER *)shmem_get(scfilename, SHMEMSIZE, &shnumber, &new, &shlockfile);          shdata =
205                            (SHRMEMHEADER *) shmem_get(scfilename, SHMEMSIZE, &shnumber, &new,
206          /* attempt to lock the shared area to say I am home */                                                                     &shlockfile);
207            
208            /*
209             * attempt to lock the shared area to say I am home
210             */
211    
212          if (!lockarea(shlockfile, 3, 1, F_WRLCK, FALSE))          if (!lockarea(shlockfile, 3, 1, F_WRLCK, FALSE))
213                  ERRORMSGFATAL("Muddleftpd already running!");                  ERRORMSGFATAL("Muddleftpd already running!");
214            
215          /* make sure no-body else gets in */          /*
216             * make sure no-body else gets in
217             */
218          lockarea(shlockfile, 0, 3, F_WRLCK, TRUE);          lockarea(shlockfile, 0, 3, F_WRLCK, TRUE);
219          shinfo_setuparea();          shinfo_setuparea();
220          lockarea(shlockfile, 0, 3, F_UNLCK, TRUE);          lockarea(shlockfile, 0, 3, F_UNLCK, TRUE);
           
         procdata = (SCRMEMREC *)((char *)(shdata) + SHMEMPROCSTART);  
221    
222          /* initalize pidmapping list. stores free list as negative values */          procdata = (SCRMEMREC *) ((char *) (shdata) + SHMEMPROCSTART);
223    
224            /*
225             * initalize pidmapping list. stores free list as negative values
226             */
227          for (count = 0; count < config->defaults->maxusers; count++)          for (count = 0; count < config->defaults->maxusers; count++)
228          {          {
229                  procdata[count].pid = -(count + 1);                  procdata[count].pid = -(count + 1);
# Line 201  void shinfo_init(char *scfilename) Line 232  void shinfo_init(char *scfilename)
232                  procdata[count].group = -1;                  procdata[count].group = -1;
233                  procdata[count].ip = 0;                  procdata[count].ip = 0;
234          }          }
235            
236          shdata->numrecs = config->defaults->maxusers;          shdata->numrecs = config->defaults->maxusers;
237            
238          /* set the start pointer to the first thread */          /*
239             * set the start pointer to the first thread
240             */
241          nextthid = 0;          nextthid = 0;
242  }  }
243    
# Line 222  void shinfo_reinit(void) Line 255  void shinfo_reinit(void)
255          int count, count2;          int count, count2;
256          SHRMEMHEADER *old = mallocwrapper(SHMEMPROCSTART);          SHRMEMHEADER *old = mallocwrapper(SHMEMPROCSTART);
257          SHRMEMDATA *datnew, *datold;          SHRMEMDATA *datnew, *datold;
258            
259          lockarea(shlockfile, 0, 3, F_WRLCK, TRUE);          lockarea(shlockfile, 0, 3, F_WRLCK, TRUE);
260          lockarea(shlockfile, 10, shdata->numrecs, F_WRLCK, TRUE);          lockarea(shlockfile, 10, shdata->numrecs, F_WRLCK, TRUE);
261          memcpy(old, shdata, sizeof(SHRMEMHEADER) + sizeof(SHRMEMDATA) *          memcpy(old, shdata, sizeof(SHRMEMHEADER) + sizeof(SHRMEMDATA) *
262                              (shdata->numvserver + shdata->numgroups));                     (shdata->numvserver + shdata->numgroups));
263            
264          /* reinitalize area */          /*
265             * reinitalize area
266             */
267          shinfo_setuparea();          shinfo_setuparea();
268            
269          /* move over old data */          /*
270             * move over old data
271             */
272          shdata->serverusercount = old->serverusercount;          shdata->serverusercount = old->serverusercount;
273          shdata->numrecs = old->numrecs;          shdata->numrecs = old->numrecs;
274    
275          /* update vservers */          /*
276             * update vservers
277             */
278          datnew = SHRMEMDATPOS(shdata, 0);          datnew = SHRMEMDATPOS(shdata, 0);
279          for(count = 0; count < shdata->numvserver; count++)          for (count = 0; count < shdata->numvserver; count++)
280          {          {
281                  datold = SHRMEMDATPOS(old, 0);                  datold = SHRMEMDATPOS(old, 0);
282                  for(count2 = 0; count2 < old->numvserver; count2++)                  for (count2 = 0; count2 < old->numvserver; count2++)
283                  {                  {
284                          if (strncmp(datnew->name, datold->name, MAXSECTIONLEN) == 0)                          if (strncmp(datnew->name, datold->name, MAXSECTIONLEN) == 0)
285                                  datnew->count = datold->count;                                  datnew->count = datold->count;
# Line 249  void shinfo_reinit(void) Line 288  void shinfo_reinit(void)
288                  datnew++;                  datnew++;
289          }          }
290    
291          for(count = 0; count < shdata->numgroups; count++)          for (count = 0; count < shdata->numgroups; count++)
292          {          {
293                  datold = SHRMEMDATPOS(old, old->numvserver);                  datold = SHRMEMDATPOS(old, old->numvserver);
294                  for(count2 = 0; count2 < old->numgroups; count2++)                  for (count2 = 0; count2 < old->numgroups; count2++)
295                  {                  {
296                          if (strncmp(datnew->name, datold->name, MAXSECTIONLEN) == 0)                          if (strncmp(datnew->name, datold->name, MAXSECTIONLEN) == 0)
297                                  datnew->count = datold->count;                                  datnew->count = datold->count;
# Line 261  void shinfo_reinit(void) Line 300  void shinfo_reinit(void)
300                  datnew++;                  datnew++;
301          }          }
302    
303          /* now update all vserver and group id numbers */          /*
304             * now update all vserver and group id numbers
305             */
306          for (count = 0; count < shdata->numrecs; count++)          for (count = 0; count < shdata->numrecs; count++)
307          {          {
308                  char *vserver, *group;                  char *vserver, *group;
309    
310                  if ((procdata[count].vserver != -1) && (shdata->numvserver > 0))                  if ((procdata[count].vserver != -1) && (shdata->numvserver > 0))
311                  {                  {
312                          datold = SHRMEMDATPOS(old, procdata[count].vserver);                          datold = SHRMEMDATPOS(old, procdata[count].vserver);
# Line 277  void shinfo_reinit(void) Line 319  void shinfo_reinit(void)
319                                          procdata[count].vserver = count2;                                          procdata[count].vserver = count2;
320                                  datnew++;                                  datnew++;
321                          }                          }
322                  }                        }
323                  else                  else
324                          procdata[count].vserver = -1;                          procdata[count].vserver = -1;
325                    
326                  if (procdata[count].group >= 0)                  if (procdata[count].group >= 0)
327                  {                  {
328                          datold = SHRMEMDATPOS(old, old->numvserver + procdata[count].group);                          datold =
329                                    SHRMEMDATPOS(old, old->numvserver + procdata[count].group);
330                          group = datold->name;                          group = datold->name;
331                          datnew = SHRMEMDATPOS(shdata, shdata->numvserver);                          datnew = SHRMEMDATPOS(shdata, shdata->numvserver);
332                          /* say the user is orphaned */                          /*
333                             * say the user is orphaned
334                             */
335                          procdata[count].group = -2;                          procdata[count].group = -2;
336                          for (count2 = 0; count2 < shdata->numgroups; count2++)                          for (count2 = 0; count2 < shdata->numgroups; count2++)
337                          {                          {
338                                  if (strncmp(group, datnew->name, MAXSECTIONLEN) == 0)                                    if (strncmp(group, datnew->name, MAXSECTIONLEN) == 0)
339                                          procdata[count].group = count2;                                          procdata[count].group = count2;
340                                  datnew++;                                  datnew++;
341                          }                          }
342                  }                  }
343          }          }
344                            
345          if (shdata->servermaxcount > shdata->numrecs)          if (shdata->servermaxcount > shdata->numrecs)
346          {          {
347                  for (count = shdata->numrecs; count < shdata->servermaxcount; count++)                  for (count = shdata->numrecs; count < shdata->servermaxcount; count++)
# Line 307  void shinfo_reinit(void) Line 352  void shinfo_reinit(void)
352                          procdata[count].group = -1;                          procdata[count].group = -1;
353                          procdata[count].ip = -1;                          procdata[count].ip = -1;
354                  }                  }
355                            
356                  shdata->numrecs = shdata->servermaxcount;                  shdata->numrecs = shdata->servermaxcount;
357          }          }
358            
359          lockarea(shlockfile, 0, 3, F_UNLCK, TRUE);          lockarea(shlockfile, 0, 3, F_UNLCK, TRUE);
360          lockarea(shlockfile, 10, old->numrecs, F_UNLCK, TRUE);          lockarea(shlockfile, 10, old->numrecs, F_UNLCK, TRUE);
361          freewrapper(old);          freewrapper(old);
# Line 319  void shinfo_reinit(void) Line 364  void shinfo_reinit(void)
364  /* set the vserver for a logged in user. REQUIRED for HOST command  /* set the vserver for a logged in user. REQUIRED for HOST command
365     support */     support */
366    
367  int shinfo_setvserver_standalone(int cthid, char *vservername, unsigned int ip,  int shinfo_setvserver_standalone(int cthid,
368                                   int viplimit, int *error)                                                                   char *vservername,
369                                                                     unsigned int ip,
370                                                                     int viplimit,
371                                                                     int *error)
372  {  {
373          int pos = 0;          int pos = 0;
374          int done = 0;          int done = 0;
375          SHRMEMDATA *d = SHRMEMDATPOS(shdata, 0);          SHRMEMDATA *d = SHRMEMDATPOS(shdata, 0);
376          int vipcount = 0;          int vipcount = 0;
377            
378          /* lock vserver-group counts */          /*
379             * lock vserver-group counts
380             */
381          *error = VSERVERFULL;          *error = VSERVERFULL;
382            
383          lockarea(shlockfile, 0, 2, F_WRLCK, TRUE);          lockarea(shlockfile, 0, 2, F_WRLCK, TRUE);
384          while((pos < shdata->numvserver) && (!done))          while ((pos < shdata->numvserver) && (!done))
385          {          {
386                  if (strncmp(vservername, d->name, MAXSECTIONLEN) == 0)                  if (strncmp(vservername, d->name, MAXSECTIONLEN) == 0)
387                          if (d->count < d->max)                          if (d->count < d->max)
388                                  /* found it all */                                  /*
389                                     * found it all
390                                     */
391                                  done = TRUE;                                  done = TRUE;
392                  if (!done)                  if (!done)
393                  {                  {
# Line 343  int shinfo_setvserver_standalone(int cth Line 395  int shinfo_setvserver_standalone(int cth
395                          d++;                          d++;
396                  }                  }
397          }          }
398            
399          if ((done) && (viplimit > 0))          if ((done) && (viplimit > 0))
400          {          {
401                  /* safe as long as IP is not set elsewhere */                  /*
402                     * safe as long as IP is not set elsewhere
403                     */
404                  int count;                  int count;
405    
406                  for (count = 0; count < shdata->numrecs; count++)                  for (count = 0; count < shdata->numrecs; count++)
407                  {                  {
408                          lockarea(shlockfile, 10 + count, 1, F_WRLCK, TRUE);                          lockarea(shlockfile, 10 + count, 1, F_WRLCK, TRUE);
409                          if ((procdata[count].ip == ip) &&                          if ((procdata[count].ip == ip) &&
410                              (procdata[count].vserver == pos))                                  (procdata[count].vserver == pos))
411                                  vipcount++;                                  vipcount++;
412                          lockarea(shlockfile, 10 + count, 1, F_UNLCK, TRUE);                          lockarea(shlockfile, 10 + count, 1, F_UNLCK, TRUE);
413                  }                  }
# Line 369  int shinfo_setvserver_standalone(int cth Line 423  int shinfo_setvserver_standalone(int cth
423                  lockarea(shlockfile, 10 + cthid, 1, F_WRLCK, TRUE);                  lockarea(shlockfile, 10 + cthid, 1, F_WRLCK, TRUE);
424                  procdata[thid].vserver = pos;                  procdata[thid].vserver = pos;
425                  d->count++;                  d->count++;
426                  lockarea(shlockfile, 10 + cthid, 1, F_UNLCK, TRUE);                              lockarea(shlockfile, 10 + cthid, 1, F_UNLCK, TRUE);
427          }          }
428            
429          lockarea(shlockfile, 0, 2, F_UNLCK, TRUE);          lockarea(shlockfile, 0, 2, F_UNLCK, TRUE);
430          return(done);          return (done);
431  }                                }
432    
433  int shinfo_setvserver_inetd(int newthid, char *vservername, unsigned int ip,  int shinfo_setvserver_inetd(int newthid,
434                              int vlimit, int viplimit, int *error)                                                          char *vservername,
435                                                            unsigned int ip,
436                                                            int vlimit,
437                                                            int viplimit,
438                                                            int *error)
439  {  {
440          int pos;          int pos;
441          int vipcount;          int vipcount;
# Line 385  int shinfo_setvserver_inetd(int newthid, Line 443  int shinfo_setvserver_inetd(int newthid,
443          int done;          int done;
444          SCRFILEREC d;          SCRFILEREC d;
445    
446          pos = 0; vcount = 0; vipcount = 0; done = FALSE;          pos = 0;
447                            vcount = 0;
448          /* grab general scratchfile lock */          vipcount = 0;
449                            done = FALSE;
450    
451            /*
452             * grab general scratchfile lock
453             */
454    
455          lockarea(scratchfile, 0, 1, F_WRLCK, TRUE);          lockarea(scratchfile, 0, 1, F_WRLCK, TRUE);
456          lseek(scratchfile, 0, SEEK_SET);          lseek(scratchfile, 0, SEEK_SET);
457                            
458          while(read(scratchfile, &d, sizeof(SCRFILEREC)) == sizeof(SCRFILEREC))          while (read(scratchfile, &d, sizeof(SCRFILEREC)) == sizeof(SCRFILEREC))
459          {          {
460                  /* don't count ourselves */                  /*
461                     * don't count ourselves
462                     */
463                  if (pos == thid)                  if (pos == thid)
464                          pos++;                          pos++;
465                  else if (lockarea(scratchfile, 10 + pos, 1, F_WRLCK, FALSE))                  else if (lockarea(scratchfile, 10 + pos, 1, F_WRLCK, FALSE))
466                  {                  {
467                          /* if locking successed, no-one is home, unlock it */                          /*
468                             * if locking successed, no-one is home, unlock it
469                             */
470                          lockarea(scratchfile, 10 + pos, 1, F_UNLCK, TRUE);                          lockarea(scratchfile, 10 + pos, 1, F_UNLCK, TRUE);
471                  }                  }
472                  else                  else
# Line 420  int shinfo_setvserver_inetd(int newthid, Line 487  int shinfo_setvserver_inetd(int newthid,
487                  *error = VSERVERFULL;                  *error = VSERVERFULL;
488          else          else
489          {          {
490                  write_procstr(newthid, 0, SCRF_VSERVER, MAXSECTIONLEN,                  write_procstr(newthid, 0, SCRF_VSERVER, MAXSECTIONLEN, vservername);
                               vservername);  
491                  done = TRUE;                  done = TRUE;
492          }          }
493            
494          lockarea(scratchfile, 0, 1, F_UNLCK, TRUE);          lockarea(scratchfile, 0, 1, F_UNLCK, TRUE);
495          return(done);          return (done);
496  }  }
497    
498  int shinfo_setvserver(int cthid, char *vservername, unsigned int ip,  int shinfo_setvserver(int cthid,
499                        int vlimit, int viplimit, int *error)                                            char *vservername,
500                                              unsigned int ip,
501                                              int vlimit,
502                                              int viplimit,
503                                              int *error)
504  {  {
505          if (inetd)          if (inetd)
506                  return(shinfo_setvserver_inetd(cthid, vservername, ip, vlimit,                  return (shinfo_setvserver_inetd(cthid, vservername, ip, vlimit,
507                                                  viplimit, error));                                                                                  viplimit, error));
508          else          else
509                  return(shinfo_setvserver_standalone(cthid, vservername, ip,                  return (shinfo_setvserver_standalone(cthid, vservername, ip,
510                                                      viplimit, error));                                                                                           viplimit, error));
511  }  }
512    
513  int shinfo_newuser_standalone(unsigned int ip, int iplimit, int *error)  int shinfo_newuser_standalone(unsigned int ip,
514                                                              int iplimit,
515                                                              int *error)
516  {  {
517          int tempnextthid, done = FALSE;          int tempnextthid, done = FALSE;
518            
519          lockarea(shlockfile, 0, 2, F_WRLCK, TRUE);          lockarea(shlockfile, 0, 2, F_WRLCK, TRUE);
520            
521          /* see if we have space */          /*
522             * see if we have space
523             */
524          if (shdata->serverusercount >= shdata->servermaxcount)          if (shdata->serverusercount >= shdata->servermaxcount)
525          {          {
526                  thid = -1;                  thid = -1;
# Line 461  int shinfo_newuser_standalone(unsigned i Line 535  int shinfo_newuser_standalone(unsigned i
535    
536          if ((done) && (iplimit > 0))          if ((done) && (iplimit > 0))
537          {          {
538                  /* safe as long as IP is not set elsewhere (other than below) */                  /*
539                     * safe as long as IP is not set elsewhere (other than below)
540                     */
541                  int count;                  int count;
542                  int ipcount = 0;                  int ipcount = 0;
543    
# Line 476  int shinfo_newuser_standalone(unsigned i Line 552  int shinfo_newuser_standalone(unsigned i
552                          *error = IPHOSTFULL;                          *error = IPHOSTFULL;
553                  }                  }
554          }          }
555                      
556          if (done)          if (done)
557          {          {
558                  /* increment server count now login is confirmed */                  /*
559                  shdata->serverusercount++;                   * increment server count now login is confirmed
560                     */
561                  /* update shared memory */                  shdata->serverusercount++;
562    
563                    /*
564                     * update shared memory
565                     */
566                  lockarea(shlockfile, 10 + thid, 1, F_WRLCK, TRUE);                  lockarea(shlockfile, 10 + thid, 1, F_WRLCK, TRUE);
567                  strcpy(procdata[thid].username, "<unknown>");                  strcpy(procdata[thid].username, "<unknown>");
568                  procdata[thid].group = -1;                  procdata[thid].group = -1;
# Line 491  int shinfo_newuser_standalone(unsigned i Line 571  int shinfo_newuser_standalone(unsigned i
571                  strcpy(procdata[thid].remotehost, "unknown");                  strcpy(procdata[thid].remotehost, "unknown");
572                  procdata[thid].pid = -1;                  procdata[thid].pid = -1;
573                  procdata[thid].ip = ip;                  procdata[thid].ip = ip;
574                  lockarea(shlockfile, 10 + thid, 1, F_UNLCK, TRUE);                                lockarea(shlockfile, 10 + thid, 1, F_UNLCK, TRUE);
575    
576                  /* now move the thids along */                  /*
577                     * now move the thids along
578                     */
579                  nextthid = tempnextthid;                  nextthid = tempnextthid;
580          }          }
581          else          else
582                  thid = -1;                  thid = -1;
583    
584          lockarea(shlockfile, 0, 2, F_UNLCK, TRUE);          lockarea(shlockfile, 0, 2, F_UNLCK, TRUE);
585          return(thid);          return (thid);
586  }  }
587    
588  int shinfo_adduser_inetd(unsigned int ip, int slimit, int iplimit, int *error)  int shinfo_adduser_inetd(unsigned int ip,
589                                                     int slimit,
590                                                     int iplimit,
591                                                     int *error)
592  {  {
593          SCRFILEREC d;          SCRFILEREC d;
594          int scount, ipcount, pos, full;          int scount, ipcount, pos, full;
595            
596          /* we are running inetd. go through scratch file, find an          /*
597             empty record, and count space in file. */           * we are running inetd. go through scratch file, find an
598          pos = 0; scount = 0; thid = -1;           * empty record, and count space in file.
599             */
600            pos = 0;
601            scount = 0;
602            thid = -1;
603          full = FALSE;          full = FALSE;
604            
605          /* grab general scratchfile lock */          /*
606                             * grab general scratchfile lock
607             */
608    
609          lockarea(scratchfile, 0, 1, F_WRLCK, TRUE);          lockarea(scratchfile, 0, 1, F_WRLCK, TRUE);
610          lseek(scratchfile, 0, SEEK_SET);          lseek(scratchfile, 0, SEEK_SET);
611    
612          while(read(scratchfile, &d, sizeof(SCRFILEREC)) == sizeof(SCRFILEREC))          while (read(scratchfile, &d, sizeof(SCRFILEREC)) == sizeof(SCRFILEREC))
613          {          {
614                  if (lockarea(scratchfile, 10 + pos, 1, F_WRLCK, FALSE))                  if (lockarea(scratchfile, 10 + pos, 1, F_WRLCK, FALSE))
615                  {                  {
616                          /* if no allocated thid yet, assign it, otherwise                          /*
617                             unlock free area */                           * if no allocated thid yet, assign it, otherwise
618                             * unlock free area
619                             */
620                          if (thid == -1)                          if (thid == -1)
621                                  thid = pos;                                  thid = pos;
622                          else                          else
# Line 536  int shinfo_adduser_inetd(unsigned int ip Line 629  int shinfo_adduser_inetd(unsigned int ip
629                          scount++;                          scount++;
630                  }                  }
631                  pos++;                  pos++;
632          }                  }
633    
634          if (thid == -1)          if (thid == -1)
635          {          {
636                  thid = pos;                  thid = pos;
637                  lockarea(scratchfile, 10 + pos, 1, F_WRLCK, TRUE);                  lockarea(scratchfile, 10 + pos, 1, F_WRLCK, TRUE);
638          }          }
639                    
640          if (scount >= slimit)          if (scount >= slimit)
641          {          {
642                  *error = HOSTFULL;                  *error = HOSTFULL;
643                  full = TRUE;                  full = TRUE;
644          }          }
645            
646          if ((iplimit > 0) && (ipcount >= iplimit))          if ((iplimit > 0) && (ipcount >= iplimit))
647          {          {
648                  *error = IPHOSTFULL;                  *error = IPHOSTFULL;
649                  full = TRUE;                  full = TRUE;
650          }          }
651            
652          if (full)          if (full)
653          {          {
654                  lockarea(scratchfile, 10 + pos, 1, F_UNLCK, TRUE);                  lockarea(scratchfile, 10 + pos, 1, F_UNLCK, TRUE);
655                  lockarea(scratchfile, 0, 1, F_UNLCK, TRUE);                  lockarea(scratchfile, 0, 1, F_UNLCK, TRUE);
656                  return(-1);                  return (-1);
657          }          }
658                    
659          /* need to add data to file */          /*
660             * need to add data to file
661             */
662          memset(&d, 0, sizeof(SCRFILEREC));          memset(&d, 0, sizeof(SCRFILEREC));
663          strcpy(d.username, "<unknown>");          strcpy(d.username, "<unknown>");
664          strcpy(d.groupname, "none");          strcpy(d.groupname, "none");
665                    
666          strncpy(d.vserver, "none", MAXSECTIONLEN);          strncpy(d.vserver, "none", MAXSECTIONLEN);
667          /* null terminate d.vserver */          /*
668          d.vserver[MAXSECTIONLEN-1] = 0;           * null terminate d.vserver
669                             */
670            d.vserver[MAXSECTIONLEN - 1] = 0;
671    
672          strcpy(d.currentop, "none");          strcpy(d.currentop, "none");
673          strcpy(d.remotehost, "unknown");          strcpy(d.remotehost, "unknown");
674          d.pid = (int)getpid();          d.pid = (int) getpid();
675          d.ip = ip;          d.ip = ip;
676    
677          writescratch(thid * sizeof(SCRFILEREC), sizeof(SCRFILEREC), (char *)&d);          writescratch(thid * sizeof(SCRFILEREC), sizeof(SCRFILEREC), (char *) &d);
678          lockarea(scratchfile, 0, 1, F_UNLCK, TRUE);          lockarea(scratchfile, 0, 1, F_UNLCK, TRUE);
679          return(thid);          return (thid);
680  }  }
681    
682  void shinfo_changeop(char *operation)  void shinfo_changeop(char *operation)
683  {  {
684          write_procstr(thid, MAXNAMELEN, SCRF_CURRENTOP, DESCRIPLEN, operation);          write_procstr(thid, MAXNAMELEN, SCRF_CURRENTOP, DESCRIPLEN, operation);
685  }  }
686    
687  void shinfo_changeuser(char *username)  void shinfo_changeuser(char *username)
688  {  {
# Line 595  void shinfo_changeuser(char *username) Line 692  void shinfo_changeuser(char *username)
692  void shinfo_sethost(char *hostname)  void shinfo_sethost(char *hostname)
693  {  {
694          write_procstr(thid, MAXNAMELEN + DESCRIPLEN, SCRF_REMOTEHOST,          write_procstr(thid, MAXNAMELEN + DESCRIPLEN, SCRF_REMOTEHOST,
695                        MAXNAMELEN, hostname);                                    MAXNAMELEN, hostname);
696  }  }
697    
698  int shinfo_addusergroup(char *groupname, int limit)  int shinfo_addusergroup(char *groupname,
699                                                    int limit)
700  {  {
701          int count = 0;          int count = 0;
702          int pos = 0;          int pos = 0;
703          int done = FALSE;          int done = FALSE;
704          SHRMEMDATA *d;          SHRMEMDATA *d;
705          SCRFILEREC da;          SCRFILEREC da;
706            
707          if (inetd)          if (inetd)
708          {          {
709                  /* must find count of users in group */                  /*
710                     * must find count of users in group
711                     */
712                  lockarea(scratchfile, 0, 1, F_WRLCK, TRUE);                  lockarea(scratchfile, 0, 1, F_WRLCK, TRUE);
713                  pos = 0;                  pos = 0;
714    
715                  lseek(scratchfile, 0, SEEK_SET);                  lseek(scratchfile, 0, SEEK_SET);
716                  while(read(scratchfile, &da, sizeof(SCRFILEREC)) == sizeof(SCRFILEREC))                  while (read(scratchfile, &da, sizeof(SCRFILEREC)) ==
717                               sizeof(SCRFILEREC))
718                  {                  {
719                          if (pos != thid)                          if (pos != thid)
720                          {                          {
# Line 626  int shinfo_addusergroup(char *groupname, Line 727  int shinfo_addusergroup(char *groupname,
727                                          lockarea(scratchfile, 10 + pos, 1, F_UNLCK, TRUE);                                          lockarea(scratchfile, 10 + pos, 1, F_UNLCK, TRUE);
728                          }                          }
729                          pos++;                          pos++;
730                  }                          }
731                    
732                  done = (count < limit);                  done = (count < limit);
733                  if (done)                  if (done)
734                  {                  {
735                          /* update scratchfile */                          /*
736                             * update scratchfile
737                             */
738                          write_procstr(thid, 0, SCRF_GROUPNAME, MAXSECTIONLEN, groupname);                          write_procstr(thid, 0, SCRF_GROUPNAME, MAXSECTIONLEN, groupname);
739                          count++;                          count++;
740                  }                  }
# Line 639  int shinfo_addusergroup(char *groupname, Line 742  int shinfo_addusergroup(char *groupname,
742          }          }
743          else          else
744          {          {
745                  /* non-inetd stuff */                  /*
746                     * non-inetd stuff
747                     */
748                  lockarea(shlockfile, 0, 1, F_WRLCK, TRUE);                  lockarea(shlockfile, 0, 1, F_WRLCK, TRUE);
749                  lockarea(shlockfile, 2, 1, F_WRLCK, TRUE);                  lockarea(shlockfile, 2, 1, F_WRLCK, TRUE);
750    
751                  d = SHRMEMDATPOS(shdata, shdata->numvserver);                  d = SHRMEMDATPOS(shdata, shdata->numvserver);
752            
753                  /* find the group in the list */                  /*
754                                     * find the group in the list
755                  while((pos < shdata->numgroups) && (!done))                   */
756    
757                    while ((pos < shdata->numgroups) && (!done))
758                  {                  {
759                          if (strncmp(groupname, d->name, MAXSECTIONLEN) == 0)                          if (strncmp(groupname, d->name, MAXSECTIONLEN) == 0)
760                                  if (d->count < d->max)                                  if (d->count < d->max)
761                                  {                                  {
762                                          /* found it all */                                          /*
763                                             * found it all
764                                             */
765                                          d->count++;                                          d->count++;
766                                          count = d->count;                                          count = d->count;
767                                          done = TRUE;                                          done = TRUE;
768                                  }                                  }
769                          pos++;                          pos++;
770                          d++;                          d++;
771                  }                  }
772                    
773                  if (done)                  if (done)
774                  {                  {
775                          lockarea(shlockfile, 10 + thid, 1, F_WRLCK, TRUE);                          lockarea(shlockfile, 10 + thid, 1, F_WRLCK, TRUE);
776                          if (procdata[thid].group != -1)                          if (procdata[thid].group != -1)
777                                  ERRORMSG("ack, inconsistency found!");                                  ERRORMSG("ack, inconsistency found!");
778                          procdata[thid].group = pos - 1;                          procdata[thid].group = pos - 1;
779                          lockarea(shlockfile, 10 + thid, 1, F_UNLCK, TRUE);                                lockarea(shlockfile, 10 + thid, 1, F_UNLCK, TRUE);
780                  }                  }
781                  lockarea(shlockfile, 2, 1, F_UNLCK, TRUE);                  lockarea(shlockfile, 2, 1, F_UNLCK, TRUE);
782                  lockarea(shlockfile, 0, 1, F_UNLCK, TRUE);                  lockarea(shlockfile, 0, 1, F_UNLCK, TRUE);
783          }          }
784          if (done)          if (done)
785                  return(count);                  return (count);
786          else          else
787                  return(-1);                  return (-1);
788            
789  }  }
790    
791  void shinfo_setpid(int thrid, int pid)  void shinfo_setpid(int thrid,
792                                       int pid)
793  {  {
794          if (!inetd)          if (!inetd)
795                  procdata[thrid].pid = pid;                  procdata[thrid].pid = pid;
796          else          else
797                  writescratch(thrid * sizeof(SCRFILEREC) + SCRF_PID,                  writescratch(thrid * sizeof(SCRFILEREC) + SCRF_PID,
798                               sizeof(int), (char *)&pid);                                           sizeof(int), (char *) &pid);
799  }  }
800    
801  void shinfo_delusergroup(char *groupname)  void shinfo_delusergroup(char *groupname)
# Line 700  void shinfo_delusergroup(char *groupname Line 810  void shinfo_delusergroup(char *groupname
810                  lockarea(shlockfile, 2, 1, F_WRLCK, TRUE);                  lockarea(shlockfile, 2, 1, F_WRLCK, TRUE);
811    
812                  d = SHRMEMDATPOS(shdata, shdata->numvserver);                  d = SHRMEMDATPOS(shdata, shdata->numvserver);
813            
814                  while((pos < shdata->numgroups) && (!done))                  while ((pos < shdata->numgroups) && (!done))
815                  {                  {
816                          if (strncmp(groupname, d->name, MAXSECTIONLEN) == 0)                          if (strncmp(groupname, d->name, MAXSECTIONLEN) == 0)
817                          {                          {
818                                  /* found it all */                                  /*
819                                     * found it all
820                                     */
821                                  d->count--;                                  d->count--;
822                                  done = TRUE;                                  done = TRUE;
823                          }                          }
# Line 720  void shinfo_delusergroup(char *groupname Line 832  void shinfo_delusergroup(char *groupname
832                  lockarea(shlockfile, 0, 1, F_UNLCK, TRUE);                  lockarea(shlockfile, 0, 1, F_UNLCK, TRUE);
833          }          }
834          else          else
835                  /* update scratchfile */                  /*
836                  writescratch(thid * sizeof(SCRFILEREC) + SCRF_GROUPNAME,                   * update scratchfile
837                               5, "none");                   */
838                    writescratch(thid * sizeof(SCRFILEREC) + SCRF_GROUPNAME, 5, "none");
839  }  }
840    
841  void shinfo_freebynum(int threadnum)  void shinfo_freebynum(int threadnum)
842  {  {
843          SHRMEMDATA *d;          SHRMEMDATA *d;
844    
845          /* free and update pointers */          /*
846             * free and update pointers
847             */
848          lockarea(shlockfile, 0, 3, F_WRLCK, TRUE);          lockarea(shlockfile, 0, 3, F_WRLCK, TRUE);
849          lockarea(shlockfile, 10 + threadnum, 1, F_WRLCK, TRUE);          lockarea(shlockfile, 10 + threadnum, 1, F_WRLCK, TRUE);
850          if (procdata[threadnum].group >= 0)          if (procdata[threadnum].group >= 0)
851          {          {
852                  d = SHRMEMDATPOS(shdata, shdata->numvserver + procdata[threadnum].group);                  d = SHRMEMDATPOS(shdata,
853                                                     shdata->numvserver + procdata[threadnum].group);
854                  d->count--;                  d->count--;
855          }          }
856          shdata->serverusercount--;          shdata->serverusercount--;
# Line 743  void shinfo_freebynum(int threadnum) Line 859  void shinfo_freebynum(int threadnum)
859                  d = SHRMEMDATPOS(shdata, procdata[threadnum].vserver);                  d = SHRMEMDATPOS(shdata, procdata[threadnum].vserver);
860                  d->count--;                  d->count--;
861          }          }
862            
863          procdata[threadnum].group = -1;          procdata[threadnum].group = -1;
864          procdata[threadnum].vserver = -1;          procdata[threadnum].vserver = -1;
865          procdata[threadnum].pid = -nextthid;          procdata[threadnum].pid = -nextthid;
866          procdata[threadnum].ip = -1;          procdata[threadnum].ip = -1;
867          nextthid = threadnum;          nextthid = threadnum;
868                    
869          lockarea(shlockfile, threadnum + 10, 1, F_UNLCK, TRUE);          lockarea(shlockfile, threadnum + 10, 1, F_UNLCK, TRUE);
870          lockarea(shlockfile, 0, 3, F_UNLCK, TRUE);          lockarea(shlockfile, 0, 3, F_UNLCK, TRUE);
871  }  }
872    
873  void shinfo_freethreads(int freecount, pid_t *freelist)  void shinfo_freethreads(int freecount,
874                                                    pid_t * freelist)
875  {  {
876          int count = 0;          int count = 0;
877          int listcount;          int listcount;
878    
879          /* sort the freelist */          /*
880                     * sort the freelist
881          for(listcount = 0; listcount < freecount; listcount++)           */
882          {        
883            for (listcount = 0; listcount < freecount; listcount++)
884            {
885                  count = 0;                  count = 0;
886                  while(count < shdata->numrecs)                  while (count < shdata->numrecs)
887                  {                  {
888                          /* pid is safe since parent process is the only                          /*
889                             modifier */                           * pid is safe since parent process is the only
890                             * modifier
891                             */
892                          if (procdata[count].pid == freelist[listcount])                          if (procdata[count].pid == freelist[listcount])
893                                  shinfo_freebynum(count);                                  shinfo_freebynum(count);
894                                                                    
895                          count++;                          count++;
896                  }                  }
897          }          }
# Line 779  void shinfo_freethreads(int freecount, p Line 900  void shinfo_freethreads(int freecount, p
900  void pnums_signalchildren(int signalnum)  void pnums_signalchildren(int signalnum)
901  {  {
902          int count;          int count;
903    
904          for (count = 0; count < shdata->numrecs; count++)          for (count = 0; count < shdata->numrecs; count++)
905                  if (procdata[count].pid > 0)                  if (procdata[count].pid > 0)
906                          kill((pid_t)procdata[count].pid, signalnum);                          kill((pid_t) procdata[count].pid, signalnum);
907  }  }
908            
909  void shinfo_shutdown(void)  void shinfo_shutdown(void)
910  {  {
911          if (!inetd)          if (!inetd)
912          {          {
913                  /* kill the children kindly */                  /*
914                     * kill the children kindly
915                     */
916                  pnums_signalchildren(SIGTERM);                  pnums_signalchildren(SIGTERM);
917                  shmem_finish(shnumber);                  shmem_finish(shnumber);
918                  close(shlockfile);                  close(shlockfile);

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