home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 1998 May / Pcwk5b98.iso / WEBSERVE / SAMBAR / DATA.1 / login.c < prev    next >
C/C++ Source or Header  |  1997-08-19  |  22KB  |  905 lines

  1. /*
  2. ** LOGIN
  3. **
  4. **      HTTP Wrapper for the Login/Logout/Profile Management Library
  5. **
  6. **        Confidential Property of Tod Sambar
  7. **        (c) Copyright Tod Sambar 1996-1997
  8. **        All rights reserved.
  9. **
  10. **
  11. ** Public Functions:
  12. **
  13. **        login_init
  14. **        user_login
  15. **        user_logout
  16. **        user_profile
  17. **        user_add
  18. **        user_delete
  19. **        user_update
  20. **        user_list
  21. **        user_select
  22. **        ftp_connect
  23. **
  24. **
  25. ** History:
  26. ** Chg#    Date    Description                                                Resp
  27. ** ----    -------    -------------------------------------------------------    ----
  28. **         27JAN96    Created                                                    sambar
  29. **         29MAR97    Added user list and update functions                    sambar
  30. **         30MAR97    Added FTP connection security                            sambar
  31. */
  32.  
  33. #include    <stdio.h>
  34. #include    <stdlib.h>
  35. #include    <memory.h>
  36. #include    <string.h>
  37. #include    <login.h>
  38.  
  39. /*
  40. ** Login RPC Commands
  41. */
  42. typedef struct login__rpcs
  43. {
  44.     SA_CHAR        *name;
  45.     SA_INT        auth;
  46.     SA_VOID        *func;
  47.     SA_CHAR        *descr;
  48. } LOGIN__RPCS;
  49.  
  50. static LOGIN__RPCS        login_rpcs[] =
  51. {
  52.     { "adduser",     SA_AUTHORIZATION_ADMIN,        (SA_VOID *)user_add,
  53.       "Add a user to the system." },
  54.     { "upduser",     SA_AUTHORIZATION_ADMIN,     (SA_VOID *)user_update,
  55.       "Update a user." }, 
  56.     { "deluser",     SA_AUTHORIZATION_ADMIN,     (SA_VOID *)user_delete,
  57.       "Delete a user from the system." },
  58.     { "userprop",     SA_AUTHORIZATION_ADMIN,     (SA_VOID *)user_prop,
  59.       "Get a single user attribute for display." },
  60.     { "listusers",     SA_AUTHORIZATION_ADMIN,     (SA_VOID *)user_list,
  61.       "List users for update/delete." },
  62.     { "selectuser",    SA_AUTHORIZATION_ADMIN,     (SA_VOID *)user_select,
  63.       "List users for selection in a pick list." }
  64. };
  65.  
  66. /*
  67. **  LOGIN_INIT
  68. **
  69. **    Initialize the Login/Logout/Profile Interfaces for use by the 
  70. **    Sambar Server plugins.
  71. **
  72. **
  73. **  Parameters:
  74. **    sactx        Sambar Server context
  75. **
  76. **  Returns:
  77. **    SA_SUCCEED | SA_FAIL
  78. */
  79. SA_RETCODE SA_PUBLIC
  80. login_init(sactx)
  81. SA_CTX        *sactx;
  82. {
  83.     int        i;
  84.  
  85.     /* Register the User RPCs with the server                            */
  86.     for (i = 0; i < sizeof(login_rpcs) / sizeof(LOGIN__RPCS); i++)
  87.     {
  88.         if (sa_cmd_init(sactx, login_rpcs[i].name, login_rpcs[i].auth,
  89.             login_rpcs[i].descr, (SA_RPCFUNC)login_rpcs[i].func)
  90.             != SA_SUCCEED)
  91.         {
  92.             sa_log(sactx, "Unable to initialize User Management RPCs");
  93.             return (SA_FAIL);
  94.         } 
  95.     }
  96.  
  97.     sa_log(sactx, "Login/Logout/Profile Management Initialized");
  98.  
  99.     return (SA_SUCCEED);
  100. }
  101.  
  102. /*
  103. **  USER_LOGIN
  104. **
  105. **    Login a user.  Verify the username/password against the Sambar Server
  106. **    password interfaces.  Store some profile information for use.
  107. **
  108. **  Parameters:
  109. **    sactx            Sambar Server Application context to release.
  110. **    saconn            Sambar Server User Connection handle.
  111. **    username        Name of the user logging in.
  112. **    usernamelen        Length of the user name
  113. **    password        Password of the user logging in.
  114. **    passwordlen        Length of the password.
  115. **    infop            Error return code
  116. **
  117. **  Return Values:
  118. **    SA_SUCCESS | SA_FAIL
  119. */
  120. SA_RETCODE SA_PUBLIC
  121. user_login(sactx, saconn, username, usernamelen, password, passwordlen, infop)
  122. SA_CTX        *sactx;
  123. SA_CONN        *saconn;
  124. SA_CHAR        *username;
  125. SA_INT        usernamelen;
  126. SA_CHAR        *password;
  127. SA_INT        passwordlen;
  128. SA_INT        *infop;
  129. {
  130.     SA_PASSWD            passwd;
  131.     LOGIN_PROFILE        *profile;
  132.     SA_CHAR                buffer[512];
  133.  
  134.     if (sa_passwd_lookup(sactx, username, usernamelen, &passwd) != SA_SUCCEED)
  135.     {
  136.         sprintf(buffer, "Login for user '%s' failed.", username);
  137.         sa_log(sactx, buffer);
  138.         *infop = SA_E_INVALIDLOGIN;
  139.         return (SA_FAIL);
  140.     }
  141.  
  142.     /* Verify the passwords are the same                                */
  143.     if ((passwd.passwordlen != passwordlen) ||
  144.         (strncmp(passwd.password, password, passwordlen) != 0))
  145.     {
  146.         sprintf(buffer, "Login for user '%s' failed - bad password (%s)", 
  147.             username, password);
  148.         sa_log(sactx, buffer);
  149.         *infop = SA_E_INVALIDLOGIN;
  150.         return (SA_FAIL);
  151.     }
  152.  
  153.     /* Allocate and populate a user profile structure                    */
  154.     profile = (LOGIN_PROFILE *)malloc(sizeof(LOGIN_PROFILE));
  155.     if (profile == (LOGIN_PROFILE *)NULL)
  156.     {
  157.         sprintf(buffer, "Login for user '%s' failed - no memory", username);
  158.         sa_log(sactx, buffer);
  159.         *infop = SA_E_INVALIDLOGIN;
  160.         return (SA_FAIL);
  161.     }
  162.  
  163.     if (passwd.namelen > 0)
  164.         memcpy(profile->name, passwd.name, passwd.namelen);
  165.     if (passwd.grouplen > 0)
  166.         memcpy(profile->group, passwd.group, passwd.grouplen);
  167.  
  168.     memcpy(profile->username, username, usernamelen);
  169.  
  170.     profile->name[passwd.namelen] = '\0';
  171.     profile->group[passwd.grouplen] = '\0';
  172.     profile->username[usernamelen] = '\0';
  173.  
  174.     /* Save the user's profile handle with the user connection            */
  175.     if (sa_conn_key(saconn, SA_SET, LOGIN_PROFILE_KEY, (SA_VOID **)profile)
  176.         != SA_SUCCEED)
  177.     {
  178.         (SA_VOID)free(profile);
  179.         sa_log(sactx, "sa_conn_key(SET, LOGIN_PROFILE_KEY) failed!");
  180.         return (SA_FAIL);
  181.     }
  182.  
  183. #ifdef DEBUG
  184.     sprintf(buffer, "User '%s' logged in.", username);
  185.     sa_log(sactx, buffer);
  186. #endif    /* DEBUG */
  187.  
  188.     return (SA_SUCCEED);
  189. }
  190.  
  191. /*
  192. **  USER_LOGOUT
  193. **
  194. **    Log out a user.  Free profile resources.
  195. **
  196. **  Parameters:
  197. **    sactx        Sambar Server Application context to release.
  198. **    saconn        Sambar Server User Connection handle.
  199. **
  200. **  Return Values:
  201. **    SA_SUCCESS | SA_FAIL
  202. */
  203. SA_RETCODE SA_PUBLIC
  204. user_logout(sactx, saconn)
  205. SA_CTX        *sactx;
  206. SA_CONN        *saconn;
  207. {
  208.     LOGIN_PROFILE    *profile;
  209. #ifdef DEBUG
  210.     SA_CHAR            buffer[512];
  211. #endif    /* DEBUG */
  212.  
  213.     /* Get the user's pillar handle from the user context                */
  214.     if (sa_conn_key(saconn, SA_GET, LOGIN_PROFILE_KEY, (SA_VOID **)&profile)
  215.         != SA_SUCCEED)
  216.     {
  217.         /* User never completed login                                    */
  218.         return (SA_SUCCEED);
  219.     }
  220.  
  221. #ifdef DEBUG
  222.     sprintf(buffer, "User '%s' logged out.", profile->username);
  223.     sa_log(sactx, buffer);
  224. #endif    /* DEBUG */
  225.  
  226.     (void)free(profile);
  227.  
  228.     return (SA_SUCCEED);
  229. }
  230.  
  231. /*
  232. **  USER_PROFILE
  233. **
  234. **    Respond to a user profile request.
  235. **
  236. **  Parameters:
  237. **    sactx        Sambar Server Application context to release.
  238. **    saconn        Sambar Server User Connection handle.
  239. **    buffer        Profile attribute being queried.
  240. **    buflen        Length of the profile attribute.
  241. **    data        Buffer for the profile result 
  242. **                Note: A maximum of 512 bytes may be written to the data buffer.
  243. **
  244. **  Return Values:
  245. **    SA_SUCCESS | SA_FAIL
  246. */
  247. SA_RETCODE SA_PUBLIC
  248. user_profile(sactx, saconn, buffer, buflen, data)
  249. SA_CTX        *sactx;
  250. SA_CONN        *saconn;
  251. SA_CHAR        *buffer;
  252. SA_INT        buflen;
  253. SA_CHAR        *data;
  254. {
  255.     LOGIN_PROFILE    *profile;
  256.  
  257.     /* Get the user's profile handle                                     */
  258.     if (sa_conn_key(saconn, SA_GET, LOGIN_PROFILE_KEY, (SA_VOID **)&profile)
  259.         != SA_SUCCEED)
  260.     {
  261.         /* User never completed login                                    */
  262.         return (SA_FAIL);
  263.     }
  264.  
  265.     if (profile == (LOGIN_PROFILE *)NULL)
  266.     {
  267.         sa_log(sactx, "user_profile:  LOGIN_PROFILE_KEY returned NULL!");
  268.         return (SA_FAIL);
  269.     }
  270.  
  271.     /* 
  272.     ** Profile lookup
  273.     */
  274.     if ((buflen == 5) && (strncmp(buffer, "group", 5) == 0))
  275.     {
  276.         strcpy(data, profile->group);
  277.     }
  278.     else if ((buflen == 4) && (strncmp(buffer, "name", 4) == 0))
  279.     {
  280.         strcpy(data, profile->name);
  281.     }
  282.     else
  283.     {
  284.         SA_CHAR        tmp[512];
  285.  
  286.         sprintf(tmp, "Profile attribute '%s' not supported!", buffer);
  287.         sa_log(sactx, tmp);
  288.         return (SA_FAIL);
  289.     }
  290.  
  291.     return (SA_SUCCEED);
  292. }
  293.  
  294. /*
  295. **  USER_ADD
  296. **
  297. **    Add a new user
  298. **
  299. **  Parameters:
  300. **    sactx        Sambar Server context
  301. **    saconn        Sambar Server connection
  302. **    saparams    RPC Parameters
  303. **    infop        Error parameters
  304. **
  305. **  Returns:
  306. **    SA_SUCCEED | SA_FAIL
  307. */
  308. SA_RETCODE SA_PUBLIC
  309. user_add(sactx, saconn, saparams, infop)
  310. SA_CTX        *sactx;
  311. SA_CONN        *saconn;
  312. SA_PARAMS    *saparams;
  313. SA_INT        *infop;
  314. {
  315.     SA_CHAR        *data;
  316.     SA_INT        datalen;
  317.     SA_PASSWD    passwd;
  318.  
  319.     memset(&passwd, 0, sizeof(SA_PASSWD));
  320.  
  321.     if (sa_param(sactx, saparams, "password", &data, &datalen) != SA_SUCCEED)
  322.     {
  323.         *infop = SA_E_INVALIDDATA;
  324.         sa_log(sactx, "user_add(): Expected parameter 'password'!");
  325.         return (SA_FAIL);
  326.     }
  327.     
  328.     passwd.passwordlen = MIN(datalen, SA_MAX_NAME);
  329.     memcpy(passwd.password, data, passwd.passwordlen);
  330.  
  331.     if (sa_param(sactx, saparams, "group", &data, &datalen) != SA_SUCCEED)
  332.     {
  333.         *infop = SA_E_INVALIDDATA;
  334.         sa_log(sactx, "user_add(): Expected parameter 'group'!");
  335.         return (SA_FAIL);
  336.     }
  337.     
  338.     passwd.grouplen = MIN(datalen, SA_MAX_NAME);
  339.     memcpy(passwd.group, data, passwd.grouplen);
  340.  
  341.     if (sa_param(sactx, saparams, "name", &data, &datalen) != SA_SUCCEED)
  342.     {
  343.         *infop = SA_E_INVALIDDATA;
  344.         sa_log(sactx, "user_add(): Expected parameter 'name'!");
  345.         return (SA_FAIL);
  346.     }
  347.     
  348.     passwd.namelen = MIN(datalen, SA_MAX_NAME);
  349.     memcpy(passwd.name, data, passwd.namelen);
  350.  
  351.     if (sa_param(sactx, saparams, "dir", &data, &datalen) != SA_SUCCEED)
  352.     {
  353.         *infop = SA_E_INVALIDDATA;
  354.         sa_log(sactx, "user_add(): Expected parameter 'dir'!");
  355.         return (SA_FAIL);
  356.     }
  357.     
  358.     passwd.dirlen = MIN(datalen, SA_MAX_NAME);
  359.     memcpy(passwd.dir, data, passwd.dirlen);
  360.  
  361.     if (sa_param(sactx, saparams, "ftpprivs", &data, &datalen) != SA_SUCCEED)
  362.     {
  363.         *infop = SA_E_INVALIDDATA;
  364.         sa_log(sactx, "user_add(): Expected parameter 'ftpprivs'!");
  365.         return (SA_FAIL);
  366.     }
  367.  
  368.     passwd.ftpprivs = atol(data);
  369.     
  370.     if (sa_param(sactx, saparams, "nntpprivs", &data, &datalen) != SA_SUCCEED)
  371.     {
  372.         *infop = SA_E_INVALIDDATA;
  373.         sa_log(sactx, "user_add(): Expected parameter 'nttpprivs'!");
  374.         return (SA_FAIL);
  375.     }
  376.     
  377.     passwd.nntpprivs = atol(data);
  378.  
  379.     if (sa_param(sactx, saparams, "username", &data, &datalen) != SA_SUCCEED)
  380.     {
  381.         *infop = SA_E_INVALIDDATA;
  382.         sa_log(sactx, "user_add(): Expected parameter 'username'!");
  383.         return (SA_FAIL);
  384.     }
  385.  
  386.     if ((datalen == 0) || (datalen > SA_MAX_NAME))
  387.     {
  388.         *infop = SA_E_INVALIDDATA;
  389.         sa_log(sactx, "user_add(): 'username' field left NULL!");
  390.         return (SA_FAIL);
  391.     }
  392.  
  393.     if (sa_passwd_add(sactx, data, datalen, &passwd) != SA_SUCCEED)
  394.     {
  395.         *infop = SA_E_ALREADYDEFINED;
  396.         sa_log(sactx, "user_add(): sa_passwd_add() failed!");
  397.         return (SA_FAIL);
  398.     }
  399.  
  400.     return (SA_SUCCEED);
  401. }
  402.  
  403. /*
  404. **  USER_UPDATE
  405. **
  406. **    Update an existing user
  407. **
  408. **  Parameters:
  409. **    sactx        Sambar Server context
  410. **    saconn        Sambar Server connection
  411. **    saparams    RPC Parameters
  412. **    infop        Error parameters
  413. **
  414. **  Returns:
  415. **    SA_SUCCEED | SA_FAIL
  416. */
  417. SA_RETCODE SA_PUBLIC
  418. user_update(sactx, saconn, saparams, infop)
  419. SA_CTX        *sactx;
  420. SA_CONN        *saconn;
  421. SA_PARAMS    *saparams;
  422. SA_INT        *infop;
  423. {
  424.     SA_CHAR        *data;
  425.     SA_INT        datalen;
  426.     SA_PASSWD    passwd;
  427.  
  428.     memset(&passwd, 0, sizeof(SA_PASSWD));
  429.  
  430.     if (sa_param(sactx, saparams, "password", &data, &datalen) != SA_SUCCEED)
  431.     {
  432.         *infop = SA_E_INVALIDDATA;
  433.         sa_log(sactx, "user_update(): Expected parameter 'password'!");
  434.         return (SA_FAIL);
  435.     }
  436.     
  437.     passwd.passwordlen = MIN(datalen, SA_MAX_NAME);
  438.     memcpy(passwd.password, data, passwd.passwordlen);
  439.  
  440.     if (sa_param(sactx, saparams, "group", &data, &datalen) != SA_SUCCEED)
  441.     {
  442.         *infop = SA_E_INVALIDDATA;
  443.         sa_log(sactx, "user_update(): Expected parameter 'group'!");
  444.         return (SA_FAIL);
  445.     }
  446.     
  447.     passwd.grouplen = MIN(datalen, SA_MAX_NAME);
  448.     memcpy(passwd.group, data, passwd.grouplen);
  449.  
  450.     if (sa_param(sactx, saparams, "name", &data, &datalen) != SA_SUCCEED)
  451.     {
  452.         *infop = SA_E_INVALIDDATA;
  453.         sa_log(sactx, "user_update(): Expected parameter 'name'!");
  454.         return (SA_FAIL);
  455.     }
  456.     
  457.     passwd.namelen = MIN(datalen, SA_MAX_NAME);
  458.     memcpy(passwd.name, data, passwd.namelen);
  459.  
  460.     if (sa_param(sactx, saparams, "dir", &data, &datalen) != SA_SUCCEED)
  461.     {
  462.         *infop = SA_E_INVALIDDATA;
  463.         sa_log(sactx, "user_update(): Expected parameter 'dir'!");
  464.         return (SA_FAIL);
  465.     }
  466.     
  467.     passwd.dirlen = MIN(datalen, SA_MAX_NAME);
  468.     memcpy(passwd.dir, data, passwd.dirlen);
  469.  
  470.     if (sa_param(sactx, saparams, "ftpprivs", &data, &datalen) != SA_SUCCEED)
  471.     {
  472.         *infop = SA_E_INVALIDDATA;
  473.         sa_log(sactx, "user_update(): Expected parameter 'ftpprivs'!");
  474.         return (SA_FAIL);
  475.     }
  476.     
  477.     passwd.ftpprivs = atol(data);
  478.  
  479.     if (sa_param(sactx, saparams, "nntpprivs", &data, &datalen) != SA_SUCCEED)
  480.     {
  481.         *infop = SA_E_INVALIDDATA;
  482.         sa_log(sactx, "user_update(): Expected parameter 'nntpprivs'!");
  483.         return (SA_FAIL);
  484.     }
  485.     
  486.     passwd.nntpprivs = atol(data);
  487.  
  488.     if (sa_param(sactx, saparams, "username", &data, &datalen) != SA_SUCCEED)
  489.     {
  490.         *infop = SA_E_INVALIDDATA;
  491.         sa_log(sactx, "user_update(): Expected parameter 'username'!");
  492.         return (SA_FAIL);
  493.     }
  494.  
  495.     if ((datalen == 0) || (datalen > SA_MAX_NAME))
  496.     {
  497.         *infop = SA_E_INVALIDDATA;
  498.         sa_log(sactx, "user_update(): 'username' field left NULL!");
  499.         return (SA_FAIL);
  500.     }
  501.  
  502.     if (sa_passwd_update(sactx, data, datalen, &passwd) != SA_SUCCEED)
  503.     {
  504.         *infop = SA_E_ALREADYDEFINED;
  505.         sa_log(sactx, "user_update(): sa_passwd_update() failed!");
  506.         return (SA_FAIL);
  507.     }
  508.  
  509.     return (SA_SUCCEED);
  510. }
  511.  
  512. /*
  513. **  USER_DELETE
  514. **
  515. **    Delete an existing user
  516. **
  517. **  Parameters:
  518. **    sactx        Sambar Server context
  519. **    saconn        Sambar Server connection
  520. **    saparams    RPC Parameters
  521. **    infop        Error parameters
  522. **
  523. **  Returns:
  524. **    SA_SUCCEED | SA_FAIL
  525. */
  526. SA_RETCODE SA_PUBLIC
  527. user_delete(sactx, saconn, saparams, infop)
  528. SA_CTX        *sactx;
  529. SA_CONN        *saconn;
  530. SA_PARAMS    *saparams;
  531. SA_INT        *infop;
  532. {
  533.     SA_INT        datalen;
  534.     SA_CHAR        *data;
  535.  
  536.     if (sa_param(sactx, saparams, "username", &data, &datalen) != SA_SUCCEED)
  537.     {
  538.         *infop = SA_E_INVALIDDATA;
  539.         sa_log(sactx, "user_delete(): Expected parameter 'username'!");
  540.         return (SA_FAIL);
  541.     }
  542.  
  543.     if ((datalen == 0) || (datalen > SA_MAX_NAME))
  544.     {
  545.         *infop = SA_E_INVALIDDATA;
  546.         sa_log(sactx, "user_delete(): 'username' field left NULL!");
  547.         return (SA_FAIL);
  548.     }
  549.  
  550.     (SA_VOID)sa_passwd_delete(sactx, data, datalen);
  551.  
  552.     return (SA_SUCCEED);
  553. }
  554.  
  555. /*
  556. **  USER_SELECT
  557. **
  558. **    List all users for selection.
  559. **
  560. **  Parameters:
  561. **    sactx        Sambar Server context
  562. **    saconn        Sambar Server connection
  563. **    saparams    RPC Parameters
  564. **    infop        Error parameters
  565. **
  566. **  Returns:
  567. **    SA_SUCCEED | SA_FAIL
  568. **
  569. **  Notes:
  570. **    This list is truncated at 100 names.
  571. */
  572. SA_RETCODE SA_PUBLIC
  573. user_select(sactx, saconn, saparams, infop)
  574. SA_CTX        *sactx;
  575. SA_CONN        *saconn;
  576. SA_PARAMS    *saparams;
  577. SA_INT        *infop;
  578. {
  579.     SA_INT            i;
  580.     SA_BOOL            selected;
  581.     SA_INT            usernamelen;
  582.     SA_INT            numusers;
  583.     SA_CHAR            *username;
  584.     SA_PASSWDUSER    users[100];
  585.     SA_CHAR            buffer[256];
  586.  
  587.     /* If a username is provided make it SELECTED in the OPTION list    */
  588.     (SA_VOID)sa_param(sactx, saparams, "username", &username, &usernamelen);
  589.  
  590.     if (sa_passwd_list(sactx, users, 100, &numusers) != SA_SUCCEED)
  591.     {
  592.         sa_log(sactx, "user_select(): failure retrieving names list!");
  593.         return (SA_FAIL);
  594.     }
  595.  
  596.     /* We could sort the names alphabetically at this point...            */
  597.  
  598.     /* Display the list of users.                                        */
  599.     for (i = 0; i < numusers; i++)
  600.     {
  601.         selected = 0;
  602.         if ((users[i].usernamelen == usernamelen) &&
  603.             (memcmp(username, users[i].username, usernamelen) == 0))
  604.         {
  605.             selected = 1;
  606.         }
  607.  
  608.         sprintf(buffer, "<OPTION %s>%s\n", (selected ? "SELECTED" : ""),
  609.                 users[i].username);
  610.  
  611.         if (sa_conn_send(saconn, buffer, SA_NULLTERM) != SA_SUCCEED)
  612.             return (SA_FAIL);
  613.     }
  614.  
  615.     return (SA_SUCCEED);
  616. }
  617.  
  618. /*
  619. **  USER_LIST
  620. **
  621. **    List all users for delete or update.
  622. **
  623. **  Parameters:
  624. **    sactx        Sambar Server context
  625. **    saconn        Sambar Server connection
  626. **    saparams    RPC Parameters
  627. **    infop        Error parameters
  628. **
  629. **  Returns:
  630. **    SA_SUCCEED | SA_FAIL
  631. **
  632. **  Notes:
  633. **    This list is truncated at 100 names.
  634. */
  635. SA_RETCODE SA_PUBLIC
  636. user_list(sactx, saconn, saparams, infop)
  637. SA_CTX        *sactx;
  638. SA_CONN        *saconn;
  639. SA_PARAMS    *saparams;
  640. SA_INT        *infop;
  641. {
  642.     SA_INT            i;
  643.     SA_INT            sysadminlen;
  644.     SA_INT            numusers;
  645.     SA_PASSWDUSER    users[100];
  646.     SA_CHAR            sysadmin[SA_MAX_NAME + 1];
  647.     SA_CHAR            buffer[256];
  648.  
  649.     /* Get the sysadmin user (disallow deletes)                            */
  650.     if (sa_ctx_props(sactx, SA_GET, SA_CTXPROP_SYSADMIN, (SA_BYTE *)sysadmin,
  651.         SA_MAX_NAME, &sysadminlen) != SA_SUCCEED)
  652.     {
  653.         sa_log(sactx, "user_list(): failure retrieving system admin user!");
  654.         return (SA_FAIL);
  655.     }
  656.  
  657.     if (sa_passwd_list(sactx, users, 100, &numusers) != SA_SUCCEED)
  658.     {
  659.         sa_log(sactx, "user_list(): failure retrieving names list!");
  660.         return (SA_FAIL);
  661.     }
  662.  
  663.     /* We could sort the list alphabetically at this point.                */
  664.  
  665.     /* Display the list of users.                                        */
  666.     for (i = 0; i < numusers; i++)
  667.     {
  668.         if ((users[i].usernamelen != sysadminlen) ||
  669.             (strncmp(sysadmin, users[i].username, sysadminlen) != 0))
  670.         {
  671.             sprintf(buffer, 
  672.                 "<A HREF=\"/session/deluser?username=%s&RCpage=/sysadmin/userlist.stm\" onClick=\"return confirmDelete()\"><IMG border=0 HEIGHT=20 WIDTH=20 SRC=\"/images/system/trash.gif\"></A>\n",
  673.                 users[i].username);
  674.  
  675.             if (sa_conn_send(saconn, buffer, SA_NULLTERM) != SA_SUCCEED)
  676.                 return (SA_FAIL);
  677.         }
  678.  
  679.         sprintf(buffer, 
  680.             "<A HREF=\"/sysadmin/upduser.stm?RCSusername=%s\" TARGET=body><IMG border=0 HEIGHT=20 WIDTH=20 SRC=\"/images/system/info.gif\"> %s</A><BR>\n",
  681.             users[i].username, users[i].username);
  682.  
  683.         if (sa_conn_send(saconn, buffer, SA_NULLTERM) != SA_SUCCEED)
  684.             return (SA_FAIL);
  685.     }
  686.  
  687.     return (SA_SUCCEED);
  688. }
  689.  
  690. /*
  691. **  USER_PROP
  692. **
  693. **    Lookup and return a single user property.
  694. **
  695. **  Parameters:
  696. **    sactx        Sambar Server context
  697. **    saconn        Sambar Server connection
  698. **    saparams    RPC Parameters
  699. **    infop        Error parameters
  700. **
  701. **  Return Values:
  702. **    SA_SUCCESS | SA_FAIL
  703. */
  704. SA_RETCODE SA_PUBLIC
  705. user_prop(sactx, saconn, saparams, infop)
  706. SA_CTX        *sactx;
  707. SA_CONN        *saconn;
  708. SA_PARAMS    *saparams;
  709. SA_INT        *infop;
  710. {
  711.     SA_PASSWD            passwd;
  712.     SA_INT                userlen;
  713.     SA_CHAR                *user;
  714.     SA_INT                proplen;
  715.     SA_CHAR                *prop;
  716.     SA_CHAR                buffer[256];
  717.  
  718.     /* Get the page to direct to for user update                        */
  719.     if (sa_param(sactx, saparams, "username", &user, &userlen) != SA_SUCCEED)
  720.     {
  721.         *infop = SA_E_INVALIDDATA;
  722.         sa_log(sactx, "user_prop(): Expected parameter 'username'!");
  723.         return (SA_FAIL);
  724.     }
  725.  
  726.     if ((userlen == 0) || (userlen > SA_MAX_NAME))
  727.     {
  728.         *infop = SA_E_INVALIDDATA;
  729.         sa_log(sactx, "user_prop(): 'username' field left NULL!");
  730.         return (SA_FAIL);
  731.     }
  732.  
  733.     /* Get the property being looked up.                                */
  734.     if (sa_param(sactx, saparams, "prop", &prop, &proplen) != SA_SUCCEED)
  735.     {
  736.         *infop = SA_E_INVALIDDATA;
  737.         sa_log(sactx, "user_prop(): Expected parameter 'prop'!");
  738.         return (SA_FAIL);
  739.     }
  740.  
  741.     if ((proplen == 0) || (proplen > SA_MAX_NAME))
  742.     {
  743.         *infop = SA_E_INVALIDDATA;
  744.         sa_log(sactx, "user_prop(): 'prop' field left NULL!");
  745.         return (SA_FAIL);
  746.     }
  747.  
  748.     if (sa_passwd_lookup(sactx, user, userlen, &passwd) != SA_SUCCEED)
  749.     {
  750.         sprintf(buffer, "User lookup for '%s' failed.", user);
  751.         sa_log(sactx, buffer);
  752.         *infop = SA_E_INVALIDDATA;
  753.         return (SA_FAIL);
  754.     }
  755.  
  756.     /* Lookup and return the appropriate property                        */
  757.     if (strcmp(prop, "group") == 0)
  758.     {
  759.         if (sa_conn_send(saconn, passwd.group, passwd.grouplen) != SA_SUCCEED)
  760.             return (SA_FAIL);
  761.     }
  762.     else if (strcmp(prop, "password") == 0)
  763.     {
  764.         if (sa_conn_send(saconn, passwd.password, passwd.passwordlen) 
  765.             != SA_SUCCEED)
  766.         {
  767.             return (SA_FAIL);
  768.         }
  769.     }
  770.     else if (strcmp(prop, "name") == 0)
  771.     {
  772.         if (sa_conn_send(saconn, passwd.name, passwd.namelen) != SA_SUCCEED)
  773.             return (SA_FAIL);
  774.     }
  775.     else if (strcmp(prop, "dir") == 0)
  776.     {
  777.         if (sa_conn_send(saconn, passwd.dir, passwd.dirlen) != SA_SUCCEED)
  778.             return (SA_FAIL);
  779.     }
  780.     else if (strcmp(prop, "ftpprivs") == 0)
  781.     {
  782.         if (sa_param(sactx, saparams, "checked", &prop, &proplen) == SA_SUCCEED)
  783.         {
  784.             int        tmp;
  785.  
  786.             tmp = 0;
  787.             if (proplen > 0)
  788.                 tmp = atoi(prop);
  789.         
  790.             if (tmp == passwd.ftpprivs)
  791.             {
  792.                 if (sa_conn_send(saconn, "CHECKED", SA_NULLTERM) != SA_SUCCEED)
  793.                     return (SA_FAIL);
  794.             }
  795.         }
  796.         else
  797.         {
  798.             sprintf(buffer, "%ld", passwd.ftpprivs);
  799.             if (sa_conn_send(saconn, buffer, SA_NULLTERM) != SA_SUCCEED)
  800.                 return (SA_FAIL);
  801.         }
  802.     }
  803.     else if (strcmp(prop, "nntpprivs") == 0)
  804.     {
  805.         if (sa_param(sactx, saparams, "checked", &prop, &proplen) == SA_SUCCEED)
  806.         {
  807.             int        tmp;
  808.  
  809.             tmp = 0;
  810.             if (proplen > 0)
  811.                 tmp = atoi(prop);
  812.         
  813.             if (tmp == passwd.nntpprivs)
  814.             {
  815.                 if (sa_conn_send(saconn, "CHECKED", SA_NULLTERM) != SA_SUCCEED)
  816.                     return (SA_FAIL);
  817.             }
  818.         }
  819.         else
  820.         {
  821.             sprintf(buffer, "%ld", passwd.nntpprivs);
  822.             if (sa_conn_send(saconn, buffer, SA_NULLTERM) != SA_SUCCEED)
  823.                 return (SA_FAIL);
  824.         }
  825.     }
  826.     else 
  827.     {
  828.         sprintf(buffer, "User lookup for property '%s' failed.", prop);
  829.         sa_log(sactx, buffer);
  830.         *infop = SA_E_INVALIDDATA;
  831.         return (SA_FAIL);
  832.     }
  833.  
  834.     return (SA_SUCCEED);
  835. }
  836.  
  837. /*
  838. **  FTP_CONNECT
  839. **
  840. **    Process an FTP Connect request. Verify the username/password against 
  841. **    the Sambar Server password interfaces and return the root directory
  842. **    and priviledges.
  843. **
  844. **  Parameters:
  845. **    sactx            Sambar Server Application context to release.
  846. **    saconn            Sambar Server User Connection handle.
  847. **    username        Name of the user logging in.
  848. **    usernamelen        Length of the user name
  849. **    password        Password of the user logging in.
  850. **    passwordlen        Length of the password.
  851. **    infop            Error return code
  852. **
  853. **  Return Values:
  854. **    SA_SUCCESS         Login Accepted.
  855. **    SA_FAIL            Login Denied.
  856. */
  857. SA_RETCODE SA_PUBLIC
  858. ftp_connect(sactx, name, namelen, password, passwordlen, ftpresp)
  859. SA_CTX        *sactx;
  860. SA_CHAR        *name;
  861. SA_INT        namelen;
  862. SA_CHAR        *password;
  863. SA_INT        passwordlen;
  864. SA_FTP        *ftpresp;
  865. {
  866.     SA_PASSWD            passwd;
  867.     SA_CHAR                buffer[512];
  868.  
  869.     if (sa_passwd_lookup(sactx, name, namelen, &passwd) != SA_SUCCEED)
  870.     {
  871.         sprintf(buffer, "FTP login for user '%s' failed.", name);
  872.         sa_log(sactx, buffer);
  873.         return (SA_FAIL);
  874.     }
  875.  
  876.     /* Verify the passwords are the same                                */
  877.     if ((passwd.passwordlen != passwordlen) ||
  878.         (strncmp(passwd.password, password, passwordlen) != 0))
  879.     {
  880.         /* Special case for anonymous user - NULL password match OK        */
  881.         if ((namelen != 9) || (strncmp(name, "anonymous", 9) != 0))
  882.         {
  883.             sprintf(buffer, "FTP login for user '%s' failed - bad password (%s)", 
  884.                 name, password);
  885.             sa_log(sactx, buffer);
  886.             return (SA_FAIL);
  887.         }
  888.     }
  889.  
  890.     /* Return the directory and access priviledges                    */
  891.     ftpresp->privs = passwd.ftpprivs;
  892.     ftpresp->dirlen = passwd.dirlen;
  893.     if (ftpresp->dirlen > 0)
  894.         strncpy(ftpresp->dir, passwd.dir, ftpresp->dirlen);
  895.  
  896.     ftpresp->dir[ftpresp->dirlen] = '\0';
  897.  
  898. #ifdef DEBUG
  899.     sprintf(buffer, "FTP User '%s' logged in.", name);
  900.     sa_log(sactx, buffer);
  901. #endif    /* DEBUG */
  902.  
  903.     return (SA_SUCCEED);
  904. }
  905.