|
|
1.1 ! root 1: #include <sys/types.h> ! 2: #include <sys/filio.h> ! 3: #include <pwd.h> ! 4: #include <ipc.h> ! 5: #include <libc.h> ! 6: #include "defs.h" ! 7: ! 8: /* buffer definition */ ! 9: #define BUFLEN 512 ! 10: ! 11: /* have to define this somewhere */ ! 12: char *ipcname; ! 13: ! 14: /* ! 15: * Recieve an fd ! 16: */ ! 17: _fd_read(onfd, pp) ! 18: int onfd; ! 19: struct passfd *pp; ! 20: { ! 21: return(ioctl(onfd, FIORCVFD, pp)); ! 22: } ! 23: ! 24: /* ! 25: * Send an fd ! 26: */ ! 27: _fd_write(onfd, tofd) ! 28: { ! 29: return ioctl(onfd, FIOSNDFD, &tofd); ! 30: } ! 31: ! 32: /* ! 33: * convert an int to a signed string ! 34: */ ! 35: static char * ! 36: itoa(n) ! 37: int n; ! 38: { ! 39: static char num[64]; ! 40: char bw[64]; ! 41: char *bp, *np; ! 42: ! 43: np = num; ! 44: if(n < 0){ ! 45: *np++ = '-'; ! 46: n = -n; ! 47: } ! 48: for(bp = bw; n; n = n/10) ! 49: *bp++ = (n % 10) + '0'; ! 50: while(bp > bw) ! 51: *np++ = *--bp; ! 52: *np = 0; ! 53: return num; ! 54: } ! 55: ! 56: /* ! 57: * Send the connection info. ! 58: */ ! 59: int ! 60: _info_write(fd, ip) ! 61: int fd; ! 62: ipcinfo *ip; ! 63: { ! 64: char b[BUFLEN]; ! 65: int n; ! 66: ! 67: if (ip->name==NULL) ! 68: ip->name = ""; ! 69: if (ip->param==NULL) ! 70: ip->param = ""; ! 71: if (ip->machine==NULL) ! 72: ip->machine = ""; ! 73: if (ip->user==NULL) ! 74: ip->user = ""; ! 75: if (ip->myname==NULL) ! 76: ip->myname = ""; ! 77: strcpy(b, ip->myname); ! 78: strcat(b, "\n"); ! 79: strcat(b, ip->name); ! 80: strcat(b, "\n"); ! 81: strcat(b, ip->param); ! 82: strcat(b, "\n"); ! 83: strcat(b, ip->machine); ! 84: strcat(b, "\n"); ! 85: strcat(b, ip->user); ! 86: strcat(b, "\n"); ! 87: strcat(b, itoa(ip->flags)); ! 88: strcat(b, "\n"); ! 89: strcat(b, itoa(ip->uid)); ! 90: strcat(b, "\n"); ! 91: strcat(b, itoa(ip->gid)); ! 92: strcat(b, "\n"); ! 93: n = strlen(b); ! 94: if (write(fd, b, n)!=n) ! 95: return ABORT(errno, "can't send request", NULLINFO); ! 96: return 0; ! 97: } ! 98: ! 99: /* ! 100: * Read the connection info. If an error occurs, ip->reply and ip->conn are ! 101: * closed. ! 102: */ ! 103: int ! 104: _info_read(fd, ip) ! 105: int fd; ! 106: ipcinfo *ip; ! 107: { ! 108: static char b[BUFLEN]; ! 109: char *f[8]; ! 110: int n; ! 111: char *oldfields; ! 112: ! 113: n=read(fd, b, sizeof(b)); ! 114: if (n <= 0) ! 115: return ABORT(errno, "error reading request", ip); ! 116: b[n] = '\0'; ! 117: oldfields = setfields("\n"); ! 118: if (getfields(b, f, 8)!=8){ ! 119: setfields(oldfields); ! 120: return ABORT(EINVAL, "protocol botch", ip); ! 121: } ! 122: ip->myname = f[0]; ! 123: ip->name = f[1]; ! 124: ip->param = f[2]; ! 125: ip->machine = f[3]; ! 126: ip->flags = atoi(f[5]); ! 127: ! 128: /* supply a system name */ ! 129: if (ip->uid!=ROOTUID || ip->machine[0]=='\0') ! 130: ip->machine = ""; ! 131: ! 132: /* supply a user name */ ! 133: if (ip->uid==ROOTUID && *(f[4])!='\0') ! 134: ip->user = f[4]; ! 135: ! 136: /* supply uid/gid cruft */ ! 137: if (ip->uid==ROOTUID && atoi(f[6])!=-1) { ! 138: ip->uid = atoi(f[6]); ! 139: ip->gid = atoi(f[7]); ! 140: } ! 141: setfields(oldfields); ! 142: return 0; ! 143: } ! 144: ! 145: /* ! 146: * Send a reply to a connection request ! 147: */ ! 148: _reply_write(fd, no, str) ! 149: int fd; ! 150: int no; ! 151: char *str; ! 152: { ! 153: char b[BUFLEN]; ! 154: int n; ! 155: ! 156: if (str==NULL) ! 157: str = ""; ! 158: strcpy(b, itoa(no)); ! 159: strcat(b, "\n"); ! 160: strcat(b, str); ! 161: strcat(b, "\n"); ! 162: n = strlen(b); ! 163: if (write(fd, b, n)!=n) ! 164: return -1; ! 165: return 0; ! 166: } ! 167: ! 168: /* ! 169: * Get a reply to a connection request. ! 170: */ ! 171: int ! 172: _reply_read(fd) ! 173: int fd; ! 174: { ! 175: static char b[BUFLEN]; ! 176: char *f[2]; ! 177: char *ptr; ! 178: int n; ! 179: ! 180: while((n=read(fd, b, sizeof(b)))<0 && errno==EINTR) ! 181: ; ! 182: if (n <= 0) ! 183: return ABORT(errno, "error reading request", NULLINFO); ! 184: b[n] = '\0'; ! 185: setfields("\n"); ! 186: getfields(b, f, 2); ! 187: errno = atoi(f[0]); ! 188: if ((ptr=strchr(f[1], '\n'))!=NULL) ! 189: *ptr = '\0'; ! 190: if (errno!=0) ! 191: errstr = f[1]; ! 192: else ! 193: ipcname = f[1]; ! 194: return 0; ! 195: } ! 196:
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.