pidentd  3.0.19
About: implementation of the RFC1413 identification server (more or less complete rewrite compared to version 2)
  Fossies Dox: pidentd-3.0.19.tar.gz  ("inofficial" and yet experimental doxygen-generated source code documentation)  

k_irix5.c
Go to the documentation of this file.
1 /*
2 ** k_irix.c - SGI IRIX -
3 **
4 ** Copyright (c) 1997 Peter Eriksson <pen@lysator.liu.se>
5 **
6 ** This program is free software; you can redistribute it and/or
7 ** modify it as you wish - as long as you don't claim that you wrote
8 ** it.
9 **
10 ** This program is distributed in the hope that it will be useful,
11 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
12 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 */
14 /*
15 ** =========================================================================
16 ** Luc Chouinard, lucc@sgi.com
17 ** =========================================================================
18 */
19 
20 #include "config.h"
21 
22 #ifdef _POSIX_C_SOURCE /* Ugly hack below - pen */
23 
24 #define __EXTENSIONS__ 1
25 #define _SGI_SOURCE 1
26 
27 #include <standards.h>
28 
29 #undef _XOPEN4UX
30 #define _XOPEN4UX 1
31 #undef _SGIAPI
32 #define _SGIAPI 1
33 
34 #endif /* End ugly hack */
35 
36 
37 #include <time.h>
38 #include <netdb.h>
39 #include <paths.h>
40 #include <fcntl.h>
41 #include <bstring.h>
42 #include <mntent.h>
43 #include <sys/select.h>
44 #include <stdlib.h>
45 #include <unistd.h>
46 
47 #include <sys/types.h>
48 #include <sys/utssys.h>
49 #include <sys/var.h>
50 #include <netinet/in.h>
51 #include <sys/cred.h>
52 #include <sys/vfs.h>
53 #include <sys/sysmp.h>
54 #include <sys/param.h>
55 #include <sys/socket.h>
56 #include <sys/procfs.h>
57 
58 #include "pidentd.h"
59 
60 
61 typedef struct {
62  f_user_t *pl, *pr;
63 } kpriv_t;
64 
65 /* initialize and check os version current-vs-compiled
66  This version of the program runs on any machine that will
67  allow it to run */
68 int
69 ka_init(void)
70 {
71  return 0;
72 }
73 
74 /*
75  * allocate enough memory for the maximum number of procs.
76  */
77 ka_open(void **misc)
78 {
79  kpriv_t *kp;
80  off_t vaddr;
81  int kfd;
82 
83  kp = s_malloc(sizeof(kpriv_t));
84  vaddr = (off_t) sysmp(MP_KERNADDR, MPKA_VAR);
85  if ((kfd = open(_PATH_KMEM, O_RDONLY)) >= 0)
86  {
87  if (lseek(kfd, vaddr, SEEK_SET) == vaddr)
88  {
89  struct var v;
90 
91  if (read(kfd, &v, sizeof(v)) == sizeof(v))
92  {
93  kp->pl = s_malloc(v.v_proc * sizeof(f_user_t));
94  kp->pr = s_malloc(v.v_proc * sizeof(f_user_t));
95  *misc = (void *) kp;
96  return 0;
97  }
98  }
99  close(kfd);
100  }
101 }
102 
103 /*
104  * Get a list of process using a addr/port couple
105  */
106 static int
107 getusers(struct sockaddr_in *sin, f_user_t *u)
108 {
109  struct fid fid;
110  f_anonid_t fa;
111  int n;
112 
113  fid.fid_len = sizeof(struct sockaddr_in);
114  sin->sin_family = AF_INET;
115  memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
116  memcpy(fid.fid_data, sin, sizeof(*sin));
117  fid.fid_data[sizeof(*sin)] = 7;
118  fa.fa_fid = &fid;
119  (void) strncpy(fa.fa_fsid, FSID_SOCKET, sizeof fa.fa_fsid);
120  if ((n = utssys(&fa, F_ANONYMOUS, UTS_FUSERS, u)) <= 0)
121  return 0;
122  return n;
123 }
124 
125 /*
126  * set status and return
127  * if possible get cmd and args also through procfs
128  */
129 static int
130 stval(f_user_t *u, struct kernel *kp)
131 {
132  char pname[256];
133  int pfd;
134 
135 #ifdef IRIX4
136  s_snprintf(pname, sizeof(pname), "/debug/%05d", (int)u->fu_pid);
137 #else
138  s_snprintf(pname, sizeof(pname), "/proc/pinfo/%d", (int)u->fu_pid);
139 #endif
140 
141  kp->cmd = NULL;
142  kp->argv = NULL;
143  kp->ruid = kp->euid=u->fu_uid;
144  if ((pfd = s_open(pname, O_RDONLY)) >= 0)
145  {
146  prpsinfo_t info;
147  prcred_t cr;
148 
149  if (ioctl(pfd, PIOCPSINFO, &info) >= 0)
150  {
151  kp->cmd = s_malloc(strlen(info.pr_fname) + 1);
152  strcpy(kp->cmd, info.pr_fname);
153  kp->argv = s_malloc(strlen(info.pr_psargs) + 1);
154  strcpy(kp->argv, info.pr_psargs);
155  }
156  if (ioctl(pfd, PIOCCRED, &cr) >= 0)
157  {
158  kp->euid=cr.pr_euid;
159  kp->ruid=cr.pr_ruid;
160  }
161  close(pfd);
162  }
163  kp->status = 1;
164  kp->pid = u->fu_pid;
165  return 1;
166 }
167 
168 /*
169 ** Return the user number for the connection owner
170 */
171 int
172 ka_lookup(void *vp, struct kernel *kp)
173 {
174  int nr, nl;
175  kpriv_t *pr = (kpriv_t *)vp;
176 
177  kp->status = -1;
178  nl = getusers(&kp->local, pr->pl);
179  nr = getusers(&kp->remote, pr->pr);
180  if (nr != 0 && nl != 0)
181  return stval(&pr->pl[0], kp);
182  else
183  {
184  if (nl != 0)
185  {
186  int i, j;
187 
188  for (i = 0; i < nr; i++)
189  for (j = 0; j < nl; j++)
190  if (pr->pr[i].fu_pid == pr->pl[j].fu_pid)
191  return stval(&pr->pr[i], kp);
192 
193  return stval(&pr->pl[0], kp);
194  }
195  else
196  return stval(&pr->pr[0], kp);
197  }
198  return 0;
199 }
200 
s_snprintf
int s_snprintf(char *buf, size_t bufsize, const char *format,...)
Definition: safeio.c:269
ka_init
int ka_init(void)
Definition: k_irix5.c:69
pidentd.h
kernel::remote
struct sockaddr_gen remote
Definition: kernel.h:29
kernel::local
struct sockaddr_gen local
Definition: kernel.h:28
ka_lookup
int ka_lookup(void *vp, struct kernel *kp)
Definition: k_irix5.c:172
s_malloc
void * s_malloc(size_t size)
Definition: safeio.c:132
info
Definition: pdes.h:18
ka_open
ka_open(void **misc)
Definition: k_irix5.c:77
kernel::euid
uid_t euid
Definition: kernel.h:35
kernel::ruid
uid_t ruid
Definition: kernel.h:36
kernel::pid
pid_t pid
Definition: kernel.h:38
s_open
int s_open(const char *path, int oflag,...)
Definition: safeio.c:44
kernel::cmd
char * cmd
Definition: kernel.h:39
getusers
static int getusers(struct sockaddr_in *sin, f_user_t *u)
Definition: k_irix5.c:107
kernel::argv
char * argv
Definition: kernel.h:40
kpriv_t::pr
f_user_t * pr
Definition: k_irix5.c:62
kernel::status
int status
Definition: kernel.h:34
kpriv_t::pl
f_user_t * pl
Definition: k_irix5.c:62
stval
static int stval(f_user_t *u, struct kernel *kp)
Definition: k_irix5.c:130
kernel
Definition: kernel.h:25
kpriv_t
Definition: k_irix5.c:61