1 /* tools/convertilines.c
2 * Copyright (c) 2002 Hybrid Development Team
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
8 * 1.Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * 2.Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3.The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
20 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
24 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
25 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
37 #define FLAGS_RESTRICTED 0x001
38 #define FLAGS_EXCEEDLIMIT 0x002
39 #define FLAGS_KLINEEXEMPT 0x004
40 #define FLAGS_NEEDIDENT 0x010
41 #define FLAGS_NOTILDE 0x020
43 struct flag_table_struct
48 static struct flag_table_struct flag_table
[] =
50 { "restricted", FLAGS_RESTRICTED
},
51 { "exceed_limit", FLAGS_EXCEEDLIMIT
},
52 { "kline_exempt", FLAGS_KLINEEXEMPT
},
53 { "need_ident", FLAGS_NEEDIDENT
},
54 { "no_tilde", FLAGS_NOTILDE
},
60 struct AuthBlock
*next
;
70 /* indicates one of above */
75 static struct AuthBlock
*auth_spoof
= NULL
;
76 static struct AuthBlock
*auth_special
= NULL
;
77 static struct AuthBlock
*auth_passwd
= NULL
;
78 static struct AuthBlock
*auth_general
= NULL
;
79 static struct AuthBlock
*auth_restricted
= NULL
;
81 static void ConvertConf(FILE* file
,FILE *out
);
82 static void set_flags(struct AuthBlock
*, const char *, const char *);
83 static void usage(void);
84 static char *getfield(char *);
85 static struct AuthBlock
*find_matching_conf(struct AuthBlock
*);
86 static void ReplaceQuotes(char *out
, char *in
);
87 static void oldParseOneLine(FILE *out
, char *in
);
88 static void write_auth_entries(FILE *out
);
89 static void write_specific(FILE *out
, struct AuthBlock
*);
90 static int match(struct AuthBlock
*, struct AuthBlock
*);
92 int main(int argc
,char *argv
[])
100 if((in
= fopen(argv
[1],"r")) == NULL
)
102 fprintf(stderr
, "Can't open %s for reading\n", argv
[1]);
106 if((out
= fopen(argv
[2],"w")) == NULL
)
108 fprintf(stderr
, "Can't open %s for writing\n", argv
[2]);
112 ConvertConf(in
, out
);
119 fprintf(stderr
, "convertilines conf.old conf.new\n");
125 * Read configuration file.
128 * Inputs - FILE* to config file to convert
129 * - FILE* to output for new style conf
133 #define MAXCONFLINKS 150
135 static void ConvertConf(FILE* file
, FILE *out
)
138 char quotedLine
[BUFSIZE
];
141 while (fgets(line
, sizeof(line
), file
))
143 if ((p
= strchr(line
, '\n')))
146 ReplaceQuotes(quotedLine
,line
);
148 if(!*quotedLine
|| quotedLine
[0] == '#' || quotedLine
[0] == '\n' ||
149 quotedLine
[0] == ' ' || quotedLine
[0] == '\t')
152 if(quotedLine
[0] == '.')
157 if(!strncmp(quotedLine
+1,"include ",8))
159 if( (filename
= strchr(quotedLine
+8,'"')) )
163 fprintf(stderr
, "Bad config line: %s", quotedLine
);
167 if((back
= strchr(filename
,'"')))
171 fprintf(stderr
, "Bad config line: %s", quotedLine
);
178 /* Could we test if it's conf line at all? -Vesa */
179 if (quotedLine
[1] == ':')
180 oldParseOneLine(out
,quotedLine
);
186 write_auth_entries(out
);
192 * Inputs - input line to quote
193 * Output - quoted line
194 * Side Effects - All quoted chars in input are replaced
195 * with quoted values in output, # chars replaced with '\0'
196 * otherwise input is copied to output.
198 static void ReplaceQuotes(char* quotedLine
,char *inputLine
)
202 static char quotes
[] = {
234 * Do quoting of characters and # detection.
236 for (out
= quotedLine
,in
= inputLine
; *in
; out
++, in
++)
246 *out
= quotes
[ (unsigned int) (*in
& 0x1F) ];
261 * Inputs - pointer to line to parse
262 * - pointer to output to write
264 * Side Effects - Parse one old style conf line.
267 static void oldParseOneLine(FILE *out
,char* line
)
271 const char* host_field
=NULL
;
272 const char* passwd_field
=NULL
;
273 const char* user_field
=NULL
;
274 const char* port_field
= NULL
;
275 const char* classconf_field
= NULL
;
278 tmp
= getfield(line
);
282 for (;;) /* Fake loop, that I can use break here --msa */
285 if ((host_field
= getfield(NULL
)) == NULL
)
289 if ((passwd_field
= getfield(NULL
)) == NULL
)
293 if ((user_field
= getfield(NULL
)) == NULL
)
297 if ((port_field
= getfield(NULL
)) == NULL
)
301 if ((classconf_field
= getfield(NULL
)) == NULL
)
314 class_field
= atoi(classconf_field
);
316 switch( conf_letter
)
321 struct AuthBlock
*ptr
;
322 struct AuthBlock
*tempptr
;
324 tempptr
= malloc(sizeof(struct AuthBlock
));
325 memset(tempptr
, 0, sizeof(*tempptr
));
327 if(conf_letter
== 'i')
329 tempptr
->flags
|= FLAGS_RESTRICTED
;
330 tempptr
->specialk
= 1;
333 if(passwd_field
&& *passwd_field
)
334 tempptr
->passwd
= strdup(passwd_field
);
336 tempptr
->class = class_field
;
338 set_flags(tempptr
, user_field
, host_field
);
340 /* dont add specials/passworded ones to existing auth blocks */
341 if((ptr
= find_matching_conf(tempptr
)))
345 authindex
= ptr
->hostnum
;
348 ptr
->hostname
= realloc((void *)ptr
->hostname
, ptr
->hostnum
* sizeof(void *));
350 ptr
->hostname
[authindex
] = strdup(tempptr
->hostname
[0]);
352 free(tempptr
->hostname
[0]);
353 free(tempptr
->hostname
);
362 ptr
->next
= auth_spoof
;
365 else if(ptr
->special
)
367 ptr
->next
= auth_special
;
372 ptr
->next
= auth_passwd
;
375 else if(ptr
->specialk
)
377 ptr
->next
= auth_restricted
;
378 auth_restricted
= ptr
;
382 ptr
->next
= auth_general
;
394 static void write_auth_entries(FILE *out
)
396 struct AuthBlock
*ptr
;
398 for(ptr
= auth_spoof
; ptr
; ptr
= ptr
->next
)
399 write_specific(out
, ptr
);
401 for(ptr
= auth_special
; ptr
; ptr
= ptr
->next
)
402 write_specific(out
, ptr
);
404 for(ptr
= auth_passwd
; ptr
; ptr
= ptr
->next
)
405 write_specific(out
, ptr
);
407 for(ptr
= auth_general
; ptr
; ptr
= ptr
->next
)
408 write_specific(out
, ptr
);
410 for(ptr
= auth_restricted
; ptr
; ptr
= ptr
->next
)
411 write_specific(out
, ptr
);
415 static void write_specific(FILE *out
, struct AuthBlock
*ptr
)
420 fprintf(out
, "auth {\n");
422 for(i
= 0; i
< ptr
->hostnum
; i
++)
423 fprintf(out
, "\tuser = \"%s\";\n", ptr
->hostname
[i
]);
426 fprintf(out
, "\tspoof = \"%s\";\n", ptr
->spoof
);
429 fprintf(out
, "\tpassword = \"%s\";\n", ptr
->passwd
);
433 fprintf(out
, "\tflags = ");
435 for(i
= 0; flag_table
[i
].flag
; i
++)
437 if(ptr
->flags
& flag_table
[i
].flag
)
449 fprintf(out
, "\tclass = \"%d\";\n", ptr
->class);
450 fprintf(out
, "};\n");
454 * field breakup for ircd.conf file.
456 static char *getfield(char *newline
)
458 static char *line
= NULL
;
468 if ((end
= strchr(line
,':')) == NULL
)
471 if ((end
= strchr(field
,'\n')) == NULL
)
472 end
= field
+ strlen(field
);
480 struct AuthBlock
*find_matching_conf(struct AuthBlock
*acptr
)
482 struct AuthBlock
*ptr
;
484 for(ptr
= auth_spoof
; ptr
; ptr
= ptr
->next
)
486 if(match(ptr
, acptr
))
490 for(ptr
= auth_special
; ptr
; ptr
= ptr
->next
)
492 if(match(ptr
, acptr
))
496 for(ptr
= auth_passwd
; ptr
; ptr
= ptr
->next
)
498 if(match(ptr
, acptr
))
502 for(ptr
= auth_restricted
; ptr
; ptr
= ptr
->next
)
504 if(match(ptr
, acptr
))
508 for(ptr
= auth_general
; ptr
; ptr
= ptr
->next
)
510 if(match(ptr
, acptr
))
518 static int match(struct AuthBlock
*ptr
, struct AuthBlock
*acptr
)
520 if((ptr
->class == acptr
->class) &&
521 (ptr
->flags
== acptr
->flags
))
525 /* check the spoofs match.. */
539 /* now check the passwords match.. */
559 void set_flags(struct AuthBlock
*ptr
, const char *user_field
, const char *host_field
)
561 for(; *user_field
; user_field
++)
567 ptr
->spoof
= strdup(host_field
);
573 ptr
->flags
|= FLAGS_NOTILDE
;
578 ptr
->flags
|= FLAGS_NEEDIDENT
;
582 case '^': /* is exempt from k/g lines */
583 ptr
->flags
|= FLAGS_KLINEEXEMPT
;
588 ptr
->flags
|= FLAGS_EXCEEDLIMIT
;
602 authindex
= ptr
->hostnum
;
605 ptr
->hostname
= realloc((void *)ptr
->hostname
, ptr
->hostnum
* sizeof(void *));
607 /* if the IP field contains something useful, use that */
608 if(strcmp(host_field
, "NOMATCH") && (*host_field
!= 'x') &&
609 strcmp(host_field
, "*") && !ptr
->spoof
)
610 ptr
->hostname
[authindex
] = strdup(host_field
);
612 ptr
->hostname
[authindex
] = strdup(user_field
);