home *** CD-ROM | disk | FTP | other *** search
/ Il CD di internet / CD.iso / SOURCE / N / TCPIP / LINUX_PC.TAR / pcnfsd_linux2 / pcnfsd_v2.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-04-06  |  12.0 KB  |  589 lines

  1. /* RE_SID: @(%)/tmp_mnt/vol/dosnfs/shades_SCCS/unix/pcnfsd/v2/src/SCCS/s.pcnfsd_v2.c 1.7 93/01/29 17:54:10 SMI */
  2. /*
  3. **=====================================================================
  4. ** Copyright (c) 1986-1993 by Sun Microsystems, Inc.
  5. **      @(#)pcnfsd_v2.c    1.7     1/29/93
  6. **=====================================================================
  7. *    Code hacked by meister@ftp.com to allow Ultrix4.X auth database to be 
  8. *    understood 10/15/92
  9. */
  10. #include "common.h"
  11. /*
  12. **=====================================================================
  13. **             I N C L U D E   F I L E   S E C T I O N                *
  14. **                                                                    *
  15. ** If your port requires different include files, add a suitable      *
  16. ** #define in the customization section, and make the inclusion or    *
  17. ** exclusion of the files conditional on this.                        *
  18. **=====================================================================
  19. */
  20. #include "pcnfsd.h"
  21.  
  22. #include <stdio.h>
  23. #include <pwd.h>
  24. #include <grp.h>
  25. #include <sys/file.h>
  26. #include <signal.h>
  27. #include <sys/stat.h>
  28. #include <sys/ioctl.h>
  29. #include <netdb.h>
  30. #include <string.h>
  31.  
  32. #ifdef USE_YP
  33. #include <rpcsvc/ypclnt.h>
  34. #endif
  35.  
  36. #ifndef SYSV
  37. #include <sys/wait.h>
  38. #endif
  39.  
  40. #ifdef ISC_2_0
  41. #include <sys/fcntl.h>
  42. #endif
  43.  
  44. #ifdef SHADOW_SUPPORT
  45. #include <shadow.h>
  46. #endif
  47.  
  48. #ifdef ULTRIX_AUTH
  49. # include <auth.h>
  50. #endif
  51.  
  52. /*
  53. **---------------------------------------------------------------------
  54. ** Other #define's 
  55. **---------------------------------------------------------------------
  56. */
  57.  
  58. void            fillin_extra_groups();
  59. extern char    *my_strdup();
  60. extern void     scramble();
  61. extern void    *grab();
  62. extern char    *crypt();
  63. extern int      build_pr_list();
  64. extern void    mon_printers();
  65. extern pirstat  build_pr_queue();
  66. extern psrstat  pr_start();
  67. extern psrstat  pr_start2();
  68. extern pcrstat  pr_cancel();
  69. extern pirstat  get_pr_status();
  70.  
  71. extern struct passwd  *get_password();
  72.  
  73. #ifdef WTMP
  74. extern void wlogin();
  75. #endif
  76.  
  77. #ifdef USE_YP
  78. char *find_entry();
  79. #endif
  80.  
  81. /*
  82. **---------------------------------------------------------------------
  83. **                       Misc. variable definitions
  84. **---------------------------------------------------------------------
  85. */
  86.  
  87. extern pr_list         printers;
  88. extern pr_queue        queue;
  89.  
  90. /*
  91. **=====================================================================
  92. **                      C O D E   S E C T I O N                       *                    **=====================================================================
  93. */
  94.  
  95.  
  96. static char no_comment[] = "No comment";
  97. static char not_supported[] = "Not supported";
  98. static char pcnfsd_version[] = "@(#)pcnfsd_v2.c    1.6 - rpc.pcnfsd V2.0 (c) 1991 Sun Technology Enterprises, Inc.";
  99.  
  100. /*ARGSUSED*/
  101. void *pcnfsd2_null_2(arg)
  102. void*arg;
  103. {
  104. static char dummy;
  105. return((void *)&dummy);
  106. }
  107.  
  108. v2_auth_results *pcnfsd2_auth_2(arg)
  109. v2_auth_args *arg;
  110. {
  111. static v2_auth_results  r;
  112.  
  113. char            uname[32];
  114. char            pw[64];
  115. int             c1, c2;
  116. struct passwd  *p;
  117. static u_int           extra_gids[EXTRAGIDLEN];
  118. static char     home[256];
  119. char           *msgp = NULL;
  120. #ifdef USE_YP
  121. char           *yphome;
  122. char           *cp;
  123. #endif /*USE_YP*/
  124.  
  125. #ifdef ULTRIX_AUTH
  126.     struct authorization *authbuf;
  127.     char   salt [2];
  128. #endif
  129.  
  130.     r.stat = AUTH_RES_FAIL;    /* assume failure */
  131.     r.uid = (int)-2;
  132.     r.gid = (int)-2;
  133.     r.cm = &no_comment[0];
  134.     r.gids.gids_len = 0;
  135.     r.gids.gids_val = &extra_gids[0];
  136.     home[0] = '\0';
  137.     r.home = &home[0];
  138.     r.def_umask = umask(0);
  139.     (void)umask(r.def_umask);    /* or use 022 */
  140.  
  141.     scramble(arg->id, uname);
  142.     scramble(arg->pw, pw);
  143.  
  144. #ifdef USER_CACHE
  145.     if(check_cache(uname, pw, &r.uid, &r.gid)) {
  146.          r.stat = AUTH_RES_OK;
  147. #ifdef WTMP
  148.         wlogin(uname);
  149. #endif
  150.                  fillin_extra_groups
  151.             (uname, r.gid, &r.gids.gids_len, extra_gids);
  152. #ifdef USE_YP
  153.         yphome = find_entry(uname, "auto.home");
  154.         if(yphome) {
  155.             strcpy(home, yphome);
  156.             free(yphome);
  157.             cp = strchr(home, ':');
  158.             cp++;
  159.             cp = strchr(cp, ':');
  160.             if(cp)
  161.                 *cp = '/';
  162.         }
  163. #endif
  164.          return (&r);
  165.    }
  166. #endif
  167.  
  168.     p = get_password(uname, &msgp);
  169.     if(msgp)
  170.         r.cm = msgp;
  171.     if (p == (struct passwd *)NULL)
  172.        return (&r);
  173.  
  174. #ifdef ULTRIX_AUTH
  175.         if (p->pw_passwd [0] == '*')
  176.         {
  177.         authbuf = getauthuid (p->pw_uid);
  178.             if (!authbuf)
  179.             {    return (&r);
  180.             }
  181.  
  182.             salt [0] = authbuf->a_password [0];
  183.             salt [1] = authbuf->a_password [1];
  184.             c1 = strlen(pw);
  185.             c2 = strlen(authbuf->a_password);
  186.             if ((c1 && !c2) || (c2 && !c1))
  187.                 return (&r);
  188.             if (strncmp (crypt16 (pw, salt), authbuf->a_password, 
  189.                 CRYPT_PASSWORD_LENGTH))
  190.             {   return (&r);
  191.             }
  192.         }   
  193.         else
  194.         {
  195. #endif
  196.     c1 = strlen(pw);
  197.     c2 = strlen(p->pw_passwd);
  198.     if ((c1 && !c2) || (c2 && !c1) ||
  199.        (strcmp(p->pw_passwd, crypt(pw, p->pw_passwd)))) 
  200.            {
  201.        return (&r);
  202.        }
  203. #ifdef ULTRIX_AUTH
  204.        }
  205. #endif
  206.  
  207.     r.stat = AUTH_RES_OK;
  208.     r.uid = p->pw_uid;
  209.     r.gid = p->pw_gid;
  210. #ifdef WTMP
  211.     wlogin(uname);
  212. #endif
  213.         fillin_extra_groups(uname, r.gid, &r.gids.gids_len, extra_gids);
  214.  
  215. #ifdef USE_YP
  216.     yphome = find_entry(uname, "auto.home");
  217.     if(yphome) {
  218.         strcpy(home, yphome);
  219.         free(yphome);
  220.         cp = strchr(home, ':');
  221.         cp++;
  222.         cp = strchr(cp, ':');
  223.         if(cp)
  224.             *cp = '/';
  225.     }
  226. #endif
  227.  
  228. #ifdef USER_CACHE
  229.     add_cache_entry(p);
  230. #endif
  231.  
  232. return(&r);
  233.  
  234. }
  235.  
  236. v2_pr_init_results *pcnfsd2_pr_init_2(arg)
  237. v2_pr_init_args *arg;
  238. {
  239. static v2_pr_init_results res;
  240.  
  241.     res.stat = 
  242.      (pirstat) pr_init(arg->system, arg->pn, &res.dir);
  243.     res.cm = &no_comment[0];
  244.  
  245.  
  246. return(&res);
  247. }
  248.  
  249. v2_pr_start_results *pcnfsd2_pr_start_2(arg)
  250. v2_pr_start_args *arg;
  251. {
  252. static v2_pr_start_results res;
  253.  
  254.     res.stat =
  255.       (psrstat) pr_start2(arg->system, arg->pn, arg->user,
  256.       arg ->file, arg->opts, &res.id);
  257.     res.cm = &no_comment[0];
  258.  
  259. return(&res);
  260. }
  261.  
  262. /*ARGSUSED*/
  263. v2_pr_list_results *pcnfsd2_pr_list_2(arg)
  264. void *arg;
  265. {
  266. static v2_pr_list_results res;
  267.  
  268.     mon_printers();
  269.     if(printers == NULL)
  270.         (void)build_pr_list();
  271.     res.cm = &no_comment[0];
  272.     res.printers = printers;
  273.  
  274. return(&res);
  275. }
  276.  
  277. v2_pr_queue_results *pcnfsd2_pr_queue_2(arg)
  278. v2_pr_queue_args *arg;
  279. {
  280. static v2_pr_queue_results res;
  281.  
  282.     res.stat = build_pr_queue(arg->pn, arg->user,
  283.         arg->just_mine, &res.qlen, &res.qshown);
  284.     res.cm = &no_comment[0];
  285.     res.just_yours = arg->just_mine;
  286.     res.jobs = queue;
  287.     
  288.  
  289. return(&res);
  290. }
  291.  
  292. v2_pr_status_results *pcnfsd2_pr_status_2(arg)
  293. v2_pr_status_args *arg;
  294. {
  295. static v2_pr_status_results res;
  296. static char status[128];
  297.  
  298.     res.stat = get_pr_status(arg->pn, &res.avail, &res.printing,
  299.         &res.qlen, &res.needs_operator, &status[0]);
  300.     res.status = &status[0];    
  301.     res.cm = &no_comment[0];
  302.  
  303. return(&res);
  304. }
  305.  
  306. v2_pr_cancel_results *pcnfsd2_pr_cancel_2(arg)
  307. v2_pr_cancel_args *arg;
  308. {
  309. static v2_pr_cancel_results res;
  310.  
  311.     res.stat = pr_cancel(arg->pn, arg->user, arg->id);
  312.     res.cm = &no_comment[0];
  313.  
  314. return(&res);
  315. }
  316.  
  317. /*ARGSUSED*/
  318. v2_pr_requeue_results *pcnfsd2_pr_requeue_2(arg)
  319. v2_pr_requeue_args *arg;
  320. {
  321. static v2_pr_requeue_results res;
  322.     res.stat = PC_RES_FAIL;
  323.     res.cm = ¬_supported[0];
  324.  
  325. return(&res);
  326. }
  327.  
  328. /*ARGSUSED*/
  329. v2_pr_hold_results *pcnfsd2_pr_hold_2(arg)
  330. v2_pr_hold_args *arg;
  331. {
  332. static v2_pr_hold_results res;
  333.  
  334.     res.stat = PC_RES_FAIL;
  335.     res.cm = ¬_supported[0];
  336.  
  337. return(&res);
  338. }
  339.  
  340. /*ARGSUSED*/
  341. v2_pr_release_results *pcnfsd2_pr_release_2(arg)
  342. v2_pr_release_args *arg;
  343. {
  344. static v2_pr_release_results res;
  345.  
  346.     res.stat = PC_RES_FAIL;
  347.     res.cm = ¬_supported[0];
  348.  
  349. return(&res);
  350. }
  351.  
  352. /*ARGSUSED*/
  353. v2_pr_admin_results *pcnfsd2_pr_admin_2(arg)
  354. v2_pr_admin_args *arg;
  355. {
  356. static v2_pr_admin_results res;
  357. /*
  358. ** The default action for admin is to fail.
  359. ** If someone wishes to implement an administration
  360. ** mechanism, and isn't worried about the security
  361. ** holes, go right ahead.
  362. */
  363.  
  364.     res.cm = ¬_supported[0];
  365.     res.stat = PI_RES_FAIL;
  366.  
  367. return(&res);
  368. }
  369.  
  370. void
  371. free_mapreq_results(p)
  372. mapreq_res p;
  373. {
  374.     if(p->mapreq_next)
  375.         free_mapreq_results(p->mapreq_next); /* recurse */
  376.     if(p->name)
  377.         (void)free(p->name);
  378.     (void)free(p);
  379.     return;
  380. }
  381.  
  382.  
  383. v2_mapid_results *pcnfsd2_mapid_2(arg)
  384. v2_mapid_args *arg;
  385. {
  386. static v2_mapid_results res;
  387. struct passwd *p_passwd;
  388. struct group  *p_group;
  389.  
  390. mapreq_arg a;
  391. mapreq_res next_r;
  392. mapreq_res last_r = NULL;
  393.  
  394.  
  395.     if(res.res_list) {
  396.         free_mapreq_results(res.res_list);
  397.         res.res_list = NULL;
  398.     }
  399.  
  400.     a = arg->req_list;
  401.     while(a) {
  402.         next_r = (struct mapreq_res_item *)
  403.             grab(sizeof(struct mapreq_res_item));
  404.         next_r->stat = MAP_RES_UNKNOWN;
  405.         next_r->req = a->req;
  406.         next_r->id = a->id;
  407.         next_r->name = NULL;
  408.         next_r->mapreq_next = NULL;
  409.  
  410.         if(last_r == NULL)
  411.             res.res_list = next_r;
  412.         else
  413.             last_r->mapreq_next = next_r;
  414.         last_r = next_r;
  415.         switch(a->req) {
  416.         case MAP_REQ_UID:
  417.             p_passwd = getpwuid((uid_t)a->id);
  418.             if(p_passwd) {
  419.                 next_r->name = my_strdup(p_passwd->pw_name);
  420.                 next_r->stat = MAP_RES_OK;
  421.             }
  422.             break;
  423.         case MAP_REQ_GID:
  424.             p_group = getgrgid((gid_t)a->id);
  425.             if(p_group) {
  426.                 next_r->name = my_strdup(p_group->gr_name);
  427.                 next_r->stat = MAP_RES_OK;
  428.             }
  429.             break;
  430.         case MAP_REQ_UNAME:
  431.             next_r->name = my_strdup(a->name);
  432.             p_passwd = getpwnam(a->name);
  433.             if(p_passwd) {
  434.                 next_r->id = p_passwd->pw_uid;
  435.                 next_r->stat = MAP_RES_OK;
  436.             }
  437.             break;
  438.         case MAP_REQ_GNAME:
  439.             next_r->name = my_strdup(a->name);
  440.             p_group = getgrnam(a->name);
  441.             if(p_group) {
  442.                 next_r->id = p_group->gr_gid;
  443.                 next_r->stat = MAP_RES_OK;
  444.             }
  445.             break;
  446.         }
  447.         if(next_r->name == NULL)
  448.             next_r->name = my_strdup("");
  449.         a = a->mapreq_next;
  450.     }
  451.  
  452.     res.cm = &no_comment[0];
  453.  
  454. return(&res);
  455. }
  456.  
  457.     
  458. /*ARGSUSED*/
  459. v2_alert_results *pcnfsd2_alert_2(arg)
  460. v2_alert_args *arg;
  461. {
  462. static v2_alert_results res;
  463.  
  464.     res.stat = ALERT_RES_FAIL;
  465.     res.cm = ¬_supported[0];
  466.  
  467. return(&res);
  468. }
  469.  
  470. /*ARGSUSED*/
  471. v2_info_results *pcnfsd2_info_2(arg)
  472. v2_info_args *arg;
  473. {
  474. static v2_info_results res;
  475. static int facilities[FACILITIESMAX];
  476. static int onetime = 1;
  477.  
  478. #define UNSUPPORTED -1
  479. #define QUICK 100
  480. #define SLOW 2000
  481.  
  482.     if(onetime) {
  483.         onetime = 0;
  484.         facilities[PCNFSD2_NULL] = QUICK;
  485.         facilities[PCNFSD2_INFO] = QUICK;
  486.         facilities[PCNFSD2_PR_INIT] = QUICK;
  487.         facilities[PCNFSD2_PR_START] = SLOW;
  488.         facilities[PCNFSD2_PR_LIST] = QUICK; /* except first time */
  489.         facilities[PCNFSD2_PR_QUEUE] = SLOW;
  490.         facilities[PCNFSD2_PR_STATUS] = SLOW;
  491.         facilities[PCNFSD2_PR_CANCEL] = SLOW;
  492.         facilities[PCNFSD2_PR_ADMIN] = UNSUPPORTED;
  493.         facilities[PCNFSD2_PR_REQUEUE] = UNSUPPORTED;
  494.         facilities[PCNFSD2_PR_HOLD] = UNSUPPORTED;
  495.         facilities[PCNFSD2_PR_RELEASE] = UNSUPPORTED;
  496.         facilities[PCNFSD2_MAPID] = QUICK;
  497.         facilities[PCNFSD2_AUTH] = QUICK;
  498.         facilities[PCNFSD2_ALERT] = QUICK;
  499.     }
  500.     res.facilities.facilities_len = PCNFSD2_ALERT+1;
  501.     res.facilities.facilities_val = facilities;
  502.     
  503.     res.vers = &pcnfsd_version[0];
  504.     res.cm = &no_comment[0];
  505.  
  506. return(&res);
  507. }
  508.  
  509.  
  510.  
  511. void
  512. fillin_extra_groups(uname, main_gid, len, extra_gids)
  513. char *uname;
  514. u_int main_gid;
  515. int *len;
  516. u_int extra_gids[EXTRAGIDLEN];
  517. {
  518. struct group *grp;
  519. char **members;
  520. int n = 0;
  521.  
  522.     setgrent();
  523.  
  524.     while(n < EXTRAGIDLEN) {
  525.         grp = getgrent();
  526.         if(grp == NULL)
  527.             break;
  528.         if(grp->gr_gid == main_gid)
  529.             continue;
  530.         for(members = grp->gr_mem; members && *members; members++) {
  531.             if(!strcmp(*members, uname)) {
  532.                 extra_gids[n++] = grp->gr_gid;
  533.                 break;
  534.             }
  535.         }
  536.     }
  537.     endgrent();
  538.     *len = n;
  539. }
  540.  
  541. #ifdef USE_YP
  542. /* the following is from rpcsvc/yp_prot.h */
  543. #define YPMAXDOMAIN 64
  544. /*
  545.  * find_entry returns NULL on any error (printing a message) and
  546.  * otherwise returns a pointer to the malloc'd result. The caller
  547.  * is responsible for free()ing the result string.
  548.  */
  549. char *
  550. find_entry(key, map)
  551. char *key;
  552. char *map;
  553. {
  554.     int err;
  555.     char *val = NULL;
  556.     char *cp;
  557.     int len = 0;
  558.     static char domain[YPMAXDOMAIN+1];
  559.  
  560.     if(getdomainname(domain, YPMAXDOMAIN) ) {
  561. #ifdef DEBUG
  562.         msg_out("rpc.pcnfsd: getdomainname failed");
  563. #endif
  564.         return(NULL);
  565.     }
  566.  
  567.     if (err = yp_bind(domain)) {
  568. #ifdef DEBUG
  569.         msg_out("rpc.pcnfsd: yp_bind failed");
  570. #endif
  571.         return(NULL);
  572.     }
  573.  
  574.     err = yp_match(domain, map, key, strlen(key), &val, &len);
  575.  
  576.     if (err) {
  577. #ifdef DEBUG
  578.         msg_out("rpc.pcnfsd: yp_match failed");
  579. #endif
  580.         return(NULL);
  581.     }
  582.  
  583.     if(cp = strchr(val, '\n'))
  584.             *cp = '\0';        /* in case we get an extra NL at the end */
  585.     return(val);
  586. }
  587.  
  588. #endif
  589.