source: zwrite.c @ 823671c

debianrelease-1.10release-1.4release-1.5release-1.6release-1.7release-1.8release-1.9
Last change on this file since 823671c was 823671c, checked in by Nelson Elhage <nelhage@mit.edu>, 15 years ago
owl_zwrite_send_message: avoid buffer overflow with to The variable to was set up with sprintf without bounds checking; use dynamic allocation.
  • Property mode set to 100644
File size: 8.7 KB
Line 
1#include <string.h>
2#include <pwd.h>
3#include <sys/types.h>
4#include <unistd.h>
5#include "owl.h"
6
7static const char fileIdent[] = "$Id$";
8
9int owl_zwrite_create_from_line(owl_zwrite *z, char *line)
10{
11  int argc, badargs, myargc;
12  char **argv, **myargv;
13
14  badargs=0;
15 
16  /* start with null entires */
17  z->realm=NULL;
18  z->class=NULL;
19  z->inst=NULL;
20  z->opcode=NULL;
21  z->zsig=NULL;
22  z->message=NULL;
23  z->cc=0;
24  z->noping=0;
25  owl_list_create(&(z->recips));
26
27  /* parse the command line for options */
28  argv=myargv=owl_parseline(line, &argc);
29  if (argc<0) {
30    owl_function_error("Unbalanced quotes in zwrite");
31    return(-1);
32  }
33  myargc=argc;
34  if (myargc && *(myargv[0])!='-') {
35    myargc--;
36    myargv++;
37  }
38  while (myargc) {
39    if (!strcmp(myargv[0], "-c")) {
40      if (myargc<2) {
41        badargs=1;
42        break;
43      }
44      z->class=owl_get_iso_8859_1_if_possible(myargv[1]);
45      myargv+=2;
46      myargc-=2;
47    } else if (!strcmp(myargv[0], "-i")) {
48      if (myargc<2) {
49        badargs=1;
50        break;
51      }
52      z->inst=owl_get_iso_8859_1_if_possible(myargv[1]);
53      myargv+=2;
54      myargc-=2;
55    } else if (!strcmp(myargv[0], "-r")) {
56      if (myargc<2) {
57        badargs=1;
58        break;
59      }
60      z->realm=owl_get_iso_8859_1_if_possible(myargv[1]);
61      myargv+=2;
62      myargc-=2;
63    } else if (!strcmp(myargv[0], "-s")) {
64      if (myargc<2) {
65        badargs=1;
66        break;
67      }
68      z->zsig=owl_get_iso_8859_1_if_possible(myargv[1]);
69      myargv+=2;
70      myargc-=2;
71    } else if (!strcmp(myargv[0], "-O")) {
72      if (myargc<2) {
73        badargs=1;
74        break;
75      }
76      z->opcode=owl_get_iso_8859_1_if_possible(myargv[1]);
77      myargv+=2;
78      myargc-=2;
79    } else if (!strcmp(myargv[0], "-m")) {
80      if (myargc<2) {
81        badargs=1;
82        break;
83      }
84      /* we must already have users or a class or an instance*/
85      if (owl_list_get_size(&(z->recips))<1 && (!z->class) && (!z->inst)) {
86        badargs=1;
87        break;
88      }
89
90      /* Once we have -m, gobble up everything else on the line */
91      myargv++;
92      myargc--;
93      z->message=owl_get_iso_8859_1_if_possible("");
94      while (myargc) {
95        z->message=owl_realloc(z->message, strlen(z->message)+strlen(myargv[0])+5);
96        strcat(z->message, myargv[0]);
97        strcat(z->message, " ");
98        myargc--;
99        myargv++;
100      }
101      z->message[strlen(z->message)-1]='\0'; /* remove last space */
102      break;
103    } else if (!strcmp(myargv[0], "-C")) {
104      z->cc=1;
105      myargv++;
106      myargc--;
107    } else if (!strcmp(myargv[0], "-n")) {
108      z->noping=1;
109      myargv++;
110      myargc--;
111    } else {
112      /* anything unattached is a recipient */
113      owl_list_append_element(&(z->recips), owl_get_iso_8859_1_if_possible(myargv[0]));
114      myargv++;
115      myargc--;
116    }
117  }
118
119  owl_parsefree(argv, argc);
120
121  if (badargs) {
122    return(-1);
123  }
124
125  if (z->class == NULL &&
126      z->inst == NULL &&
127      owl_list_get_size(&(z->recips))==0) {
128    owl_function_error("You must specify a recipient for zwrite");
129    return(-1);
130  }
131
132  /* now deal with defaults */
133  if (z->class==NULL) z->class=owl_strdup("message");
134  if (z->inst==NULL) z->inst=owl_strdup("personal");
135  if (z->realm==NULL) z->realm=owl_strdup("");
136  if (z->opcode==NULL) z->opcode=owl_strdup("");
137  /* z->message is allowed to stay NULL */
138 
139  return(0);
140}
141
142void owl_zwrite_populate_zsig(owl_zwrite *z)
143{
144  char *zsigproc, *zsigowlvar, *zsigzvar, *ptr;
145  struct passwd *pw;
146
147  /* get a zsig, if not given */
148  if (z->zsig==NULL) {
149    zsigproc = owl_global_get_zsigproc(&g);
150    zsigowlvar = owl_global_get_zsig(&g);
151    zsigzvar = owl_zephyr_get_variable("zwrite-signature");
152
153    if (zsigowlvar && *zsigowlvar) {
154      z->zsig=owl_get_iso_8859_1_if_possible(zsigowlvar);
155    } else if (zsigproc && *zsigproc) {
156      FILE *file;
157      char buff[LINE], *openline;
158     
159      /* simple hack for now to nuke stderr */
160      openline=owl_malloc(strlen(zsigproc)+40);
161      strcpy(openline, zsigproc);
162#if !(OWL_STDERR_REDIR)
163      strcat(openline, " 2> /dev/null");
164#endif
165      file=popen(openline, "r");
166      owl_free(openline);
167      if (!file) {
168        if (zsigzvar && *zsigzvar) {
169          z->zsig=owl_get_iso_8859_1_if_possible(zsigzvar);
170        }
171      } else {
172        z->zsig=owl_malloc(LINE*5);
173        strcpy(z->zsig, "");
174        while (fgets(buff, LINE, file)) { /* wrong sizing */
175          strcat(z->zsig, buff);
176        }
177        pclose(file);
178        if (z->zsig[strlen(z->zsig)-1]=='\n') {
179          z->zsig[strlen(z->zsig)-1]='\0';
180        }
181      }
182    } else if (zsigzvar) {
183      z->zsig=owl_get_iso_8859_1_if_possible(zsigzvar);
184    } else if (((pw=getpwuid(getuid()))!=NULL) && (pw->pw_gecos)) {
185      z->zsig=owl_get_iso_8859_1_if_possible(pw->pw_gecos);
186      ptr=strchr(z->zsig, ',');
187      if (ptr) {
188        ptr[0]='\0';
189      }
190    }
191  }
192}
193
194void owl_zwrite_send_ping(owl_zwrite *z)
195{
196  int i, j;
197  char *to;
198
199  if (z->noping) return;
200 
201  if (strcasecmp(z->class, "message") ||
202      strcasecmp(z->inst, "personal")) {
203    return;
204  }
205
206  /* if there are no recipients we won't send a ping, which
207     is what we want */
208  j=owl_list_get_size(&(z->recips));
209  for (i=0; i<j; i++) {
210    if (strcmp(z->realm, "")) {
211      to = owl_sprintf("%s@%s", (char *) owl_list_get_element(&(z->recips), i), z->realm);
212    } else {
213      to = owl_strdup(owl_list_get_element(&(z->recips), i));
214    }
215    send_ping(to);
216    owl_free(to);
217  }
218
219}
220
221void owl_zwrite_set_message(owl_zwrite *z, char *msg)
222{
223  int i, j;
224  char *toline = NULL;
225  char *tmp = NULL;
226
227  if (z->message) owl_free(z->message);
228
229  j=owl_list_get_size(&(z->recips));
230  if (j>0 && z->cc) {
231    toline = owl_strdup( "CC: ");
232    for (i=0; i<j; i++) {
233      tmp = toline;
234      if (strcmp(z->realm, "")) {
235        toline = owl_sprintf( "%s%s@%s ", toline, (char *) owl_list_get_element(&(z->recips), i), z->realm);
236      } else {
237        toline = owl_sprintf( "%s%s ", toline, (char *) owl_list_get_element(&(z->recips), i));
238      }
239      owl_free(tmp);
240      tmp = NULL;
241    }
242    tmp = owl_get_iso_8859_1_if_possible(msg);
243    z->message=owl_sprintf("%s\n%s", toline, tmp);
244    owl_free(toline);
245  } else {
246    z->message=owl_get_iso_8859_1_if_possible(msg);
247  }
248  if (tmp) owl_free(tmp);
249}
250
251char *owl_zwrite_get_message(owl_zwrite *z)
252{
253  if (z->message) return(z->message);
254  return("");
255}
256
257int owl_zwrite_is_message_set(owl_zwrite *z)
258{
259  if (z->message) return(1);
260  return(0);
261}
262
263int owl_zwrite_send_message(owl_zwrite *z)
264{
265  int i, j;
266  char *to = NULL;
267
268  if (z->message==NULL) return(-1);
269
270  j=owl_list_get_size(&(z->recips));
271  if (j>0) {
272    for (i=0; i<j; i++) {
273      if (strcmp(z->realm, "")) {
274        to = owl_sprintf("%s@%s", (char *) owl_list_get_element(&(z->recips), i), z->realm);
275      } else {
276        to = owl_strdup( owl_list_get_element(&(z->recips), i));
277      }
278      send_zephyr(z->opcode, z->zsig, z->class, z->inst, to, z->message);
279      owl_free(to);
280      to = NULL;
281    }
282  } else {
283    to = owl_sprintf( "@%s", z->realm);
284    send_zephyr(z->opcode, z->zsig, z->class, z->inst, to, z->message);
285  }
286  owl_free(to);
287  return(0);
288}
289
290int owl_zwrite_create_and_send_from_line(char *cmd, char *msg)
291{
292  owl_zwrite z;
293  int rv;
294  rv=owl_zwrite_create_from_line(&z, cmd);
295  if (rv) return(rv);
296  if (!owl_zwrite_is_message_set(&z)) {
297    owl_zwrite_set_message(&z, msg);
298  }
299  owl_zwrite_populate_zsig(&z);
300  owl_zwrite_send_message(&z);
301  owl_zwrite_free(&z);
302  return(0);
303}
304
305char *owl_zwrite_get_class(owl_zwrite *z)
306{
307  return(z->class);
308}
309
310char *owl_zwrite_get_instance(owl_zwrite *z)
311{
312  return(z->inst);
313}
314
315char *owl_zwrite_get_opcode(owl_zwrite *z)
316{
317  return(z->opcode);
318}
319
320void owl_zwrite_set_opcode(owl_zwrite *z, char *opcode)
321{
322  if (z->opcode) owl_free(z->opcode);
323  z->opcode=owl_get_iso_8859_1_if_possible(opcode);
324}
325
326char *owl_zwrite_get_realm(owl_zwrite *z)
327{
328  return(z->realm);
329}
330
331char *owl_zwrite_get_zsig(owl_zwrite *z)
332{
333  if (z->zsig) return(z->zsig);
334  return("");
335}
336
337void owl_zwrite_get_recipstr(owl_zwrite *z, char *buff)
338{
339  int i, j;
340
341  strcpy(buff, "");
342  j=owl_list_get_size(&(z->recips));
343  for (i=0; i<j; i++) {
344    strcat(buff, owl_list_get_element(&(z->recips), i));
345    strcat(buff, " ");
346  }
347  buff[strlen(buff)-1]='\0';
348}
349
350int owl_zwrite_get_numrecips(owl_zwrite *z)
351{
352  return(owl_list_get_size(&(z->recips)));
353}
354
355char *owl_zwrite_get_recip_n(owl_zwrite *z, int n)
356{
357  return(owl_list_get_element(&(z->recips), n));
358}
359
360int owl_zwrite_is_personal(owl_zwrite *z)
361{
362  /* return true if at least one of the recipients is personal */
363  int i, j;
364  char *foo;
365
366  j=owl_list_get_size(&(z->recips));
367  for (i=0; i<j; i++) {
368    foo=owl_list_get_element(&(z->recips), i);
369    if (foo[0]!='@') return(1);
370  }
371  return(0);
372}
373
374void owl_zwrite_free(owl_zwrite *z)
375{
376  owl_list_free_all(&(z->recips), &owl_free);
377  if (z->class) owl_free(z->class);
378  if (z->inst) owl_free(z->inst);
379  if (z->opcode) owl_free(z->opcode);
380  if (z->realm) owl_free(z->realm);
381  if (z->message) owl_free(z->message);
382  if (z->zsig) owl_free(z->zsig);
383}
Note: See TracBrowser for help on using the repository browser.