]> git.decadent.org.uk Git - nfs-utils.git/blob - support/nfs/rpcmisc.c
Initial revision
[nfs-utils.git] / support / nfs / rpcmisc.c
1 /*
2  * support/nfs/rpcmisc.c
3  *
4  * Miscellaneous functions for RPC startup and shutdown.
5  * This code is partially snarfed from rpcgen -s tcp -s udp,
6  * partly written by Mark Shand, Donald Becker, and Rick 
7  * Sladkey. It was tweaked slightly by Olaf Kirch to be
8  * usable by both unfsd and mountd.
9  *
10  * This software may be used for any purpose provided
11  * the above copyright notice is retained.  It is supplied
12  * as is, with no warranty expressed or implied.
13  */
14
15 #include "config.h"
16
17 #include <sys/types.h>
18 #include <sys/ioctl.h>
19 #include <sys/stat.h>
20 #include <sys/socket.h>
21 #include <rpc/rpc.h>
22 #include <rpc/pmap_clnt.h>
23 #include <netinet/in.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <signal.h>
28 #include <fcntl.h>
29 #include <memory.h>
30 #include <errno.h>
31 #include <unistd.h>
32 #include "nfslib.h"
33
34 static void     closedown(int sig);
35 static int      makesock(int port, int proto, int socksz);
36
37 #define _RPCSVC_CLOSEDOWN       120
38 int     _rpcpmstart = 0;
39 int     _rpcfdtype = 0;
40 int     _rpcsvcdirty = 0;
41
42 void
43 rpc_init(char *name, int prog, int vers, void (*dispatch)(), int defport,
44                                                         int bufsiz)
45 {
46         struct sockaddr_in saddr;
47         SVCXPRT *transp;
48         int     sock;
49         int     asize;
50
51         asize = sizeof(saddr);
52         sock = 0;
53         _rpcfdtype = 0;
54         if (getsockname(0, (struct sockaddr *) &saddr, &asize) == 0) {
55                 int ssize = sizeof (int);
56                 if (saddr.sin_family != AF_INET)
57                         xlog(L_FATAL, "init: stdin is bound to non-inet addr");
58                 if (getsockopt(0, SOL_SOCKET, SO_TYPE,
59                                 (char *)&_rpcfdtype, &ssize) == -1)
60                         xlog(L_FATAL, "getsockopt failed: %s", strerror(errno));
61                 _rpcpmstart = 1;
62         } else {
63                 pmap_unset(prog, vers);
64                 sock = RPC_ANYSOCK;
65         }
66
67         if ((_rpcfdtype == 0) || (_rpcfdtype == SOCK_DGRAM)) {
68                 if (_rpcfdtype == 0 && defport != 0 &&
69                     ((sock = makesock(defport, IPPROTO_UDP, bufsiz)) < 0)) {
70                         xlog(L_FATAL, "%s: could not make a UDP socket\n",
71                                         name);
72                 }
73                 transp = svcudp_create(sock);
74                 if (transp == NULL) {
75                         xlog(L_FATAL, "cannot create udp service.");
76                 }
77                 if (!svc_register(transp, prog, vers, dispatch, IPPROTO_UDP)) {
78                         xlog(L_FATAL, "unable to register (%s, %d, udp).",
79                                         name, vers);
80                 }
81         }
82
83         if ((_rpcfdtype == 0) || (_rpcfdtype == SOCK_STREAM)) {
84                 if (_rpcfdtype == 0 && defport != 0 &&
85                     ((sock = makesock(defport, IPPROTO_TCP, bufsiz)) < 0)) {
86                         xlog(L_FATAL, "%s: could not make a TCP socket\n",
87                                         name);
88                 }
89                 transp = svctcp_create(sock, 0, 0);
90                 if (transp == NULL) {
91                         xlog(L_FATAL, "cannot create tcp service.");
92                 }
93                 if (!svc_register(transp, prog, vers, dispatch, IPPROTO_TCP)) {
94                         xlog(L_FATAL, "unable to register (%s, %d, tcp).",
95                                         name, vers);
96                 }
97         }
98
99         if (_rpcpmstart) {
100                 signal (SIGALRM, closedown);
101                 alarm (_RPCSVC_CLOSEDOWN);
102         }
103 }
104
105 static void closedown(sig)
106 int sig;
107 {
108         (void) signal(sig, closedown);
109         if (_rpcsvcdirty == 0) {
110                 extern fd_set svc_fdset;
111                 static int size;
112                 int i, openfd;
113
114                 if (_rpcfdtype == SOCK_DGRAM)
115                         exit(0);
116                 if (size == 0) {
117                         size = getdtablesize();
118                 }
119                 for (i = 0, openfd = 0; i < size && openfd < 2; i++)
120                         if (FD_ISSET(i, &svc_fdset))
121                                 openfd++;
122                 if (openfd <= 1)
123                         exit(0);
124         }
125         (void) alarm(_RPCSVC_CLOSEDOWN);
126 }
127
128 static int makesock(port, proto, socksz)
129 int port;
130 int proto;
131 int socksz;
132 {
133         struct sockaddr_in sin;
134         int     s;
135         int     sock_type;
136         int     val;
137
138         sock_type = (proto == IPPROTO_UDP) ? SOCK_DGRAM : SOCK_STREAM;
139         s = socket(AF_INET, sock_type, proto);
140         if (s < 0) {
141                 xlog(L_FATAL, "Could not make a socket: %s\n",
142                                         strerror(errno));
143                 return (-1);
144         }
145         memset((char *) &sin, 0, sizeof(sin));
146         sin.sin_family = AF_INET;
147         sin.sin_addr.s_addr = INADDR_ANY;
148         sin.sin_port = htons(port);
149
150         val = 1;
151         if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &val, sizeof(val)) < 0)
152                 xlog(L_ERROR, "setsockopt failed: %s\n", strerror(errno));
153
154 #ifdef SO_SNDBUF
155         {
156                 int sblen, rblen;
157
158                 /* 1024 for rpc & transport overheads */
159                 sblen = rblen = socksz + 1024;
160                 if (setsockopt(s, SOL_SOCKET, SO_SNDBUF, &sblen, sizeof sblen) < 0 ||
161                     setsockopt(s, SOL_SOCKET, SO_RCVBUF, &rblen, sizeof rblen) < 0)
162                         xlog(L_ERROR, "setsockopt failed: %s\n", strerror(errno));
163         }
164 #endif                          /* SO_SNDBUF */
165
166         if (bind(s, (struct sockaddr *) &sin, sizeof(sin)) == -1) {
167                 xlog(L_FATAL, "Could not bind name to socket: %s\n",
168                                         strerror(errno));
169                 return (-1);
170         }
171         return (s);
172 }
173
174
175 /* Log an incoming call. */
176 void
177 rpc_logcall(struct svc_req *rqstp, char *xname, char *arg)
178 {
179         char            buff[1024];
180         int             buflen=sizeof(buff);
181         int             len;
182         char            *sp;
183         int             i;
184
185         if (!xlog_enabled(D_CALL))
186                 return;
187
188         sp = buff;
189         switch (rqstp->rq_cred.oa_flavor) {
190         case AUTH_NULL:
191                 sprintf(sp, "NULL");
192                 break;
193         case AUTH_UNIX: {
194                 struct authunix_parms *unix_cred;
195                 struct tm *tm;
196
197                 unix_cred = (struct authunix_parms *) rqstp->rq_clntcred;
198                 tm = localtime(&unix_cred->aup_time);
199                 snprintf(sp, buflen, "UNIX %d/%d/%d %02d:%02d:%02d %s %d.%d",
200                         tm->tm_year, tm->tm_mon + 1, tm->tm_mday,
201                         tm->tm_hour, tm->tm_min, tm->tm_sec,
202                         unix_cred->aup_machname,
203                         unix_cred->aup_uid,
204                         unix_cred->aup_gid);
205                 sp[buflen-1] = 0;
206                 len = strlen(sp);
207                 sp += buflen;
208                 buflen -= len;
209                 if ((int) unix_cred->aup_len > 0) {
210                         snprintf(sp, buflen, "+%d", unix_cred->aup_gids[0]);
211                         sp[buflen-1] = 0;
212                         len = strlen(sp);
213                         sp += buflen;
214                         buflen -= len;
215                         for (i = 1; i < unix_cred->aup_len; i++) {
216                                 snprintf(sp, buflen, ",%d", 
217                                         unix_cred->aup_gids[i]);
218                                 sp[buflen-1] = 0;
219                                 len = strlen(sp);
220                                 sp += buflen;
221                                 buflen -= len;
222                         }
223                 }
224                 }
225                 break;
226         default:
227                 sprintf(sp, "CRED %d", rqstp->rq_cred.oa_flavor);
228         }
229         xlog(D_CALL, "%s [%s]\n\t%s\n", xname, buff, arg);
230 }