File: cgilibcso.c

package info (click to toggle)
cgiemail 1.5-2
  • links: PTS
  • area: main
  • in suites: slink
  • size: 204 kB
  • ctags: 93
  • sloc: ansic: 1,366; makefile: 110
file content (289 lines) | stat: -rw-r--r-- 7,448 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
/**********************************************************************
 *  cgilibcso.c -- libcgi portion for finger/CSO stuff
 *
 * Copyright 1994 by the Massachusetts Institute of Technology
 * For copying and distribution information, please see the file
 * <mit-copyright.h>.
 **********************************************************************/
#include "mit-copyright.h"

#define CGI_FINGER_MAX 32768

#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <ctype.h>
#include "cgi.h"
#include "cgicso.h"

char *
cgi_next_line_in_string(string, linebuf, linebuflen)
     char *string, *linebuf;
     int linebuflen;
{
  char *ptr;

  ptr=strchr(string, '\n');

  if (ptr)
    {
      int linelen;

      linelen = ptr - string;
      if (linelen >= linebuflen) linelen = linebuflen-1;
      strncpy(linebuf, string, linelen);
      linebuf[linelen]='\0';
      return(ptr+1);
    }
  strncpy(linebuf, string, linebuflen);
  return((char *)0);
}

/* returns 1 if field found, 0 otherwise */
/* fills in valuebuf with field value on match */
int
cgi_cso_field(linebuf, fieldname, valuebuf, buflen)
     char *linebuf, *fieldname, *valuebuf;
     int buflen;
{
  char *ptr;

  ptr=strchr(linebuf, ':');
  if (!ptr) return(0);		/* not of form name: value */

  /* check if field name matches */
  if (strncmp(fieldname, ptr-strlen(fieldname), strlen(fieldname)))
    return(0);			/* doesn't match */

  /* match! */
  strncpy(valuebuf, ptr+2, buflen);
  return(1);
}

void
cgi_cso_header(formp, query)
     cgi_form *formp;
     char *query;
{
#ifdef CSOHEADER
  char buf[BUFSIZ];
  int nbytes;
#endif

  /* print top of HTML doc */
  puts("Content-Type: text/html\n");

#ifdef CSOHEADER
  /* First try filling a template file; must contain [query] */
  if (!cgi_template_fill(formp, CSOHEADER))
    {
      rewind(formp->tmpf);
      do {
	if ((nbytes = fread(buf, sizeof(char), BUFSIZ, formp->tmpf)) > 0)
	  fwrite(buf, sizeof(char), nbytes, stdout);
      } while (nbytes == BUFSIZ);
      return;
    }
#endif
  fputs("<HEAD><TITLE>Results of directory query: \"", stdout);
  fputs(query, stdout);
  puts("\"</TITLE></HEAD>");
  fputs("<BODY><H1>Results of directory query: \"", stdout);
  fputs(query, stdout);
  puts("\"</H1>");
  return;
  /* be sure to close </BODY> */
}

void
cgi_cso_footer()
{
#ifdef CSOFOOTER
  FILE *fp;
  char buf[BUFSIZ];
  int nbytes;

  /* Try to open CSOFOOTER and dump contents to stdout. */
  if (0 != (fp=fopen(CSOFOOTER, "r")))
    {
      do {
	if ((nbytes = fread(buf, sizeof(char), BUFSIZ, fp)) > 0)
	  fwrite(buf, sizeof(char), nbytes, stdout);
      } while (nbytes == BUFSIZ);
      fclose(fp);
      return;
    }
#endif
  puts("</BODY>");
  return;
}

void
cgi_url_print(linebuf)
     char linebuf[];
{
  /* Look for URL field or any field like http://... */
  if (!strncmp(linebuf, "       url:", 11) || strstr(linebuf, "://"))
    {
      char *ptr1;

      fwrite(linebuf, 1, 12, stdout);

      /* make the URL into a hyperlink */
      fputs("<a href=\"", stdout);
      for(ptr1=linebuf+12; *ptr1 && !isspace(*ptr1); ptr1++)
	putchar((int) *ptr1);
      fputs("\">", stdout);
      fwrite(linebuf+12, 1, ptr1-(linebuf+12), stdout);
      fputs("</a>", stdout);

      /* print the rest of the line */
      puts(ptr1);
    }
  else puts(linebuf);
  return;
}

