int authsasl_plain(const char *method, const char *initresponse, char *(*getresp)(const char *), char **authtype, char **authdata) { char *uid; char *pw; char *p; int n; int i; if (initresponse) { p=malloc(strlen(initresponse)+1); if (!p) { perror("malloc"); return (AUTHSASL_ERROR); } strcpy(p, initresponse); } else { p=authsasl_tobase64("", -1); if (!p) { perror("malloc"); return (AUTHSASL_ERROR); } uid=getresp(p); free(p); p=uid; if (!p) { perror("malloc"); return (AUTHSASL_ERROR); } if (*p == '*') { free(p); return (AUTHSASL_ABORTED); } } if ((n=authsasl_frombase64(p)) < 0) { free(p); return (AUTHSASL_ABORTED); } p[n]=0; uid=pw=0; for (i=0; i<n; i++) { if (p[i] == 0) { ++i; for (uid=p+i; i<n; i++) if (p[i] == 0) { pw=p+i+1; break; } } } if (pw == 0) { free(p); return (AUTHSASL_ABORTED); /* Bad message */ } if ( (*authtype=malloc(sizeof(AUTHTYPE_LOGIN))) == 0) { free(p); perror("malloc"); return (AUTHSASL_ERROR); } strcpy( *authtype, AUTHTYPE_LOGIN); if ( (*authdata=malloc(strlen(uid)+strlen(pw)+3)) == 0) { free( *authtype ); free(p); perror("malloc"); return (AUTHSASL_ERROR); } strcat(strcat(strcat(strcpy(*authdata, uid), "\n"), pw), "\n"); free(p); return (AUTHSASL_OK); }
int auth_sasl_ex(const char *method, const char *initresponse, const char *externalauth, char *(*callback_func)(const char *, void *), void *callback_arg, char **authtype_ptr, /* Returned - AUTHTYPE */ char **authdata_ptr) { char *uid; int n; if (strcmp(method, "EXTERNAL")) return auth_sasl(method, initresponse, callback_func, callback_arg, authtype_ptr, authdata_ptr); if (!externalauth || !*externalauth) return AUTHSASL_ERROR; if (initresponse && !*initresponse) initresponse=NULL; if (initresponse && strcmp(initresponse, externalauth)) return AUTHSASL_ERROR; if (!initresponse) { uid=callback_func("", callback_arg); if (*uid == '*') { free(uid); return (AUTHSASL_ABORTED); } n=authsasl_frombase64(uid); if (n < 0) { free(uid); return AUTHSASL_ABORTED; } uid[n]=0; if (uid[0]) { free(uid); return AUTHSASL_ABORTED; } free(uid); } if ((*authtype_ptr=strdup("EXTERNAL")) == NULL) return AUTHSASL_ABORTED; if ((*authdata_ptr=strdup(externalauth)) == NULL) { free(authtype_ptr); return AUTHSASL_ABORTED; } return 0; }