Annotation of www/porting.html, Revision 1.42
1.20 rohee 1: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
1.1 marc 2: <html>
3: <head>
4: <meta http-equiv="Content-Type"
1.15 espie 5: content="text/html; charset=iso-8859-1">
1.1 marc 6: <meta name="resource-type"
1.15 espie 7: content="document">
1.1 marc 8: <meta name="description"
1.15 espie 9: CONTENT="How to make an OpenBSD port">
1.1 marc 10: <meta name="keywords"
1.15 espie 11: content="openbsd,ports">
1.1 marc 12: <meta name="distribution"
1.15 espie 13: content="global">
1.1 marc 14: <meta name="copyright"
1.40 jufi 15: content="This document copyright 1997-2002 by OpenBSD.">
1.1 marc 16: <title>Building an OpenBSD port</title>
17: <link rev="made" HREF="mailto:www@openbsd.org">
18: </head>
19: <body text="#000000" bgcolor="#FFFFFF" link="#23238E">
1.6 pauls 20: <img height=30 width=141 src=images/smalltitle.gif alt="[OpenBSD]" >
1.1 marc 21:
1.20 rohee 22: <h2><font color="#e00000">Building an OpenBSD port</font></h2>
1.1 marc 23:
24: So you've just compiled your favorite software package on your
25: OpenBSD machine and you want to share your effort by turning
26: it into a standard port. What to do?
27: <p>
1.25 espie 28: The most important thing you can is to <strong>communicate</strong>.
29: Ask people on <a href="mailto:ports@openbsd.org">ports@openbsd.org</a>
30: if they are working on the same port. <em>Tell the original software
31: author about it</em>, including problems you may find. If licensing
32: information appears incorrect tell him. If you had to jump through
33: loops to make the port build, tell him what he can fix. If they are
1.32 deraadt 34: only developing on Linux and feel like ignoring the rest of the Unix
1.25 espie 35: world, try to make them change their view.
36: <p>
37: <strong>COMMUNICATION</strong> makes the difference between a successful
1.34 jufi 38: port and a port that will slowly be abandoned by everyone.
1.25 espie 39: <p>
1.9 marc 40: First look at the porting information on this page. Then check
41: out the referenced documents, especially the OpenBSD porting
1.26 espie 42: <a href="checklist.html">checklist</a>.
1.1 marc 43: <p>
1.9 marc 44: Test, then re-test, and finally test again!
45: <p>
46: Submit the port. Create a gzipped tarball of the port directory.
47: You can then either place it on a public FTP or HTTP server, sending
1.20 rohee 48: its address to <a href="mailto:ports@openbsd.org">ports@openbsd.org</a>
1.9 marc 49: or send the port mime encoded to the same address. Pick whichever
50: method works best for you.
1.35 naddy 51:
52: <h3><font color="#0000e0">Index of Porting Documentation</font></h3>
53: <ul>
54: <li><a href="#Avail">Available Porting Information</a></li>
55: <li><a href="#Policy">OpenBSD Porting Policy</a></li>
56: <li><a href="#Security">Security Recommendations</a></li>
57: <li><a href="#Generic">Generic Porting Hints</a></li>
58: <li><a href="#Other">Other Helpful Hints</a></li>
59: </ul>
60:
61: <h3><font color="#0000e0"><a name="Avail">Available Porting Information</a></font></h3>
1.1 marc 62: <ul>
1.38 espie 63: <li>OpenBSD porting <a href="checklist.html">checklist</a>.
64: <li>The man page
1.41 rohee 65: <a href="http://www.openbsd.org/cgi-bin/man.cgi?query=bsd.port.mk&sektion=5">bsd.port.mk(5)</a>.
1.38 espie 66: This documents the ports infrastructure makefile that is
67: included at the end of each individual port makefile.
68: There are still a few comments at the start of
1.39 horacio 69: the file itself, but most of the useful information is now
1.38 espie 70: documented.
71: <li>Some differences from other BSD port systems, mostly a summary
72: of <a href="porting/diffs.html">infrastructure differences</a>.
73: <li><a href="porting/libraries.html">Using shared libraries
74: in OpenBSD Ports</a>. The rules there are <strong>very
75: important</strong> as soon as you use shared libraries, especially if
76: pkg_update is to work one day...
77: <li><a href="audio-port.html">Porting audio applications to OpenBSD</a>.
1.1 marc 78: <li>The
1.13 art 79: <a href="http://www.netbsd.org/Documentation/software/packages.html">
80: NetBSD Package System</a> documentation. This document describes
81: NetBSD's version of the FreeBSD ports system and is quite helpful.
1.33 naddy 82: <li>The
1.42 ! pvalchev 83: <a href="http://www.freebsd.org/doc/en_US.ISO8859-1/books/porters-handbook/index.html">FreeBSD
1.33 naddy 84: Porter's Handbook</a>. This is the FreeBSD porting bible.
1.1 marc 85: <li>The OpenBSD ports mailing list,
86: <a href="mailto:ports@openbsd.org">ports@OpenBSD.ORG</a>.
87: </ul>
1.35 naddy 88: <h3><font color="#0000e0"><a name="Policy">OpenBSD Porting Policy</a></font></h3>
1.1 marc 89: <ul>
1.24 rohee 90: <li>OpenBSD does NOT use <code>/usr/local/etc/rc.d</code>.<br>
1.7 espie 91: <code>/usr/local</code> is often shared between several machines
1.15 espie 92: thanks to NFS. For this reason, configuration files that are specific
93: to a given machine can't be stored under <code>/usr/local</code>,
94: <code>/etc</code> is the central repository for per machine
95: configuration files. Moreover, OpenBSD policy is to never update
96: files under <code>/etc</code> automatically. Ports that need some
97: specific boot setup should advise the administrator about what to do
98: instead of blindly installing files.
1.1 marc 99: <li>OpenBSD does NOT compress man pages.
100: <li>OpenBSD does NOT require <code>-lcrypt</code>.<br>
101: DES encryption is part of the standard <code>libc</code>.
1.10 espie 102: <li>OpenBSD is strongly security-oriented. You should read and understand
1.42 ! pvalchev 103: this page's <a href="#Security">security section</a>.
1.24 rohee 104: <li>Be sure to add the <code>$OpenBSD$</code> CVS tag to
1.10 espie 105: the Makefile. If importing a port from another system be sure to
106: leave their tag in the Makefile, too. However, replace the FreeBSD
1.24 rohee 107: <code>$Id$</code> tag with the
108: <code>$FreeBSD$</code> tag.
1.10 espie 109: <li>The goal is to get all ported applications to support OpenBSD. To
110: achieve this goal you <strong>must</strong> feed any OpenBSD patches
111: back to the application maintainer.
112: </ul>
1.35 naddy 113: <h3><font color="#0000e0"><a name="Security">Security Recommendations</a></font></h3>
1.10 espie 114: There are many security problems to worry about. If
1.2 marc 115: you are not absolutely sure of what you are doing please request
1.1 marc 116: help from the <a href="mailto:ports@openbsd.org">ports</a> mailing
117: list.
1.10 espie 118:
119: <ul>
1.20 rohee 120: <li>Do <em>not</em> use alpha or beta code when preparing a port. Use the
1.10 espie 121: latest regular or patch release.
122:
1.1 marc 123: <li>Any software to be installed as a server should be scanned
124: for buffer overflows, especially unsafe use of
125: <code>strcat/strcpy/strcmp/sprintf</code>. In general,
126: <code>sprintf</code> should be replaced with <code>snprintf</code>.
1.10 espie 127:
1.17 espie 128: <li>Never use filenames instead of true security. There are numerous race
1.10 espie 129: conditions where you don't have proper control. For instance, an attacker
130: who already has user privileges on your machines may replace files in
131: <code>/tmp</code> with symbolic links to more strategic files, such as
1.19 rohee 132: <code>/etc/master.passwd</code>.
1.16 espie 133:
134: <li>For instance, both <code>fopen</code> and <code>freopen</code>
135: <strong>create a new file or open an existing file</strong> for
136: writing. An attacker may create a symbolic link from
1.19 rohee 137: <code>/etc/master.passwd</code> to <code>/tmp/addrpool_dump</code>. The
1.16 espie 138: instant you open it, your password file is hosed. Yes, even with
139: an <code>unlink</code> right before. You only narrow the window
140: of opportunity. Use <code>open</code> with
1.22 rohee 141: <code>O_CREAT|O_EXCL</code> and <code>fdopen</code> instead.
1.15 espie 142:
1.16 espie 143: <li>Another very common problem is the <code>mktemp</code>
1.17 espie 144: function. Heed the warnings of the bsd linker about its uses.
1.15 espie 145: <strong>These must be fixed</strong>.
146: This is not quite as simple as <code>s/mktemp/mkstemp/g</code>. <br>
147: Refer to the <code>mktemp(3)</code> man page of OpenBSD current
148: for more indications.
149: Correct code using <code>mkstemp</code> includes the source to
150: <code>ed</code> or <code>mail</code>.
151: A rare instance of code that uses <code>mktemp</code> correctly
152: can be found in the <code>rsync</code> port.
1.10 espie 153:
154: <li>Just because you can read it doesn't mean you should. A well-known hole
155: of this nature was the <code>startx</code> problem. As a setuid program,
156: you could launch startx with any file as a script. If the file was not
157: a valid shell script, a syntax error message would follow, along with the
158: first line of the offending file, without any further permission check.
159: Pretty handy to grab the first line of a shadow passwd file, considering
1.12 deraadt 160: these often start with root entry. Do not open your file, and then do
161: an <code>fstat</code> on the open descriptor to check if you should have
1.15 espie 162: been able to open it (or the attacker will play with /dev/rst0 and rewind
1.12 deraadt 163: your tape) -- open it with the correct uid/gid/grouplist set.
1.10 espie 164:
165: <li>Don't use anything that forks a shell in setuid programs before dropping
1.15 espie 166: your privileges. This includes <code>popen</code> and
167: <code>system</code>.
1.10 espie 168: Use <code>fork</code>, <code>pipe</code> and <code>execve</code> instead.
169:
1.15 espie 170: <li>Pass open descriptors instead of filenames to other programs to
171: avoid race conditions. Even if a program does not accept file
172: descriptors, you can still use <code>/dev/fd/0</code>.
1.10 espie 173:
1.15 espie 174: <li>Access rights are attached to file descriptors. If you need setuid rights
1.10 espie 175: to open a file, open that file, then drop your privileges. You can still
176: access the open descriptor, but you have less to worry about. This is
177: double-edged: even after dropping privileges, you should still watch out
178: for those descriptors.
179:
180: <li>Avoid root setuid as much as you can. Basically, root can do anything,
1.15 espie 181: but root rights are very rarely needed, except maybe to create
182: socket ports with a number under 1024. It is arguably better to
183: keep that under <code>inetd</code>
1.10 espie 184: control and just add the relevant entries to <code>inetd.conf</code>.
185: You must know the appropriate magic for writing daemons to achieve that.
1.15 espie 186: It could be argued that you have no business writing setuid programs
187: if you don't know how to do that.
1.10 espie 188:
1.15 espie 189: <li>Use setgid instead of setuid. Apart from those specific files needed
190: by setgid programs, most files are not group-writable. Hence, a
191: security problem in a setgid program won't compromise your system as
192: much: with only group rights, the worst an intruder will be able to
193: do is hack a games score table or some such.
1.10 espie 194: See the <code>xkobo</code> port for an instance of such a change.
195:
1.15 espie 196: <li>Don't trust group-writable files. Even though they are audited,
197: setgid programs are not perceived as important potential security
198: holes. Hence stuff they can tamper with shouldn't be considered
199: sensitive information.
200:
201: <li>Don't trust your environment ! This involves simple things such as
202: your <code>PATH</code> (never use <code>system</code> with an
203: unqualified name, avoid <code>execvp</code>), but also more subtle
204: items such as your locale, timezone, termcap, and so on.
205: Be aware of transitivity: even though you're taking full precautions,
206: programs you call directly won't necessarily. <strong>Never</strong>
207: use <code>system</code> in privileged programs, build your command
208: line, a controlled environment, and call <code>execve</code> directly.
209: The <code>perlsec</code> man page is a good tutorial on such problems.
210:
1.31 jufi 211: <li>Never use setuid shell-scripts. These are inherently insecure.
1.15 espie 212: Wrap them into some C code that ensures a proper environment.
213: On the other hand, OpenBSD features secure perl scripts.
214:
215: <li>Beware the dynamic loader. If you are running setuid, it will only
216: use trusted libraries that were scanned with ldconfig.
217: Setgid is not enough.
218:
219: <li>Dynamic libraries are tricky. C++ code sets a similar problem.
220: Basically, libraries may take some action based upon your environment
221: before your main program even gets to check its setuid status.
222: OpenBSD <code>issetugid</code> addresses this problem, from the
223: library writer point of view. Don't try to port libraries unless you
224: understand this issue thoroughly.
1.10 espie 225: </ul>
1.35 naddy 226: <h3><font color="#0000e0"><a name="Generic">Generic Porting Hints</a></font></h3>
1.10 espie 227: <ul>
228: <li><code>__OpenBSD__</code> should be used sparingly, if at all.
229: Constructs that look like
230: <pre>
231: #if defined(__NetBSD__) || defined(__FreeBSD__)
232: </pre>
233: are often inappropriate. Don't add blindly <code>__OpenBSD__</code>
234: to it. Instead, try to figure out what's going on, and what actual
235: feature is needed. Manual pages are often useful, as they include
236: historic comments, stating when a particular feature was incorporated
237: into BSD. Checking the numeric value of <code>BSD</code> against known
238: releases is often the right way. See
1.17 espie 239: <a href="ftp://ftp.netbsd.org/pub/NetBSD/packages/pkgsrc/Packages.txt">the NetBSD package guide</a>
1.10 espie 240: for more information.
241: <li>Defining <code>BSD</code> is a bad idea. Try to include <code>sys/param.h</code>.
242: This not only defines <code>BSD</code>, it also gives it a proper value.
243: The right code fragment should look like:
244: <pre>
1.23 rohee 245: #if (defined(__unix__) || defined(unix)) && !defined(USG)
1.10 espie 246: #include <sys/param.h>
247: #endif
248: </pre>
249: <li>Test for features, not for specific OSes. In the long run, it is much
1.15 espie 250: better to test whether <code>tcgetattr</code> works than whether
251: you're running under BSD 4.3 or later, or SystemVR4. These kind of
252: tests just confuse the issue. The way to go about it is, for instance,
253: to test for one particular system, set up a slew of
254: <code>HAVE_TCGETATTR</code> defines, then proceed to the next system.
255: This technique separates features tests from specific OSes.
256: In a hurry, another porter can just add the whole set of
257: <code>-DHAVE_XXX</code> defines to the Makefile. One may also write
258: or add to a configure script to check for that feature and add it
259: automatically. As an example not to follow, check nethack 3.2.2
260: source: it assumes loads of things based on the system type. Most
261: of these assumptions are obsolete and no longer reflect reality:
262: POSIX functions are more useful than older BSD versus SystemV
263: differences, to the point that some traditional bsd functions are
1.10 espie 264: now only supported through compatibility libraries.
265:
266: <li>Avoid include files that include other includes that... First, because
1.15 espie 267: this is inefficient. Your project will end up including a file that
268: includes everything. Also, because it makes some problems difficult
269: to fix. It becomes harder to <em>not</em> include one particular file
270: at a given point.
1.10 espie 271:
272: <li>Avoid bizarre macro tricks. Undefining a macro that was defined by a
273: header file is a bad idea. Defining macros to get some specific behavior
1.15 espie 274: from an include file is also a bad idea: compilation modes should be
275: global. If you want POSIX behavior, say so, and
276: <code>#define POSIX_C_SOURCE</code>
1.10 espie 277: throughout the whole project, not when you feel like it.
278:
279: <li>Don't ever write system function prototypes. All modern systems,
280: OpenBSD included, have a standard location for these prototypes. Likely
1.15 espie 281: places include <code>unistd.h</code>, <code>fcntl.h</code> or
282: <code>termios.h</code>.
283: The man page frequently states where the prototype can be found.
284: You might need another slew of <code>HAVE_XXX</code> macros to
285: procure the right file. Don't worry about including the same file
286: twice, include files have guards that prevent all kinds of nastiness.<br>
1.10 espie 287: If some broken system needs you to write the prototype, don't impose
1.15 espie 288: on all other systems. Roll-your-own prototypes will break because they
289: may use types that are equivalent on your system, but are not portable
290: to other systems (<code>unsigned long</code> instead of
291: <code>size_t</code>), or get some <code>const</code> status wrong.
292: Also, some compilers, such as OpenBSD's own gcc,
1.10 espie 293: are able to do a better job with some very frequent functions such as
294: <code>strlen</code> if you include the right header file.
295:
296: <li>Don't use the name of a standard system function for anything else.
1.15 espie 297: If you want to write your own function, give it its own name, and
298: call that function everywhere. If you wish to revert to the
299: default system function, you just need to comment your code out,
300: and define your own name to the system function. Don't do it the
301: other way round. Code should look like this
1.10 espie 302: <pre>
303: /* prototype part */
304: #ifdef USE_OWN_GCVT
305: char *foo_gcvt(double number, size_t ndigit, char *buf);
306: #else
307: /* include correct file */
308: #include <stdlib.h>
309: /* use system function */
310: #define foo_gcvt gcvt
311: #endif
312:
313: /* definition part */
314: #ifdef USE_OWN_GCVT
315: char *foo_gcvt(double number, size_t ndigit, char *buf)
316: {
1.15 espie 317: /* proper definition */
318: }
1.10 espie 319:
320: /* typical use */
321: s = foo_gcvt(n, 15, b);
322: </pre>
1.1 marc 323: </ul>
1.35 naddy 324: <h3><font color="#0000e0"><a name="Other">Other Helpful Hints</a></font></h3>
1.1 marc 325: <ul>
1.15 espie 326: <li>Recent versions of <code>bsd.port.mk</code> set the installers
327: path. Specifically, they set <code>/usr/bin</code> and
328: <code>/bin</code> to be searched <em>before</em>
329: <code>/usr/local/bin</code> and <code>/usr/X11R6/bin</code>.
1.19 rohee 330: <li>Do <em>NOT</em> generate shared libraries if
1.21 rohee 331: <code>${NO_SHARED_LIBS}</code> is defined (beware, it can be defined
332: only after inclusion of <code>bsd.port.mk</code>). If your port has
333: a GNU configure simply add the line
334: <code>CONFIGURE_ARGS += ${CONFIGURE_SHARED}</code> to the Makefile.
1.19 rohee 335: <li>If you rely on a feature that appeared in a recent version of
336: <code>bsd.port.mk</code> don't forget to add a line
337: <code>NEED_VERSION = x.yy</code> in the Makefile.
1.1 marc 338: <li>In OpenBSD <code>curses.h/libcurses/libtermlib</code> are the
339: ``new curses''. Change:<br>
1.15 espie 340: <code>ncurses.h ==> curses.h</code><br>
341: <code>-lncurses ==> -lcurses</code><br>
342: ``old (BSD) curses'' is available by defining
343: <code>_USE_OLD_CURSES_</code>
1.11 millert 344: before including <code>curses.h</code> (usually in a Makefile) and
1.15 espie 345: linking with <code>-locurses</code>.
1.11 millert 346: <li>In OpenBSD, terminal discipline has been upgraded from the older BSD
347: <code>sgtty</code> to the newer POSIX <code>tcgetattr</code> family.
1.15 espie 348: Avoid the older style in new code. Some code may define
349: <code>tcgetattr</code> to be a synonym for the older
350: <code>sgtty</code>, but this is at best a stopgap measure on OpenBSD.
351: The <code>xterm</code> source code is a very good example of
352: what not to do. Try to get your system functionality right: you
353: want a type that remembers the state of your terminal
354: (possible typedef), you want a function that extracts the current
355: state, and a function that sets the new state.
356: Functions that modify this state are more difficult, as they tend
357: to vary depending upon the system. Also, don't forget that you will
358: have to handle cases where you are not connected to a terminal,
359: and that you need to handle signals: not only termination, but
360: also background (<code>SIGTSTP</code>). You should always leave
361: the terminal in a sane state. Do your tests under an older shell,
362: such as sh, which does not reset the terminal in any way at
1.10 espie 363: program's termination.
1.15 espie 364: <li>The newer termcap/terminfo and curses, as included with OpenBSD,
365: include standard sequences for controlling color terminals. You
366: should use these if possible, reverting to standard ANSI color
367: sequences on other systems. However, some terminal descriptions
368: have not been updated yet, and you may need to be able to specify
369: these sequences manually. This is the way vim handles it. This is
370: not strictly necessary. Except for privileged programs, it is
371: generally possible to override a termcap definition through the
1.10 espie 372: <code>TERMCAP</code> variable and get it to work properly.
1.15 espie 373: <li>Signal semantics are tricky, and vary from one system to another.
374: Use <code>sigaction</code> to ensure a specific semantics, along
375: with other system calls referenced in the corresponding manpage.
1.1 marc 376: </ul>
377: <hr>
1.6 pauls 378: <a href="index.html"><img height=24 width=24 src=back.gif border=0 alt=OpenBSD></a>
1.20 rohee 379: <a href="mailto:www@openbsd.org">www@openbsd.org</a>
1.42 ! pvalchev 380: <br><small>$OpenBSD: porting.html,v 1.41 2002/02/18 23:40:18 rohee Exp $</small>
1.1 marc 381: </body>
382: </html>