int
cgi_standard_cso()
{
  cgi_form form;
  char *query, fingerbuf[CGI_FINGER_MAX], *ptr;
  char linebuf[256], namebuf[256], deptbuf[256],
  aliasbuf[256], titlebuf[256], yearbuf[256], emailbuf[256];
  int i, fieldlen, motdlen=0;
  char *fingerhost;

  if (cgi_alloc_form(&form) ||
      cgi_parse_form(&form))
    {
      cgi_output_failure(&form, "Request was not processed due to an error.");
      cgi_free_form(&form);
      return(1);
    }
  query=cgi_value(&form, "query");

#ifdef CGI_CSO_HARDCODE
  fingerhost=CGI_CSO_FINGERHOST;
#else
  /* make sure form has required values */
  fingerhost=cgi_value(&form, "fingerhost");
  if (!*fingerhost)
    {
      form.errcond=1;
      strcpy(form.errmsg, "400 Required field missing: fingerhost");
      cgi_output_failure(&form, "Request was not processed due to an error.");
      cgi_free_form(&form);
      return(1);
    }
#endif

  /* get info from finger */
  if (finger(query, fingerhost, fingerbuf, CGI_FINGER_MAX) < 0)
    {
      form.errcond=1;
      sprintf(form.errmsg, "500 Could not finger %s@%s", query, fingerhost);
      cgi_concat_errno(form.errmsg);
      cgi_output_failure(&form, "Request was not processed due to an error.");
      cgi_free_form(&form);
      return(1);
    }

  cgi_fix_crlf(fingerbuf);

  /* go through query looking for "There were NN matches" */
  ptr=fingerbuf; 
  while ((ptr=cgi_next_line_in_string(ptr, linebuf, 256)) != (char *)0)
    {
      if (!strncmp(linebuf, "There were ", 11)) break;
      motdlen = ptr - fingerbuf;
    }

  /* not found -- print results with special handling of certain fields */
  if (!ptr)
    {
      cgi_cso_header(&form, query);
      linebuf[0]='\0';
      ptr=strstr(fingerbuf, "There was 1 match to your request");
      if (!ptr) ptr = fingerbuf;
      motdlen = ptr - fingerbuf;
      if (motdlen) ptr=cgi_next_line_in_string(ptr, linebuf, 256);
      printf("<p>%s</p>\n<PRE>", linebuf);

      while ((ptr=cgi_next_line_in_string(ptr, linebuf, 256)) != (char *)0)
	{
	  /* Skip alias field */
	  if (cgi_cso_field(linebuf, "alias", aliasbuf, 256))
	      continue;

	  /* make mailto: for known email address */
	  if (cgi_cso_field(linebuf, "email", emailbuf, 256)
	      && strcmp(emailbuf, "Unknown"))
	    {
	      printf("%10s: <A HREF=\"mailto:%s\">%s</A>\n", "email",
		     emailbuf, emailbuf);
	      continue;
	    }

	  /* make hyperlink if appropriate; otherwise print the line */
	  cgi_url_print(linebuf);
	}
    }
  else
    {
      /* found -- print nice HTML list of matches */
      deptbuf[0] = '\0';		/* no dept. found yet */
      cgi_cso_header(&form, query);
      printf("<p>%s</p>\n<PRE>", linebuf);
      while ((ptr=cgi_next_line_in_string(ptr, linebuf, 256)) != (char *)0)
	{
	  if (cgi_cso_field(linebuf, "department", deptbuf, 256)) continue;
	  if (cgi_cso_field(linebuf, "name", namebuf, 256)) continue;
	  if (cgi_cso_field(linebuf, "title", titlebuf, 256)) continue;
	  if (cgi_cso_field(linebuf, "year", yearbuf, 256)) continue;
	  if (cgi_cso_field(linebuf, "alias", aliasbuf, 256))
	    {
	      /* put hyperlink to more specific query */
	      fputs("<A HREF=\"", stdout);
	      fputs(getenv("SCRIPT_NAME"), stdout);
	      fputs("?query=alias%3D", stdout);
	      fputs(aliasbuf, stdout);
	      fputs("\">", stdout);
	      fputs(namebuf, stdout);

	      /* put periods up to next field */
	      fputs("</A>...", stdout);
	      fieldlen = 24-strlen(namebuf);
	      for(i=0; i<fieldlen; i++) putchar((int) '.');

	      /* put "dept, year n", "dept", or "Year n" as appropriate */
	      /* fall back on "title" if available */
	      if (*deptbuf)
		{
		  if (*yearbuf)
		    {
		      fputs(deptbuf, stdout);
		      fputs(", year ", stdout);
		      puts(yearbuf);
		    }
		  else puts(deptbuf);
		}
	      else
		if (*yearbuf)
		  {
		    fputs("Year ", stdout);
		    puts(yearbuf);
		  }
		else
		  if (*titlebuf) puts(titlebuf);
		  else puts("");
	      deptbuf[0] = titlebuf[0] = yearbuf[0] = '\0';
	    }
	}
    }

  puts("</PRE>");
  fwrite(fingerbuf, sizeof(char), motdlen, stdout);
  cgi_cso_footer();
  return(0);
}