]> andersk Git - moira.git/blob - clients/userreg/disable.c
${CRYPT} in Imakefile
[moira.git] / clients / userreg / disable.c
1 /* $Header$
2  *
3  * disabled: check to see if registration is enabled right now.  Most of this
4  * code is stolen from the cron daemon.
5  *
6  *  (c) Copyright 1988 by the Massachusetts Institute of Technology.
7  *  For copying and distribution information, please see the file
8  *  <mit-copyright.h>.
9  */
10
11 #include <mit-copyright.h>
12 #include <stdio.h>
13 #include <ctype.h>
14 #include <sys/types.h>
15 #include <sys/time.h>
16 #ifdef _AUX_SOURCE
17 #include <time.h>
18 #endif /* _AUX_SOURCE */
19 #include <sys/file.h>
20 #include "files.h"
21
22 #define LISTS   (2*BUFSIZ)
23 #define MAXLIN  BUFSIZ
24
25 #define EXACT   100
26 #define ANY     101
27 #define LIST    102
28 #define RANGE   103
29 #define EOS     104
30
31 time_t  itime, time();
32 struct  tm *loct;
33 struct  tm *localtime();
34 char    *malloc();
35 char    *realloc();
36 int     flag;
37 char    *list;
38 char    *listend;
39 unsigned listsize;
40
41 char *cmp();
42
43 /* This routine will determine if registration is enabled at this time.  If
44  * NULL is returned, registration is OK.  Otherwise, the string returned
45  * will indicate the time that registration will be re-enabled.
46  */
47
48 char *disabled(msg)
49 char **msg;
50 {
51     register char *cp;
52     int hit;
53
54     *msg = 0;
55     init();
56     append(DISABLE_FILE);
57     *listend++ = EOS;
58     *listend++ = EOS;
59
60     (void) time(&itime);
61     itime -= localtime(&itime)->tm_sec;
62     loct = localtime(&itime);
63     loct->tm_mon++;              /* 1-12 for month */
64     if (loct->tm_wday == 0)
65       loct->tm_wday = 7;        /* sunday is 7, not 0 */
66     hit = 0;
67     for(cp = list; *cp != EOS;) {
68         flag = 0;
69         cp = cmp(cp, loct->tm_min);
70         cp = cmp(cp, loct->tm_hour);
71         cp = cmp(cp, loct->tm_mday);
72         cp = cmp(cp, loct->tm_mon);
73         cp = cmp(cp, loct->tm_wday);
74         if(flag == 0) {
75             *msg = cp;
76             hit++;
77             break;
78         }
79         while(*cp++ != 0)
80           ;
81     }
82     if (!hit)
83       return(NULL);
84     while (hit) {
85         itime += 60;            /* add a minute */
86         loct = localtime(&itime);
87         loct->tm_mon++;         /* 1-12 for month */
88         if (loct->tm_wday == 0)
89           loct->tm_wday = 7;    /* sunday is 7, not 0 */
90         hit = 0;
91         for(cp = list; *cp != EOS;) {
92             flag = 0;
93             cp = cmp(cp, loct->tm_min);
94             cp = cmp(cp, loct->tm_hour);
95             cp = cmp(cp, loct->tm_mday);
96             cp = cmp(cp, loct->tm_mon);
97             cp = cmp(cp, loct->tm_wday);
98             if(flag == 0) {
99                 hit++;
100                 break;
101             }
102             while(*cp++ != 0)
103               ;
104         }
105     }
106     return(ctime(&itime));
107 }
108
109 char *
110 cmp(p, v)
111 char *p;
112 {
113         register char *cp;
114
115         cp = p;
116         switch(*cp++) {
117
118         case EXACT:
119                 if (*cp++ != v)
120                         flag++;
121                 return(cp);
122
123         case ANY:
124                 return(cp);
125
126         case LIST:
127                 while(*cp != LIST)
128                         if(*cp++ == v) {
129                                 while(*cp++ != LIST)
130                                         ;
131                                 return(cp);
132                         }
133                 flag++;
134                 return(cp+1);
135
136         case RANGE:
137                 if(*cp > v || cp[1] < v)
138                         flag++;
139                 return(cp+2);
140         }
141         if(cp[-1] != v)
142                 flag++;
143         return(cp);
144 }
145
146 init()
147 {
148         /*
149          * Don't free in case was longer than LISTS.  Trades off
150          * the rare case of crontab shrinking vs. the common case of
151          * extra realloc's needed in append() for a large crontab.
152          */
153         if (list == 0) {
154                 list = malloc(LISTS);
155                 listsize = LISTS;
156         }
157         listend = list;
158 }
159
160 append(fn)
161 char *fn;
162 {
163         register int i, c;
164         register char *cp;
165         register char *ocp;
166         register int n;
167         FILE *f, *fopen();
168
169         if ((f = fopen(fn, "r")) == (FILE *) NULL)
170                 return;
171         cp = listend;
172 loop:
173         if(cp > list+listsize-MAXLIN) {
174                 int length = cp - list;
175
176                 listsize += LISTS;
177                 list = realloc(list, listsize);
178                 cp = list + length;
179         }
180         ocp = cp;
181         for(i=0;; i++) {
182                 do
183                         c = getc(f);
184                 while(c == ' ' || c == '\t')
185                         ;
186                 if(c == EOF || c == '\n')
187                         goto ignore;
188                 if(i == 5)
189                         break;
190                 if(c == '*') {
191                         *cp++ = ANY;
192                         continue;
193                 }
194                 if ((n = number(c, f)) < 0)
195                         goto ignore;
196                 c = getc(f);
197                 if(c == ',')
198                         goto mlist;
199                 if(c == '-')
200                         goto mrange;
201                 if(c != '\t' && c != ' ')
202                         goto ignore;
203                 *cp++ = EXACT;
204                 *cp++ = n;
205                 continue;
206
207         mlist:
208                 *cp++ = LIST;
209                 *cp++ = n;
210                 do {
211                         if ((n = number(getc(f), f)) < 0)
212                                 goto ignore;
213                         *cp++ = n;
214                         c = getc(f);
215                 } while (c==',');
216                 if(c != '\t' && c != ' ')
217                         goto ignore;
218                 *cp++ = LIST;
219                 continue;
220
221         mrange:
222                 *cp++ = RANGE;
223                 *cp++ = n;
224                 if ((n = number(getc(f), f)) < 0)
225                         goto ignore;
226                 c = getc(f);
227                 if(c != '\t' && c != ' ')
228                         goto ignore;
229                 *cp++ = n;
230         }
231         while(c != '\n') {
232                 if(c == EOF)
233                         goto ignore;
234                 if(c == '%')
235                         c = '\n';
236                 *cp++ = c;
237                 c = getc(f);
238         }
239         *cp++ = '\n';
240         *cp++ = 0;
241         goto loop;
242
243 ignore:
244         cp = ocp;
245         while(c != '\n') {
246                 if(c == EOF) {
247                         (void) fclose(f);
248                         listend = cp;
249                         return;
250                 }
251                 c = getc(f);
252         }
253         goto loop;
254 }
255
256 number(c, f)
257 register char c;
258 FILE *f;
259 {
260         register int n = 0;
261
262         while (isdigit(c)) {
263                 n = n*10 + c - '0';
264                 c = getc(f);
265         }
266         (void) ungetc(c, f);
267         if (n>=100)
268                 return(-1);
269         return(n);
270 }
271
This page took 0.339371 seconds and 5 git commands to generate.