📄 tf_util.c
字号:
char *inst;{ if (fd < 0) { if (krb_debug) fprintf(stderr, "tf_get_pinst called before tf_init.\n"); return TKT_FIL_INI; } if (tf_gets(inst, INST_SZ) < 1) return TKT_FIL_FMT; return KSUCCESS;}/* * tf_get_cred() reads a CREDENTIALS record from a ticket file and fills * in the given structure "c". It should only be called after tf_init(), * tf_get_pname(), and tf_get_pinst() have been called. If all goes well, * KSUCCESS is returned. Possible error codes are: * * TKT_FIL_INI - tf_init wasn't called first * TKT_FIL_FMT - bad format * EOF - end of file encountered */tf_get_cred(c) CREDENTIALS *c;{ KTEXT ticket = &c->ticket_st; /* pointer to ticket */ int k_errno; if (fd < 0) { if (krb_debug) fprintf(stderr, "tf_get_cred called before tf_init.\n"); return TKT_FIL_INI; } if ((k_errno = tf_gets(c->service, SNAME_SZ)) < 2) switch (k_errno) { case TOO_BIG: case 1: /* can't be just a null */ tf_close(); return TKT_FIL_FMT; case 0: return EOF; } if ((k_errno = tf_gets(c->instance, INST_SZ)) < 1) switch (k_errno) { case TOO_BIG: return TKT_FIL_FMT; case 0: return EOF; } if ((k_errno = tf_gets(c->realm, REALM_SZ)) < 2) switch (k_errno) { case TOO_BIG: case 1: /* can't be just a null */ tf_close(); return TKT_FIL_FMT; case 0: return EOF; } if ( tf_read((char *) (c->session), KEY_SZ) < 1 || tf_read((char *) &(c->lifetime), sizeof(c->lifetime)) < 1 || tf_read((char *) &(c->kvno), sizeof(c->kvno)) < 1 || tf_read((char *) &(ticket->length), sizeof(ticket->length)) < 1 || /* don't try to read a silly amount into ticket->dat */ ticket->length > MAX_KTXT_LEN || tf_read((char *) (ticket->dat), ticket->length) < 1 || tf_read((char *) &(c->issue_date), sizeof(c->issue_date)) < 1 ) { tf_close(); return TKT_FIL_FMT; }#ifdef TKT_SHMEM bcopy(tmp_shm_addr,c->session,KEY_SZ); tmp_shm_addr += KEY_SZ;#endif /* TKT_SHMEM */ return KSUCCESS;}/* * tf_close() closes the ticket file and sets "fd" to -1. If "fd" is * not a valid file descriptor, it just returns. It also clears the * buffer used to read tickets. * * The return value is not defined. */tf_close(){ if (!(fd < 0)) {#ifdef TKT_SHMEM if (shmdt(krb_shm_addr)) { /* what kind of error? */ if (krb_debug) fprintf(stderr, "shmdt 0x%x: errno %d",krb_shm_addr, errno); } else { krb_shm_addr = 0; }#endif TKT_SHMEM (void) flock(fd, LOCK_UN); (void) close(fd); fd = -1; /* see declaration of fd above */ } bzero(tfbfr, sizeof(tfbfr));}/* * tf_gets() is an internal routine. It takes a string "s" and a count * "n", and reads from the file until either it has read "n" characters, * or until it reads a null byte. When finished, what has been read exists * in "s". If it encounters EOF or an error, it closes the ticket file. * * Possible return values are: * * n the number of bytes read (including null terminator) * when all goes well * * 0 end of file or read error * * TOO_BIG if "count" characters are read and no null is * encountered. This is an indication that the ticket * file is seriously ill. */static tf_gets(s, n) register char *s;{ register count; if (fd < 0) { if (krb_debug) fprintf(stderr, "tf_gets called before tf_init.\n"); return TKT_FIL_INI; } for (count = n - 1; count > 0; --count) { if (curpos >= sizeof(tfbfr)) { lastpos = read(fd, tfbfr, sizeof(tfbfr)); curpos = 0; } if (curpos == lastpos) { tf_close(); return 0; } *s = tfbfr[curpos++]; if (*s++ == '\0') return (n - count); } tf_close(); return TOO_BIG;}/* * tf_read() is an internal routine. It takes a string "s" and a count * "n", and reads from the file until "n" bytes have been read. When * finished, what has been read exists in "s". If it encounters EOF or * an error, it closes the ticket file. * * Possible return values are: * * n the number of bytes read when all goes well * * 0 on end of file or read error */statictf_read(s, n) register char *s; register n;{ register count; for (count = n; count > 0; --count) { if (curpos >= sizeof(tfbfr)) { lastpos = read(fd, tfbfr, sizeof(tfbfr)); curpos = 0; } if (curpos == lastpos) { tf_close(); return 0; } *s++ = tfbfr[curpos++]; } return n;} char *tkt_string();/* * tf_save_cred() appends an incoming ticket to the end of the ticket * file. You must call tf_init() before calling tf_save_cred(). * * The "service", "instance", and "realm" arguments specify the * server's name; "session" contains the session key to be used with * the ticket; "kvno" is the server key version number in which the * ticket is encrypted, "ticket" contains the actual ticket, and * "issue_date" is the time the ticket was requested (local host's time). * * Returns KSUCCESS if all goes well, TKT_FIL_INI if tf_init() wasn't * called previously, and KFAILURE for anything else that went wrong. */tf_save_cred(service, instance, realm, session, lifetime, kvno, ticket, issue_date) char *service; /* Service name */ char *instance; /* Instance */ char *realm; /* Auth domain */ C_Block session; /* Session key */ int lifetime; /* Lifetime */ int kvno; /* Key version number */ KTEXT ticket; /* The ticket itself */ long issue_date; /* The issue time */{ off_t lseek(); int count; /* count for write */#ifdef TKT_SHMEM int *skey_check;#endif /* TKT_SHMEM */ if (fd < 0) { /* fd is ticket file as set by tf_init */ if (krb_debug) fprintf(stderr, "tf_save_cred called before tf_init.\n"); return TKT_FIL_INI; } /* Find the end of the ticket file */ (void) lseek(fd, (off_t)0, 2);#ifdef TKT_SHMEM /* scan to end of existing keys: pick first 'empty' slot. we assume that no real keys will be completely zero (it's a weak key under DES) */ skey_check = (int *) krb_shm_addr; while (*skey_check && *(skey_check+1)) skey_check += 2; tmp_shm_addr = (char *)skey_check;#endif /* TKT_SHMEM */ /* Write the ticket and associated data */ /* Service */ count = strlen(service) + 1; if (write(fd, service, count) != count) goto bad; /* Instance */ count = strlen(instance) + 1; if (write(fd, instance, count) != count) goto bad; /* Realm */ count = strlen(realm) + 1; if (write(fd, realm, count) != count) goto bad; /* Session key */#ifdef TKT_SHMEM bcopy(session,tmp_shm_addr,8); tmp_shm_addr+=8; if (write(fd,krb_dummy_skey,8) != 8) goto bad;#else /* ! TKT_SHMEM */ if (write(fd, (char *) session, 8) != 8) goto bad;#endif /* TKT_SHMEM */ /* Lifetime */ if (write(fd, (char *) &lifetime, sizeof(int)) != sizeof(int)) goto bad; /* Key vno */ if (write(fd, (char *) &kvno, sizeof(int)) != sizeof(int)) goto bad; /* Tkt length */ if (write(fd, (char *) &(ticket->length), sizeof(int)) != sizeof(int)) goto bad; /* Ticket */ count = ticket->length; if (write(fd, (char *) (ticket->dat), count) != count) goto bad; /* Issue date */ if (write(fd, (char *) &issue_date, sizeof(long)) != sizeof(long)) goto bad; /* Actually, we should check each write for success */ return (KSUCCESS);bad: return (KFAILURE);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -