auth.c 29 KB

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