+
+/* access_zephyr */
+int access_zephyr(struct query *q, char *argv[], client *cl)
+{
+ EXEC SQL BEGIN DECLARE SECTION;
+ char type[ZEPHYR_OWNER_TYPE_SIZE];
+ char *class;
+ int id;
+ EXEC SQL END DECLARE SECTION;
+ int status;
+
+ class = argv[ZA_CLASS];
+ EXEC SQL SELECT owner_type, owner_id INTO :type, :id
+ FROM zephyr WHERE class = :class;
+ if (sqlca.sqlcode)
+ return MR_PERM;
+
+ status = find_member(type, id, cl);
+ if (status)
+ return MR_SUCCESS;
+ else
+ return MR_PERM;
+}
+
+/* access_container - check access for most container operations
+ *
+ * Inputs: argv[0] - cnt_id
+ * q - query name
+ * cl - client name
+ *
+ * - check if that client is a member of the access control list
+ * - OR, if the query is add_machine_to_container or delete_machine_from_container
+ * check if the client is a memeber of the mem_acl list
+ * - if the query is update_container and the container is to be renamed and
+ * it is a top-level container, only priviledged users can do it
+ */
+
+int access_container(struct query *q, char *argv[], client *cl)
+{
+ EXEC SQL BEGIN DECLARE SECTION;
+ int cnt_id, acl_id, memacl_id, mach_id, machine_owner_id, flag;
+ char acl_type[CONTAINERS_ACL_TYPE_SIZE], memacl_type[CONTAINERS_ACL_TYPE_SIZE];
+ char name[CONTAINERS_NAME_SIZE], *newname;
+ char machine_owner_type[MACHINE_OWNER_TYPE_SIZE];
+ EXEC SQL END DECLARE SECTION;
+ int status;
+
+ cnt_id = *(int *)argv[0];
+
+ /* if amcn or dmcn, container id is the second argument */
+ if (strcmp(q->shortname, "amcn") == 0 || strcmp(q->shortname, "dmcn") == 0)
+ {
+ mach_id = *(int *)argv[0];
+ cnt_id = *(int *)argv[1];
+ }
+
+ EXEC SQL SELECT acl_id, acl_type, memacl_id, memacl_type, name, publicflg
+ INTO :acl_id, :acl_type, :memacl_id, :memacl_type, :name, :flag
+ FROM containers
+ WHERE cnt_id = :cnt_id;
+
+ if (sqlca.sqlerrd[2] != 1)
+ return MR_INTERNAL;
+
+ /* trim off the trailing spaces */
+ strcpy(name, strtrim(name));
+
+ /* Only dbadmin can rename containers. */
+ if (!strcmp(q->shortname, "ucon"))
+ {
+ newname = argv[1];
+ if (strcmp(name, newname))
+ return MR_PERM;
+ }
+
+ /* check for client in access control list and return success right
+ * away if it's there. */
+ if (find_member(acl_type, acl_id, cl))
+ return MR_SUCCESS;
+
+ /* If not amcn, dmcn, we lose. */
+ if (strcmp(q->shortname, "amcn") && strcmp(q->shortname, "dmcn"))
+ return MR_PERM;
+
+ if (find_member(memacl_type, memacl_id, cl))
+ return MR_SUCCESS;
+
+ /* if the container is public or the query is delete, grant access if client
+ * is on owner list */
+ if (flag || q->type == MR_Q_DELETE)
+ {
+ EXEC SQL SELECT owner_type, owner_id INTO :machine_owner_type,
+ :machine_owner_id
+ FROM machine
+ WHERE mach_id = :mach_id;
+
+ if (sqlca.sqlerrd[2] == 1 && strcmp("NONE", machine_owner_type) &&
+ find_member(machine_owner_type, machine_owner_id, cl))
+ return MR_SUCCESS;
+ }
+ /* Otherwise fail. */
+ return MR_PERM;
+}
+
+int check_mail_string(char *mailstring)
+{
+ EXEC SQL BEGIN DECLARE SECTION;
+ char mname[MACHINE_NAME_SIZE];
+ EXEC SQL END DECLARE SECTION;
+ char *p, *host, *hostdomain;
+ struct hostent *hp;
+ struct mxentry *mxrecords = NULL;
+ int index;
+
+ p = strchr(mailstring, '@');
+ if (p)
+ {
+ host = strdup(++p);
+
+ /* Replace .LOCAL at end of host with .MIT.EDU if needed. */
+ hostdomain = strrchr(host, '.');
+ if (hostdomain && !strcasecmp(hostdomain, ".LOCAL"))
+ {
+ index = hostdomain - host;
+ host[index] = '\0';
+ host = realloc(host, strlen(host) + strlen(".MIT.EDU") + 1);
+ strcat(host, ".MIT.EDU");
+ }
+
+ hp = gethostbyname(host);
+ if (hp)
+ {
+ host = realloc(host, strlen(hp->h_name) + 1);
+ if (host)
+ strcpy(host, hp->h_name);
+ }
+ else
+ {
+ /* Possibly a host with no A record but MX records. Check. */
+ mxrecords = getmxrecords(host);
+ if (!mxrecords)
+ return MR_BAD_MAIL_STRING;
+ else
+ return MR_SUCCESS;
+ }
+
+ if (!strcasecmp(host, "MIT.EDU"))
+ {
+ free(host);
+ return MR_BAD_MAIL_STRING;
+ }
+
+ EXEC SQL DECLARE csr_listmem CURSOR FOR
+ SELECT UNIQUE m.name FROM machine m, serverhosts sh
+ WHERE m.mach_id = sh.mach_id
+ AND (sh.service = 'MAILHUB' or sh.service = 'POSTOFFICE');
+ if (dbms_errno)
+ {
+ free(host);
+ return mr_errcode;
+ }
+ EXEC SQL OPEN csr_listmem;
+ if (dbms_errno)
+ {
+ free(host);
+ return mr_errcode;
+ }
+ while (1)
+ {
+ EXEC SQL FETCH csr_listmem INTO :mname;
+ if (sqlca.sqlcode)
+ break;
+
+ if (!strcasecmp(host, strtrim(mname)))
+ {
+ free(host);
+ return MR_BAD_MAIL_STRING;
+ }
+ }
+ free(host);
+ }
+
+ return MR_SUCCESS;
+}
+
+/*
+ * This ought to be in the bind library. It's adapted from sendmail.
+ */
+
+/*
+ * These are defined in RFC833. Some bind interface headers don't declare them.
+ * Ghod help us if they're ever actually incompatible with what's in
+ * the arpa/nameser.h header.
+ */
+#ifndef PACKETSZ
+#define PACKETSZ 512 /* maximum packet size */
+#endif
+#ifndef HFIXEDSZ
+#define HFIXEDSZ 12 /* #/bytes of fixed data in header */
+#endif
+#ifndef INT32SZ
+#define INT32SZ 4 /* for systems without 32-bit ints */
+#endif
+#ifndef INT16SZ
+#define INT16SZ 2 /* for systems without 16-bit ints */
+#endif
+
+/* minimum possible size of MX record in packet */
+#define MIN_MX_SIZE 8 /* corresp to "a.com 0" w/ terminating space */
+
+struct mxentry *getmxrecords(const char *name)
+{
+ char answer[PACKETSZ], *eom, *cp, *bp;
+ int n, ancount, qdcount, buflen, type, pref, ind;
+ static struct mxentry pmx[(PACKETSZ - HFIXEDSZ) / MIN_MX_SIZE];
+ static char MXHostBuf[PACKETSZ - HFIXEDSZ];
+ HEADER *hp;
+
+ pmx->name = (char *)NULL;
+ pmx->pref = -1;
+ n = res_search(name, C_IN,T_MX, (unsigned char *)&answer, sizeof(answer));
+ if (n == -1)
+ return((struct mxentry *)NULL);
+ if ((size_t)n > sizeof(answer))
+ n = sizeof(answer);
+
+ hp = (HEADER *)&answer;
+ cp = answer + HFIXEDSZ;
+ eom = answer + n;
+ h_errno = 0;
+ for (qdcount = ntohs(hp->qdcount); qdcount--; cp += n + QFIXEDSZ)
+ if ((n = dn_skipname((unsigned char *)cp, (unsigned char *)eom)) < 0)
+ return((struct mxentry *)NULL);
+ buflen = sizeof(MXHostBuf) - 1;
+ bp = MXHostBuf;
+ ind = 0;
+ ancount = ntohs(hp->ancount);
+ while (--ancount >= 0 && cp < eom)
+ {
+ if ((n = dn_expand((unsigned char *)answer, (unsigned char *)eom,
+ (unsigned char *)cp, bp, buflen)) < 0)
+ break;
+ cp += n;
+ GETSHORT(type, cp);
+ cp += INT16SZ + INT32SZ;
+ GETSHORT(n, cp);
+ if (type != T_MX)
+ {
+ cp += n;
+ continue;
+ }
+ GETSHORT(pref, cp);
+ if ((n = dn_expand((unsigned char *)answer, (unsigned char *)eom,
+ (unsigned char *)cp, bp, buflen)) < 0)
+ break;
+ cp += n;
+
+ pmx[ind].name = bp;
+ pmx[ind].pref = pref;
+ ++ind;
+
+ n = strlen((const char *)bp);
+ bp += n;
+ *bp++ = '\0';
+
+ buflen -= n + 1;
+ }
+
+ pmx[ind].name = (char *)NULL;
+ pmx[ind].pref = -1;
+ return(pmx);
+}