+ retdata = obj->options.sleep /120;
+ if (!retdata) {
+ retdata = 1;
+ }
+ *rbuflen = sizeof(retdata);
+ retdata = htonl(retdata);
+ memcpy(rbuf, &retdata, sizeof(retdata));
+ if (obj->sleep)
+ obj->sleep();
+ rbuf += sizeof(retdata);
+ return AFP_OK;
+}
+
+/* ---------------------- */
+static int create_session_token(AFPObj *obj)
+{
+ pid_t pid;
+
+ /* use 8 bytes for token as OSX, don't know if it helps */
+ if ( sizeof(pid_t) > SESSIONTOKEN_LEN) {
+ LOG(log_error, logtype_afpd, "sizeof(pid_t) > %u", SESSIONTOKEN_LEN );
+ return AFPERR_MISC;
+ }
+
+ if ( NULL == (obj->sinfo.sessiontoken = malloc(SESSIONTOKEN_LEN)) )
+ return AFPERR_MISC;
+
+ memset(obj->sinfo.sessiontoken, 0, SESSIONTOKEN_LEN);
+ obj->sinfo.sessiontoken_len = SESSIONTOKEN_LEN;
+ pid = getpid();
+ memcpy(obj->sinfo.sessiontoken, &pid, sizeof(pid_t));
+
+ return 0;
+}
+
+static int create_session_key(AFPObj *obj)
+{
+ /* create session key */
+ if (obj->sinfo.sessionkey == NULL) {
+ if (NULL == (obj->sinfo.sessionkey = malloc(SESSIONKEY_LEN)) )
+ return AFPERR_MISC;
+ uam_random_string(obj, obj->sinfo.sessionkey, SESSIONKEY_LEN);
+ obj->sinfo.sessionkey_len = SESSIONKEY_LEN;
+ }
+ return AFP_OK;
+}
+
+
+/* ---------------------- */
+int afp_getsession(obj, ibuf, ibuflen, rbuf, rbuflen )
+AFPObj *obj;
+char *ibuf, *rbuf;
+unsigned int ibuflen, *rbuflen;
+{
+ u_int16_t type;
+ u_int32_t idlen = 0;
+ u_int32_t boottime;
+ u_int32_t tklen, tp;
+ char *token;
+ char *p;
+
+ *rbuflen = 0;
+ tklen = 0;
+
+ ibuf += 2;
+ ibuflen -= 2;
+
+ memcpy(&type, ibuf, sizeof(type));
+ type = ntohs(type);
+ ibuf += sizeof(type);
+ ibuflen -= sizeof(type);
+
+ if ( obj->sinfo.sessiontoken == NULL ) {
+ if ( create_session_token( obj ) )
+ return AFPERR_MISC;
+ }
+
+ /*
+ *
+ */
+ switch (type) {
+ case 0: /* old version ?*/
+ tklen = obj->sinfo.sessiontoken_len;
+ token = obj->sinfo.sessiontoken;
+ break;
+ case 1: /* disconnect */
+ case 2: /* reconnect update id */
+ if (ibuflen >= sizeof(idlen)) {
+ memcpy(&idlen, ibuf, sizeof(idlen));
+ idlen = ntohl(idlen);
+ ibuf += sizeof(idlen);
+ ibuflen -= sizeof(idlen);
+ if (ibuflen < idlen) {
+ return AFPERR_PARAM;
+ }
+ /* memcpy (id, ibuf, idlen) */
+ tklen = obj->sinfo.sessiontoken_len;
+ token = obj->sinfo.sessiontoken;
+ }
+ break;
+ case 3: /* Jaguar */
+ case 4:
+ if (ibuflen >= 8 ) {
+ p = ibuf;
+ memcpy( &idlen, ibuf, sizeof(idlen));
+ idlen = ntohl(idlen);
+ ibuf += sizeof(idlen);
+ ibuflen -= sizeof(idlen);
+ ibuf += sizeof(boottime);
+ ibuflen -= sizeof(boottime);
+ if (ibuflen < idlen || idlen > (90-10)) {
+ return AFPERR_PARAM;
+ }
+ server_ipc_write(IPC_GETSESSION, idlen+8, p );
+ tklen = obj->sinfo.sessiontoken_len;
+ token = obj->sinfo.sessiontoken;
+ }
+ break;
+ case 8: /* Panther Kerberos Token */
+ tklen = obj->sinfo.cryptedkey_len;
+ token = obj->sinfo.cryptedkey;
+ break;
+ default:
+ return AFPERR_NOOP;
+ break;
+
+ }
+
+ if (tklen == 0)
+ return AFPERR_MISC;
+
+ tp = htonl(tklen);
+ memcpy(rbuf, &tp, sizeof(tklen));
+ rbuf += sizeof(tklen);
+ *rbuflen += sizeof(tklen);
+
+ memcpy(rbuf, token, tklen);
+ *rbuflen += tklen;
+
+ return AFP_OK;
+}
+
+/* ---------------------- */
+int afp_disconnect(obj, ibuf, ibuflen, rbuf, rbuflen )
+AFPObj *obj _U_;
+char *ibuf, *rbuf _U_;
+int ibuflen _U_, *rbuflen;
+{
+ u_int16_t type;
+
+ u_int32_t tklen;
+ pid_t token;
+ int i;
+
+ *rbuflen = 0;
+ ibuf += 2;
+
+#if 0
+ /* check for guest user */
+ if ( 0 == (strcasecmp(obj->username, obj->options.guest)) ) {
+ return AFPERR_MISC;
+ }
+#endif
+
+ memcpy(&type, ibuf, sizeof(type));
+ type = ntohs(type);
+ ibuf += sizeof(type);
+
+ memcpy(&tklen, ibuf, sizeof(tklen));
+ tklen = ntohl(tklen);
+ ibuf += sizeof(tklen);
+
+ if ( sizeof(pid_t) > SESSIONTOKEN_LEN) {
+ LOG(log_error, logtype_afpd, "sizeof(pid_t) > %u", SESSIONTOKEN_LEN );
+ return AFPERR_MISC;
+ }
+ if (tklen != SESSIONTOKEN_LEN) {
+ return AFPERR_MISC;
+ }
+ tklen = sizeof(pid_t);
+ memcpy(&token, ibuf, tklen);
+
+ /* our stuff is pid + zero pad */
+ ibuf += tklen;
+ for (i = tklen; i < SESSIONTOKEN_LEN; i++, ibuf++) {
+ if (*ibuf != 0) {
+ return AFPERR_MISC;
+ }
+ }
+
+ /* killed old session, not easy */
+ server_ipc_write(IPC_KILLTOKEN, tklen, &token);
+ sleep(1);
+
+ return AFPERR_SESSCLOS; /* was AFP_OK */
+}
+
+/* ---------------------- */
+static int get_version(obj, ibuf, ibuflen, len)
+AFPObj *obj;
+char *ibuf;
+int ibuflen;
+int len;
+{
+ int num,i;
+
+ if (!len || len > ibuflen)
+ return AFPERR_BADVERS;