masqmail-0.2
diff 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 diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/libident/id_parse.c Fri Sep 26 17:05:23 2008 +0200 1.3 @@ -0,0 +1,229 @@ 1.4 +/* 1.5 +** id_parse.c Receive and parse a reply from an IDENT server 1.6 +** 1.7 +** Author: Peter Eriksson <pen@lysator.liu.se> 1.8 +** Fiddling: Pär Emanuelsson <pell@lysator.liu.se> 1.9 +*/ 1.10 + 1.11 +#ifdef NeXT3 1.12 +# include <libc.h> 1.13 +#endif 1.14 + 1.15 +#include <stdio.h> 1.16 +#include <string.h> 1.17 +#include <errno.h> 1.18 +#include <ctype.h> 1.19 + 1.20 +#ifdef HAVE_ANSIHEADERS 1.21 +# include <stdlib.h> 1.22 +# include <string.h> 1.23 +# include <unistd.h> 1.24 +#endif 1.25 + 1.26 +#include <sys/types.h> 1.27 +#include <sys/wait.h> 1.28 +#include <sys/time.h> 1.29 + 1.30 +#ifdef _AIX 1.31 +# include <sys/select.h> 1.32 +#endif 1.33 + 1.34 +#ifdef _AIX 1.35 +# include <sys/select.h> 1.36 +#endif 1.37 +#ifdef VMS 1.38 +# include <sys/socket.h> /* for fd_set */ 1.39 +#endif 1.40 +#define IN_LIBIDENT_SRC 1.41 +#include "ident.h" 1.42 + 1.43 + 1.44 +/* 1.45 +int id_parse __P7(ident_t *, id, 1.46 + struct timeval *, timeout, 1.47 + int *, lport, 1.48 + int *, fport, 1.49 + char **, identifier, 1.50 + char **, opsys, 1.51 + char **, charset) 1.52 +*/ 1.53 + 1.54 +int id_parse __P(( ident_t *id, 1.55 + __STRUCT_TIMEVAL_P timeout, 1.56 + int *lport, 1.57 + int *fport, 1.58 + char **identifier, 1.59 + char **opsys, 1.60 + char **charset)) 1.61 +{ 1.62 + char c, *cp, *tmp_charset; 1.63 + fd_set rs; 1.64 + int pos, res=0, lp, fp; 1.65 + 1.66 + errno = 0; 1.67 + 1.68 + tmp_charset = 0; 1.69 + 1.70 + if (!id) 1.71 + return -1; 1.72 + if (lport) 1.73 + *lport = 0; 1.74 + if (fport) 1.75 + *fport = 0; 1.76 + if (identifier) 1.77 + *identifier = 0; 1.78 + if (opsys) 1.79 + *opsys = 0; 1.80 + if (charset) 1.81 + *charset = 0; 1.82 + 1.83 + pos = strlen(id->buf); 1.84 + 1.85 + if (timeout) 1.86 + { 1.87 + FD_ZERO(&rs); 1.88 + FD_SET(id->fd, &rs); 1.89 + 1.90 +#ifdef __hpux 1.91 + if ((res = select(FD_SETSIZE, (int *) &rs, (int *)0, (int *)0, timeout)) < 0) 1.92 +#else 1.93 + if ((res = select(FD_SETSIZE, &rs, (fd_set *)0, (fd_set *)0, timeout)) < 0) 1.94 +#endif 1.95 + return -1; 1.96 + 1.97 + if (res == 0) 1.98 + { 1.99 + errno = ETIMEDOUT; 1.100 + return -1; 1.101 + } 1.102 + } 1.103 + 1.104 + /* Every octal value is allowed except 0, \n and \r */ 1.105 + while (pos < sizeof(id->buf) && 1.106 + (res = read(id->fd, id->buf + pos, 1)) == 1 && 1.107 + id->buf[pos] != '\n' && id->buf[pos] != '\r') 1.108 + pos++; 1.109 + 1.110 + if (res < 0) 1.111 + return -1; 1.112 + 1.113 + if (res == 0) 1.114 + { 1.115 + errno = ENOTCONN; 1.116 + return -1; 1.117 + } 1.118 + 1.119 + if (id->buf[pos] != '\n' && id->buf[pos] != '\r') 1.120 + return 0; /* Not properly terminated string */ 1.121 + 1.122 + id->buf[pos++] = '\0'; 1.123 + 1.124 + /* 1.125 + ** Get first field (<lport> , <fport>) 1.126 + */ 1.127 + cp = id_strtok(id->buf, ":", &c); 1.128 + if (!cp) 1.129 + return -2; 1.130 + 1.131 + if (sscanf(cp, " %d , %d", &lp, &fp) != 2) 1.132 + { 1.133 + if (identifier) 1.134 + { 1.135 + *identifier = id_strdup(cp); 1.136 + if (*identifier == NULL) 1.137 + return -4; 1.138 + } 1.139 + return -2; 1.140 + } 1.141 + 1.142 + if (lport) 1.143 + *lport = lp; 1.144 + if (fport) 1.145 + *fport = fp; 1.146 + 1.147 + /* 1.148 + ** Get second field (USERID or ERROR) 1.149 + */ 1.150 + cp = id_strtok((char *)0, ":", &c); 1.151 + if (!cp) 1.152 + return -2; 1.153 + 1.154 + if (strcmp(cp, "ERROR") == 0) 1.155 + { 1.156 + cp = id_strtok((char *)0, "\n\r", &c); 1.157 + if (!cp) 1.158 + return -2; 1.159 + 1.160 + if (identifier) 1.161 + { 1.162 + *identifier = id_strdup(cp); 1.163 + if (*identifier == NULL) 1.164 + return -4; 1.165 + } 1.166 + 1.167 + return 2; 1.168 + } 1.169 + else if (strcmp(cp, "USERID") == 0) 1.170 + { 1.171 + /* 1.172 + ** Get first subfield of third field <opsys> 1.173 + */ 1.174 + cp = id_strtok((char *) 0, ",:", &c); 1.175 + if (!cp) 1.176 + return -2; 1.177 + 1.178 + if (opsys) 1.179 + { 1.180 + *opsys = id_strdup(cp); 1.181 + if (*opsys == NULL) 1.182 + return -4; 1.183 + } 1.184 + 1.185 + /* 1.186 + ** We have a second subfield (<charset>) 1.187 + */ 1.188 + if (c == ',') 1.189 + { 1.190 + cp = id_strtok((char *)0, ":", &c); 1.191 + if (!cp) 1.192 + return -2; 1.193 + 1.194 + tmp_charset = cp; 1.195 + if (charset) 1.196 + { 1.197 + *charset = id_strdup(cp); 1.198 + if (*charset == NULL) 1.199 + return -4; 1.200 + } 1.201 + 1.202 + /* 1.203 + ** We have even more subfields - ignore them 1.204 + */ 1.205 + if (c == ',') 1.206 + id_strtok((char *)0, ":", &c); 1.207 + } 1.208 + 1.209 + if (tmp_charset && strcmp(tmp_charset, "OCTET") == 0) 1.210 + cp = id_strtok((char *)0, (char *)0, &c); 1.211 + else 1.212 + cp = id_strtok((char *)0, "\n\r", &c); 1.213 + 1.214 + if (identifier) 1.215 + { 1.216 + *identifier = id_strdup(cp); 1.217 + if (*identifier == NULL) 1.218 + return -4; 1.219 + } 1.220 + return 1; 1.221 + } 1.222 + else 1.223 + { 1.224 + if (identifier) 1.225 + { 1.226 + *identifier = id_strdup(cp); 1.227 + if (*identifier == NULL) 1.228 + return -4; 1.229 + } 1.230 + return -3; 1.231 + } 1.232 +}