@@ -15,6 +15,8 @@ typedef struct LookupParameters {
1515 union in_addr_union address ;
1616 size_t address_size ;
1717 char * name ;
18+ uint16_t class ;
19+ uint16_t type ;
1820} LookupParameters ;
1921
2022typedef struct LookupParametersResolveService {
@@ -1074,6 +1076,158 @@ static int vl_method_resolve_service(Varlink* link, JsonVariant* parameters, Var
10741076 return 1 ;
10751077}
10761078
1079+ static void vl_method_resolve_record_complete (DnsQuery * query ) {
1080+ _cleanup_ (json_variant_unrefp ) JsonVariant * array = NULL ;
1081+ _cleanup_ (dns_query_freep ) DnsQuery * q = query ;
1082+ DnsQuestion * question ;
1083+ int r ;
1084+
1085+ assert (q );
1086+
1087+ if (q -> state != DNS_TRANSACTION_SUCCESS ) {
1088+ r = reply_query_state (q );
1089+ goto finish ;
1090+ }
1091+
1092+ r = dns_query_process_cname_many (q );
1093+ if (r == - ELOOP ) {
1094+ r = varlink_error (q -> varlink_request , "io.systemd.Resolve.CNAMELoop" , NULL );
1095+ goto finish ;
1096+ }
1097+ if (r < 0 )
1098+ goto finish ;
1099+ if (r == DNS_QUERY_CNAME ) {
1100+ /* This was a cname, and the query was restarted. */
1101+ TAKE_PTR (q );
1102+ return ;
1103+ }
1104+
1105+ question = dns_query_question_for_protocol (q , q -> answer_protocol );
1106+
1107+ unsigned added = 0 ;
1108+ int ifindex ;
1109+ DnsResourceRecord * rr ;
1110+ DNS_ANSWER_FOREACH_IFINDEX (rr , ifindex , q -> answer ) {
1111+ _cleanup_ (json_variant_unrefp ) JsonVariant * v = NULL ;
1112+
1113+ r = dns_question_matches_rr (question , rr , NULL );
1114+ if (r < 0 )
1115+ goto finish ;
1116+ if (r == 0 )
1117+ continue ;
1118+
1119+ r = dns_resource_record_to_json (rr , & v );
1120+ if (r < 0 )
1121+ goto finish ;
1122+
1123+ r = dns_resource_record_to_wire_format (rr , /* canonical= */ false); /* don't use DNSSEC canonical format, since it removes casing, but we want that for DNS_SD compat */
1124+ if (r < 0 )
1125+ goto finish ;
1126+
1127+ r = json_variant_append_arrayb (
1128+ & array ,
1129+ JSON_BUILD_OBJECT (JSON_BUILD_PAIR_CONDITION (ifindex > 0 , "ifindex" , JSON_BUILD_INTEGER (ifindex )),
1130+ JSON_BUILD_PAIR_CONDITION (v , "rr" , JSON_BUILD_VARIANT (v )),
1131+ JSON_BUILD_PAIR ("raw" , JSON_BUILD_BASE64 (rr -> wire_format , rr -> wire_format_size ))));
1132+ if (r < 0 )
1133+ goto finish ;
1134+
1135+ added ++ ;
1136+ }
1137+
1138+ if (added <= 0 ) {
1139+ r = varlink_error (q -> varlink_request , "io.systemd.Resolve.NoSuchResourceRecord" , NULL );
1140+ goto finish ;
1141+ }
1142+
1143+ r = varlink_replyb (q -> varlink_request ,
1144+ JSON_BUILD_OBJECT (
1145+ JSON_BUILD_PAIR ("rrs" , JSON_BUILD_VARIANT (array )),
1146+ JSON_BUILD_PAIR ("flags" , JSON_BUILD_INTEGER (dns_query_reply_flags_make (q )))));
1147+ finish :
1148+ if (r < 0 ) {
1149+ log_full_errno (ERRNO_IS_DISCONNECT (r ) ? LOG_DEBUG : LOG_ERR , r , "Failed to send record reply: %m" );
1150+ varlink_error_errno (q -> varlink_request , r );
1151+ }
1152+ }
1153+
1154+ static int vl_method_resolve_record (Varlink * link , JsonVariant * parameters , VarlinkMethodFlags flags , void * userdata ) {
1155+ static const JsonDispatch dispatch_table [] = {
1156+ { "ifindex" , _JSON_VARIANT_TYPE_INVALID , json_dispatch_int , offsetof(LookupParameters , ifindex ), 0 },
1157+ { "name" , JSON_VARIANT_STRING , json_dispatch_string , offsetof(LookupParameters , name ), JSON_MANDATORY },
1158+ { "class" , _JSON_VARIANT_TYPE_INVALID , json_dispatch_uint16 , offsetof(LookupParameters , class ), 0 },
1159+ { "type" , _JSON_VARIANT_TYPE_INVALID , json_dispatch_uint16 , offsetof(LookupParameters , type ), JSON_MANDATORY },
1160+ { "flags" , _JSON_VARIANT_TYPE_INVALID , json_dispatch_uint64 , offsetof(LookupParameters , flags ), 0 },
1161+ {}
1162+ };
1163+
1164+ _cleanup_ (lookup_parameters_destroy ) LookupParameters p = {
1165+ .class = DNS_CLASS_IN ,
1166+ .type = _DNS_TYPE_INVALID ,
1167+ };
1168+ _cleanup_ (dns_query_freep ) DnsQuery * q = NULL ;
1169+ Manager * m ;
1170+ int r ;
1171+
1172+ assert (link );
1173+
1174+ m = ASSERT_PTR (varlink_server_get_userdata (varlink_get_server (link )));
1175+
1176+ if (FLAGS_SET (flags , VARLINK_METHOD_ONEWAY ))
1177+ return - EINVAL ;
1178+
1179+ r = varlink_dispatch (link , parameters , dispatch_table , & p );
1180+ if (r != 0 )
1181+ return r ;
1182+
1183+ if (p .ifindex < 0 )
1184+ return varlink_error_invalid_parameter (link , JSON_VARIANT_STRING_CONST ("ifindex" ));
1185+
1186+ r = dns_name_is_valid (p .name );
1187+ if (r < 0 )
1188+ return r ;
1189+ if (r == 0 )
1190+ return varlink_error_invalid_parameter (link , JSON_VARIANT_STRING_CONST ("name" ));
1191+
1192+ if (!dns_type_is_valid_query (p .type ))
1193+ return varlink_error (link , "io.systemd.Resolve.ResourceRecordTypeInvalidForQuery" , NULL );
1194+ if (dns_type_is_zone_transfer (p .type ))
1195+ return varlink_error (link , "io.systemd.Resolve.ZoneTransfersNotPermitted" , NULL );
1196+ if (dns_type_is_obsolete (p .type ))
1197+ return varlink_error (link , "io.systemd.Resolve.ResourceRecordTypeObsolete" , NULL );
1198+
1199+ if (!validate_and_mangle_flags (p .name , & p .flags , SD_RESOLVED_NO_SEARCH ))
1200+ return varlink_error_invalid_parameter (link , JSON_VARIANT_STRING_CONST ("flags" ));
1201+
1202+ _cleanup_ (dns_question_unrefp ) DnsQuestion * question = dns_question_new (1 );
1203+ if (!question )
1204+ return - ENOMEM ;
1205+
1206+ _cleanup_ (dns_resource_key_unrefp ) DnsResourceKey * key = NULL ;
1207+ key = dns_resource_key_new (p .class , p .type , p .name );
1208+ if (!key )
1209+ return - ENOMEM ;
1210+
1211+ r = dns_question_add (question , key , /* flags= */ 0 );
1212+ if (r < 0 )
1213+ return r ;
1214+
1215+ r = dns_query_new (m , & q , question , question , NULL , p .ifindex , p .flags |SD_RESOLVED_NO_SEARCH |SD_RESOLVED_CLAMP_TTL );
1216+ if (r < 0 )
1217+ return r ;
1218+
1219+ q -> varlink_request = varlink_ref (link );
1220+ varlink_set_userdata (link , q );
1221+ q -> complete = vl_method_resolve_record_complete ;
1222+
1223+ r = dns_query_go (q );
1224+ if (r < 0 )
1225+ return r ;
1226+
1227+ TAKE_PTR (q );
1228+ return 1 ;
1229+ }
1230+
10771231static int vl_method_subscribe_query_results (Varlink * link , JsonVariant * parameters , VarlinkMethodFlags flags , void * userdata ) {
10781232 Manager * m ;
10791233 int r ;
@@ -1297,9 +1451,10 @@ static int varlink_main_server_init(Manager *m) {
12971451
12981452 r = varlink_server_bind_method_many (
12991453 s ,
1300- "io.systemd.Resolve.ResolveHostname" , vl_method_resolve_hostname ,
1301- "io.systemd.Resolve.ResolveAddress" , vl_method_resolve_address ,
1302- "io.systemd.Resolve.ResolveService" , vl_method_resolve_service );
1454+ "io.systemd.Resolve.ResolveHostname" , vl_method_resolve_hostname ,
1455+ "io.systemd.Resolve.ResolveAddress" , vl_method_resolve_address ,
1456+ "io.systemd.Resolve.ResolveService" , vl_method_resolve_service ,
1457+ "io.systemd.Resolve.ResolveRecord" , vl_method_resolve_record );
13031458 if (r < 0 )
13041459 return log_error_errno (r , "Failed to register varlink methods: %m" );
13051460
0 commit comments