libtelnet: Fix styles in sra.c
authorAaron LI <aly@aaronly.me>
Thu, 5 Jul 2018 11:49:49 +0000 (19:49 +0800)
committerAaron LI <aly@aaronly.me>
Thu, 5 Jul 2018 11:49:49 +0000 (19:49 +0800)
Fix various small style(9) issues in sra.c, making some of the logic
clearer.

lib/libtelnet/sra.c

index de83888..c9ec93d 100644 (file)
@@ -62,8 +62,8 @@ extern char line[16];
 static int sra_valid = 0;
 static int passwd_sent = 0;
 
-static unsigned char str_data[1024] = { IAC, SB, TELOPT_AUTHENTICATION, 0,
-                                       AUTHTYPE_SRA, };
+static unsigned char str_data[1024] = {
+       IAC, SB, TELOPT_AUTHENTICATION, 0, AUTHTYPE_SRA, };
 
 #define SMALL_LEN      256
 #define XSMALL_LEN     513
@@ -81,32 +81,32 @@ static int check_user(char *, const char *);
 static int
 Data(Authenticator *ap, int type, void *d, int c)
 {
-        unsigned char *p = str_data + 4;
+       unsigned char *p = str_data + 4;
        unsigned char *cd = (unsigned char *)d;
 
        if (c == -1)
                c = strlen((char *)cd);
 
-        if (auth_debug_mode) {
-                printf("%s:%d: [%d] (%d)",
-                        str_data[3] == TELQUAL_IS ? ">>>IS" : ">>>REPLY",
-                        str_data[3],
-                        type, c);
-                printd(d, c);
-                printf("\r\n");
-        }
+       if (auth_debug_mode) {
+               printf("%s:%d: [%d] (%d)",
+                       str_data[3] == TELQUAL_IS ? ">>>IS" : ">>>REPLY",
+                       str_data[3],
+                       type, c);
+               printd(d, c);
+               printf("\r\n");
+       }
        *p++ = ap->type;
        *p++ = ap->way;
        *p++ = type;
-        while (c-- > 0) {
-                if ((*p++ = *cd++) == IAC)
-                        *p++ = IAC;
-        }
-        *p++ = IAC;
-        *p++ = SE;
+       while (c-- > 0) {
+               if ((*p++ = *cd++) == IAC)
+                       *p++ = IAC;
+       }
+       *p++ = IAC;
+       *p++ = SE;
        if (str_data[3] == TELQUAL_IS)
                printsub('>', &str_data[2], p - (&str_data[2]));
-        return(net_write(str_data, p - str_data));
+       return(net_write(str_data, p - str_data));
 }
 
 int
@@ -127,7 +127,7 @@ sra_init(Authenticator *ap __unused, int server)
 
        passwd_sent = 0;
        
-       genkeys(pka,ska);
+       genkeys(pka, ska);
        return(1);
 }
 
@@ -158,15 +158,14 @@ sra_is(Authenticator *ap, unsigned char *data, int cnt)
 
        if (cnt-- < 1)
                goto bad;
