]> arthur.barton.de Git - netatalk.git/blob - etc/afpd/messages.c
commenting changes, more autoconf support in afpd, etc
[netatalk.git] / etc / afpd / messages.c
1 /* 
2  * $Id: messages.c,v 1.9 2001-06-20 18:33:04 rufustfirefly Exp $
3  *
4  * Copyright (c) 1997 Adrian Sun (asun@zoology.washington.edu)
5  * All Rights Reserved.  See COPYRIGHT.
6  */
7
8 #ifdef HAVE_CONFIG_H
9 #include "config.h"
10 #endif /* HAVE_CONFIG_H */
11
12 #include <stdio.h>
13 #include <string.h>
14 #include <atalk/afp.h>
15 #include <syslog.h>
16 #ifdef HAVE_UNISTD_H
17 #include <unistd.h>
18 #endif /* HAVE_UNISTD_H */
19 #include "globals.h"
20 #include "misc.h"
21
22 #define MAXMESGSIZE 199
23
24 /* this is only used by afpd children, so it's okay. */
25 static char servermesg[MAXMESGSIZE] = "";
26
27 void setmessage(const char *message)
28 {
29   strncpy(servermesg, message, MAXMESGSIZE);
30 }
31
32 void readmessage(void)
33 {
34 /* Read server message from file defined as SERVERTEXT */
35 #ifdef SERVERTEXT
36   FILE *message;
37   char * filename;
38   int i, rc;
39   static int c;
40   uid_t euid;
41
42   i=0;
43   // Construct file name SERVERTEXT/message.[pid]
44   filename=malloc(sizeof(SERVERTEXT)+15);
45   sprintf(filename, "%s/message.%d", SERVERTEXT, getpid());
46
47 #ifdef DEBUG
48   syslog (LOG_DEBUG, "Reading file %s ", filename);
49 #endif /* DEBUG */
50   
51   message=fopen(filename, "r");
52   if (message==NULL) {
53     syslog (LOG_INFO, "Unable to open file %s", filename);
54     sprintf(filename, "%s/message", SERVERTEXT);
55     message=fopen(filename, "r");
56   }
57
58   /* if either message.pid or message exists */
59   if (message!=NULL) {
60     /* added while loop to get characters and put in servermesg */
61     while ((( c=fgetc(message)) != EOF) && (i < (MAXMESGSIZE - 1))) {
62       if ( c == '\n')  c = ' ';
63       servermesg[i++] = c;
64       }
65     servermesg[i] = 0;
66
67     /* cleanup */
68     fclose(message);
69
70     /* Save effective uid and switch to root to delete file. */
71     /* Delete will probably fail otherwise, but let's try anyways */
72     euid = geteuid();
73     if (seteuid(0) < 0) {
74       syslog(LOG_ERR, "Could not switch back to root: %m");
75     }
76
77     rc = unlink(filename);
78
79     /* Drop privs again, failing this is very bad */
80     if (seteuid(euid) < 0) {
81       syslog(LOG_ERR, "Could not switch back to uid %d: %m", euid);
82     }
83
84     if (rc < 0) {
85       syslog (LOG_ERR, "Error deleting %s: %m", filename);
86     }
87 #ifdef DEBUG
88     else {
89       syslog (LOG_INFO, "Deleted %s", filename);
90     }
91
92     syslog (LOG_INFO, "Set server message to \"%s\"", servermesg);
93 #endif /* DEBUG */
94   }
95   free(filename);
96 #endif /* SERVERTEXT */
97 }
98
99 int afp_getsrvrmesg(obj, ibuf, ibuflen, rbuf, rbuflen)
100      AFPObj *obj;
101      char *ibuf, *rbuf;
102      int ibuflen, *rbuflen;
103 {
104   char *message;
105   u_int16_t type, bitmap;
106
107   memcpy(&type, ibuf + 2, sizeof(type));
108   memcpy(&bitmap, ibuf + 4, sizeof(bitmap));
109
110   switch (ntohs(type)) {
111   case AFPMESG_LOGIN: /* login */
112     message = obj->options.loginmesg;
113     break;
114   case AFPMESG_SERVER: /* server */
115     message = servermesg;
116     break;
117   default:
118     *rbuflen = 0;
119     return AFPERR_BITMAP;
120   }
121
122   /* output format:
123    * message type:   2 bytes
124    * bitmap:         2 bytes
125    * message length: 1 byte
126    * message:        up to 199 bytes
127    */
128   memcpy(rbuf, &type, sizeof(type));
129   rbuf += sizeof(type);
130   memcpy(rbuf, &bitmap, sizeof(bitmap));
131   rbuf += sizeof(bitmap);
132   *rbuflen = strlen(message);
133   if (*rbuflen > MAXMESGSIZE)
134     *rbuflen = MAXMESGSIZE;
135   *rbuf++ = *rbuflen;
136   memcpy(rbuf, message, *rbuflen);
137
138   *rbuflen += 5;
139
140   return AFP_OK;
141 }