+ 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(
+ AFPObj *obj,
+ char *ibuf, size_t ibuflen,
+ char *rbuf, size_t *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;
+
+ if (ibuflen < 2 + sizeof(type)) {
+ return AFPERR_PARAM;
+ }
+
+ 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:
+ 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;
+ }
+ ipc_child_write(obj->ipc_fd, 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(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
+{
+ DSI *dsi = (DSI *)obj->handle;
+ 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;
+ }
+ }
+
+ LOG(log_note, logtype_afpd, "afp_disconnect: trying primary reconnect");
+
+ /* check for old session, possibly transfering session from here to there */
+ if (ipc_child_write(obj->ipc_fd, IPC_DISCOLDSESSION, tklen, &token) == -1)
+ goto exit;
+ /* write uint16_t DSI request ID */
+ if (writet(obj->ipc_fd, &dsi->header.dsi_requestID, 2, 0, 2) != 2) {
+ LOG(log_error, logtype_afpd, "afp_disconnect: couldn't send DSI request ID");
+ goto exit;
+ }
+ /* now send our connected AFP client socket */
+ if (send_fd(obj->ipc_fd, dsi->socket) != 0)
+ goto exit;
+ /* Now see what happens: either afpd master kills us because our session */
+ /* has been transfered to a old disconnected session, or we continue */
+ sleep(2);
+
+exit:
+ LOG(log_error, logtype_afpd, "afp_disconnect: primary reconnect failed");
+ return AFPERR_MISC;
+}
+
+/* ---------------------- */
+static int get_version(AFPObj *obj, char *ibuf, size_t ibuflen, size_t len)
+{
+ int num,i;
+
+ if (!len || len > ibuflen)
+ return AFPERR_BADVERS;