-       switch (*data++) {
 
+       switch (*data++) {
        case SRA_KEY:
                if (cnt < HEXKEYBYTES) {
                        Data(ap, SRA_REJECT, (void *)0, 0);
                        auth_finished(ap, AUTH_USER);
-                       if (auth_debug_mode) {
+                       if (auth_debug_mode)
                                printf("SRA user rejected for bad PKB\r\n");
-                       }
                        return;
                }
                if (auth_debug_mode)
@@ -176,35 +175,34 @@ sra_is(Authenticator *ap, unsigned char *data, int cnt)
                                printf("Not enough room\r\n");
                        return;
                }
-               memcpy(pkb,data,HEXKEYBYTES);
+               memcpy(pkb, data, HEXKEYBYTES);
                pkb[HEXKEYBYTES] = '\0';
-               common_key(ska,pkb,&ik,&ck);
+               common_key(ska, pkb, &ik, &ck);
                return;
 
        case SRA_USER:
                /* decode KAB(u) */
                if (cnt > XSMALL_LEN - 1) /* Attempted buffer overflow */
                        break;
-               memcpy(xuser,data,cnt);
+               memcpy(xuser, data, cnt);
                xuser[cnt] = '\0';
-               pk_decode(xuser,user,&ck);
+               pk_decode(xuser, user, &ck);
                auth_encrypt_user(user);
                Data(ap, SRA_CONTINUE, (void *)0, 0);
-
                return;
 
        case SRA_PASS:
                if (cnt > XSMALL_LEN - 1) /* Attempted buffer overflow */
                        break;
                /* decode KAB(P) */
-               memcpy(xpass,data,cnt);
+               memcpy(xpass, data, cnt);
                xpass[cnt] = '\0';
-               pk_decode(xpass,pass,&ck);
+               pk_decode(xpass, pass, &ck);
 
                /* check user's password */
-               valid = check_user(user,pass);
+               valid = check_user(user, pass);
 
-               if(valid) {
+               if (valid) {
                        Data(ap, SRA_ACCEPT, (void *)0, 0);
                        skey.data = ck;
                        skey.type = SK_DES;
@@ -213,20 +211,17 @@ sra_is(Authenticator *ap, unsigned char *data, int cnt)
 
                        sra_valid = 1;
                        auth_finished(ap, AUTH_VALID);
-                       if (auth_debug_mode) {
+                       if (auth_debug_mode)
                                printf("SRA user accepted\r\n");
-                       }
-               }
-               else {
+               } else {
                        Data(ap, SRA_CONTINUE, (void *)0, 0);
 /*
                        Data(ap, SRA_REJECT, (void *)0, 0);
                        sra_valid = 0;
                        auth_finished(ap, AUTH_REJECT);
 */
-                       if (auth_debug_mode) {
+                       if (auth_debug_mode)
                                printf("SRA user failed\r\n");
-                       }
                }
                return;
 
@@ -234,6 +229,7 @@ sra_is(Authenticator *ap, unsigned char *data, int cnt)
                if (auth_debug_mode)
                        printf("Unknown SRA option %d\r\n", data[-1]);
        }
+
 bad:
        Data(ap, SRA_REJECT, 0, 0);
        sra_valid = 0;
@@ -250,35 +246,32 @@ sra_reply(Authenticator *ap, unsigned char *data, int cnt)
 
        if (cnt-- < 1)
                return;
-       switch (*data++) {
 
+       switch (*data++) {
        case SRA_KEY:
                /* calculate common key */
                if (cnt < HEXKEYBYTES) {
-                       if (auth_debug_mode) {
+                       if (auth_debug_mode)
                                printf("SRA user rejected for bad PKB\r\n");
-                       }
                        return;
                }
-               memcpy(pkb,data,HEXKEYBYTES);
+               memcpy(pkb, data, HEXKEYBYTES);
                pkb[HEXKEYBYTES] = '\0';                
+               common_key(ska, pkb, &ik, &ck);
 
-               common_key(ska,pkb,&ik,&ck);
-
-       enc_user:
-
+enc_user:
                /* encode user */
-               memset(tuser,0,sizeof(tuser));
-               sprintf(uprompt,"User (%s): ",UserNameRequested);
-               if (telnet_gets(uprompt, tuser, SMALL_LEN - 1, 1) == NULL) {
+               memset(tuser, 0, sizeof(tuser));
+               sprintf(uprompt, "User (%s): ", UserNameRequested);
+               if (telnet_gets(uprompt, tuser, SMALL_LEN-1, 1) == NULL) {
                        printf("\n");
                        exit(1);
                }
-               if (tuser[0] == '\n' || tuser[0] == '\r' )
+               if (tuser[0] == '\n' || tuser[0] == '\r' ) {
                        strlcpy(user, UserNameRequested, SMALL_LEN);
-               else {
+               else {
                        /* telnet_gets leaves the newline on */
-                       for(i = 0; i < sizeof(tuser); i++) {
+                       for (i = 0; i < sizeof(tuser); i++) {
                                if (tuser[i] == '\n') {
                                        tuser[i] = '\0';
                                        break;
@@ -286,7 +279,7 @@ sra_reply(Authenticator *ap, unsigned char *data, int cnt)
                        }
                        strlcpy(user, tuser, SMALL_LEN);
                }
-               pk_encode(user,xuser,&ck);
+               pk_encode(user, xuser, &ck);
 
                /* send it off */
                if (auth_debug_mode)
@@ -325,7 +318,7 @@ sra_reply(Authenticator *ap, unsigned char *data, int cnt)
        case SRA_REJECT:
                printf("[ SRA refuses authentication ]\r\n");
                printf("Trying plaintext login:\r\n");
-               auth_finished(0,AUTH_REJECT);
+               auth_finished(0, AUTH_REJECT);
                return;
 
        case SRA_ACCEPT:
@@ -334,9 +327,9 @@ sra_reply(Authenticator *ap, unsigned char *data, int cnt)
                skey.type = SK_DES;
                skey.length = 8;
                encrypt_session_key(&skey, 0);
-
                auth_finished(ap, AUTH_VALID);
                return;
+
        default:
                if (auth_debug_mode)
                        printf("Unknown SRA option %d\r\n", data[-1]);
@@ -352,12 +345,13 @@ sra_status(Authenticator *ap __unused, char *name, int level)
        if (UserNameRequested && sra_valid) {
                strcpy(name, UserNameRequested);
                return(AUTH_VALID);
-       } else
+       } else {
                return(AUTH_USER);
+       }
 }
 
-#define        BUMP(buf, len)          while (*(buf)) {++(buf), --(len);}
-#define        ADDC(buf, len, c)       if ((len) > 0) {*(buf)++ = (c); --(len);}
+#define        BUMP(buf, len)          while (*(buf)) { ++(buf), --(len); }
+#define        ADDC(buf, len, c)       if ((len) > 0) { *(buf)++ = (c); --(len); }
 
 void
 sra_printsub(unsigned char *data, int cnt, unsigned char *ubuf, int buflen)
@@ -368,8 +362,7 @@ sra_printsub(unsigned char *data, int cnt, unsigned char *ubuf, int buflen)
        buf[buflen-1] = '\0';           /* make sure its NULL terminated */
        buflen -= 1;
 
-       switch(data[3]) {
-
+       switch (data[3]) {
        case SRA_CONTINUE:
                strncpy(buf, " CONTINUE ", buflen);
                goto common;
@@ -381,7 +374,7 @@ sra_printsub(unsigned char *data, int cnt, unsigned char *ubuf, int buflen)
        case SRA_ACCEPT:                /* Accepted (name might follow) */
                strncpy(buf, " ACCEPT ", buflen);
 
-       common:
+common:
                BUMP(buf, buflen);
                if (cnt <= 4)
                        break;
@@ -407,7 +400,8 @@ sra_printsub(unsigned char *data, int cnt, unsigned char *ubuf, int buflen)
        default:
                snprintf(lbuf, sizeof(lbuf), " %d (unknown)", data[3]);
                strncpy(buf, lbuf, buflen);
-       common2:
+
+common2:
                BUMP(buf, buflen);
                for (i = 4; i < cnt; i++) {
                        snprintf(lbuf, sizeof(lbuf), " %d", data[i]);
@@ -429,6 +423,7 @@ isroot(const char *usr)
        if (getpwnam_r(usr, &pws, pwbuf, sizeof(pwbuf), &pwd) != 0 ||
            pwd == NULL)
                return 0;
+
        return (!pwd->pw_uid);
 }
 
@@ -487,7 +482,7 @@ typedef struct cred_t cred_t;
 
 static int
 auth_conv(int num_msg, const struct pam_message **msg,
-    struct pam_response **resp, void *appdata)
+         struct pam_response **resp, void *appdata)
 {
        int i;
        cred_t *cred = appdata;
@@ -571,25 +566,25 @@ check_user(char *name, const char *cred)
                 * point of view, the template user is always passed
                 * back as a changed value of the PAM_USER item.
                 */
-               if ((e = pam_get_item(pamh, PAM_USER, &item)) ==
-                   PAM_SUCCESS) {
+               if ((e = pam_get_item(pamh, PAM_USER,
+                                     &item)) == PAM_SUCCESS)
                        strlcpy(name, item, SMALL_LEN);
-               else
+               else
                        syslog(LOG_ERR, "Couldn't get PAM_USER: %s",
-                       pam_strerror(pamh, e));
+                              pam_strerror(pamh, e));
+
+               rval = 1;
 #if 0  /* pam_securetty(8) should be used to enforce this */
                if (isroot(name) && !rootterm(line))
                        rval = 0;
-               else
 #endif
-                       rval = 1;
                break;
 
        case PAM_AUTH_ERR:
        case PAM_USER_UNKNOWN:
        case PAM_MAXTRIES:
                rval = 0;
-       break;
+               break;
 
        default:
                syslog(LOG_ERR, "auth_pam: %s", pam_strerror(pamh, e));