e82f7154 |
1 | /* -*-c-*- |
2 | * |
5f12a82a |
3 | * $Id: ident.c,v 1.2 1999/07/26 23:26:44 mdw Exp $ |
e82f7154 |
4 | * |
5 | * Parse replies from RFC931 servers |
6 | * |
5f12a82a |
7 | * (c) 1999 Straylight/Edgeware |
e82f7154 |
8 | */ |
9 | |
10 | /*----- Licensing notice --------------------------------------------------* |
11 | * |
12 | * This file is part of the `fw' port forwarder. |
13 | * |
14 | * `fw' is free software; you can redistribute it and/or modify |
15 | * it under the terms of the GNU General Public License as published by |
16 | * the Free Software Foundation; either version 2 of the License, or |
17 | * (at your option) any later version. |
18 | * |
19 | * `fw' is distributed in the hope that it will be useful, |
20 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
21 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
22 | * GNU General Public License for more details. |
23 | * |
24 | * You should have received a copy of the GNU General Public License |
25 | * along with `fw'; if not, write to the Free Software Foundation, |
26 | * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
27 | */ |
28 | |
29 | /*----- Revision history --------------------------------------------------* |
30 | * |
31 | * $Log: ident.c,v $ |
5f12a82a |
32 | * Revision 1.2 1999/07/26 23:26:44 mdw |
33 | * Change copyright notice. |
34 | * |
35 | * Revision 1.1.1.1 1999/07/01 08:56:23 mdw |
36 | * Initial revision. |
e82f7154 |
37 | * |
38 | * Revision 1.1.1.1 1999/05/21 22:22:27 mdw |
39 | * Initial import. Code not really finished yet. |
40 | * |
41 | */ |
42 | |
43 | /*----- Header files ------------------------------------------------------*/ |
44 | |
45 | #include <ctype.h> |
46 | #include <stdio.h> |
47 | #include <stdlib.h> |
48 | #include <string.h> |
49 | |
50 | #include "ident.h" |
51 | |
52 | /*----- Main code ---------------------------------------------------------*/ |
53 | |
54 | /* --- @next@ --- * |
55 | * |
56 | * Arguments: @char **pp@ = address of string pointer |
57 | * |
58 | * Returns: Address of next token. |
59 | * |
60 | * Use: Reads the next token from the result string. Tokens are |
61 | * terminated by whitespace, or `:' or `,' characters. The |
62 | * result string has had `\' escapes removed; it's stored in |
63 | * the same memory as the input string. The actual content |
64 | * of the delimiter doesn't seem to be interesting, so it |
65 | * gets thrown away. |
66 | */ |
67 | |
68 | static char *next(char **pp) |
69 | { |
70 | char *p, *q, *r; |
71 | |
72 | /* --- Deal with reads past the end of the string --- */ |
73 | |
74 | if (!*pp) |
75 | return (""); |
76 | |
77 | /* --- Initialize various pointers into the string --- */ |
78 | |
79 | p = q = r = *pp; |
80 | |
81 | /* --- Skip past any leading whitespace --- */ |
82 | |
83 | while (isspace((unsigned char)*p)) |
84 | p++; |
85 | |
86 | /* --- Now start work on the string itself --- */ |
87 | |
88 | for (;;) { |
89 | if (*p == 0 || *p == ':' || *p == ',' || isspace((unsigned char)*p)) |
90 | break; |
91 | else if (*p == '\\') { |
92 | p++; |
93 | if (!*p) { |
94 | *q++ = '\\'; |
95 | break; |
96 | } |
97 | } |
98 | *q++ = *p++; |
99 | } |
100 | |
101 | /* --- Tidy up afterwards --- */ |
102 | |
103 | while (isspace((unsigned char)*p)) |
104 | p++; |
105 | if (*p == 0) |
106 | *pp = 0; |
107 | else if (*p == ':' || *p == ',') |
108 | *pp = p + 1; |
109 | else |
110 | *pp = p; |
111 | *q = 0; |
112 | |
113 | return (r); |
114 | } |
115 | |
116 | /* --- @ident_parse@ --- * |
117 | * |
118 | * Arguments: @char *p@ = pointer to input string from identd |
119 | * @ident *i@ = pointer to output block |
120 | * |
121 | * Returns: --- |
122 | * |
123 | * Use: Parses a result string from an RFC931 (identd) server. |
124 | */ |
125 | |
126 | void ident_parse(char *p, ident *i) |
127 | { |
128 | char *q; |
129 | |
130 | /* --- Read the source and destination port numbers --- */ |
131 | |
132 | i->sport = atoi(next(&p)); |
133 | i->dport = atoi(next(&p)); |
134 | |
135 | /* --- Find out what sort of a reply this is --- */ |
136 | |
137 | q = next(&p); |
138 | if (strcmp(q, "USERID") == 0) { |
139 | i->type = ident_userid; |
140 | i->u.userid.os = next(&p); |
141 | i->u.userid.user = next(&p); |
142 | } else if (strcmp(q, "ERROR") == 0) { |
143 | i->type = ident_error; |
144 | i->u.error = next(&p); |
145 | } else |
146 | i->type = ident_bad; |
147 | } |
148 | |
149 | /*----- That's all, folks -------------------------------------------------*/ |