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: */ 7: 8: #ifdef NeXT3 9: # include <libc.h> 10: #endif 11: 12: #include <stdio.h> 13: #include <string.h> 14: #include <errno.h> 15: #include <ctype.h> 16: 17: #ifdef HAVE_ANSIHEADERS 18: # include <stdlib.h> 19: # include <string.h> 20: # include <unistd.h> 21: #endif 22: 23: #include <sys/types.h> 24: #include <sys/wait.h> 25: #include <sys/time.h> 26: 27: #ifdef _AIX 28: # include <sys/select.h> 29: #endif 30: 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" 39: 40: 41: int id_parse __P7(ident_t *, id, 42: struct timeval *, timeout, 43: int *, lport, 44: int *, fport, 45: char **, identifier, 46: char **, opsys, 47: char **, charset) 48: { 49: char c, *cp, *tmp_charset; 50: fd_set rs; 51: int pos, res=0, lp, fp; 52: 53: errno = 0; 54: 55: tmp_charset = 0; 56: 57: if (!id) 58: return -1; 59: if (lport) 60: *lport = 0; 61: if (fport) 62: *fport = 0; 63: if (identifier) 64: *identifier = 0; 65: if (opsys) 66: *opsys = 0; 67: if (charset) 68: *charset = 0; 69: 70: pos = strlen(id->buf); 71: 72: if (timeout) 73: { 74: FD_ZERO(&rs); 75: FD_SET(id->fd, &rs); 76: 77: #ifdef __hpux 78: if ((res = select(FD_SETSIZE, (int *) &rs, (int *)0, (int *)0, timeout)) < 0) 79: #else 80: if ((res = select(FD_SETSIZE, &rs, (fd_set *)0, (fd_set *)0, timeout)) < 0) 81: #endif 82: return -1; 83: 84: if (res == 0) 85: { 86: errno = ETIMEDOUT; 87: return -1; 88: } 89: } 90: 91: /* Every octal value is allowed except 0, \n and \r */ 92: while (pos < sizeof(id->buf) && 93: (res = read(id->fd, id->buf + pos, 1)) == 1 && 94: id->buf[pos] != '\n' && id->buf[pos] != '\r') 95: pos++; 96: 97: if (res < 0) 98: return -1; 99: 100: if (res == 0) 101: { 102: errno = ENOTCONN; 103: return -1; 104: } 105: 106: if (id->buf[pos] != '\n' && id->buf[pos] != '\r') 107: return 0; /* Not properly terminated string */ 108: 109: id->buf[pos++] = '\0'; 110: 111: /* 112: ** Get first field (<lport> , <fport>) 113: */ 114: cp = id_strtok(id->buf, ":", &c); 115: if (!cp) 116: return -2; 117: 118: if (sscanf(cp, " %d , %d", &lp, &fp) != 2) 119: { 120: if (identifier) 121: { 122: *identifier = id_strdup(cp); 123: if (*identifier == NULL) 124: return -4; 125: } 126: return -2; 127: } 128: 129: if (lport) 130: *lport = lp; 131: if (fport) 132: *fport = fp; 133: 134: /* 135: ** Get second field (USERID or ERROR) 136: */ 137: cp = id_strtok((char *)0, ":", &c); 138: if (!cp) 139: return -2; 140: 141: if (strcmp(cp, "ERROR") == 0) 142: { 143: cp = id_strtok((char *)0, "\n\r", &c); 144: if (!cp) 145: return -2; 146: 147: if (identifier) 148: { 149: *identifier = id_strdup(cp); 150: if (*identifier == NULL) 151: return -4; 152: } 153: 154: return 2; 155: } 156: else if (strcmp(cp, "USERID") == 0) 157: { 158: /* 159: ** Get first subfield of third field <opsys> 160: */ 161: cp = id_strtok((char *) 0, ",:", &c); 162: if (!cp) 163: return -2; 164: 165: if (opsys) 166: { 167: *opsys = id_strdup(cp); 168: if (*opsys == NULL) 169: return -4; 170: } 171: 172: /* 173: ** We have a second subfield (<charset>) 174: */ 175: if (c == ',') 176: { 177: cp = id_strtok((char *)0, ":", &c); 178: if (!cp) 179: return -2; 180: 181: tmp_charset = cp; 182: if (charset) 183: { 184: *charset = id_strdup(cp); 185: if (*charset == NULL) 186: return -4; 187: } 188: 189: /* 190: ** We have even more subfields - ignore them 191: */ 192: if (c == ',') 193: id_strtok((char *)0, ":", &c); 194: } 195: 196: if (tmp_charset && strcmp(tmp_charset, "OCTET") == 0) 197: cp = id_strtok((char *)0, (char *)0, &c); 198: else 199: cp = id_strtok((char *)0, "\n\r", &c); 200: 201: if (identifier) 202: { 203: *identifier = id_strdup(cp); 204: if (*identifier == NULL) 205: return -4; 206: } 207: return 1; 208: } 209: else 210: { 211: if (identifier) 212: { 213: *identifier = id_strdup(cp); 214: if (*identifier == NULL) 215: return -4; 216: } 217: return -3; 218: } 219: }