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.
28 * $Id: convertilines.c 17867 2004-09-22 17:16:44Z androsyn $
38 #define FLAGS_RESTRICTED 0x001
39 #define FLAGS_EXCEEDLIMIT 0x002
40 #define FLAGS_KLINEEXEMPT 0x004
41 #define FLAGS_GLINEEXEMPT 0x008
42 #define FLAGS_NEEDIDENT 0x010
43 #define FLAGS_NOTILDE 0x020
45 struct flag_table_struct
50 static struct flag_table_struct flag_table
[] =
52 { "restricted", FLAGS_RESTRICTED
},
53 { "exceed_limit", FLAGS_EXCEEDLIMIT
},
54 { "kline_exempt", FLAGS_KLINEEXEMPT
},
55 { "gline_exempt", FLAGS_GLINEEXEMPT
},
56 { "need_ident", FLAGS_NEEDIDENT
},
57 { "no_tilde", FLAGS_NOTILDE
},
63 struct AuthBlock
*next
;
73 /* indicates one of above */
78 static struct AuthBlock
*auth_spoof
= NULL
;
79 static struct AuthBlock
*auth_special
= NULL
;
80 static struct AuthBlock
*auth_passwd
= NULL
;
81 static struct AuthBlock
*auth_general
= NULL
;
82 static struct AuthBlock
*auth_restricted
= NULL
;
84 static void ConvertConf(FILE* file
,FILE *out
);
85 static void set_flags(struct AuthBlock
*, const char *, const char *);
86 static void usage(void);
87 static char *getfield(char *);
88 static struct AuthBlock
*find_matching_conf(struct AuthBlock
*);
89 static void ReplaceQuotes(char *out
, char *in
);
90 static void oldParseOneLine(FILE *out
, char *in
);
91 static void write_auth_entries(FILE *out
);
92 static void write_specific(FILE *out
, struct AuthBlock
*);
93 static int match(struct AuthBlock
*, struct AuthBlock
*);
95 int main(int argc
,char *argv
[])
103 if((in
= fopen(argv
[1],"r")) == NULL
)
105 fprintf(stderr
, "Can't open %s for reading\n", argv
[1]);
109 if((out
= fopen(argv
[2],"w")) == NULL
)
111 fprintf(stderr
, "Can't open %s for writing\n", argv
[2]);
115 ConvertConf(in
, out
);
122 fprintf(stderr
, "convertilines conf.old conf.new\n");
128 * Read configuration file.
131 * Inputs - FILE* to config file to convert
132 * - FILE* to output for new style conf
136 #define MAXCONFLINKS 150
138 static void ConvertConf(FILE* file
, FILE *out
)
141 char quotedLine
[BUFSIZE
];
144 while (fgets(line
, sizeof(line
), file
))
146 if ((p
= strchr(line
, '\n')))
149 ReplaceQuotes(quotedLine
,line
);
151 if(!*quotedLine
|| quotedLine
[0] == '#' || quotedLine
[0] == '\n' ||
152 quotedLine
[0] == ' ' || quotedLine
[0] == '\t')
155 if(quotedLine
[0] == '.')
160 if(!strncmp(quotedLine
+1,"include ",8))
162 if( (filename
= strchr(quotedLine
+8,'"')) )
166 fprintf(stderr
, "Bad config line: %s", quotedLine
);
170 if((back
= strchr(filename
,'"')))
174 fprintf(stderr
, "Bad config line: %s", quotedLine
);
181 /* Could we test if it's conf line at all? -Vesa */
182 if (quotedLine
[1] == ':')
183 oldParseOneLine(out
,quotedLine
);
189 write_auth_entries(out
);
195 * Inputs - input line to quote
196 * Output - quoted line
197 * Side Effects - All quoted chars in input are replaced
198 * with quoted values in output, # chars replaced with '\0'
199 * otherwise input is copied to output.
201 static void ReplaceQuotes(char* quotedLine
,char *inputLine
)
205 static char quotes
[] = {
237 * Do quoting of characters and # detection.
239 for (out
= quotedLine
,in
= inputLine
; *in
; out
++, in
++)
249 *out
= quotes
[ (unsigned int) (*in
& 0x1F) ];
264 * Inputs - pointer to line to parse
265 * - pointer to output to write
267 * Side Effects - Parse one old style conf line.
270 static void oldParseOneLine(FILE *out
,char* line
)
274 const char* host_field
=NULL
;
275 const char* passwd_field
=NULL
;
276 const char* user_field
=NULL
;
277 const char* port_field
= NULL
;
278 const char* classconf_field
= NULL
;
281 tmp
= getfield(line
);
285 for (;;) /* Fake loop, that I can use break here --msa */
288 if ((host_field
= getfield(NULL
)) == NULL
)
292 if ((passwd_field
= getfield(NULL
)) == NULL
)
296 if ((user_field
= getfield(NULL
)) == NULL
)
300 if ((port_field
= getfield(NULL
)) == NULL
)
304 if ((classconf_field
= getfield(NULL
)) == NULL
)
317 class_field
= atoi(classconf_field
);
319 switch( conf_letter
)
324 struct AuthBlock
*ptr
;
325 struct AuthBlock
*tempptr
;
327 tempptr
= malloc(sizeof(struct AuthBlock
));
328 memset(tempptr
, 0, sizeof(*tempptr
));
330 if(conf_letter
== 'i')
332 tempptr
->flags
|= FLAGS_RESTRICTED
;
333 tempptr
->specialk
= 1;
336 if(passwd_field
&& *passwd_field
)
337 tempptr
->passwd
= strdup(passwd_field
);
339 tempptr
->class = class_field
;
341 set_flags(tempptr
, user_field
, host_field
);
343 /* dont add specials/passworded ones to existing auth blocks */
344 if((ptr
= find_matching_conf(tempptr
)))
348 authindex
= ptr
->hostnum
;
351 ptr
->hostname
= realloc((void *)ptr
->hostname
, ptr
->hostnum
* sizeof(void *));
353 ptr
->hostname
[authindex
] = strdup(tempptr
->hostname
[0]);
355 free(tempptr
->hostname
[0]);
356 free(tempptr
->hostname
);
365 ptr
->next
= auth_spoof
;
368 else if(ptr
->special
)
370 ptr
->next
= auth_special
;
375 ptr
->next
= auth_passwd
;
378 else if(ptr
->specialk
)
380 ptr
->next
= auth_restricted
;
381 auth_restricted
= ptr
;
385 ptr
->next
= auth_general
;
397 static void write_auth_entries(FILE *out
)
399 struct AuthBlock
*ptr
;
401 for(ptr
= auth_spoof
; ptr
; ptr
= ptr
->next
)
402 write_specific(out
, ptr
);
404 for(ptr
= auth_special
; ptr
; ptr
= ptr
->next
)
405 write_specific(out
, ptr
);
407 for(ptr
= auth_passwd
; ptr
; ptr
= ptr
->next
)
408 write_specific(out
, ptr
);
410 for(ptr
= auth_general
; ptr
; ptr
= ptr
->next
)
411 write_specific(out
, ptr
);
413 for(ptr
= auth_restricted
; ptr
; ptr
= ptr
->next
)
414 write_specific(out
, ptr
);
418 static void write_specific(FILE *out
, struct AuthBlock
*ptr
)
423 fprintf(out
, "auth {\n");
425 for(i
= 0; i
< ptr
->hostnum
; i
++)
426 fprintf(out
, "\tuser = \"%s\";\n", ptr
->hostname
[i
]);
429 fprintf(out
, "\tspoof = \"%s\";\n", ptr
->spoof
);
432 fprintf(out
, "\tpassword = \"%s\";\n", ptr
->passwd
);
436 fprintf(out
, "\tflags = ");
438 for(i
= 0; flag_table
[i
].flag
; i
++)
440 if(ptr
->flags
& flag_table
[i
].flag
)
452 fprintf(out
, "\tclass = \"%d\";\n", ptr
->class);
453 fprintf(out
, "};\n");
457 * field breakup for ircd.conf file.
459 static char *getfield(char *newline
)
461 static char *line
= NULL
;
471 if ((end
= strchr(line
,':')) == NULL
)
474 if ((end
= strchr(field
,'\n')) == NULL
)
475 end
= field
+ strlen(field
);
483 struct AuthBlock
*find_matching_conf(struct AuthBlock
*acptr
)
485 struct AuthBlock
*ptr
;
487 for(ptr
= auth_spoof
; ptr
; ptr
= ptr
->next
)
489 if(match(ptr
, acptr
))
493 for(ptr
= auth_special
; ptr
; ptr
= ptr
->next
)
495 if(match(ptr
, acptr
))
499 for(ptr
= auth_passwd
; ptr
; ptr
= ptr
->next
)
501 if(match(ptr
, acptr
))
505 for(ptr
= auth_restricted
; ptr
; ptr
= ptr
->next
)
507 if(match(ptr
, acptr
))
511 for(ptr
= auth_general
; ptr
; ptr
= ptr
->next
)
513 if(match(ptr
, acptr
))
521 static int match(struct AuthBlock
*ptr
, struct AuthBlock
*acptr
)
523 if((ptr
->class == acptr
->class) &&
524 (ptr
->flags
== acptr
->flags
))
528 /* check the spoofs match.. */
542 /* now check the passwords match.. */
562 void set_flags(struct AuthBlock
*ptr
, const char *user_field
, const char *host_field
)
564 for(; *user_field
; user_field
++)
570 ptr
->spoof
= strdup(host_field
);
576 ptr
->flags
|= FLAGS_NOTILDE
;
581 ptr
->flags
|= FLAGS_NEEDIDENT
;
585 case '^': /* is exempt from k/g lines */
586 ptr
->flags
|= FLAGS_KLINEEXEMPT
;
591 ptr
->flags
|= FLAGS_EXCEEDLIMIT
;
596 ptr
->flags
|= FLAGS_GLINEEXEMPT
;
610 authindex
= ptr
->hostnum
;
613 ptr
->hostname
= realloc((void *)ptr
->hostname
, ptr
->hostnum
* sizeof(void *));
615 /* if the IP field contains something useful, use that */
616 if(strcmp(host_field
, "NOMATCH") && (*host_field
!= 'x') &&
617 strcmp(host_field
, "*") && !ptr
->spoof
)
618 ptr
->hostname
[authindex
] = strdup(host_field
);
620 ptr
->hostname
[authindex
] = strdup(user_field
);