masqmail-0.2

view src/libident/id_parse.c @ 0:08114f7dcc23

this is masqmail-0.2.21 from oliver kurth
author meillo@marmaro.de
date Fri, 26 Sep 2008 17:05:23 +0200
parents
children 26e34ae9a3e3
line source
1 /*
2 ** id_parse.c Receive and parse a reply from an IDENT server
3 **
4 ** Author: Peter Eriksson <pen@lysator.liu.se>
5 ** Fiddling: Pär Emanuelsson <pell@lysator.liu.se>
6 */
8 #ifdef NeXT3
9 # include <libc.h>
10 #endif
12 #include <stdio.h>
13 #include <string.h>
14 #include <errno.h>
15 #include <ctype.h>
17 #ifdef HAVE_ANSIHEADERS
18 # include <stdlib.h>
19 # include <string.h>
20 # include <unistd.h>
21 #endif
23 #include <sys/types.h>
24 #include <sys/wait.h>
25 #include <sys/time.h>
27 #ifdef _AIX
28 # include <sys/select.h>
29 #endif
31 #ifdef _AIX
32 # include <sys/select.h>
33 #endif
34 #ifdef VMS
35 # include <sys/socket.h> /* for fd_set */
36 #endif
37 #define IN_LIBIDENT_SRC
38 #include "ident.h"
41 /*
42 int id_parse __P7(ident_t *, id,
43 struct timeval *, timeout,
44 int *, lport,
45 int *, fport,
46 char **, identifier,
47 char **, opsys,
48 char **, charset)
49 */
51 int id_parse __P(( ident_t *id,
52 __STRUCT_TIMEVAL_P timeout,
53 int *lport,
54 int *fport,
55 char **identifier,
56 char **opsys,
57 char **charset))
58 {
59 char c, *cp, *tmp_charset;
60 fd_set rs;
61 int pos, res=0, lp, fp;
63 errno = 0;
65 tmp_charset = 0;
67 if (!id)
68 return -1;
69 if (lport)
70 *lport = 0;
71 if (fport)
72 *fport = 0;
73 if (identifier)
74 *identifier = 0;
75 if (opsys)
76 *opsys = 0;
77 if (charset)
78 *charset = 0;
80 pos = strlen(id->buf);
82 if (timeout)
83 {
84 FD_ZERO(&rs);
85 FD_SET(id->fd, &rs);
87 #ifdef __hpux
88 if ((res = select(FD_SETSIZE, (int *) &rs, (int *)0, (int *)0, timeout)) < 0)
89 #else
90 if ((res = select(FD_SETSIZE, &rs, (fd_set *)0, (fd_set *)0, timeout)) < 0)
91 #endif
92 return -1;
94 if (res == 0)
95 {
96 errno = ETIMEDOUT;
97 return -1;
98 }
99 }
101 /* Every octal value is allowed except 0, \n and \r */
102 while (pos < sizeof(id->buf) &&
103 (res = read(id->fd, id->buf + pos, 1)) == 1 &&
104 id->buf[pos] != '\n' && id->buf[pos] != '\r')
105 pos++;
107 if (res < 0)
108 return -1;
110 if (res == 0)
111 {
112 errno = ENOTCONN;
113 return -1;
114 }
116 if (id->buf[pos] != '\n' && id->buf[pos] != '\r')
117 return 0; /* Not properly terminated string */
119 id->buf[pos++] = '\0';
121 /*
122 ** Get first field (<lport> , <fport>)
123 */
124 cp = id_strtok(id->buf, ":", &c);
125 if (!cp)
126 return -2;
128 if (sscanf(cp, " %d , %d", &lp, &fp) != 2)
129 {
130 if (identifier)
131 {
132 *identifier = id_strdup(cp);
133 if (*identifier == NULL)
134 return -4;
135 }
136 return -2;
137 }
139 if (lport)
140 *lport = lp;
141 if (fport)
142 *fport = fp;
144 /*
145 ** Get second field (USERID or ERROR)
146 */
147 cp = id_strtok((char *)0, ":", &c);
148 if (!cp)
149 return -2;
151 if (strcmp(cp, "ERROR") == 0)
152 {
153 cp = id_strtok((char *)0, "\n\r", &c);
154 if (!cp)
155 return -2;
157 if (identifier)
158 {
159 *identifier = id_strdup(cp);
160 if (*identifier == NULL)
161 return -4;
162 }
164 return 2;
165 }
166 else if (strcmp(cp, "USERID") == 0)
167 {
168 /*
169 ** Get first subfield of third field <opsys>
170 */
171 cp = id_strtok((char *) 0, ",:", &c);
172 if (!cp)
173 return -2;
175 if (opsys)
176 {
177 *opsys = id_strdup(cp);
178 if (*opsys == NULL)
179 return -4;
180 }
182 /*
183 ** We have a second subfield (<charset>)
184 */
185 if (c == ',')
186 {
187 cp = id_strtok((char *)0, ":", &c);
188 if (!cp)
189 return -2;
191 tmp_charset = cp;
192 if (charset)
193 {
194 *charset = id_strdup(cp);
195 if (*charset == NULL)
196 return -4;
197 }
199 /*
200 ** We have even more subfields - ignore them
201 */
202 if (c == ',')
203 id_strtok((char *)0, ":", &c);
204 }
206 if (tmp_charset && strcmp(tmp_charset, "OCTET") == 0)
207 cp = id_strtok((char *)0, (char *)0, &c);
208 else
209 cp = id_strtok((char *)0, "\n\r", &c);
211 if (identifier)
212 {
213 *identifier = id_strdup(cp);
214 if (*identifier == NULL)
215 return -4;
216 }
217 return 1;
218 }
219 else
220 {
221 if (identifier)
222 {
223 *identifier = id_strdup(cp);
224 if (*identifier == NULL)
225 return -4;
226 }
227 return -3;
228 }
229 }