check for username, too
[freeradius.git] / src / main / auth.c
index 9b1667d..79e639e 100644 (file)
@@ -38,19 +38,19 @@ char *auth_name(char *buf, size_t buflen, REQUEST *request, bool do_cli)
 {
        VALUE_PAIR      *cli;
        VALUE_PAIR      *pair;
-       uint16_t        port = 0;
+       uint32_t        port = 0;       /* RFC 2865 NAS-Port is 4 bytes */
        char const      *tls = "";
 
-       if ((cli = pairfind(request->packet->vps, PW_CALLING_STATION_ID, 0, TAG_ANY)) == NULL) {
+       if ((cli = fr_pair_find_by_num(request->packet->vps, PW_CALLING_STATION_ID, 0, TAG_ANY)) == NULL) {
                do_cli = false;
        }
 
-       if ((pair = pairfind(request->packet->vps, PW_NAS_PORT, 0, TAG_ANY)) != NULL) {
+       if ((pair = fr_pair_find_by_num(request->packet->vps, PW_NAS_PORT, 0, TAG_ANY)) != NULL) {
                port = pair->vp_integer;
        }
 
        if (request->packet->dst_port == 0) {
-               if (pairfind(request->packet->vps, PW_FREERADIUS_PROXIED_TO, 0, TAG_ANY)) {
+               if (fr_pair_find_by_num(request->packet->vps, PW_FREERADIUS_PROXIED_TO, 0, TAG_ANY)) {
                        tls = " via TLS tunnel";
                } else {
                        tls = " via proxy to virtual server";
@@ -90,7 +90,7 @@ static int rad_authlog(char const *msg, REQUEST *request, int goodpass)
         * Get the correct username based on the configured value
         */
        if (!log_stripped_names) {
-               username = pairfind(request->packet->vps, PW_USER_NAME, 0, TAG_ANY);
+               username = fr_pair_find_by_num(request->packet->vps, PW_USER_NAME, 0, TAG_ANY);
        } else {
                username = request->username;
        }
@@ -101,9 +101,7 @@ static int rad_authlog(char const *msg, REQUEST *request, int goodpass)
        if (username == NULL) {
                strcpy(clean_username, "<no User-Name attribute>");
        } else {
-               fr_print_string(username->vp_strvalue,
-                               username->length,
-                               clean_username, sizeof(clean_username), '\0');
+               fr_prints(clean_username, sizeof(clean_username), username->vp_strvalue, username->vp_length, '\0');
        }
 
        /*
@@ -113,7 +111,7 @@ static int rad_authlog(char const *msg, REQUEST *request, int goodpass)
                if (!request->password) {
                        VALUE_PAIR *auth_type;
 
-                       auth_type = pairfind(request->config_items, PW_AUTH_TYPE, 0, TAG_ANY);
+                       auth_type = fr_pair_find_by_num(request->config, PW_AUTH_TYPE, 0, TAG_ANY);
                        if (auth_type) {
                                snprintf(clean_password, sizeof(clean_password),
                                         "<via Auth-Type = %s>",
@@ -122,12 +120,11 @@ static int rad_authlog(char const *msg, REQUEST *request, int goodpass)
                        } else {
                                strcpy(clean_password, "<no User-Password attribute>");
                        }
-               } else if (pairfind(request->packet->vps, PW_CHAP_PASSWORD, 0, TAG_ANY)) {
+               } else if (fr_pair_find_by_num(request->packet->vps, PW_CHAP_PASSWORD, 0, TAG_ANY)) {
                        strcpy(clean_password, "<CHAP-Password>");
                } else {
-                       fr_print_string(request->password->vp_strvalue,
-                                        request->password->length,
-                                       clean_password, sizeof(clean_password), '\0');
+                       fr_prints(clean_password, sizeof(clean_password),
+                                 request->password->vp_strvalue, request->password->vp_length, '\0');
                }
        }
 
@@ -180,16 +177,16 @@ static int CC_HINT(nonnull) rad_check_password(REQUEST *request)
 
        /*
         *      Look for matching check items. We skip the whole lot
-        *      if the authentication type is PW_AUTHTYPE_ACCEPT or
-        *      PW_AUTHTYPE_REJECT.
+        *      if the authentication type is PW_AUTH_TYPE_ACCEPT or
+        *      PW_AUTH_TYPE_REJECT.
         */
-       fr_cursor_init(&cursor, &request->config_items);
+       fr_cursor_init(&cursor, &request->config);
        while ((auth_type_pair = fr_cursor_next_by_num(&cursor, PW_AUTH_TYPE, 0, TAG_ANY))) {
                auth_type = auth_type_pair->vp_integer;
                auth_type_count++;
 
                RDEBUG2("Found Auth-Type = %s", dict_valnamebyattr(PW_AUTH_TYPE, 0, auth_type));
-               if (auth_type == PW_AUTHTYPE_REJECT) {
+               if (auth_type == PW_AUTH_TYPE_REJECT) {
                        RDEBUG2("Auth-Type = Reject, rejecting user");
 
                        return -2;
@@ -200,7 +197,7 @@ static int CC_HINT(nonnull) rad_check_password(REQUEST *request)
         *      Warn if more than one Auth-Type was found, because only the last
         *      one found will actually be used.
         */
-       if ((auth_type_count > 1) && (debug_flag)) {
+       if ((auth_type_count > 1) && (rad_debug_lvl) && request->username) {
                RERROR("Warning:  Found %d auth-types on request for user '%s'",
                        auth_type_count, request->username->vp_strvalue);
        }
@@ -210,7 +207,7 @@ static int CC_HINT(nonnull) rad_check_password(REQUEST *request)
         *      rejected in the above loop. So that means it is accepted and we
         *      do no further authentication.
         */
-       if ((auth_type == PW_AUTHTYPE_ACCEPT)
+       if ((auth_type == PW_AUTH_TYPE_ACCEPT)
 #ifdef WITH_PROXY
            || (request->proxy)
 #endif
@@ -226,11 +223,11 @@ static int CC_HINT(nonnull) rad_check_password(REQUEST *request)
         *      been set, and complain if so.
         */
        if (auth_type < 0) {
-               if (pairfind(request->config_items, PW_CRYPT_PASSWORD, 0, TAG_ANY) != NULL) {
+               if (fr_pair_find_by_num(request->config, PW_CRYPT_PASSWORD, 0, TAG_ANY) != NULL) {
                        RWDEBUG2("Please update your configuration, and remove 'Auth-Type = Crypt'");
                        RWDEBUG2("Use the PAP module instead");
                }
-               else if (pairfind(request->config_items, PW_CLEARTEXT_PASSWORD, 0, TAG_ANY) != NULL) {
+               else if (fr_pair_find_by_num(request->config, PW_CLEARTEXT_PASSWORD, 0, TAG_ANY) != NULL) {
                        RWDEBUG2("Please update your configuration, and remove 'Auth-Type = Local'");
                        RWDEBUG2("Use the PAP or CHAP modules instead");
                }
@@ -294,15 +291,29 @@ int rad_postauth(REQUEST *request)
        int     postauth_type = 0;
        VALUE_PAIR *vp;
 
+       if (request->reply->code == PW_CODE_ACCESS_CHALLENGE) {
+               fr_pair_delete_by_num(&request->config, PW_POST_AUTH_TYPE, 0, TAG_ANY);
+               vp = pair_make_config("Post-Auth-Type", "Challenge", T_OP_SET);
+               if (!vp) return RLM_MODULE_OK;
+
+       } else if (request->reply->code == PW_CODE_ACCESS_REJECT) {
+               fr_pair_delete_by_num(&request->config, PW_POST_AUTH_TYPE, 0, TAG_ANY);
+               vp = pair_make_config("Post-Auth-Type", "Reject", T_OP_SET);
+               if (!vp) return RLM_MODULE_OK;
+
+       } else {
+               vp = fr_pair_find_by_num(request->config, PW_POST_AUTH_TYPE, 0, TAG_ANY);
+       }
+
        /*
-        *      Do post-authentication calls. ignoring the return code.
+        *      If a method was chosen, use that.
         */
-       vp = pairfind(request->config_items, PW_POST_AUTH_TYPE, 0, TAG_ANY);
        if (vp) {
                postauth_type = vp->vp_integer;
                RDEBUG2("Using Post-Auth-Type %s",
                        dict_valnamebyattr(PW_POST_AUTH_TYPE, 0, postauth_type));
        }
+
        result = process_post_auth(postauth_type, request);
        switch (result) {
        /*
@@ -313,7 +324,18 @@ int rad_postauth(REQUEST *request)
        case RLM_MODULE_REJECT:
        case RLM_MODULE_USERLOCK:
        default:
-               request->reply->code = PW_CODE_ACCESS_REJECT;
+               /*
+                *      We WERE going to have a nice reply, but
+                *      something went wrong.  So we've got to run
+                *      Post-Auth-Type Reject.
+                */
+               if (request->reply->code != PW_CODE_ACCESS_REJECT) {
+                       RDEBUG("Using Post-Auth-Type Reject");
+
+                       request->reply->code = PW_CODE_ACCESS_REJECT;
+                       process_post_auth(PW_POST_AUTH_TYPE_REJECT, request);
+               }
+
                fr_state_discard(request, request->packet);
                result = RLM_MODULE_REJECT;
                break;
@@ -340,6 +362,35 @@ int rad_postauth(REQUEST *request)
                }
                break;
        }
+
+       /*
+        *      Rejects during authorize, etc. are handled by the
+        *      earlier code, which logs a reason for the rejection.
+        *      If the packet is rejected in post-auth, we need to log
+        *      that as a separate reason.
+        */
+       if (result == RLM_MODULE_REJECT) {
+               if (request->reply->code != RLM_MODULE_REJECT) {
+                       rad_authlog("Rejected in post-auth", request, 0);
+               }
+               request->reply->code = PW_CODE_ACCESS_REJECT;
+       }
+
+       /*
+        *      If we're still accepting the user, say so.
+        */
+       if (request->reply->code == PW_CODE_ACCESS_ACCEPT) {
+               if ((vp = fr_pair_find_by_num(request->packet->vps, PW_MODULE_SUCCESS_MESSAGE, 0, TAG_ANY)) != NULL) {
+                       char msg[MAX_STRING_LEN+12];
+
+                       snprintf(msg, sizeof(msg), "Login OK (%s)",
+                                vp->vp_strvalue);
+                       rad_authlog(msg, request, 1);
+               } else {
+                       rad_authlog("Login OK", request, 1);
+               }
+       }
+
        return result;
 }
 
@@ -380,10 +431,10 @@ int rad_authenticate(REQUEST *request)
                 *      accordingly.
                 */
                case PW_CODE_ACCESS_ACCEPT:
-                       tmp = radius_paircreate(request,
-                                               &request->config_items,
+                       tmp = radius_pair_create(request,
+                                               &request->config,
                                                PW_AUTH_TYPE, 0);
-                       if (tmp) tmp->vp_integer = PW_AUTHTYPE_ACCEPT;
+                       if (tmp) tmp->vp_integer = PW_AUTH_TYPE_ACCEPT;
                        goto authenticate;
 
                /*
@@ -392,6 +443,7 @@ int rad_authenticate(REQUEST *request)
                 */
                case PW_CODE_ACCESS_CHALLENGE:
                        request->reply->code = PW_CODE_ACCESS_CHALLENGE;
+                       fr_state_put_vps(request, request->packet, request->reply);
                        return RLM_MODULE_OK;
 
                /*
@@ -405,11 +457,13 @@ int rad_authenticate(REQUEST *request)
                        rad_authlog("Login incorrect (Home Server says so)",
                                    request, 0);
                        request->reply->code = PW_CODE_ACCESS_REJECT;
+                       fr_state_discard(request, request->packet);
                        return RLM_MODULE_REJECT;
 
                default:
                        rad_authlog("Login incorrect (Home Server failed to respond)",
                                    request, 0);
+                       fr_state_discard(request, request->packet);
                        return RLM_MODULE_REJECT;
                }
        }
@@ -418,10 +472,10 @@ int rad_authenticate(REQUEST *request)
         *      Look for, and cache, passwords.
         */
        if (!request->password) {
-               request->password = pairfind(request->packet->vps, PW_USER_PASSWORD, 0, TAG_ANY);
+               request->password = fr_pair_find_by_num(request->packet->vps, PW_USER_PASSWORD, 0, TAG_ANY);
        }
        if (!request->password) {
-               request->password = pairfind(request->packet->vps, PW_CHAP_PASSWORD, 0, TAG_ANY);
+               request->password = fr_pair_find_by_num(request->packet->vps, PW_CHAP_PASSWORD, 0, TAG_ANY);
        }
 
        /*
@@ -447,7 +501,7 @@ autz_redo:
        case RLM_MODULE_REJECT:
        case RLM_MODULE_USERLOCK:
        default:
-               if ((module_msg = pairfind(request->packet->vps, PW_MODULE_FAILURE_MESSAGE, 0, TAG_ANY)) != NULL) {
+               if ((module_msg = fr_pair_find_by_num(request->packet->vps, PW_MODULE_FAILURE_MESSAGE, 0, TAG_ANY)) != NULL) {
                        char msg[MAX_STRING_LEN + 16];
                        snprintf(msg, sizeof(msg), "Invalid user (%s)",
                                 module_msg->vp_strvalue);
@@ -459,7 +513,7 @@ autz_redo:
                return result;
        }
        if (!autz_retry) {
-               tmp = pairfind(request->config_items, PW_AUTZ_TYPE, 0, TAG_ANY);
+               tmp = fr_pair_find_by_num(request->config, PW_AUTZ_TYPE, 0, TAG_ANY);
                if (tmp) {
                        autz_type = tmp->vp_integer;
                        RDEBUG2("Using Autz-Type %s",
@@ -479,7 +533,7 @@ autz_redo:
 #ifdef WITH_PROXY
            (request->proxy == NULL) &&
 #endif
-           ((tmp = pairfind(request->config_items, PW_PROXY_TO_REALM, 0, TAG_ANY)) != NULL)) {
+           ((tmp = fr_pair_find_by_num(request->config, PW_PROXY_TO_REALM, 0, TAG_ANY)) != NULL)) {
                REALM *realm;
 
                realm = realm_find2(tmp->vp_strvalue);
@@ -507,7 +561,7 @@ autz_redo:
        }
 
 #ifdef WITH_PROXY
- authenticate:
+authenticate:
 #endif
 
        /*
@@ -516,15 +570,6 @@ autz_redo:
        do {
                result = rad_check_password(request);
                if (result > 0) {
-                       /*
-                        *      We presume that the reply has been set by someone.
-                        */
-                       if (request->reply->code == PW_CODE_ACCESS_CHALLENGE) {
-                               fr_state_put_vps(request, request->packet, request->reply);
-
-                       } else {
-                               fr_state_discard(request, request->packet);
-                       }
                        return RLM_MODULE_HANDLED;
                }
 
@@ -541,7 +586,7 @@ autz_redo:
                RDEBUG2("Failed to authenticate the user");
                request->reply->code = PW_CODE_ACCESS_REJECT;
 
-               if ((module_msg = pairfind(request->packet->vps, PW_MODULE_FAILURE_MESSAGE, 0, TAG_ANY)) != NULL){
+               if ((module_msg = fr_pair_find_by_num(request->packet->vps, PW_MODULE_FAILURE_MESSAGE, 0, TAG_ANY)) != NULL){
                        char msg[MAX_STRING_LEN+19];
 
                        snprintf(msg, sizeof(msg), "Login incorrect (%s)",
@@ -554,14 +599,14 @@ autz_redo:
                if (request->password) {
                        VERIFY_VP(request->password);
                        /* double check: maybe the secret is wrong? */
-                       if ((debug_flag > 1) && (request->password->da->attr == PW_USER_PASSWORD)) {
+                       if ((rad_debug_lvl > 1) && (request->password->da->attr == PW_USER_PASSWORD)) {
                                uint8_t const *p;
 
                                p = (uint8_t const *) request->password->vp_strvalue;
                                while (*p) {
                                        int size;
 
-                                       size = fr_utf8_char(p);
+                                       size = fr_utf8_char(p, -1);
                                        if (!size) {
                                                RWDEBUG("Unprintable characters in the password.  Double-check the "
                                                        "shared secret on the server and the NAS!");
@@ -575,12 +620,12 @@ autz_redo:
 
 #ifdef WITH_SESSION_MGMT
        if (result >= 0 &&
-           (check_item = pairfind(request->config_items, PW_SIMULTANEOUS_USE, 0, TAG_ANY)) != NULL) {
+           (check_item = fr_pair_find_by_num(request->config, PW_SIMULTANEOUS_USE, 0, TAG_ANY)) != NULL) {
                int r, session_type = 0;
                char            logstr[1024];
                char            umsg[MAX_STRING_LEN + 1];
 
-               tmp = pairfind(request->config_items, PW_SESSION_TYPE, 0, TAG_ANY);
+               tmp = fr_pair_find_by_num(request->config, PW_SESSION_TYPE, 0, TAG_ANY);
                if (tmp) {
                        session_type = tmp->vp_integer;
                        RDEBUG2("Using Session-Type %s",
@@ -599,7 +644,7 @@ autz_redo:
                                /* Multilink attempt. Check if port-limit > simultaneous-use */
                                VALUE_PAIR *port_limit;
 
-                               if ((port_limit = pairfind(request->reply->vps, PW_PORT_LIMIT, 0, TAG_ANY)) != NULL &&
+                               if ((port_limit = fr_pair_find_by_num(request->reply->vps, PW_PORT_LIMIT, 0, TAG_ANY)) != NULL &&
                                        port_limit->vp_integer > check_item->vp_integer){
                                        RDEBUG2("MPP is OK");
                                        mpp_ok = 1;
@@ -607,14 +652,10 @@ autz_redo:
                        }
                        if (!mpp_ok){
                                if (check_item->vp_integer > 1) {
-                                       snprintf(umsg, sizeof(umsg),
-                                                "\r\n%s (%d)\r\n\n",
-                                                main_config.denied_msg,
-                                                (int)check_item->vp_integer);
+                                       snprintf(umsg, sizeof(umsg), "%s (%u)", main_config.denied_msg,
+                                                check_item->vp_integer);
                                } else {
-                                       snprintf(umsg, sizeof(umsg),
-                                                "\r\n%s\r\n\n",
-                                                main_config.denied_msg);
+                                       strlcpy(umsg, main_config.denied_msg, sizeof(umsg));
                                }
 
                                request->reply->code = PW_CODE_ACCESS_REJECT;
@@ -623,9 +664,8 @@ autz_redo:
                                 *      They're trying to log in too many times.
                                 *      Remove ALL reply attributes.
                                 */
-                               pairfree(&request->reply->vps);
-                               pairmake_reply("Reply-Message",
-                                              umsg, T_OP_SET);
+                               fr_pair_list_free(&request->reply->vps);
+                               pair_make_reply("Reply-Message", umsg, T_OP_SET);
 
                                snprintf(logstr, sizeof(logstr), "Multiple logins (max %d) %s",
                                        check_item->vp_integer,
@@ -650,17 +690,8 @@ autz_redo:
         *      Set the reply to Access-Accept, if it hasn't already
         *      been set to something.  (i.e. Access-Challenge)
         */
-       if (request->reply->code == 0)
-         request->reply->code = PW_CODE_ACCESS_ACCEPT;
-
-       if ((module_msg = pairfind(request->packet->vps, PW_MODULE_SUCCESS_MESSAGE, 0, TAG_ANY)) != NULL){
-               char msg[MAX_STRING_LEN+12];
-
-               snprintf(msg, sizeof(msg), "Login OK (%s)",
-                        module_msg->vp_strvalue);
-               rad_authlog(msg, request, 1);
-       } else {
-               rad_authlog("Login OK", request, 1);
+       if (request->reply->code == 0) {
+               request->reply->code = PW_CODE_ACCESS_ACCEPT;
        }
 
        return result;
@@ -675,9 +706,101 @@ int rad_virtual_server(REQUEST *request)
        VALUE_PAIR *vp;
        int result;
 
-       RDEBUG("Virtual server received request");
+       RDEBUG("Virtual server %s received request", request->server);
        rdebug_pair_list(L_DBG_LVL_1, request, request->packet->vps, NULL);
 
+       if (!request->username) {
+               request->username = fr_pair_find_by_num(request->packet->vps, PW_USER_NAME, 0, TAG_ANY);
+       }
+
+       /*
+        *      Complain about possible issues related to tunnels.
+        */
+       if (request->parent && request->parent->username && request->username) {
+               /*
+                *      Look at the full User-Name with realm.
+                */
+               if (request->parent->username->da->attr == PW_STRIPPED_USER_NAME) {
+                       vp = fr_pair_find_by_num(request->parent->packet->vps, PW_USER_NAME, 0, TAG_ANY);
+                       rad_assert(vp != NULL);
+               } else {
+                       vp = request->parent->username;
+               }
+
+               /*
+                *      If the names aren't identical, we do some detailed checks.
+                */
+               if (strcmp(vp->vp_strvalue, request->username->vp_strvalue) != 0) {
+                       char const *outer, *inner;
+
+                       outer = strchr(vp->vp_strvalue, '@');
+
+                       /*
+                        *      If there's no realm, or there's a user identifier before
+                        *      the realm name, check the user identifier.
+                        *
+                        *      It SHOULD be "anonymous", or "anonymous@realm"
+                        */
+                       if (outer) {
+                               if ((outer != vp->vp_strvalue) &&
+                                   ((vp->vp_length < 10) || (memcmp(vp->vp_strvalue, "anonymous@", 10) != 0))) {
+                                       RWDEBUG("Outer User-Name is not anonymized.  User privacy is compromised.");
+                               } /* else it is anonymized */
+
+                               /*
+                                *      Check when there's no realm, and without the trailing '@'
+                                */
+                       } else if ((vp->vp_length < 9) || (memcmp(vp->vp_strvalue, "anonymous", 9) != 0)) {
+                                       RWDEBUG("Outer User-Name is not anonymized.  User privacy is compromised.");
+
+                       } /* else the user identifier is anonymized */
+
+                       /*
+                        *      Look for an inner realm, which may or may not exist.
+                        */
+                       inner = strchr(request->username->vp_strvalue, '@');
+                       if (outer && inner) {
+                               outer++;
+                               inner++;
+
+                               /*
+                                *      The realms are different, do
+                                *      more detailed checks.
+                                */
+                               if (strcmp(outer, inner) != 0) {
+                                       size_t outer_len, inner_len;
+
+                                       outer_len = vp->vp_length;
+                                       outer_len -= (outer - vp->vp_strvalue);
+
+                                       inner_len = request->username->vp_length;
+                                       inner_len -= (inner - request->username->vp_strvalue);
+
+                                       /*
+                                        *      Inner: secure.example.org
+                                        *      Outer: example.org
+                                        */
+                                       if (inner_len > outer_len) {
+                                               char const *suffix;
+
+                                               suffix = inner + (inner_len - outer_len) - 1;
+
+                                               if ((*suffix != '.') ||
+                                                   (strcmp(suffix + 1, outer) != 0)) {
+                                                       RWDEBUG("Possible spoofing: Inner realm '%s' is not a subdomain of the outer realm '%s'", inner, outer);
+                                               }
+
+                                       } else {
+                                               RWDEBUG("Possible spoofing: Inner realm and outer realms are different");
+                                       }
+                               }
+                       }
+
+               } else {
+                       RWDEBUG("Outer and inner identities are the same.  User privacy is compromised.");
+               }
+       }
+
        RDEBUG("server %s {", request->server);
        RINDENT();
 
@@ -690,8 +813,8 @@ int rad_virtual_server(REQUEST *request)
        result = rad_authenticate(request);
 
        if (request->reply->code == PW_CODE_ACCESS_REJECT) {
-               pairdelete(&request->config_items, PW_POST_AUTH_TYPE, 0, TAG_ANY);
-               vp = pairmake_config("Post-Auth-Type", "Reject", T_OP_SET);
+               fr_pair_delete_by_num(&request->config, PW_POST_AUTH_TYPE, 0, TAG_ANY);
+               vp = pair_make_config("Post-Auth-Type", "Reject", T_OP_SET);
                if (vp) rad_postauth(request);
        }