3 * Check access to queries
5 * Copyright (C) 1987-1998 by the Massachusetts Institute of Technology
6 * For copying and distribution information, please see the file
10 #include <mit-copyright.h>
11 #include "mr_server.h"
18 #include <sys/types.h>
19 #include <netinet/in.h>
20 #include <arpa/nameser.h>
22 EXEC SQL INCLUDE sqlca;
27 extern int dbms_errno, mr_errcode;
29 EXEC SQL WHENEVER SQLERROR DO dbmserr();
32 /* Specialized Access Routines */
34 /* access_user - verify that client name equals specified login name
36 * - since field validation routines are called first, a users_id is
37 * now in argv[0] instead of the login name.
40 int access_user(struct query *q, char *argv[], client *cl)
42 if (cl->users_id != *(int *)argv[0])
48 int access_update_user(struct query *q, char *argv[], client *cl)
50 EXEC SQL BEGIN DECLARE SECTION;
51 int users_id, unix_uid, status, comments, secure;
52 char login[USERS_LOGIN_SIZE], shell[USERS_SHELL_SIZE];
53 char winconsoleshell[USERS_WINCONSOLESHELL_SIZE], last[USERS_LAST_SIZE];
54 char first[USERS_FIRST_SIZE], middle[USERS_MIDDLE_SIZE];
55 char clearid[USERS_CLEARID_SIZE], type[USERS_TYPE_SIZE];
56 char signature[USERS_SIGNATURE_SIZE];
57 EXEC SQL END DECLARE SECTION;
59 /* The two fields we let users update themselves didn't appear until
65 if (cl->users_id != *(int *)argv[0])
68 users_id = *(int *)argv[0];
70 EXEC SQL SELECT u.login, u.unix_uid, u.shell, u.winconsoleshell, u.last,
71 u.first, u.middle, u.status, u.clearid, u.type, u.comments, u.signature,
72 u.secure INTO :login, :unix_uid, :shell, :winconsoleshell, :last, :first,
73 :middle, :status, :clearid, :type, :comments, :signature, :secure
74 FROM USERS u WHERE u.users_id = :users_id;
76 /* None of these things can have changed. */
77 if (strcmp(argv[1], strtrim(login)) ||
78 (unix_uid != atoi(argv[2])) ||
79 strcmp(argv[3], strtrim(shell)) ||
80 strcmp(argv[4], strtrim(winconsoleshell)) ||
81 strcmp(argv[5], strtrim(last)) ||
82 strcmp(argv[6], strtrim(first)) ||
83 strcmp(argv[7], strtrim(middle)) ||
84 (status != atoi(argv[8])) ||
85 strcmp(argv[9], strtrim(clearid)) ||
86 strcmp(argv[10], strtrim(type)) ||
87 (comments != *(int *)argv[11]) ||
88 strcmp(argv[12], strtrim(signature)) ||
89 (secure != atoi(argv[13])))
95 /* access_login - verify that client name equals specified login name
97 * argv[0...n] contain search info. q->
100 int access_login(struct query *q, char *argv[], client *cl)
102 EXEC SQL BEGIN DECLARE SECTION;
104 EXEC SQL END DECLARE SECTION;
109 if (!strcmp(q->shortname, "gual"))
111 EXEC SQL SELECT users_id INTO :id FROM users
112 WHERE login = :argv[0] AND users_id != 0;
114 else if (!strcmp(q->shortname, "gubl"))
116 EXEC SQL SELECT users_id INTO :id FROM users u
117 WHERE u.login = :argv[0] AND u.users_id != 0;
119 else if (!strcmp(q->shortname, "guau"))
121 EXEC SQL SELECT users_id INTO :id FROM users
122 WHERE unix_uid = :argv[0] AND users_id != 0;
124 else if (!strcmp(q->shortname, "gubu"))
126 EXEC SQL SELECT users_id INTO :id FROM users u
127 WHERE u.unix_uid = :argv[0] AND u.users_id != 0;
130 if (sqlca.sqlcode == SQL_NO_MATCH)
131 return MR_NO_MATCH; /* ought to be MR_USER, but this is what
132 gual returns, so we have to be consistent */
133 else if (sqlca.sqlerrd[2] != 1 || id != cl->users_id)
140 /* access_spob - check access for set_pobox */
142 int access_spob(struct query *q, char *argv[], client *cl)
144 EXEC SQL BEGIN DECLARE SECTION;
146 EXEC SQL END DECLARE SECTION;
149 if (!strcmp(argv[1], "IMAP"))
151 EXEC SQL SELECT owner INTO :id FROM filesys f
152 WHERE f.label = :argv[2] AND f.type = 'IMAP' AND
153 f.lockertype = 'USER';
154 if (cl->users_id != id)
158 /* Non-query owners can't forward mail to a POSTOFFICE or MAILHUB server,
159 * nor to a nonresolving domain.
161 if (!strcmp(argv[1], "SMTP") || !strcmp(argv[1], "SPLIT"))
163 status = check_mail_string(argv[2]);
168 if (cl->users_id != *(int *)argv[0])
175 /* access_list - check access for most list operations
177 * Inputs: argv[0] - list_id
179 * argv[2] - member ID (only for queries "amtl" and "dmfl")
180 * argv[7] - group ID (only for query "ulis")
183 * - check that client is a member of the access control list
184 * - OR, if the query is add_member_to_list or delete_member_from_list
185 * and the list is public, allow access if client = member
188 int access_list(struct query *q, char *argv[], client *cl)
190 EXEC SQL BEGIN DECLARE SECTION;
191 int list_id, acl_id, flags, gid, users_id, member_id, member_acl_id;
192 int memacl_id, mailman, mailman_id;
193 char acl_type[LIST_ACL_TYPE_SIZE], name[LIST_NAME_SIZE], *newname;
194 char member_acl_type[LIST_ACL_TYPE_SIZE], memacl_type[LIST_ACL_TYPE_SIZE];
195 EXEC SQL END DECLARE SECTION;
199 list_id = *(int *)argv[0];
200 member_id = *(int *)argv[2];
201 EXEC SQL SELECT acl_id, acl_type, memacl_id, memacl_type,
202 gid, publicflg, name, mailman, mailman_id
203 INTO :acl_id, :acl_type, :memacl_id, :memacl_type,
204 :gid, :flags, :name, :mailman, :mailman_id
206 WHERE list_id = :list_id;
208 if (sqlca.sqlerrd[2] != 1)
211 /* if update_list, don't allow them to change the GID or rename to a
212 username other than their own */
213 if (!strcmp("ulis", q->shortname))
215 if (!strcmp(argv[7], UNIQUE_GID))
222 if (gid != atoi(argv[7]))
228 /* Check that it doesn't conflict with the Grouper namespace. */
229 if (strlen(newname) > 4 && isdigit(newname[2]) &&
230 isdigit(newname[3]) && newname[4] == '-')
232 if (!strncasecmp(newname, "fa", 2) ||
233 !strncasecmp(newname, "sp", 2) ||
234 !strncasecmp(newname, "su", 2) ||
235 !strncasecmp(newname, "ja", 2))
239 /* Don't let anyone take owner-foo list names. They interact
240 * weirdly with the aliases automatically generated by
243 if (!strncasecmp(newname, "owner-", 6))
246 EXEC SQL SELECT users_id INTO :users_id FROM users
247 WHERE login = :newname;
248 if ((sqlca.sqlcode != SQL_NO_MATCH) && strcmp(strtrim(name), newname) &&
249 (users_id != cl->users_id))
252 /* For modern enough clients, don't allow ordinary users to toggle
253 * the mailman bit or change the server.
255 if (q->version >= 10)
257 if (mailman != atoi(argv[9]))
260 if (mailman_id != *(int *)argv[10])
265 /* Don't allow non-query owners to add STRINGs to lists if they end
266 * in a domain that's MIT.EDU or one of the hosts that provide the
267 * MAILHUB or POSTOFFICE services.
269 if (!strcmp(q->shortname, "amtl") || !strcmp(q->shortname, "atml"))
271 if (!strcmp("STRING", argv[1]))
274 status = id_to_name(*(int *)argv[2], STRINGS_TABLE, &buf);
278 status = check_mail_string(buf);
285 /* check for client in access control list and return success right
286 * away if it's there. */
287 if (find_member(acl_type, acl_id, cl))
290 /* If not amtl, atml, or dmfl, we lose. */
291 if (strcmp(q->shortname, "amtl") && strcmp(q->shortname, "atml") &&
292 strcmp(q->shortname, "dmfl") && strcmp(q->shortname, "tmol"))
295 if (find_member(memacl_type, memacl_id, cl))
298 if (flags || q->type == MR_Q_DELETE)
300 if (!strcmp("USER", argv[1]) && *(int *)argv[2] == cl->users_id)
302 if (!strcmp("KERBEROS", argv[1]) && *(int *)argv[2] == -cl->client_id)
304 if (!strcmp("LIST", argv[1]) && !strcmp("dmfl", q->shortname))
306 EXEC SQL SELECT acl_id, acl_type INTO :member_acl_id,
309 WHERE list_id = :member_id;
311 if (find_member(member_acl_type, member_acl_id, cl))
316 /* Otherwise fail. */
321 /* access_visible_list - allow access to list only if it is not hidden,
322 * or if the client is on the ACL
324 * Inputs: argv[0] - list_id
325 * cl - client identifier
328 int access_visible_list(struct query *q, char *argv[], client *cl)
330 EXEC SQL BEGIN DECLARE SECTION;
331 int list_id, acl_id, memacl_id, flags ;
332 char acl_type[LIST_ACL_TYPE_SIZE], memacl_type[LIST_ACL_TYPE_SIZE];
333 EXEC SQL END DECLARE SECTION;
336 list_id = *(int *)argv[0];
337 EXEC SQL SELECT hidden, acl_id, acl_type, memacl_id, memacl_type
338 INTO :flags, :acl_id, :acl_type, :memacl_id, :memacl_type
340 WHERE list_id = :list_id;
341 if (sqlca.sqlerrd[2] != 1)
346 /* check for client in access control list */
347 status = find_member(acl_type, acl_id, cl);
350 status = find_member(memacl_type, memacl_id, cl);
358 /* access_vis_list_by_name - allow access to list only if it is not hidden,
359 * or if the client is on the ACL
361 * Inputs: argv[0] - list name
362 * cl - client identifier
365 int access_vis_list_by_name(struct query *q, char *argv[], client *cl)
367 EXEC SQL BEGIN DECLARE SECTION;
368 int acl_id, memacl_id, flags, rowcount, list_id;
369 char acl_type[LIST_ACL_TYPE_SIZE], memacl_type[LIST_ACL_TYPE_SIZE];
371 EXEC SQL END DECLARE SECTION;
375 EXEC SQL SELECT hidden, acl_id, acl_type, memacl_id, memacl_type, list_id
376 INTO :flags, :acl_id, :acl_type, :memacl_id, :memacl_type, :list_id
378 WHERE name = :listname;
380 rowcount = sqlca.sqlerrd[2];
388 /* If the user is a member of the acl, memacl, or the list itself,
391 status = find_member(acl_type, acl_id, cl);
393 status = find_member(memacl_type, memacl_id, cl);
395 status = find_member("LIST", list_id, cl);
403 /* access_member - allow user to access member of type "USER" and name matches
404 * username, or to access member of type "KERBEROS" and the principal matches
405 * the user, or to access member of type "LIST" and list is one that user is
406 * on the acl of, or the list is visible. Allow anyone to look up list
407 * memberships of MACHINEs.
410 int access_member(struct query *q, char *argv[], client *cl)
412 if (!strcmp(argv[0], "LIST") || !strcmp(argv[0], "RLIST"))
413 return access_visible_list(q, &argv[1], cl);
415 if (!strcmp(argv[0], "USER") || !strcmp(argv[0], "RUSER"))
417 if (cl->users_id == *(int *)argv[1])
421 if (!strcmp(argv[0], "KERBEROS") || !strcmp(argv[0], "RKERBEROS"))
423 if (cl->client_id == -*(int *)argv[1])
427 if (!strcmp(argv[0], "MACHINE") || !strcmp(argv[0], "RMACHINE"))
434 /* access_qgli - special access routine for Qualified_get_lists. Allows
435 * access iff argv[0] == "TRUE" and argv[2] == "FALSE".
438 int access_qgli(struct query *q, char *argv[], client *cl)
440 if (!strcmp(argv[0], "TRUE") && !strcmp(argv[2], "FALSE"))
446 /* access_service - allow access if user is on ACL of service. Don't
447 * allow access if a wildcard is used.
450 int access_service(struct query *q, char *argv[], client *cl)
452 EXEC SQL BEGIN DECLARE SECTION;
454 char *name, acl_type[LIST_ACL_TYPE_SIZE];
455 EXEC SQL END DECLARE SECTION;
460 for (c = name; *c; c++)
465 EXEC SQL SELECT acl_id, acl_type INTO :acl_id, :acl_type FROM servers
467 if (sqlca.sqlerrd[2] > 1)
470 /* check for client in access control list */
471 status = find_member(acl_type, acl_id, cl);
479 /* access_filesys - verify that client is owner or on owners list of filesystem
483 int access_filesys(struct query *q, char *argv[], client *cl)
485 EXEC SQL BEGIN DECLARE SECTION;
486 int users_id, list_id;
488 EXEC SQL END DECLARE SECTION;
492 EXEC SQL SELECT owner, owners INTO :users_id, :list_id FROM filesys
495 if (sqlca.sqlerrd[2] != 1)
497 if (users_id == cl->users_id)
499 status = find_member("LIST", list_id, cl);
507 /* access_host - successful if owner of host, or subnet containing host
510 int access_host(struct query *q, char *argv[], client *cl)
512 EXEC SQL BEGIN DECLARE SECTION;
513 int mid, sid, id, subnet_status;
514 char mtype[MACHINE_OWNER_TYPE_SIZE], stype[SUBNET_OWNER_TYPE_SIZE];
515 char *account_number;
516 EXEC SQL END DECLARE SECTION;
521 else if (q->version >= 6 && q->version < 8)
526 if (q->type == MR_Q_APPEND)
528 /* Non-query owner must set use to zero */
529 if (atoi(argv[6 + idx]) != 0)
532 /* ... and start the hostname with a letter */
533 if (isdigit(argv[0][0]))
536 id = *(int *)argv[8 + idx];
537 EXEC SQL SELECT s.owner_type, s.owner_id, s.status
538 INTO :stype, :sid, :subnet_status FROM subnet s
539 WHERE s.snet_id = :id;
542 /* Non query owner must provide valid billing information. */
545 if (subnet_status == SNET_STATUS_BILLABLE)
547 account_number = argv[7];
548 EXEC SQL SELECT account_number FROM accountnumbers
549 WHERE account_number = :account_number;
550 if (sqlca.sqlcode == SQL_NO_MATCH)
551 return MR_ACCOUNT_NUMBER;
555 if (find_member(stype, sid, cl))
560 else /* q-type == MR_Q_UPDATE */
562 EXEC SQL BEGIN DECLARE SECTION;
563 int status, acomment, use, ocomment, snid;
564 char contact[MACHINE_CONTACT_SIZE], address[MACHINE_ADDRESS_SIZE];
565 char name[MACHINE_NAME_SIZE];
566 char billing_contact[MACHINE_BILLING_CONTACT_SIZE];
567 EXEC SQL END DECLARE SECTION;
569 id = *(int *)argv[0];
570 EXEC SQL SELECT m.name, m.use, m.contact, m.billing_contact, m.status,
571 m.address, m.owner_type, m.owner_id, m.acomment, m.ocomment, m.snet_id,
572 s.owner_type, s.owner_id, s.status INTO :name, :use, :contact,
573 :billing_contact, :status, :address, :mtype, :mid, :acomment,
574 :ocomment, :snid, :stype, :sid, :subnet_status
575 FROM machine m, subnet s
576 WHERE m.mach_id = :id AND s.snet_id = m.snet_id;
580 /* Non query owner must provide valid billing information. */
583 if ((subnet_status == SNET_STATUS_BILLABLE) &&
584 (atoi(argv[10]) != 3))
586 account_number = argv[8];
587 EXEC SQL SELECT account_number FROM accountnumbers
588 WHERE account_number = :account_number;
589 if (sqlca.sqlcode == SQL_NO_MATCH)
590 return MR_ACCOUNT_NUMBER;
594 /* non-query-owner cannot change use or ocomment */
595 if ((use != atoi(argv[7 + idx])) || (ocomment != *(int *)argv[14 + idx]))
598 /* or rename to start with digit */
599 if (isdigit(argv[1][0]) && strcmp(strtrim(name), argv[1]))
602 if (!find_member(stype, sid, cl))
604 if (find_member(mtype, mid, cl))
606 /* host owner also cannot change contact, status, address,
607 owner, or acomment */
608 if (strcmp(argv[6], strtrim(contact)) ||
609 (status != atoi(argv[8 + idx])) ||
610 strcmp(argv[10 + idx], strtrim(address)) ||
611 strcmp(argv[11 + idx], strtrim(mtype)) ||
612 (mid != *(int *)argv[12 + idx]) ||
613 (acomment != *(int *)argv[13 + idx]))
615 /* Billing contact field didn't appear until version 6 */
617 if (strcmp(argv[7], strtrim(billing_contact)))
624 /* If moving to a new subnet, make sure user is on acl there */
625 id = *(int *)argv[9 + idx];
628 EXEC SQL SELECT owner_type, owner_id INTO :stype, :sid
629 FROM subnet WHERE snet_id=:id;
630 if (!find_member(stype, sid, cl))
639 /* access_ahal - check for adding a host alias.
640 * successful if host has less then 2 aliases and (client is owner of
642 * If deleting an alias, any owner will do.
645 int access_ahal(struct query *q, char *argv[], client *cl)
647 EXEC SQL BEGIN DECLARE SECTION;
648 int cnt, id, mid, sid;
649 char mtype[MACHINE_OWNER_TYPE_SIZE], stype[SUBNET_OWNER_TYPE_SIZE];
650 EXEC SQL END DECLARE SECTION;
653 if (q->type == MR_Q_RETRIEVE)
656 id = *(int *)argv[1];
658 if (q->type == MR_Q_APPEND && isdigit(argv[0][0]))
661 EXEC SQL SELECT count(name) INTO :cnt from hostalias WHERE mach_id = :id;
664 /* if the type is MR_Q_APPEND, this is ahal and we need to make sure there
665 * will be no more than 2 aliases. If it's not, it must be dhal and
668 if (q->type == MR_Q_APPEND && cnt >= 2)
670 EXEC SQL SELECT m.owner_type, m.owner_id, s.owner_type, s.owner_id
671 INTO :mtype, :mid, :stype, :sid FROM machine m, subnet s
672 WHERE m.mach_id = :id and s.snet_id = m.snet_id;
673 status = find_member(mtype, mid, cl);
676 status = find_member(stype, sid, cl);
684 /* access_snt - check for retrieving network structure
687 int access_snt(struct query *q, char *argv[], client *cl)
689 if (q->type == MR_Q_RETRIEVE)
697 int access_printer(struct query *q, char *argv[], client *cl)
699 EXEC SQL BEGIN DECLARE SECTION;
700 char type[PRINTSERVERS_OWNER_TYPE_SIZE];
702 EXEC SQL END DECLARE SECTION;
705 mach_id = *(int *)argv[PRN_RM];
706 EXEC SQL SELECT owner_type, owner_id INTO :type, :id
707 FROM printservers WHERE mach_id = :mach_id;
711 status = find_member(type, id, cl);
719 int access_zephyr(struct query *q, char *argv[], client *cl)
721 EXEC SQL BEGIN DECLARE SECTION;
722 char type[ZEPHYR_OWNER_TYPE_SIZE];
725 EXEC SQL END DECLARE SECTION;
728 class = argv[ZA_CLASS];
729 EXEC SQL SELECT owner_type, owner_id INTO :type, :id
730 FROM zephyr WHERE class = :class;
734 status = find_member(type, id, cl);
741 /* access_container - check access for most container operations
743 * Inputs: argv[0] - cnt_id
747 * - check if that client is a member of the access control list
748 * - OR, if the query is add_machine_to_container or delete_machine_from_container
749 * check if the client is a memeber of the mem_acl list
750 * - if the query is update_container and the container is to be renamed and
751 * it is a top-level container, only priviledged users can do it
754 int access_container(struct query *q, char *argv[], client *cl)
756 EXEC SQL BEGIN DECLARE SECTION;
757 int cnt_id, acl_id, memacl_id, mach_id, machine_owner_id, flag;
758 char acl_type[CONTAINERS_ACL_TYPE_SIZE], memacl_type[CONTAINERS_ACL_TYPE_SIZE];
759 char name[CONTAINERS_NAME_SIZE], *newname;
760 char machine_owner_type[MACHINE_OWNER_TYPE_SIZE];
761 EXEC SQL END DECLARE SECTION;
764 cnt_id = *(int *)argv[0];
766 /* if amcn or dmcn, container id is the second argument */
767 if (strcmp(q->shortname, "amcn") == 0 || strcmp(q->shortname, "dmcn") == 0)
769 mach_id = *(int *)argv[0];
770 cnt_id = *(int *)argv[1];
773 EXEC SQL SELECT acl_id, acl_type, memacl_id, memacl_type, name, publicflg
774 INTO :acl_id, :acl_type, :memacl_id, :memacl_type, :name, :flag
776 WHERE cnt_id = :cnt_id;
778 if (sqlca.sqlerrd[2] != 1)
781 /* trim off the trailing spaces */
782 strcpy(name, strtrim(name));
784 /* Only dbadmin can rename containers. */
785 if (!strcmp(q->shortname, "ucon"))
788 if (strcmp(name, newname))
792 /* check for client in access control list and return success right
793 * away if it's there. */
794 if (find_member(acl_type, acl_id, cl))
797 /* If not amcn, dmcn, we lose. */
798 if (strcmp(q->shortname, "amcn") && strcmp(q->shortname, "dmcn"))
801 if (find_member(memacl_type, memacl_id, cl))
804 /* if the container is public or the query is delete, grant access if client
805 * is on owner list */
806 if (flag || q->type == MR_Q_DELETE)
808 EXEC SQL SELECT owner_type, owner_id INTO :machine_owner_type,
811 WHERE mach_id = :mach_id;
813 if (sqlca.sqlerrd[2] == 1 && strcmp("NONE", machine_owner_type) &&
814 find_member(machine_owner_type, machine_owner_id, cl))
817 /* Otherwise fail. */
821 int check_mail_string(char *mailstring)
823 EXEC SQL BEGIN DECLARE SECTION;
824 char mname[MACHINE_NAME_SIZE];
825 EXEC SQL END DECLARE SECTION;
826 char *p, *host, *hostdomain;
828 struct mxentry *mxrecords = NULL;
831 p = strchr(mailstring, '@');
836 /* Replace .LOCAL at end of host with .MIT.EDU if needed. */
837 hostdomain = strrchr(host, '.');
838 if (hostdomain && !strcasecmp(hostdomain, ".LOCAL"))
840 index = hostdomain - host;
842 host = realloc(host, strlen(host) + strlen(".MIT.EDU") + 1);
843 strcat(host, ".MIT.EDU");
846 hp = gethostbyname(host);
849 host = realloc(host, strlen(hp->h_name) + 1);
851 strcpy(host, hp->h_name);
855 /* Possibly a host with no A record but MX records. Check. */
856 mxrecords = getmxrecords(host);
858 return MR_BAD_MAIL_STRING;
863 if (!strcasecmp(host, "MIT.EDU"))
866 return MR_BAD_MAIL_STRING;
869 EXEC SQL DECLARE csr_listmem CURSOR FOR
870 SELECT UNIQUE m.name FROM machine m, serverhosts sh
871 WHERE m.mach_id = sh.mach_id
872 AND (sh.service = 'MAILHUB' or sh.service = 'POSTOFFICE');
878 EXEC SQL OPEN csr_listmem;
886 EXEC SQL FETCH csr_listmem INTO :mname;
890 if (!strcasecmp(host, strtrim(mname)))
893 return MR_BAD_MAIL_STRING;
903 * This ought to be in the bind library. It's adapted from sendmail.
907 * These are defined in RFC833. Some bind interface headers don't declare them.
908 * Ghod help us if they're ever actually incompatible with what's in
909 * the arpa/nameser.h header.
912 #define PACKETSZ 512 /* maximum packet size */
915 #define HFIXEDSZ 12 /* #/bytes of fixed data in header */
918 #define INT32SZ 4 /* for systems without 32-bit ints */
921 #define INT16SZ 2 /* for systems without 16-bit ints */
924 /* minimum possible size of MX record in packet */
925 #define MIN_MX_SIZE 8 /* corresp to "a.com 0" w/ terminating space */
927 struct mxentry *getmxrecords(const char *name)
929 char answer[PACKETSZ], *eom, *cp, *bp;
930 int n, ancount, qdcount, buflen, type, pref, ind;
931 static struct mxentry pmx[(PACKETSZ - HFIXEDSZ) / MIN_MX_SIZE];
932 static char MXHostBuf[PACKETSZ - HFIXEDSZ];
935 pmx->name = (char *)NULL;
937 n = res_search(name, C_IN,T_MX, (unsigned char *)&answer, sizeof(answer));
939 return((struct mxentry *)NULL);
940 if ((size_t)n > sizeof(answer))
943 hp = (HEADER *)&answer;
944 cp = answer + HFIXEDSZ;
947 for (qdcount = ntohs(hp->qdcount); qdcount--; cp += n + QFIXEDSZ)
948 if ((n = dn_skipname((unsigned char *)cp, (unsigned char *)eom)) < 0)
949 return((struct mxentry *)NULL);
950 buflen = sizeof(MXHostBuf) - 1;
953 ancount = ntohs(hp->ancount);
954 while (--ancount >= 0 && cp < eom)
956 if ((n = dn_expand((unsigned char *)answer, (unsigned char *)eom,
957 (unsigned char *)cp, bp, buflen)) < 0)
961 cp += INT16SZ + INT32SZ;
969 if ((n = dn_expand((unsigned char *)answer, (unsigned char *)eom,
970 (unsigned char *)cp, bp, buflen)) < 0)
975 pmx[ind].pref = pref;
978 n = strlen((const char *)bp);
985 pmx[ind].name = (char *)NULL;