auth.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132
  1. /*
  2. auth.c -- Authorization Management
  3. This modules supports a user/role/ability based authorization scheme.
  4. In this scheme Users have passwords and can have multiple roles. A role is associated with the ability to do
  5. things like "admin" or "user" or "support". A role may have abilities (which are typically verbs) like "add",
  6. "shutdown".
  7. When the web server starts up, it loads a route and authentication configuration file that specifies the Users,
  8. Roles and Routes. Routes specify the required abilities to access URLs by specifying the URL prefix. Once logged
  9. in, the user's abilities are tested against the route abilities. When the web server receivess a request, the set of
  10. Routes is consulted to select the best route. If the routes requires abilities, the user must be logged in and
  11. authenticated.
  12. Three authentication backend protocols are supported:
  13. HTTP basic authentication which uses browser dialogs and clear text passwords (insecure unless over TLS)
  14. HTTP digest authentication which uses browser dialogs
  15. Web form authentication which uses a web page form to login (insecure unless over TLS)
  16. Copyright (c) All Rights Reserved. See details at the end of the file.
  17. */
  18. /********************************* Includes ***********************************/
  19. #include "goahead.h"
  20. #include "ResultUtils.h"
  21. #if ME_GOAHEAD_AUTH
  22. #if ME_COMPILER_HAS_PAM
  23. #include <security/pam_appl.h>
  24. #endif
  25. /*********************************** Locals ***********************************/
  26. static WebsHash users = -1;
  27. static WebsHash roles = -1;
  28. static char *masterSecret;
  29. static int autoLogin = ME_GOAHEAD_AUTO_LOGIN;
  30. static WebsVerify verifyPassword = websVerifyPasswordFromFile;
  31. #if ME_COMPILER_HAS_PAM
  32. typedef struct {
  33. char *name;
  34. char *password;
  35. } UserInfo;
  36. #if MACOSX
  37. typedef int Gid;
  38. #else
  39. typedef gid_t Gid;
  40. #endif
  41. #endif
  42. /********************************** Forwards **********************************/
  43. static void computeAbilities(WebsHash abilities, cchar *role, int depth);
  44. static void computeUserAbilities(WebsUser *user);
  45. static WebsUser *createUser(cchar *username, cchar *password, cchar *roles);
  46. static void freeRole(WebsRole *rp);
  47. static void freeUser(WebsUser *up);
  48. static void logoutServiceProc(Webs *wp);
  49. static void loginServiceProc(Webs *wp);
  50. #if ME_GOAHEAD_JAVASCRIPT && FUTURE
  51. static int jsCan(int jsid, Webs *wp, int argc, char **argv);
  52. #endif
  53. #if ME_GOAHEAD_DIGEST
  54. static char *calcDigest(Webs *wp, char *username, char *password);
  55. static char *createDigestNonce(Webs *wp);
  56. static char *parseDigestNonce(char *nonce, char **secret, char **realm, WebsTime *when);
  57. #endif
  58. #if ME_COMPILER_HAS_PAM
  59. static int pamChat(int msgCount, const struct pam_message **msg, struct pam_response **resp, void *data);
  60. #endif
  61. /************************************ Code ************************************/
  62. PUBLIC bool websAuthenticate(Webs *wp)
  63. {
  64. WebsRoute *route;
  65. char *username;
  66. int cached;
  67. assert(wp);
  68. assert(wp->route);
  69. route = wp->route;
  70. if (!route || !route->authType || autoLogin) {
  71. /* Authentication not required */
  72. return 1;
  73. }
  74. cached = 0;
  75. if (wp->cookie && websGetSession(wp, 0) != 0) {
  76. /*
  77. Retrieve authentication state from the session storage. Faster than re-authenticating.
  78. */
  79. if ((username = (char*) websGetSessionVar(wp, WEBS_SESSION_USERNAME, 0)) != 0) {
  80. cached = 1;
  81. wfree(wp->username);
  82. wp->username = sclone(username);
  83. }
  84. }
  85. if (!cached) {
  86. if (wp->authType && !smatch(wp->authType, route->authType)) {
  87. websError(wp, HTTP_CODE_UNAUTHORIZED, "Access denied. Wrong authentication protocol type.");
  88. return 0;
  89. }
  90. if (wp->authDetails && route->parseAuth) {
  91. if (!(route->parseAuth)(wp)) {
  92. wp->username = 0;
  93. }
  94. }
  95. if (!wp->username || !*wp->username) {
  96. if (route->askLogin) {
  97. (route->askLogin)(wp);
  98. }
  99. websRedirectByStatus(wp, HTTP_CODE_UNAUTHORIZED);
  100. return 0;
  101. }
  102. if (!(route->verify)(wp)) {
  103. if (route->askLogin) {
  104. (route->askLogin)(wp);
  105. }
  106. websRedirectByStatus(wp, HTTP_CODE_UNAUTHORIZED);
  107. return 0;
  108. }
  109. /*
  110. Store authentication state and user in session storage
  111. */
  112. if (websGetSession(wp, 1) != 0) {
  113. websSetSessionVar(wp, WEBS_SESSION_USERNAME, wp->username);
  114. }
  115. }
  116. return 1;
  117. }
  118. PUBLIC int websOpenAuth(int minimal)
  119. {
  120. logmsg(2,"-------------open auth --------------------------------------");
  121. char sbuf[64];
  122. assert(minimal == 0 || minimal == 1);
  123. if ((users = hashCreate(-1)) < 0) {
  124. return -1;
  125. }
  126. if ((roles = hashCreate(-1)) < 0) {
  127. return -1;
  128. }
  129. // char aa = ""
  130. if (!minimal) {
  131. fmt(sbuf, sizeof(sbuf), "%x:%x", rand(), time(0));
  132. logmsg(2,"-------------open auth 2222222--------------------------------------%s", sbuf);
  133. masterSecret = websMD5(sbuf);
  134. #if ME_GOAHEAD_JAVASCRIPT && FUTURE
  135. websJsDefine("can", jsCan);
  136. #endif
  137. websDefineAction("login", loginServiceProc);
  138. websDefineAction("logout", logoutServiceProc);
  139. }
  140. if (smatch(ME_GOAHEAD_AUTH_STORE, "file")) {
  141. logmsg(2,"-------------come in there222 !!");
  142. verifyPassword = websVerifyPasswordFromFile;
  143. #if ME_COMPILER_HAS_PAM
  144. } else if (smatch(ME_GOAHEAD_AUTH_STORE, "pam")) {
  145. logmsg(2,"-------------come in11 there !!");
  146. verifyPassword = websVerifyPasswordFromPam;
  147. #endif
  148. }
  149. return 0;
  150. }
  151. PUBLIC void websCloseAuth(void)
  152. {
  153. WebsKey *key, *next;
  154. wfree(masterSecret);
  155. if (users >= 0) {
  156. for (key = hashFirst(users); key; key = next) {
  157. next = hashNext(users, key);
  158. freeUser(key->content.value.symbol);
  159. }
  160. hashFree(users);
  161. users = -1;
  162. }
  163. if (roles >= 0) {
  164. for (key = hashFirst(roles); key; key = next) {
  165. next = hashNext(roles, key);
  166. freeRole(key->content.value.symbol);
  167. }
  168. hashFree(roles);
  169. roles = -1;
  170. }
  171. }
  172. #if KEEP
  173. PUBLIC int websWriteAuthFile(char *path)
  174. {
  175. FILE *fp;
  176. WebsKey *kp, *ap;
  177. WebsRole *role;
  178. WebsUser *user;
  179. char *tempFile;
  180. assert(path && *path);
  181. tempFile = websTempFile(NULL, NULL);
  182. if ((fp = fopen(tempFile, "w" FILE_TEXT)) == 0) {
  183. error("Cannot open %s", tempFile);
  184. wfree(tempFile);
  185. return -1;
  186. }
  187. fprintf(fp, "#\n# %s - Authorization data\n#\n\n", basename(path));
  188. if (roles >= 0) {
  189. for (kp = hashFirst(roles); kp; kp = hashNext(roles, kp)) {
  190. role = kp->content.value.symbol;
  191. fprintf(fp, "role name=%s abilities=", kp->name.value.string);
  192. for (ap = hashFirst(role->abilities); ap; ap = hashNext(role->abilities, ap)) {
  193. fprintf(fp, "%s,", ap->name.value.string);
  194. }
  195. fputc('\n', fp);
  196. }
  197. fputc('\n', fp);
  198. }
  199. if (users >= 0) {
  200. for (kp = hashFirst(users); kp; kp = hashNext(users, kp)) {
  201. user = kp->content.value.symbol;
  202. fprintf(fp, "user name=%s password=%s roles=%s", user->name, user->password, user->roles);
  203. fputc('\n', fp);
  204. }
  205. }
  206. fclose(fp);
  207. unlink(path);
  208. if (rename(tempFile, path) < 0) {
  209. error("Cannot create new %s", path);
  210. wfree(tempFile);
  211. return -1;
  212. }
  213. wfree(tempFile);
  214. return 0;
  215. }
  216. #endif
  217. static WebsUser *createUser(cchar *username, cchar *password, cchar *roles)
  218. {
  219. WebsUser *user;
  220. assert(username);
  221. if ((user = walloc(sizeof(WebsUser))) == 0) {
  222. return 0;
  223. }
  224. user->name = sclone(username);
  225. user->roles = sclone(roles);
  226. user->password = sclone(password);
  227. user->abilities = -1;
  228. return user;
  229. }
  230. WebsUser *websAddUser(cchar *username, cchar *password, cchar *roles)
  231. {
  232. WebsUser *user;
  233. if (!username) {
  234. error("User is missing name");
  235. return 0;
  236. }
  237. if (websLookupUser(username)) {
  238. error("User %s already exists", username);
  239. /* Already exists */
  240. return 0;
  241. }
  242. if ((user = createUser(username, password, roles)) == 0) {
  243. return 0;
  244. }
  245. if (hashEnter(users, username, valueSymbol(user), 0) == 0) {
  246. return 0;
  247. }
  248. return user;
  249. }
  250. PUBLIC int websRemoveUser(cchar *username)
  251. {
  252. WebsKey *key;
  253. assert(username);
  254. if ((key = hashLookup(users, username)) != 0) {
  255. freeUser(key->content.value.symbol);
  256. }
  257. return hashDelete(users, username);
  258. }
  259. static void freeUser(WebsUser *up)
  260. {
  261. assert(up);
  262. hashFree(up->abilities);
  263. wfree(up->name);
  264. wfree(up->password);
  265. wfree(up->roles);
  266. wfree(up);
  267. }
  268. PUBLIC int websSetUserPassword(cchar *username, cchar *password)
  269. {
  270. WebsUser *user;
  271. assert(username);
  272. if ((user = websLookupUser(username)) == 0) {
  273. return -1;
  274. }
  275. wfree(user->password);
  276. user->password = sclone(password);
  277. return 0;
  278. }
  279. PUBLIC int websSetUserRoles(cchar *username, cchar *roles)
  280. {
  281. WebsUser *user;
  282. assert(username);
  283. if ((user = websLookupUser(username)) == 0) {
  284. return -1;
  285. }
  286. wfree(user->roles);
  287. user->roles = sclone(roles);
  288. computeUserAbilities(user);
  289. return 0;
  290. }
  291. WebsUser *websLookupUser(cchar *username)
  292. {
  293. WebsKey *key;
  294. assert(username);
  295. if ((key = hashLookup(users, username)) == 0) {
  296. return 0;
  297. }
  298. return (WebsUser*) key->content.value.symbol;
  299. }
  300. static void computeAbilities(WebsHash abilities, cchar *role, int depth)
  301. {
  302. WebsRole *rp;
  303. WebsKey *key;
  304. assert(abilities >= 0);
  305. assert(role && *role);
  306. assert(depth >= 0);
  307. if (depth > 20) {
  308. error("Recursive ability definition for %s", role);
  309. return;
  310. }
  311. if (roles >= 0) {
  312. if ((key = hashLookup(roles, role)) != 0) {
  313. rp = (WebsRole*) key->content.value.symbol;
  314. for (key = hashFirst(rp->abilities); key; key = hashNext(rp->abilities, key)) {
  315. computeAbilities(abilities, key->name.value.string, ++depth);
  316. }
  317. } else {
  318. hashEnter(abilities, role, valueInteger(0), 0);
  319. }
  320. }
  321. }
  322. static void computeUserAbilities(WebsUser *user)
  323. {
  324. char *ability, *roles, *tok;
  325. assert(user);
  326. if ((user->abilities = hashCreate(-1)) == 0) {
  327. return;
  328. }
  329. roles = sclone(user->roles);
  330. for (ability = stok(roles, " \t,", &tok); ability; ability = stok(NULL, " \t,", &tok)) {
  331. computeAbilities(user->abilities, ability, 0);
  332. }
  333. #if ME_DEBUG
  334. {
  335. WebsKey *key;
  336. trace(5 | WEBS_RAW_MSG, "User \"%s\" has abilities: ", user->name);
  337. for (key = hashFirst(user->abilities); key; key = hashNext(user->abilities, key)) {
  338. trace(5 | WEBS_RAW_MSG, "%s ", key->name.value.string);
  339. ability = key->name.value.string;
  340. }
  341. trace(5, "");
  342. }
  343. #endif
  344. wfree(roles);
  345. }
  346. PUBLIC void websComputeAllUserAbilities(void)
  347. {
  348. WebsUser *user;
  349. WebsKey *sym;
  350. if (users) {
  351. for (sym = hashFirst(users); sym; sym = hashNext(users, sym)) {
  352. user = (WebsUser*) sym->content.value.symbol;
  353. computeUserAbilities(user);
  354. }
  355. }
  356. }
  357. WebsRole *websAddRole(cchar *name, WebsHash abilities)
  358. {
  359. WebsRole *rp;
  360. if (!name) {
  361. error("Role is missing name");
  362. return 0;
  363. }
  364. if (hashLookup(roles, name)) {
  365. error("Role %s already exists", name);
  366. /* Already exists */
  367. return 0;
  368. }
  369. if ((rp = walloc(sizeof(WebsRole))) == 0) {
  370. return 0;
  371. }
  372. rp->abilities = abilities;
  373. if (hashEnter(roles, name, valueSymbol(rp), 0) == 0) {
  374. return 0;
  375. }
  376. return rp;
  377. }
  378. static void freeRole(WebsRole *rp)
  379. {
  380. assert(rp);
  381. hashFree(rp->abilities);
  382. wfree(rp);
  383. }
  384. /*
  385. Does not recompute abilities for users that use this role
  386. */
  387. PUBLIC int websRemoveRole(cchar *name)
  388. {
  389. WebsRole *rp;
  390. WebsKey *sym;
  391. assert(name && *name);
  392. if (roles) {
  393. if ((sym = hashLookup(roles, name)) == 0) {
  394. return -1;
  395. }
  396. rp = sym->content.value.symbol;
  397. hashFree(rp->abilities);
  398. wfree(rp);
  399. return hashDelete(roles, name);
  400. }
  401. return -1;
  402. }
  403. PUBLIC WebsHash websGetUsers(void)
  404. {
  405. return users;
  406. }
  407. PUBLIC WebsHash websGetRoles(void)
  408. {
  409. return roles;
  410. }
  411. PUBLIC bool websLoginUser(Webs *wp, cchar *username, cchar *password)
  412. {
  413. assert(wp);
  414. assert(wp->route);
  415. assert(username);
  416. assert(password);
  417. logmsg(2, "%s-----------username auth0- ---------", username);
  418. logmsg(2, "%s-----------password auth0- ---------", password);
  419. logmsg(2, "%s-----------begin auth1 ---------", wp->route);
  420. if (!wp->route || !wp->route->verify) {
  421. return 0;
  422. }
  423. wfree(wp->username);
  424. wp->username = sclone(username);
  425. wfree(wp->password);
  426. wp->password = sclone(password);
  427. if (!(wp->route->verify)(wp)) {
  428. trace(2, "Password does not match");
  429. return 0;
  430. }
  431. trace(2, "Login successful for %s", username);
  432. websCreateSession(wp);
  433. websSetSessionVar(wp, WEBS_SESSION_USERNAME, wp->username);
  434. return 1;
  435. }
  436. /*update by lusa fix redirect to return json*/
  437. PUBLIC bool websLogoutUser(Webs *wp)
  438. {
  439. assert(wp);
  440. websRemoveSessionVar(wp, WEBS_SESSION_USERNAME);
  441. websDestroySession(wp);
  442. if (smatch(wp->authType, "basic") || smatch(wp->authType, "digest")) {
  443. // websError(wp, HTTP_CODE_UNAUTHORIZED, "Logged out.");
  444. errorResult(wp);
  445. return 0;
  446. }
  447. // websRedirectByStatus(wp, HTTP_CODE_OK);
  448. successResult(wp);
  449. return 1;
  450. }
  451. /*
  452. Internal login service routine for Form-based auth
  453. update by lusa 20191230
  454. */
  455. static void loginServiceProc(Webs *wp)
  456. {
  457. WebsRoute *route;
  458. char *username, *password;
  459. username = websGetVar(wp, "username", NULL);
  460. password = websGetVar(wp, "password", NULL);
  461. logmsg(1, "--------------login come in --------------%s", username);
  462. logmsg(1, "--------------login come in --------------%s", password);
  463. // HelloWord(wp); //test
  464. assert(wp);
  465. route = wp->route;
  466. assert(route);
  467. if (websLoginUser(wp, username, password)) {
  468. /* If the application defines a referrer session var, redirect to that */
  469. //update start
  470. /* cchar *referrer;
  471. if ((referrer = websGetSessionVar(wp, "referrer", 0)) != 0) {
  472. websRedirect(wp, referrer);
  473. } else {
  474. websRedirectByStatus(wp, HTTP_CODE_OK);
  475. }*/
  476. successResult(wp);
  477. //end
  478. websSetSessionVar(wp, "loginStatus", "ok");
  479. websSetSessionVar(wp, "lang", "en");
  480. } else {
  481. if (route->askLogin) {
  482. (route->askLogin)(wp);
  483. }
  484. websSetSessionVar(wp, "loginStatus", "failed");
  485. errorResult(wp);
  486. // websRedirectByStatus(wp, HTTP_CODE_UNAUTHORIZED);
  487. }
  488. }
  489. static void logoutServiceProc(Webs *wp)
  490. {
  491. websLogoutUser(wp);
  492. }
  493. static void basicLogin(Webs *wp)
  494. {
  495. assert(wp);
  496. assert(wp->route);
  497. wfree(wp->authResponse);
  498. wp->authResponse = sfmt("Basic realm=\"%s\"", ME_GOAHEAD_REALM);
  499. }
  500. void websSetPasswordStoreVerify(WebsVerify verify)
  501. {
  502. verifyPassword = verify;
  503. }
  504. WebsVerify websGetPasswordStoreVerify(void)
  505. {
  506. return verifyPassword;
  507. }
  508. PUBLIC bool websVerifyPasswordFromFile(Webs *wp)
  509. {
  510. char passbuf[ME_GOAHEAD_LIMIT_PASSWORD * 3 + 3];
  511. bool success;
  512. char *username, *password;
  513. username = websGetVar(wp, "username", NULL);
  514. password = websGetVar(wp, "password", NULL);
  515. assert(wp);
  516. if (!wp->user && (wp->user = websLookupUser(wp->username)) == 0) {
  517. trace(5, "verifyUser: Unknown user \"%s\"", wp->username);
  518. return 0;
  519. }
  520. /*
  521. Verify the password. If using Digest auth, we compare the digest of the password.
  522. Otherwise we encode the plain-text password and compare that
  523. */
  524. if (!wp->encoded) {
  525. fmt(passbuf, sizeof(passbuf), "%s:%s:%s", wp->username, ME_GOAHEAD_REALM, wp->password);
  526. wfree(wp->password);
  527. // logmsg(2, "***************start to auth pwd222***************%s" , websMD5(passbuf));
  528. wp->password = websMD5(passbuf);
  529. wp->encoded = 1;
  530. }
  531. if (wp->digest) {
  532. success = smatch(wp->password, wp->digest);
  533. } else {
  534. success = smatch(wp->password, wp->user->password);
  535. }
  536. if (success) {
  537. trace(5, "User \"%s\" authenticated", wp->username);
  538. } else {
  539. trace(5, "Password for user \"%s\" failed to authenticate", wp->username);
  540. }
  541. return success;
  542. }
  543. #if ME_COMPILER_HAS_PAM
  544. /*
  545. Copy this routine if creating your own custom password store back end
  546. */
  547. PUBLIC bool websVerifyPasswordFromPam(Webs *wp)
  548. {
  549. WebsBuf abilities;
  550. pam_handle_t *pamh;
  551. UserInfo info;
  552. struct pam_conv conv = { pamChat, &info };
  553. struct group *gp;
  554. int res, i;
  555. assert(wp);
  556. assert(wp->username && wp->username);
  557. assert(wp->password);
  558. assert(!wp->encoded);
  559. info.name = (char*) wp->username;
  560. info.password = (char*) wp->password;
  561. pamh = NULL;
  562. if ((res = pam_start("login", info.name, &conv, &pamh)) != PAM_SUCCESS) {
  563. return 0;
  564. }
  565. if ((res = pam_authenticate(pamh, PAM_DISALLOW_NULL_AUTHTOK)) != PAM_SUCCESS) {
  566. pam_end(pamh, PAM_SUCCESS);
  567. trace(5, "httpPamVerifyUser failed to verify %s", wp->username);
  568. return 0;
  569. }
  570. pam_end(pamh, PAM_SUCCESS);
  571. trace(5, "httpPamVerifyUser verified %s", wp->username);
  572. if (!wp->user) {
  573. wp->user = websLookupUser(wp->username);
  574. }
  575. if (!wp->user) {
  576. Gid groups[32];
  577. int ngroups;
  578. /*
  579. Create a temporary user with a abilities set to the groups
  580. */
  581. ngroups = sizeof(groups) / sizeof(Gid);
  582. if ((i = getgrouplist(wp->username, 99999, groups, &ngroups)) >= 0) {
  583. bufCreate(&abilities, 128, -1);
  584. for (i = 0; i < ngroups; i++) {
  585. if ((gp = getgrgid(groups[i])) != 0) {
  586. bufPutStr(&abilities, gp->gr_name);
  587. bufPutc(&abilities, ' ');
  588. }
  589. }
  590. bufAddNull(&abilities);
  591. trace(5, "Create temp user \"%s\" with abilities: %s", wp->username, abilities.servp);
  592. if ((wp->user = websAddUser(wp->username, 0, abilities.servp)) == 0) {
  593. return 0;
  594. }
  595. computeUserAbilities(wp->user);
  596. }
  597. }
  598. return 1;
  599. }
  600. /*
  601. Callback invoked by the pam_authenticate function
  602. */
  603. static int pamChat(int msgCount, const struct pam_message **msg, struct pam_response **resp, void *data)
  604. {
  605. UserInfo *info;
  606. struct pam_response *reply;
  607. int i;
  608. i = 0;
  609. reply = 0;
  610. info = (UserInfo*) data;
  611. if (resp == 0 || msg == 0 || info == 0) {
  612. return PAM_CONV_ERR;
  613. }
  614. if ((reply = calloc(msgCount, sizeof(struct pam_response))) == 0) {
  615. return PAM_CONV_ERR;
  616. }
  617. for (i = 0; i < msgCount; i++) {
  618. reply[i].resp_retcode = 0;
  619. reply[i].resp = 0;
  620. switch (msg[i]->msg_style) {
  621. case PAM_PROMPT_ECHO_ON:
  622. reply[i].resp = sclone(info->name);
  623. break;
  624. case PAM_PROMPT_ECHO_OFF:
  625. /* Retrieve the user password and pass onto pam */
  626. reply[i].resp = sclone(info->password);
  627. break;
  628. default:
  629. free(reply);
  630. return PAM_CONV_ERR;
  631. }
  632. }
  633. *resp = reply;
  634. return PAM_SUCCESS;
  635. }
  636. #endif /* ME_COMPILER_HAS_PAM */
  637. #if ME_GOAHEAD_JAVASCRIPT && FUTURE
  638. static int jsCan(int jsid, Webs *wp, int argc, char **argv)
  639. {
  640. assert(jsid >= 0);
  641. assert(wp);
  642. if (websCanString(wp, argv[0])) {
  643. // FUTURE
  644. return 0;
  645. }
  646. return 1;
  647. }
  648. #endif
  649. static bool parseBasicDetails(Webs *wp)
  650. {
  651. char *cp, *userAuth;
  652. assert(wp);
  653. /*
  654. Split userAuth into userid and password
  655. */
  656. cp = 0;
  657. if ((userAuth = websDecode64(wp->authDetails)) != 0) {
  658. if ((cp = strchr(userAuth, ':')) != NULL) {
  659. *cp++ = '\0';
  660. }
  661. }
  662. wfree(wp->username);
  663. wfree(wp->password);
  664. if (cp) {
  665. wp->username = sclone(userAuth);
  666. wp->password = sclone(cp);
  667. wp->encoded = 0;
  668. } else {
  669. wp->username = sclone("");
  670. wp->password = sclone("");
  671. }
  672. wfree(userAuth);
  673. return 1;
  674. }
  675. #if ME_GOAHEAD_DIGEST
  676. static void digestLogin(Webs *wp)
  677. {
  678. char *nonce, *opaque;
  679. assert(wp);
  680. assert(wp->route);
  681. nonce = createDigestNonce(wp);
  682. /* Opaque is unused. Set to anything */
  683. opaque = "5ccc069c403ebaf9f0171e9517f40e41";
  684. wfree(wp->authResponse);
  685. wp->authResponse = sfmt(
  686. "Digest realm=\"%s\", domain=\"%s\", qop=\"%s\", nonce=\"%s\", opaque=\"%s\", algorithm=\"%s\", stale=\"%s\"",
  687. ME_GOAHEAD_REALM, websGetServerUrl(), "auth", nonce, opaque, "MD5", "FALSE");
  688. wfree(nonce);
  689. }
  690. static bool parseDigestDetails(Webs *wp)
  691. {
  692. WebsTime when;
  693. char *decoded, *value, *tok, *key, *keyBuf, *dp, *sp, *secret, *realm;
  694. int seenComma;
  695. assert(wp);
  696. key = keyBuf = sclone(wp->authDetails);
  697. while (*key) {
  698. while (*key && isspace((uchar) *key)) {
  699. key++;
  700. }
  701. tok = key;
  702. while (*tok && !isspace((uchar) *tok) && *tok != ',' && *tok != '=') {
  703. tok++;
  704. }
  705. if (*tok) {
  706. *tok++ = '\0';
  707. }
  708. while (isspace((uchar) *tok)) {
  709. tok++;
  710. }
  711. seenComma = 0;
  712. if (*tok == '\"') {
  713. value = ++tok;
  714. while (*tok != '\"' && *tok != '\0') {
  715. tok++;
  716. }
  717. } else {
  718. value = tok;
  719. while (*tok != ',' && *tok != '\0') {
  720. tok++;
  721. }
  722. seenComma++;
  723. }
  724. if (*tok) {
  725. *tok++ = '\0';
  726. }
  727. /*
  728. Handle back-quoting
  729. */
  730. if (strchr(value, '\\')) {
  731. for (dp = sp = value; *sp; sp++) {
  732. if (*sp == '\\') {
  733. sp++;
  734. }
  735. *dp++ = *sp++;
  736. }
  737. *dp = '\0';
  738. }
  739. /*
  740. user, response, oqaque, uri, realm, nonce, nc, cnonce, qop
  741. */
  742. switch (tolower((uchar) *key)) {
  743. case 'a':
  744. if (scaselesscmp(key, "algorithm") == 0) {
  745. break;
  746. } else if (scaselesscmp(key, "auth-param") == 0) {
  747. break;
  748. }
  749. break;
  750. case 'c':
  751. if (scaselesscmp(key, "cnonce") == 0) {
  752. wfree(wp->cnonce);
  753. wp->cnonce = sclone(value);
  754. }
  755. break;
  756. case 'd':
  757. if (scaselesscmp(key, "domain") == 0) {
  758. break;
  759. }
  760. break;
  761. case 'n':
  762. if (scaselesscmp(key, "nc") == 0) {
  763. wfree(wp->nc);
  764. wp->nc = sclone(value);
  765. } else if (scaselesscmp(key, "nonce") == 0) {
  766. wfree(wp->nonce);
  767. wp->nonce = sclone(value);
  768. }
  769. break;
  770. case 'o':
  771. if (scaselesscmp(key, "opaque") == 0) {
  772. wfree(wp->opaque);
  773. wp->opaque = sclone(value);
  774. }
  775. break;
  776. case 'q':
  777. if (scaselesscmp(key, "qop") == 0) {
  778. wfree(wp->qop);
  779. wp->qop = sclone(value);
  780. }
  781. break;
  782. case 'r':
  783. if (scaselesscmp(key, "realm") == 0) {
  784. wfree(wp->realm);
  785. wp->realm = sclone(value);
  786. } else if (scaselesscmp(key, "response") == 0) {
  787. /* Store the response digest in the password field. This is MD5(user:realm:password) */
  788. wfree(wp->password);
  789. wp->password = sclone(value);
  790. wp->encoded = 1;
  791. }
  792. break;
  793. case 's':
  794. if (scaselesscmp(key, "stale") == 0) {
  795. break;
  796. }
  797. case 'u':
  798. if (scaselesscmp(key, "uri") == 0) {
  799. wfree(wp->digestUri);
  800. wp->digestUri = sclone(value);
  801. } else if (scaselesscmp(key, "username") == 0 || scaselesscmp(key, "user") == 0) {
  802. wfree(wp->username);
  803. wp->username = sclone(value);
  804. }
  805. break;
  806. default:
  807. /* Just ignore keywords we don't understand */
  808. ;
  809. }
  810. key = tok;
  811. if (!seenComma) {
  812. while (*key && *key != ',') {
  813. key++;
  814. }
  815. if (*key) {
  816. key++;
  817. }
  818. }
  819. }
  820. wfree(keyBuf);
  821. if (wp->username == 0 || wp->realm == 0 || wp->nonce == 0 || wp->route == 0 || wp->password == 0) {
  822. return 0;
  823. }
  824. if (wp->qop && (wp->cnonce == 0 || wp->nc == 0)) {
  825. return 0;
  826. }
  827. if (wp->qop == 0) {
  828. wp->qop = sclone("");
  829. }
  830. /*
  831. Validate the nonce value - prevents replay attacks
  832. */
  833. when = 0; secret = 0; realm = 0;
  834. decoded = parseDigestNonce(wp->nonce, &secret, &realm, &when);
  835. if (!smatch(masterSecret, secret)) {
  836. trace(2, "Access denied: Nonce mismatch");
  837. wfree(decoded);
  838. return 0;
  839. } else if (!smatch(realm, ME_GOAHEAD_REALM)) {
  840. trace(2, "Access denied: Realm mismatch");
  841. wfree(decoded);
  842. return 0;
  843. } else if (!smatch(wp->qop, "auth")) {
  844. trace(2, "Access denied: Bad qop");
  845. wfree(decoded);
  846. return 0;
  847. } else if ((when + (5 * 60)) < time(0)) {
  848. trace(2, "Access denied: Nonce is stale");
  849. wfree(decoded);
  850. return 0;
  851. }
  852. if (!wp->user) {
  853. if ((wp->user = websLookupUser(wp->username)) == 0) {
  854. trace(2, "Access denied: user is unknown");
  855. wfree(decoded);
  856. return 0;
  857. }
  858. }
  859. wfree(decoded);
  860. wfree(wp->digest);
  861. wp->digest = calcDigest(wp, 0, wp->user->password);
  862. return 1;
  863. }
  864. /*
  865. Create a nonce value for digest authentication (RFC 2617)
  866. */
  867. static char *createDigestNonce(Webs *wp)
  868. {
  869. static int64 next = 0;
  870. char nonce[256];
  871. assert(wp);
  872. assert(wp->route);
  873. fmt(nonce, sizeof(nonce), "%s:%s:%x:%x", masterSecret, ME_GOAHEAD_REALM, time(0), next++);
  874. return websEncode64(nonce);
  875. }
  876. static char *parseDigestNonce(char *nonce, char **secret, char **realm, WebsTime *when)
  877. {
  878. char *tok, *decoded, *whenStr;
  879. assert(nonce && *nonce);
  880. assert(secret);
  881. assert(realm);
  882. assert(when);
  883. if ((decoded = websDecode64(nonce)) == 0) {
  884. return 0;
  885. }
  886. *secret = stok(decoded, ":", &tok);
  887. *realm = stok(NULL, ":", &tok);
  888. whenStr = stok(NULL, ":", &tok);
  889. *when = hextoi(whenStr);
  890. return decoded;
  891. }
  892. /*
  893. Get a Digest value using the MD5 algorithm -- See RFC 2617 to understand this code.
  894. */
  895. static char *calcDigest(Webs *wp, char *username, char *password)
  896. {
  897. char a1Buf[256], a2Buf[256], digestBuf[256];
  898. char *ha1, *ha2, *method, *result;
  899. assert(wp);
  900. assert(password);
  901. /*
  902. Compute HA1. If username == 0, then the password is already expected to be in the HA1 format
  903. (MD5(username:realm:password).
  904. */
  905. if (username == 0) {
  906. ha1 = sclone(password);
  907. } else {
  908. fmt(a1Buf, sizeof(a1Buf), "%s:%s:%s", username, wp->realm, password);
  909. ha1 = websMD5(a1Buf);
  910. }
  911. /*
  912. HA2
  913. */
  914. method = wp->method;
  915. fmt(a2Buf, sizeof(a2Buf), "%s:%s", method, wp->digestUri);
  916. ha2 = websMD5(a2Buf);
  917. /*
  918. H(HA1:nonce:HA2)
  919. */
  920. if (scmp(wp->qop, "auth") == 0) {
  921. fmt(digestBuf, sizeof(digestBuf), "%s:%s:%s:%s:%s:%s", ha1, wp->nonce, wp->nc, wp->cnonce, wp->qop, ha2);
  922. } else if (scmp(wp->qop, "auth-int") == 0) {
  923. fmt(digestBuf, sizeof(digestBuf), "%s:%s:%s:%s:%s:%s", ha1, wp->nonce, wp->nc, wp->cnonce, wp->qop, ha2);
  924. } else {
  925. fmt(digestBuf, sizeof(digestBuf), "%s:%s:%s", ha1, wp->nonce, ha2);
  926. }
  927. result = websMD5(digestBuf);
  928. wfree(ha1);
  929. wfree(ha2);
  930. return result;
  931. }
  932. #endif /* ME_GOAHEAD_DIGEST */
  933. PUBLIC int websSetRouteAuth(WebsRoute *route, cchar *auth)
  934. {
  935. WebsParseAuth parseAuth;
  936. WebsAskLogin askLogin;
  937. assert(route);
  938. assert(auth && *auth);
  939. askLogin = 0;
  940. parseAuth = 0;
  941. if (smatch(auth, "basic")) {
  942. askLogin = basicLogin;
  943. parseAuth = parseBasicDetails;
  944. #if ME_GOAHEAD_DIGEST
  945. } else if (smatch(auth, "digest")) {
  946. askLogin = digestLogin;
  947. parseAuth = parseDigestDetails;
  948. #endif
  949. } else {
  950. auth = 0;
  951. }
  952. route->authType = sclone(auth);
  953. route->askLogin = askLogin;
  954. route->parseAuth = parseAuth;
  955. return 0;
  956. }
  957. #endif /* ME_GOAHEAD_AUTH */
  958. /*
  959. Copyright (c) Embedthis Software. All Rights Reserved.
  960. This software is distributed under commercial and open source licenses.
  961. You may use the Embedthis GoAhead open source license or you may acquire
  962. a commercial license from Embedthis Software. You agree to be fully bound
  963. by the terms of either license. Consult the LICENSE.md distributed with
  964. this software for full details and other copyrights.
  965. */