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)  

send.c
Go to the documentation of this file.
1 /*
2 ** send.c - Format and send IDENT protocol replies.
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 #include "config.h"
16 
17 #include <stdio.h>
18 #ifdef HAVE_UNISTD_H
19 #include <unistd.h>
20 #endif
21 #include <string.h>
22 #include <syslog.h>
23 #include <pwd.h>
24 
25 #include <sys/types.h>
26 #include <netinet/in.h>
27 #include <arpa/inet.h>
28 
29 #include "pidentd.h"
30 
31 int uidonly_flag = 0;
32 int noident_flag = 0;
34 
35 char *charset = NULL;
36 char *opsys = "UNIX";
37 
38 
39 static int
40 check_noident(char *dir)
41 {
42  char buf[2048];
43  size_t len;
44 
45 
46  if (dir == NULL || strlen(dir) > 1024)
47  return -1;
48 
49  len = strlen(dir);
50  memcpy(buf, dir, len);
51  if (len > 0 && buf[len-1] != '/')
52  buf[len++] = '/';
53  strcpy(buf+len, ".noident");
54 
55  if (access(buf, F_OK) == 0)
56  return 1;
57  else
58  return 0;
59 }
60 
61 
62 void
63 send_error(int fd,
64  int l_port,
65  int r_port,
66  const char *what,
67  struct sockaddr_gen *remote_addr)
68 {
69  char buf[1024];
70  char buf2[32];
71 
72 
73  s_snprintf(buf, sizeof(buf),
74  "%d , %d : ERROR : %s\r\n", l_port, r_port, what);
75  s_write(fd, buf, strlen(buf));
76 
77  if (result_syslog_level > -1)
78  syslog(result_syslog_level,
79  "reply to %s: %d , %d : ERROR: %s",
80  s_inet_ntox(remote_addr, buf2, sizeof(buf2)),
81  l_port, r_port, what);
82 }
83 
84 
85 void
86 send_result(int fd,
87  struct kernel *kp)
88 {
89  char buf[2048], pbuf[2048], buf2[32];
90  struct passwd pwb, *pp = NULL;
91  uid_t uid;
92 
93 
94  if (debug)
95  fprintf(stderr, "send_result(%d) - ruid = %ld, euid = %ld\n",
96  fd, (long) kp->ruid, (long) kp->euid);
97 
98  if (kp->ruid == NO_UID)
99  uid = kp->euid;
100  else
101  uid = kp->ruid;
102 
103  if (!uidonly_flag || noident_flag)
104  (void) s_getpwuid_r(uid, &pwb, pbuf, sizeof(pbuf), &pp);
105 
106  if (noident_flag && pp != NULL && check_noident(pp->pw_dir) == 1)
107  {
108  syslog(LOG_INFO, "User %s elected to use .noident", pp->pw_name);
109 
110  send_error(fd,
111  ntohs(SGPORT(kp->local)),
112  ntohs(SGPORT(kp->remote)),
113  "HIDDEN-USER",
114  &kp->remote);
115  return;
116  }
117 
118 #ifdef HAVE_LIBDES
119  if (encrypt_flag)
120  {
121  char buffer[33];
122 
123  pdes_encrypt(kp, buffer);
124  s_snprintf(buf, sizeof(buf),
125  "%d , %d : USERID : OTHER%s%s :[%s]\r\n",
126  ntohs(SGPORT(kp->local)),
127  ntohs(SGPORT(kp->remote)),
128  charset != NULL ? " , " : "",
129  charset != NULL ? charset : "",
130  buffer);
131  }
132  else
133 #endif
134 
135  if (!uidonly_flag && pp != NULL && strlen(pp->pw_name) < sizeof(buf)-128)
136  {
137  s_snprintf(buf, sizeof(buf),
138  "%d , %d : USERID : %s%s%s :%s\r\n",
139  ntohs(SGPORT(kp->local)),
140  ntohs(SGPORT(kp->remote)),
141  opsys,
142  charset != NULL ? " , " : "",
143  charset != NULL ? charset : "",
144  pp->pw_name);
145  }
146  else
147  {
148  s_snprintf(buf, sizeof(buf),
149  "%d , %d : USERID : OTHER :%ld\r\n",
150  ntohs(SGPORT(kp->local)),
151  ntohs(SGPORT(kp->remote)),
152  (long) uid);
153  }
154 
155  s_write(fd, buf, strlen(buf));
156 
157  if (result_syslog_level > -1)
158  syslog(result_syslog_level,
159  "reply to %s: %.*s",
160  s_inet_ntox(&kp->remote, buf2, sizeof(buf2)),
161  strlen(buf)-2, buf);
162 }
163 
164 
165 
166 void
168  struct sockaddr_gen *remote_addr)
169 {
170  char buf[1024], buf2[32];
171 
172  s_snprintf(buf, sizeof(buf),
173  "0 , 0 : X-VERSION : pidentd %s for %s (%s %s)\r\n",
175  osinfo_build, __DATE__, __TIME__);
176 
177  s_write(fd, buf, strlen(buf));
178 
179  if (result_syslog_level > -1)
180  syslog(result_syslog_level,
181  "reply to %s: %.*s",
182  s_inet_ntox(remote_addr, buf2, sizeof(buf2)),
183  strlen(buf)-2, buf);
184 }
SGPORT
#define SGPORT(sag)
Definition: sockaddr.h:57
check_noident
static int check_noident(char *dir)
Definition: send.c:40
F_OK
#define F_OK
Definition: system.h:20
opsys
char * opsys
Definition: send.c:36
send_version
void send_version(int fd, struct sockaddr_in *remote_addr)
Definition: send.c:167
send_result
void send_result(int fd, struct kernel *kp)
Definition: send.c:86
result_syslog_level
int result_syslog_level
Definition: send.c:33
s_snprintf
int s_snprintf(char *buf, size_t bufsize, const char *format,...)
Definition: safeio.c:269
uidonly_flag
int uidonly_flag
Definition: send.c:31
pidentd.h
kernel::remote
struct sockaddr_gen remote
Definition: kernel.h:29
pdes_encrypt
int pdes_encrypt(struct kernel *kp, char buffer[33])
kernel::local
struct sockaddr_gen local
Definition: kernel.h:28
server_version
char server_version[]
Definition: version.c:1
charset
char * charset
Definition: send.c:35
NO_UID
#define NO_UID
Definition: pidentd.h:55
kernel::euid
uid_t euid
Definition: kernel.h:35
debug
int debug
Definition: gen_osinfo.c:23
kernel::ruid
uid_t ruid
Definition: kernel.h:36
noident_flag
int noident_flag
Definition: send.c:32
sockaddr_gen
#define sockaddr_gen
Definition: sockaddr.h:53
s_inet_ntox
const char * s_inet_ntox(struct sockaddr_in *ia, char *buf, size_t bufsize)
Definition: support.c:468
s_write
ssize_t s_write(int fd, const char *buf, size_t len)
Definition: safeio.c:83
s_getpwuid_r
int s_getpwuid_r(uid_t uid, struct passwd *pwd, char *buffer, size_t bufsize, struct passwd **result)
Definition: support.c:254
send_error
void send_error(int fd, int l_port, int r_port, const char *what, struct sockaddr_in *remote_addr)
Definition: send.c:63
osinfo_build
char osinfo_build[]
kernel
Definition: kernel.h:25