/*
- * $Id: messages.c,v 1.16 2002-03-24 01:23:41 sibaz Exp $
+ * $Id: messages.c,v 1.16.6.1.2.4 2003-11-03 20:51:48 bfernhomberg Exp $
*
* Copyright (c) 1997 Adrian Sun (asun@zoology.washington.edu)
* All Rights Reserved. See COPYRIGHT.
#include "config.h"
#endif /* HAVE_CONFIG_H */
+#ifdef HAVE_UNISTD_H
+#include <unistd.h>
+#endif /* HAVE_UNISTD_H */
#include <stdio.h>
#include <string.h>
#include <errno.h>
+#include <stdlib.h>
#include <atalk/afp.h>
+#include <atalk/dsi.h>
#include <atalk/logger.h>
-#ifdef HAVE_UNISTD_H
-#include <unistd.h>
-#endif /* HAVE_UNISTD_H */
#include "globals.h"
#include "misc.h"
+
#define MAXMESGSIZE 199
/* this is only used by afpd children, so it's okay. */
-static char servermesg[MAXMESGSIZE] = "";
+static char servermesg[MAXPATHLEN] = "";
+static char localized_message[MAXPATHLEN] = "";
void setmessage(const char *message)
{
strncpy(servermesg, message, MAXMESGSIZE);
}
-void readmessage(void)
+void readmessage(obj)
+AFPObj *obj;
{
/* Read server message from file defined as SERVERTEXT */
#ifdef SERVERTEXT
FILE *message;
char * filename;
- int i, rc;
+ unsigned int i;
+ int rc;
static int c;
uid_t euid;
+ u_int32_t maxmsgsize;
+
+ maxmsgsize = (obj->proto == AFPPROTO_DSI)?MIN(MAX(((DSI*)obj->handle)->attn_quantum, MAXMESGSIZE),MAXPATHLEN):MAXMESGSIZE;
i=0;
/* Construct file name SERVERTEXT/message.[pid] */
- filename=malloc(sizeof(SERVERTEXT)+15);
+ if ( NULL == (filename=(char*) malloc(sizeof(SERVERTEXT)+15)) ) {
+ LOG(log_error, logtype_afpd, "readmessage: malloc: %s", strerror(errno) );
+ return;
+ }
+
sprintf(filename, "%s/message.%d", SERVERTEXT, getpid());
#ifdef DEBUG
/* if either message.pid or message exists */
if (message!=NULL) {
/* added while loop to get characters and put in servermesg */
- while ((( c=fgetc(message)) != EOF) && (i < (MAXMESGSIZE - 1))) {
+ while ((( c=fgetc(message)) != EOF) && (i < (maxmsgsize - 1))) {
if ( c == '\n') c = ' ';
servermesg[i++] = c;
}
strerror(errno));
}
- rc = unlink(filename);
+ if ( 0 < (rc = unlink(filename)) )
+ LOG(log_error, logtype_afpd, "File '%s' could not be deleted", strerror(errno));
/* Drop privs again, failing this is very bad */
if (seteuid(euid) < 0) {
{
char *message;
u_int16_t type, bitmap;
+ u_int16_t msgsize;
+ size_t outlen = 0;
+ int utf8 = 0;
+
+ msgsize = (obj->proto == AFPPROTO_DSI)?MAX(((DSI*)obj->handle)->attn_quantum, MAXMESGSIZE):MAXMESGSIZE;
memcpy(&type, ibuf + 2, sizeof(type));
memcpy(&bitmap, ibuf + 4, sizeof(bitmap));
rbuf += sizeof(type);
memcpy(rbuf, &bitmap, sizeof(bitmap));
rbuf += sizeof(bitmap);
+
+ /* Convert the message to the macs codepage
+ * according to AFP 3.1 specs page 200
+ * bit 1 set in bitmap means Unicode ?= utf8
+ * Never saw this in the wild yet
+ * Upd: Panther does ... */
+
+ utf8 = ntohs(bitmap) & 2;
*rbuflen = strlen(message);
- if (*rbuflen > MAXMESGSIZE)
- *rbuflen = MAXMESGSIZE;
- *rbuf++ = *rbuflen;
- memcpy(rbuf, message, *rbuflen);
+ if (*rbuflen > msgsize)
+ *rbuflen = msgsize;
+
+ if (*rbuflen ) {
+ if ( (size_t)-1 == (outlen = convert_string(obj->options.unixcharset, utf8?CH_UTF8_MAC:obj->options.maccharset, message, *rbuflen, localized_message, msgsize)) )
+ {
+ memcpy(rbuf+((utf8)?2:1), message, *rbuflen); /*FIXME*/
+ outlen = *rbuflen;
+ }
+ else
+ {
+ memcpy(rbuf+((utf8)?2:1), localized_message, outlen);
+ }
+ }
+
+ if ( utf8 ) {
+ /* UTF8 message, 2 byte length */
+ msgsize = htons(outlen);
+ memcpy(rbuf, &msgsize, sizeof(msgsize));
+ rbuf += sizeof(msgsize);
+ *rbuflen += sizeof(msgsize);
+ }
+ else {
+ *rbuflen = outlen;
+ *rbuf++ = *rbuflen;
+ *rbuflen++;
+ }
- *rbuflen += 5;
+ *rbuflen += 4;
return AFP_OK;
}